View Javadoc

1   package net.sf.stitch.crud;
2   
3   /*
4    * Copyright 2007
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import groovy.text.SimpleTemplateEngine;
20  import groovy.text.Template;
21  import groovy.text.TemplateEngine;
22  
23  import java.io.File;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.io.Reader;
29  import java.io.Writer;
30  import java.util.ArrayList;
31  import java.util.LinkedHashMap;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Properties;
35  
36  import org.apache.maven.plugin.AbstractMojo;
37  import org.apache.maven.plugin.MojoExecutionException;
38  
39  /**
40   * Generates a CRUD interface for Entity Beans.
41   *
42   * @goal crud
43   * @phase process-classes
44   */
45  public class CrudMojo extends AbstractMojo
46  {
47      /**
48       * Location of the file.
49       * @parameter expression="${project.build.directory}/${project.build.finalName}"
50       * @required
51       */
52      private File outputDirectory;
53      
54      /**
55       * List of Entity Classes.
56       * @parameter
57       * @required
58       */
59      private String [] entityClasses;
60      
61      /**
62       * User defined Template Properties.  Additional information passed to templates.
63       * @parameter
64       */
65      private Properties userProperties;
66  
67      /**
68       * Template location.  Users may override the default templates to get a customized look and feel.  
69       * @parameter default-value="net/sf/stitch/crud/templates/"
70       */
71      private String templateLocation;
72  
73      /**
74       * Creates a basic CRUD interface for the specified entity classes.
75       */
76      public void execute() throws MojoExecutionException
77      {
78          getLog().info("CrudMojo.execute");
79  
80          //  Validate that the output directory exists...
81          assert null != this.outputDirectory;
82          if (!this.outputDirectory.exists())
83          {
84              this.outputDirectory.mkdirs();
85          }
86  
87          //  Validate that the entity classes exist...
88          final List<CrudEntity> entityClassList = new ArrayList<CrudEntity>(entityClasses.length);
89          for (final String className : this.entityClasses)
90          {
91              getLog().debug("Loading class: " + className);
92              try
93              {
94                  final CrudEntity entityClass = new CrudEntity(className);
95                  entityClassList.add(entityClass);
96              }
97              catch (ClassNotFoundException e)
98              {
99                  throw new MojoExecutionException ("Unable to load class: " + className, e);
100             }
101         }
102 
103         try
104         {
105             generate(entityClassList);
106         }
107         catch (Exception e)
108         {
109             throw new MojoExecutionException ("Problem generating CRUD!", e);
110         }
111     }
112 
113     public void generate (final List<CrudEntity> entityClassList)
114         throws IOException, ClassNotFoundException
115     {
116         assert null != outputDirectory && outputDirectory.isDirectory();
117 
118         final String [] templateSourceFiles =
119         {
120             "Entity.page.xml",
121             "Entity.xhtml",
122             "EntityEdit.page.xml",
123             "EntityEdit.xhtml",
124             "EntityList.page.xml",
125             "EntityList.xhtml",
126         };
127 
128         final TemplateEngine engine = new SimpleTemplateEngine();
129 
130         //  Load each source file into a template...
131         final Map<String, Template> templateMap = new LinkedHashMap<String,Template>(templateSourceFiles.length);
132         for (final String templateSourceFile : templateSourceFiles)
133         {
134             getLog().debug("Parsing template source: " + templateSourceFile);
135 
136             final InputStream templateStream = getClass().getClassLoader().getResourceAsStream(this.templateLocation + templateSourceFile);
137             assert null != templateStream;
138             final Reader templateReader = new InputStreamReader( templateStream );
139             final Template template = engine.createTemplate( templateReader );
140             templateMap.put(templateSourceFile, template);
141             templateReader.close();
142         }
143 
144         //  Process each template for each entity class...
145         for (final CrudEntity entityClass : entityClassList)
146         {
147             getLog().info("Creating CRUD interface for: " + entityClass.getEntityShortName());
148 
149             final Map<String,Object> templateParameters = new LinkedHashMap<String,Object>();
150             templateParameters.put("crudEntity", entityClass);
151             templateParameters.put("userProperties", this.userProperties);
152 
153             for (final Map.Entry<String, Template> templateEntry : templateMap.entrySet())
154             {
155                 final String filename = templateEntry.getKey().replace("Entity", entityClass.getEntityShortName());
156                 final Template template = templateEntry.getValue();
157                 final File outputFile = new File(outputDirectory, filename);
158                 final Writer outputFileWriter = new FileWriter (outputFile);
159                 try
160                 {
161                     getLog().debug("Making " + outputFile.getPath());
162                     template.make(templateParameters).writeTo(outputFileWriter);
163                 }
164                 finally
165                 {
166                     outputFileWriter.close();
167                 }
168             }
169         }
170     }
171 }