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