View Javadoc

1   package org.codehaus.modello.plugins.xml;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.util.List;
26  
27  import org.codehaus.modello.model.ModelClass;
28  import org.codehaus.modello.model.ModelField;
29  import org.codehaus.modello.model.Version;
30  import org.codehaus.modello.plugin.AbstractModelloGenerator;
31  import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
32  import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
33  import org.codehaus.plexus.util.xml.XMLWriter;
34  import org.codehaus.plexus.util.xml.XmlWriterUtil;
35  
36  /**
37   * Abstract class for plugins working on XML representation of the model, without having any need to generate
38   * Java code.
39   *
40   * @author <a href="mailto:hboutemy@codehaus.org">Hervé Boutemy</a>
41   */
42  public abstract class AbstractXmlGenerator
43      extends AbstractModelloGenerator
44  {
45      protected void initHeader( XMLWriter w )
46      {
47          XmlWriterUtil.writeComment( w, getHeader() );
48      }
49  
50      /**
51       * Resolve XML tag name for a class. Note: only root class needs such a resolution.
52       *
53       * @param modelClass the model class
54       * @return the XML tag name for the class
55       */
56      protected String resolveTagName( ModelClass modelClass )
57      {
58          return XmlModelHelpers.resolveTagName( modelClass );
59      }
60  
61      /**
62       * Resolve XML tag name for a field.
63       *
64       * @param modelField the model field
65       * @param xmlFieldMetadata the XML metadata of the field
66       * @return the XML tag name for the field
67       */
68      protected String resolveTagName( ModelField modelField, XmlFieldMetadata xmlFieldMetadata )
69      {
70          return XmlModelHelpers.resolveTagName( modelField, xmlFieldMetadata );
71      }
72  
73      /**
74       * Resolve XML tag name for an item in an association with many multiplicity.
75       *
76       * @param fieldTagName the XML tag name of the field containing the association
77       * @param xmlAssociationMetadata the XML metadata of the association
78       * @return the XML tag name for items
79       */
80      protected String resolveTagName( String fieldTagName, XmlAssociationMetadata xmlAssociationMetadata )
81      {
82          return XmlModelHelpers.resolveTagName( fieldTagName, xmlAssociationMetadata );
83      }
84  
85      protected boolean hasContentField( List<ModelField> modelFields )
86      {
87          return ( getContentField( modelFields ) != null );
88      }
89  
90      /**
91       * Get the field which type is <code>Content</code> if any.
92       *
93       * @param modelFields the fields to check
94       * @return the field, or <code>null</code> if no field is <code>Content</code>
95       */
96      protected ModelField getContentField( List<ModelField> modelFields )
97      {
98          return XmlModelHelpers.getContentField( modelFields );
99      }
100 
101     /**
102      * Gets all fields that are not marked as XML attribute.
103      *
104      * @param modelFields The collection of model fields from which to extract the XML attributes, must not be
105      *            <code>null</code>.
106      * @return The list of XML attributes fields, can be empty but never <code>null</code>.
107      */
108     protected List<ModelField> getXmlAttributeFields( List<ModelField> modelFields )
109     {
110         return XmlModelHelpers.getXmlAttributeFields( modelFields );
111     }
112 
113     /**
114      * Return the XML fields of this class, with proper XML order and no XML transient fields.
115      *
116      * @param modelClass current class
117      * @param version the version of the class to use
118      * @return the list of XML fields of this class
119      */
120     protected List<ModelField> getFieldsForXml( ModelClass modelClass, Version version )
121     {
122         return XmlModelHelpers.getFieldsForXml( modelClass, version );
123     }
124 }