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  import java.util.Properties;
27  
28  import org.codehaus.modello.ModelloException;
29  import org.codehaus.modello.model.Model;
30  import org.codehaus.modello.model.ModelClass;
31  import org.codehaus.modello.model.ModelDefault;
32  import org.codehaus.modello.model.ModelField;
33  import org.codehaus.modello.model.Version;
34  import org.codehaus.modello.plugin.java.AbstractJavaModelloGenerator;
35  import org.codehaus.modello.plugin.java.javasource.JSourceCode;
36  import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
37  import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
38  
39  /**
40   * Abstract class for plugins generating Java code for XML representation of the model.
41   *
42   * @author <a href="mailto:hboutemy@codehaus.org">Hervé Boutemy</a>
43   */
44  public abstract class AbstractXmlJavaGenerator
45      extends AbstractJavaModelloGenerator
46  {
47      protected boolean strictXmlAttributes;
48  
49      protected void initialize( Model model, Properties parameters )
50          throws ModelloException
51      {
52          super.initialize( model, parameters );
53  
54          strictXmlAttributes = model.getDefault( ModelDefault.STRICT_XML_ATTRIBUTES ).getBoolean();
55      }
56  
57      protected String getFileName( String suffix )
58      {
59          String name = getModel().getName();
60  
61          return name + suffix;
62      }
63  
64      /**
65       * Resolve XML tag name for a class. Note: only root class needs such a resolution.
66       *
67       * @param modelClass the model class
68       * @return the XML tag name for the class
69       */
70      protected String resolveTagName( ModelClass modelClass )
71      {
72          return XmlModelHelpers.resolveTagName( modelClass );
73      }
74  
75      /**
76       * Resolve XML tag name for a field.
77       *
78       * @param modelField the model field
79       * @param xmlFieldMetadata the XML metadata of the field
80       * @return the XML tag name for the field
81       */
82      protected String resolveTagName( ModelField modelField, XmlFieldMetadata xmlFieldMetadata )
83      {
84          return XmlModelHelpers.resolveTagName( modelField, xmlFieldMetadata );
85      }
86  
87      /**
88       * Resolve XML tag name for an item in an association with many multiplicity.
89       *
90       * @param fieldTagName the XML tag name of the field containing the association
91       * @param xmlAssociationMetadata the XML metadata of the association
92       * @return the XML tag name for items
93       */
94      protected String resolveTagName( String fieldTagName, XmlAssociationMetadata xmlAssociationMetadata )
95      {
96          return XmlModelHelpers.resolveTagName( fieldTagName, xmlAssociationMetadata );
97      }
98  
99      /**
100      * Get the field which type is <code>Content</code> if any.
101      *
102      * @param modelFields the fields to check
103      * @return the field, or <code>null</code> if no field is <code>Content</code>
104      */
105     protected ModelField getContentField( List<ModelField> modelFields )
106     {
107         return XmlModelHelpers.getContentField( modelFields );
108     }
109 
110     /**
111      * Return the XML fields of this class, with proper XML order and no XML transient fields.
112      *
113      * @param modelClass current class
114      * @param version the version of the class to use
115      * @return the list of XML fields of this class
116      */
117     protected List<ModelField> getFieldsForXml( ModelClass modelClass, Version version )
118     {
119         return XmlModelHelpers.getFieldsForXml( modelClass, version );
120     }
121 
122     protected String getValue( String type, String initialValue, XmlFieldMetadata xmlFieldMetadata )
123     {
124         String textValue = initialValue;
125 
126         if ( "Date".equals( type ) )
127         {
128             String dateFormat = xmlFieldMetadata.getFormat();
129             if ( xmlFieldMetadata.getFormat() == null )
130             {
131                 dateFormat = DEFAULT_DATE_FORMAT;
132             }
133 
134             if ( "long".equals( dateFormat ) )
135             {
136                 textValue = "String.valueOf( " + textValue + ".getTime() )";
137             }
138             else
139             {
140                 textValue =
141                     "new java.text.SimpleDateFormat( \"" + dateFormat + "\", java.util.Locale.US ).format( " + textValue + " )";
142             }
143         }
144         else if ( !"String".equals( type ) )
145         {
146             textValue = "String.valueOf( " + textValue + " )";
147         }
148 
149         return textValue;
150     }
151 
152     protected void writeDateParsingHelper( JSourceCode sc, String exception )
153     {
154         sc.add( "if ( s != null )" );
155 
156         sc.add( "{" );
157         sc.indent();
158 
159         sc.add( "String effectiveDateFormat = dateFormat;" );
160 
161         sc.add( "if ( dateFormat == null )" );
162 
163         sc.add( "{" );
164         sc.addIndented( "effectiveDateFormat = \"" + DEFAULT_DATE_FORMAT + "\";" );
165         sc.add( "}" );
166 
167         sc.add( "if ( \"long\".equals( effectiveDateFormat ) )" );
168 
169         // parse date as a long
170         sc.add( "{" );
171         sc.indent();
172 
173         sc.add( "try" );
174 
175         sc.add( "{" );
176         sc.addIndented( "return new java.util.Date( Long.parseLong( s ) );" );
177         sc.add( "}" );
178 
179         sc.add( "catch ( NumberFormatException e )" );
180 
181         sc.add( "{" );
182         sc.addIndented( "throw " + exception + ";" );
183         sc.add( "}" );
184 
185         sc.unindent();
186         sc.add( "}" );
187 
188         sc.add( "else" );
189 
190         // parse date as a SimpleDateFormat expression
191         sc.add( "{" );
192         sc.indent();
193 
194         sc.add( "try" );
195         sc.add( "{" );
196         sc.indent();
197 
198         sc.add( "DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );" );
199         sc.add( "return dateParser.parse( s );" );
200 
201         sc.unindent();
202         sc.add( "}" );
203 
204         sc.add( "catch ( java.text.ParseException e )" );
205         sc.add( "{" );
206         sc.addIndented( "throw " + exception + ";" );
207         sc.add( "}" );
208 
209         sc.unindent();
210         sc.add( "}" );
211 
212         sc.unindent();
213         sc.add( "}" );
214 
215         sc.add( "return null;" );
216     }
217 }