KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > build > ElSourceCodeGenerator


1 /**
2  * Nov 20, 2004
3  *
4  * Copyright 2004 uitags
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 package net.sf.uitags.build;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import net.sf.uitags.build.TldUtils.Tag;
31
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.Velocity;
35 import org.apache.velocity.exception.MethodInvocationException;
36 import org.apache.velocity.exception.ParseErrorException;
37 import org.apache.velocity.exception.ResourceNotFoundException;
38 import org.w3c.dom.Document JavaDoc;
39
40 /**
41  * Generates EL source code for custom tags specified in the TLD.
42  *
43  * @goal generateElSourceCode
44  * @description Generates EL source code for custom tags specified in the TLD.
45  * @author hgani
46  * @author jonni
47  * @version $Id$
48  */

49 public final class ElSourceCodeGenerator extends AbstractMojo {
50   //////////////////////////////////////
51
////////// Maven parameters //////////
52
//////////////////////////////////////
53

54   /**
55    * The name of the TLD file to read for generating non-EL tag handlers.
56    * @parameter
57    * @required
58    */

59   private String JavaDoc tldFile;
60   /**
61    * The name of the TLD file to read for generating EL tag handlers.
62    * @parameter
63    * @required
64    */

65   private String JavaDoc elTldFile;
66   /**
67    * The directory containing the generated source code.
68    * @parameter
69    * @required
70    */

71   private String JavaDoc outputDir;
72
73
74   ////////////////////////////
75
////////// Fields //////////
76
////////////////////////////
77

78   // keyed on tag names
79
private Map JavaDoc tagMap;
80   private String JavaDoc serialVersionUid;
81
82
83   ////////////////////////////////////////////
84
////////// Initialization Methods //////////
85
////////////////////////////////////////////
86

87   /**
88    * Processes Maven parameters and save the results to instance variables.
89    */

90   private void init() {
91     Document JavaDoc tldDocument = TldUtils.getDocument(this.tldFile);
92     Document JavaDoc elTldDocument = TldUtils.getDocument(this.elTldFile);
93     this.tagMap = TldUtils.extractTags(tldDocument, elTldDocument);
94
95     this.serialVersionUid = tldDocument.getElementsByTagName(
96         "tlib-version").item(0).getFirstChild().getNodeValue();
97     this.serialVersionUid = this.serialVersionUid.replaceAll("[^0-9]", "");
98     this.serialVersionUid = this.serialVersionUid.replaceAll("^0*", "");
99     this.serialVersionUid = this.serialVersionUid + "L";
100   }
101
102
103   ////////////////////////////////////
104
////////// Action Methods //////////
105
////////////////////////////////////
106

107   private void ensureDirectoryExists(File JavaDoc directory) {
108     if (!directory.exists()) {
109       directory.mkdir();
110     }
111     else if (!directory.isDirectory()) {
112       throw new IllegalArgumentException JavaDoc(
113           directory.getName() + " is not a directory.");
114     }
115   }
116
117   private void generateCodeFor(Tag tag) {
118     try {
119       String JavaDoc outputPath = this.outputDir + tag.getElHandlerPath() + ".java";
120       ensureDirectoryExists(new File JavaDoc(outputPath).getParentFile());
121
122       Properties JavaDoc properties = new Properties JavaDoc();
123       properties.setProperty("resource.loader", "classpath");
124       properties.setProperty("classpath.resource.loader.class",
125           "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
126       Velocity.init(properties);
127
128       Map JavaDoc params = null;
129
130       params = new HashMap JavaDoc();
131       params.put("serialVersionUid", this.serialVersionUid);
132       params.put("packageName", tag.getPackageNameFraction());
133       params.put("className", tag.getHandlerNameFraction());
134       params.put("elPackageName", tag.getElPackageNameFraction());
135       params.put("elClassName", tag.getElHandlerNameFraction());
136       params.put("attributes", tag.getAttributes());
137       printToFile(outputPath, "net/sf/uitags/build/el-tagHandler.vm", params);
138
139       params = new HashMap JavaDoc();
140       params.put("packageName", tag.getPackageNameFraction());
141       params.put("elPackageName", tag.getElPackageNameFraction());
142       params.put("elClassName", tag.getElHandlerNameFraction());
143       printToFile(this.outputDir + tag.getElHandlerPath() + "BeanInfo.java",
144           "net/sf/uitags/build/el-tagBeanInfo.vm", params);
145     }
146     // Convert checked exceptions into unchecked.
147
catch (FileNotFoundException JavaDoc fnfe) {
148       throw new RuntimeException JavaDoc(fnfe);
149     }
150     catch (ResourceNotFoundException rnfe) {
151       throw new RuntimeException JavaDoc(rnfe);
152     }
153     catch (MethodInvocationException mie) {
154       throw new RuntimeException JavaDoc(mie);
155     }
156     catch (ParseErrorException pee) {
157       throw new RuntimeException JavaDoc(pee);
158     }
159     catch (Exception JavaDoc e) {
160       throw new RuntimeException JavaDoc(e);
161     }
162   }
163
164   private void printToFile(
165       String JavaDoc outputPath, String JavaDoc velocityTemplatePath, Map JavaDoc velocityParams)
166       throws FileNotFoundException JavaDoc, ResourceNotFoundException,
167           MethodInvocationException, ParseErrorException, Exception JavaDoc {
168     PrintStream JavaDoc out = new PrintStream JavaDoc(new FileOutputStream JavaDoc(outputPath));
169     StringWriter JavaDoc result = new StringWriter JavaDoc();
170     Velocity.mergeTemplate(velocityTemplatePath, "ISO8859-1",
171         new VelocityContext(velocityParams), result);
172     out.println(result.toString());
173     out.close();
174   }
175
176   public void execute() {
177     init();
178
179     getLog().info("Generating EL source code for:");
180     for (Iterator JavaDoc iter = this.tagMap.entrySet().iterator();
181         iter.hasNext(); ) {
182       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
183       Tag tag = (Tag) entry.getValue();
184       getLog().info(tag.getName());
185       generateCodeFor(tag);
186     }
187     getLog().info("Done!");
188   }
189
190   ////////////////////////////////////
191
////////// Static Methods //////////
192
////////////////////////////////////
193

194   public static void main(String JavaDoc args[]) {
195     if (args.length != 3) {
196       printUsage();
197     }
198
199     ElSourceCodeGenerator generator = new ElSourceCodeGenerator();
200     generator.tldFile = args[0];
201     generator.elTldFile = args[1];
202     generator.outputDir = args[2];
203     generator.execute();
204   }
205
206   private static void printUsage() {
207     System.err.println("Usage: PROGRAM TLD_FILE EL_TLD_FILE OUTPUT_FOLDER");
208     System.exit(1);
209   }
210 }
211
Popular Tags