View Javadoc

1   package org.codehaus.modello.plugin;
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.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Properties;
29  
30  import org.codehaus.modello.ModelloException;
31  import org.codehaus.modello.ModelloParameterConstants;
32  import org.codehaus.modello.ModelloRuntimeException;
33  import org.codehaus.modello.model.Model;
34  import org.codehaus.modello.model.ModelAssociation;
35  import org.codehaus.modello.model.ModelClass;
36  import org.codehaus.modello.model.ModelDefault;
37  import org.codehaus.modello.model.ModelField;
38  import org.codehaus.modello.model.Version;
39  import org.codehaus.plexus.PlexusConstants;
40  import org.codehaus.plexus.PlexusContainer;
41  import org.sonatype.plexus.build.incremental.BuildContext;
42  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
43  import org.codehaus.plexus.context.Context;
44  import org.codehaus.plexus.context.ContextException;
45  import org.codehaus.plexus.logging.AbstractLogEnabled;
46  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
47  import org.codehaus.plexus.util.StringUtils;
48  
49  /**
50   * @author <a href="mailto:jason@modello.org">Jason van Zyl</a>
51   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
52   */
53  public abstract class AbstractModelloGenerator
54      extends AbstractLogEnabled
55      implements ModelloGenerator, Contextualizable
56  {
57      private Model model;
58  
59      private File outputDirectory;
60  
61      private Version generatedVersion;
62  
63      private boolean packageWithVersion;
64  
65      private String encoding;
66  
67      private BuildContext buildContext;
68  
69      protected void initialize( Model model, Properties parameters )
70          throws ModelloException
71      {
72          this.model = model;
73  
74          outputDirectory = new File( getParameter( parameters, ModelloParameterConstants.OUTPUT_DIRECTORY ) );
75  
76          String version = getParameter( parameters, ModelloParameterConstants.VERSION );
77  
78          generatedVersion = new Version( version );
79  
80          packageWithVersion =
81              Boolean.valueOf( getParameter( parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION ) );
82  
83          encoding = parameters.getProperty( ModelloParameterConstants.ENCODING );
84      }
85  
86      protected Model getModel()
87      {
88          return model;
89      }
90  
91      protected Version getGeneratedVersion()
92      {
93          return generatedVersion;
94      }
95  
96      protected boolean isPackageWithVersion()
97      {
98          return packageWithVersion;
99      }
100 
101     public File getOutputDirectory()
102     {
103         return outputDirectory;
104     }
105 
106     protected String getEncoding()
107     {
108         return encoding;
109     }
110 
111     protected String getHeader()
112     {
113         String version = getClass().getPackage().getImplementationVersion();
114         return "=================== DO NOT EDIT THIS FILE ====================\n"
115             + "Generated by Modello" + ( ( version == null ) ? "" : ( ' ' + version ) ) + ",\n"
116             + "any modifications will be overwritten.\n"
117             + "==============================================================";
118     }
119 
120     protected boolean isClassInModel( String fieldType, Model model )
121     {
122         try
123         {
124             return model.getClass( fieldType, generatedVersion ) != null;
125         }
126         catch ( Exception e )
127         {
128         }
129 
130         return false;
131     }
132 
133     /**
134      * Return the child fields of this class.
135      * @param modelClass current class
136      * @return the list of fields of this class
137      */
138     protected List<ModelField> getFieldsForClass( ModelClass modelClass )
139     {
140         List<ModelField> fields = new ArrayList<ModelField>();
141 
142         while ( modelClass != null )
143         {
144             fields.addAll( modelClass.getFields( getGeneratedVersion() ) );
145 
146             String superClass = modelClass.getSuperClass();
147             if ( superClass != null )
148             {
149                 modelClass = getModel().getClass( superClass, getGeneratedVersion() );
150             }
151             else
152             {
153                 modelClass = null;
154             }
155         }
156 
157         return fields;
158     }
159 
160     protected boolean isInnerAssociation( ModelField field )
161     {
162         return field instanceof ModelAssociation
163             && isClassInModel( ( (ModelAssociation) field ).getTo(), getModel() );
164     }
165 
166     protected boolean isMap( String fieldType )
167     {
168         return ModelDefault.MAP.equals( fieldType ) || ModelDefault.PROPERTIES.equals( fieldType );
169     }
170 
171     protected boolean isCollection( String fieldType )
172     {
173         return ModelDefault.LIST.equals( fieldType ) || ModelDefault.SET.equals( fieldType );
174     }
175 
176     protected String capitalise( String str )
177     {
178         if ( StringUtils.isEmpty( str ) )
179         {
180             return str;
181         }
182 
183         return new StringBuffer( str.length() )
184             .append( Character.toTitleCase( str.charAt( 0 ) ) )
185             .append( str.substring( 1 ) )
186             .toString();
187     }
188 
189     public static String singular( String name )
190     {
191         if ( StringUtils.isEmpty( name ) )
192         {
193             return name;
194         }
195 
196         if ( name.endsWith( "ies" ) )
197         {
198             return name.substring( 0, name.length() - 3 ) + "y";
199         }
200         else if ( name.endsWith( "es" ) && name.endsWith( "ches" ) )
201         {
202             return name.substring( 0, name.length() - 2 );
203         }
204         else if ( name.endsWith( "xes" ) )
205         {
206             return name.substring( 0, name.length() - 2 );
207         }
208         else if ( name.endsWith( "s" ) && ( name.length() != 1 ) )
209         {
210             return name.substring( 0, name.length() - 1 );
211         }
212 
213         return name;
214     }
215 
216     public static String uncapitalise( String str )
217     {
218         if ( StringUtils.isEmpty( str ) )
219         {
220             return str;
221         }
222 
223         return new StringBuffer( str.length() )
224             .append( Character.toLowerCase( str.charAt( 0 ) ) )
225             .append( str.substring( 1 ) )
226             .toString();
227     }
228 
229     // ----------------------------------------------------------------------
230     // Text utils
231     // ----------------------------------------------------------------------
232 
233     protected boolean isEmpty( String string )
234     {
235         return string == null || string.trim().length() == 0;
236     }
237 
238     // ----------------------------------------------------------------------
239     // Parameter utils
240     // ----------------------------------------------------------------------
241 
242     /**
243      * @deprecated Use {@link #getParameter(Properties, String)} instead
244      */
245     protected String getParameter( String name, Properties parameters )
246     {
247         return getParameter( parameters, name );
248     }
249 
250     protected String getParameter( Properties parameters, String name )
251     {
252         String value = parameters.getProperty( name );
253 
254         if ( value == null )
255         {
256             throw new ModelloRuntimeException( "Missing parameter '" + name + "'." );
257         }
258 
259         return value;
260     }
261 
262     protected String getParameter( Properties parameters, String name, String defaultValue )
263     {
264         String value = parameters.getProperty( name );
265 
266         if ( value == null )
267         {
268             return defaultValue;
269         }
270 
271         return value;
272     }
273 
274     public void contextualize( Context ctx )
275         throws ContextException
276     {
277         PlexusContainer plexus = (PlexusContainer) ctx.get( PlexusConstants.PLEXUS_KEY );
278 
279         try
280         {
281             buildContext = (BuildContext) plexus.lookup( BuildContext.class.getName() );
282         }
283         catch( ComponentLookupException e )
284         {
285             throw new ContextException( "Unable to lookup required component", e );
286         }
287     }
288 
289     protected BuildContext getBuildContext()
290     {
291         return buildContext;
292     }
293 }