View Javadoc

1   package org.codehaus.modello.plugins.xml;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import org.codehaus.modello.ModelloRuntimeException;
8   import org.codehaus.modello.model.ModelClass;
9   import org.codehaus.modello.model.ModelField;
10  import org.codehaus.modello.model.Version;
11  import org.codehaus.modello.plugin.AbstractModelloGenerator;
12  import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
13  import org.codehaus.modello.plugins.xml.metadata.XmlClassMetadata;
14  import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
15  
16  /*
17   * Copyright (c) 2004, Codehaus.org
18   *
19   * Permission is hereby granted, free of charge, to any person obtaining a copy of
20   * this software and associated documentation files (the "Software"), to deal in
21   * the Software without restriction, including without limitation the rights to
22   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
23   * of the Software, and to permit persons to whom the Software is furnished to do
24   * so, subject to the following conditions:
25   *
26   * The above copyright notice and this permission notice shall be included in all
27   * copies or substantial portions of the Software.
28   *
29   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35   * SOFTWARE.
36   */
37  
38  /**
39   * Helper methods to deal with XML representation of the model.
40   *
41   * @author <a href="mailto:hboutemy@codehaus.org">Hervé Boutemy</a>
42   */
43  class XmlModelHelpers
44  {
45      /**
46       * Resolve XML tag name for a class. Note: only root class needs such a resolution.
47       *
48       * @param modelClass the model class
49       * @return the XML tag name for the class
50       */
51      static String resolveTagName( ModelClass modelClass )
52      {
53          XmlClassMetadata xmlClassMetadata = (XmlClassMetadata) modelClass.getMetadata( XmlClassMetadata.ID );
54  
55          String tagName;
56          if ( ( xmlClassMetadata == null ) || ( xmlClassMetadata.getTagName() == null ) )
57          {
58              tagName = AbstractModelloGenerator.uncapitalise( modelClass.getName() );
59          }
60          else
61          {
62              // tag name is overridden by xml.tagName attribute
63              tagName = xmlClassMetadata.getTagName();
64          }
65          return tagName;
66      }
67  
68      /**
69       * Resolve XML tag name for a field.
70       *
71       * @param modelField the model field
72       * @param xmlFieldMetadata the XML metadata of the field
73       * @return the XML tag name for the field
74       */
75      static String resolveTagName( ModelField modelField, XmlFieldMetadata xmlFieldMetadata )
76      {
77          String tagName;
78          if ( ( xmlFieldMetadata == null ) || ( xmlFieldMetadata.getTagName() == null ) )
79          {
80              tagName = modelField.getName();
81          }
82          else
83          {
84              // tag name is overridden by xml.tagName attribute
85              tagName = xmlFieldMetadata.getTagName();
86          }
87          return tagName;
88      }
89  
90      /**
91       * Resolve XML tag name for an item in an association with many multiplicity.
92       *
93       * @param fieldTagName the XML tag name of the field containing the association
94       * @param xmlAssociationMetadata the XML metadata of the association
95       * @return the XML tag name for items
96       */
97      static String resolveTagName( String fieldTagName, XmlAssociationMetadata xmlAssociationMetadata )
98      {
99          String tagName;
100         if ( ( xmlAssociationMetadata == null ) || ( xmlAssociationMetadata.getTagName() == null ) )
101         {
102             tagName = AbstractModelloGenerator.singular( fieldTagName );
103         }
104         else
105         {
106             // tag name is overridden by xml.tagName attribute
107             tagName = xmlAssociationMetadata.getTagName();
108         }
109         return tagName;
110     }
111 
112     /**
113      * Get the field which type is <code>Content</code> if any.
114      *
115      * @param modelFields the fields to check
116      * @return the field, or <code>null</code> if no field is <code>Content</code>
117      */
118     static ModelField getContentField( List<ModelField> modelFields )
119     {
120         if ( modelFields == null )
121         {
122             return null;
123         }
124         for ( ModelField field : modelFields )
125         {
126             XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
127 
128             if ( xmlFieldMetadata.isContent() )
129             {
130                 return field;
131             }
132         }
133         return null;
134     }
135 
136     /**
137      * Gets all fields that are not marked as XML attribute.
138      *
139      * @param modelFields The collection of model fields from which to extract the XML attributes, must not be
140      *            <code>null</code>.
141      * @return The list of XML attributes fields, can be empty but never <code>null</code>.
142      */
143     static List<ModelField> getXmlAttributeFields( List<ModelField> modelFields )
144     {
145         List<ModelField> xmlAttributeFields = new ArrayList<ModelField>();
146 
147         for ( ModelField field : modelFields )
148         {
149             XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
150 
151             if ( xmlFieldMetadata.isAttribute() )
152             {
153                 xmlAttributeFields.add( field );
154             }
155         }
156 
157         return xmlAttributeFields;
158     }
159 
160     /**
161      * Return the XML fields of this class, with proper XML order and no XML transient fields.
162      *
163      * @param modelClass current class
164      * @param version the version of the class to use
165      * @return the list of XML fields of this class
166      */
167     static List<ModelField> getFieldsForXml( ModelClass modelClass, Version version )
168     {
169         List<ModelClass> classes = new ArrayList<ModelClass>();
170 
171         // get the full inheritance
172         while ( modelClass != null )
173         {
174             classes.add( modelClass );
175 
176             String superClass = modelClass.getSuperClass();
177             if ( superClass != null )
178             {
179                 // superClass can be located outside (not generated by modello)
180                 modelClass = modelClass.getModel().getClass( superClass, version, true );
181             }
182             else
183             {
184                 modelClass = null;
185             }
186         }
187 
188         List<ModelField> fields = new ArrayList<ModelField>();
189 
190         for ( int i = classes.size() - 1; i >= 0; i-- )
191         {
192             modelClass = classes.get( i );
193 
194             Iterator<ModelField> parentIter = fields.iterator();
195 
196             fields = new ArrayList<ModelField>();
197 
198             for ( ModelField field : modelClass.getFields( version ) )
199             {
200                 XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata( XmlFieldMetadata.ID );
201 
202                 if ( xmlFieldMetadata.isTransient() )
203                 {
204                     // just ignore xml.transient fields
205                     continue;
206                 }
207 
208                 if ( xmlFieldMetadata.getInsertParentFieldsUpTo() != null )
209                 {
210                     // insert fields from parent up to the specified field
211                     boolean found = false;
212 
213                     while ( !found && parentIter.hasNext() )
214                     {
215                         ModelField parentField = parentIter.next();
216 
217                         fields.add( parentField );
218 
219                         found = parentField.getName().equals( xmlFieldMetadata.getInsertParentFieldsUpTo() );
220                     }
221 
222                     if ( !found )
223                     {
224                         // interParentFieldsUpTo not found
225                         throw new ModelloRuntimeException( "parent field not found: class " + modelClass.getName() + " xml.insertParentFieldUpTo='" + xmlFieldMetadata.getInsertParentFieldsUpTo() + "'" );
226                     }
227                 }
228 
229                 fields.add( field );
230             }
231 
232             // add every remaining fields from parent class
233             while ( parentIter.hasNext() )
234             {
235                 fields.add( parentIter.next() );
236             }
237         }
238 
239         return fields;
240     }
241 }