View Javadoc

1   /*
2   
3       dsh-codegen  Source code generation suite.
4       Copyright (c) 2004-2011 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.codegen;
25  
26  import java.io.File;
27  import java.io.FileWriter;
28  import java.io.IOException;
29  
30  import java.util.Properties;
31  
32  import org.apache.commons.lang.StringUtils;
33  
34  import org.apache.commons.io.FileUtils;
35  
36  import org.apache.velocity.Template;
37  import org.apache.velocity.VelocityContext;
38  
39  import org.apache.velocity.app.Velocity;
40  
41  /**
42   * Static source code generation methods.
43   *
44   * @author  Michael Heuer
45   * @version $Revision: 994 $ $Date: 2011-02-25 15:37:32 -0600 (Fri, 25 Feb 2011) $
46   */
47  public final class Codegen
48  {
49  
50      /**
51       * Private default constructor.
52       */
53      private Codegen()
54      {
55          // empty
56      }
57  
58  
59      /**
60       * Read license text from the specified file.
61       *
62       * @param license license file to read
63       * @return license text from the specified file or null if the
64       *   specified license file cannot be read
65       */
66      public static String readLicense(final String license)
67      {
68          String text = null;
69          try
70          {
71              text = FileUtils.readFileToString(new File(license));
72          }
73          catch (IOException e)
74          {
75              // ignore
76          }
77          return text;
78      }
79  
80      /**
81       * Generate a java source file for the specified interface
82       * description.
83       *
84       * @param id interface description, must not be null
85       */
86      public static void generateSource(final InterfaceDescription id)
87      {
88          if (id == null)
89          {
90              throw new IllegalArgumentException("id must not be null");
91          }
92  
93          FileWriter fw = null;
94          try
95          {
96              Properties p = new Properties();
97              p.setProperty("resource.loader", "class");
98              p.setProperty("class.resource.loader.class",
99                            "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
100 
101             Velocity.init(p);
102 
103             fw = new FileWriter(id.getUpper() + ".java");
104 
105             VelocityContext context = new VelocityContext();
106             context.put("id", id);
107             context.put("CodegenUtils", new CodegenUtils());
108             context.put("StringUtils", new StringUtils());
109 
110             Template template = Velocity.getTemplate("org/dishevelled/codegen/Interface.wm");
111             template.merge(context, fw);
112         }
113         catch (Exception e)
114         {
115             e.printStackTrace();
116             System.exit(1);
117         }
118         finally
119         {
120             try
121             {
122                 fw.close();
123             }
124             catch (Exception e)
125             {
126                 // empty
127             }
128         }
129     }
130 
131     /**
132      * Generate an abstract unit test java source file for the specified interface
133      * description.
134      *
135      * @param id interface description, must not be null
136      */
137     public static void generateAbstractUnitTest(final InterfaceDescription id)
138     {
139         if (id == null)
140         {
141             throw new IllegalArgumentException("id must not be null");
142         }
143 
144         FileWriter fw = null;
145         try
146         {
147             Properties p = new Properties();
148             p.setProperty("resource.loader", "class");
149             p.setProperty("class.resource.loader.class",
150                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
151 
152             Velocity.init(p);
153 
154             fw = new FileWriter("Abstract" + id.getUpper() + "Test.java");
155 
156             VelocityContext context = new VelocityContext();
157             context.put("id", id);
158             context.put("CodegenUtils", new CodegenUtils());
159             context.put("StringUtils", new StringUtils());
160 
161             Template template = Velocity.getTemplate("org/dishevelled/codegen/AbstractInterfaceTest.wm");
162             template.merge(context, fw);
163         }
164         catch (Exception e)
165         {
166             e.printStackTrace();
167             System.exit(1);
168         }
169         finally
170         {
171             try
172             {
173                 fw.close();
174             }
175             catch (Exception e)
176             {
177                 // empty
178             }
179         }
180     }
181 
182     /**
183      * Generate a java source file for the specified class
184      * description and style.
185      *
186      * @param cd class description, must not be null
187      * @param style style, must not be null
188      */
189     public static void generateSource(final ClassDescription cd, final Style style)
190     {
191         if (cd == null)
192         {
193             throw new IllegalArgumentException("cd must not be null");
194         }
195         if (style == null)
196         {
197             throw new IllegalArgumentException("style must not be null");
198         }
199 
200         FileWriter fw = null;
201         try
202         {
203             Properties p = new Properties();
204             p.setProperty("resource.loader", "class");
205             p.setProperty("class.resource.loader.class",
206                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
207 
208             Velocity.init(p);
209 
210             fw = new FileWriter(cd.getUpper() + ".java");
211 
212             VelocityContext context = new VelocityContext();
213             context.put("cd", cd);
214             context.put("CodegenUtils", new CodegenUtils());
215             context.put("StringUtils", new StringUtils());
216 
217             Template template = Velocity.getTemplate("org/dishevelled/codegen/" + style + ".wm");
218             template.merge(context, fw);
219         }
220         catch (Exception e)
221         {
222             e.printStackTrace();
223             System.exit(1);
224         }
225         finally
226         {
227             try
228             {
229                 fw.close();
230             }
231             catch (Exception e)
232             {
233                 // empty
234             }
235         }
236     }
237 
238     /**
239      * Generate a java source file for a fluent builder API for the specified class
240      * description and style.
241      *
242      * @param cd class description, must not be null
243      * @param style style, must not be null
244      */
245     public static void generateBuilderSource(final ClassDescription cd, final Style style)
246     {
247         // TODO:  perhaps style doesn't matter here, if all the templates are the same
248         if (cd == null)
249         {
250             throw new IllegalArgumentException("cd must not be null");
251         }
252         if (style == null)
253         {
254             throw new IllegalArgumentException("style must not be null");
255         }
256 
257         FileWriter fw = null;
258         try
259         {
260             Properties p = new Properties();
261             p.setProperty("resource.loader", "class");
262             p.setProperty("class.resource.loader.class",
263                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
264 
265             Velocity.init(p);
266 
267             fw = new FileWriter(cd.getUpper() + "Builder.java");
268 
269             VelocityContext context = new VelocityContext();
270             context.put("cd", cd);
271             context.put("CodegenUtils", new CodegenUtils());
272             context.put("StringUtils", new StringUtils());
273 
274             Template template = Velocity.getTemplate("org/dishevelled/codegen/" + style + "Builder.wm");
275             template.merge(context, fw);
276         }
277         catch (Exception e)
278         {
279             e.printStackTrace();
280             System.exit(1);
281         }
282         finally
283         {
284             try
285             {
286                 fw.close();
287             }
288             catch (Exception e)
289             {
290                 // empty
291             }
292         }
293     }
294 
295     /**
296      * Generate a unit test source file for the specified class
297      * description and style.
298      *
299      * @param cd class description, must not be null
300      * @param style style, must not be null
301      */
302     public static void generateUnitTest(final ClassDescription cd, final Style style)
303     {
304         if (cd == null)
305         {
306             throw new IllegalArgumentException("cd must not be null");
307         }
308         if (style == null)
309         {
310             throw new IllegalArgumentException("style must not be null");
311         }
312 
313         FileWriter fw = null;
314         try
315         {
316             Properties p = new Properties();
317             p.setProperty("resource.loader", "class");
318             p.setProperty("class.resource.loader.class",
319                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
320 
321             Velocity.init(p);
322 
323             fw = new FileWriter(cd.getUpper() + "Test.java");
324 
325             VelocityContext context = new VelocityContext();
326             context.put("cd", cd);
327             context.put("CodegenUtils", new CodegenUtils());
328             context.put("StringUtils", new StringUtils());
329 
330             Template template = Velocity.getTemplate("org/dishevelled/codegen/" + style + "Test.wm");
331             template.merge(context, fw);
332         }
333         catch (Exception e)
334         {
335             e.printStackTrace();
336             System.exit(1);
337         }
338         finally
339         {
340             try
341             {
342                 fw.close();
343             }
344             catch (Exception e)
345             {
346                 // empty
347             }
348         }
349     }
350 
351     /**
352      * Generate an enum source file for the specified class description.
353      *
354      * @param cd class description, must not be null
355      */
356     public static void generateEnum(final ClassDescription cd)
357     {
358         if (cd == null)
359         {
360             throw new IllegalArgumentException("cd must not be null");
361         }
362 
363         FileWriter fw = null;
364         try
365         {
366             Properties p = new Properties();
367             p.setProperty("resource.loader", "class");
368             p.setProperty("class.resource.loader.class",
369                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
370 
371             Velocity.init(p);
372 
373             fw = new FileWriter(cd.getUpper() + ".java");
374 
375             VelocityContext context = new VelocityContext();
376             context.put("cd", cd);
377             context.put("CodegenUtils", new CodegenUtils());
378             context.put("StringUtils", new StringUtils());
379 
380             Template template = Velocity.getTemplate("org/dishevelled/codegen/Enum.wm");
381             template.merge(context, fw);
382         }
383         catch (Exception e)
384         {
385             e.printStackTrace();
386             System.exit(1);
387         }
388         finally
389         {
390             try
391             {
392                 fw.close();
393             }
394             catch (Exception e)
395             {
396                 // empty
397             }
398         }
399     }
400 
401     /**
402      * Generate an enum with lookup source file for the specified class description.
403      *
404      * @param cd class description, must not be null
405      */
406     public static void generateEnumWithLookup(final ClassDescription cd)
407     {
408         if (cd == null)
409         {
410             throw new IllegalArgumentException("cd must not be null");
411         }
412 
413         FileWriter fw = null;
414         try
415         {
416             Properties p = new Properties();
417             p.setProperty("resource.loader", "class");
418             p.setProperty("class.resource.loader.class",
419                           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
420 
421             Velocity.init(p);
422 
423             fw = new FileWriter(cd.getUpper() + ".java");
424 
425             VelocityContext context = new VelocityContext();
426             context.put("cd", cd);
427             context.put("CodegenUtils", new CodegenUtils());
428             context.put("StringUtils", new StringUtils());
429 
430             Template template = Velocity.getTemplate("org/dishevelled/codegen/EnumWithLookup.wm");
431             template.merge(context, fw);
432         }
433         catch (Exception e)
434         {
435             e.printStackTrace();
436             System.exit(1);
437         }
438         finally
439         {
440             try
441             {
442                 fw.close();
443             }
444             catch (Exception e)
445             {
446                 // empty
447             }
448         }
449     }
450 }