1 18 package net.sf.uitags.build; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.PrintStream ; 24 import java.io.StringWriter ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Properties ; 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 ; 39 40 49 public final class ElSourceCodeGenerator extends AbstractMojo { 50 54 59 private String tldFile; 60 65 private String elTldFile; 66 71 private String outputDir; 72 73 74 78 private Map tagMap; 80 private String serialVersionUid; 81 82 83 87 90 private void init() { 91 Document tldDocument = TldUtils.getDocument(this.tldFile); 92 Document 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 107 private void ensureDirectoryExists(File directory) { 108 if (!directory.exists()) { 109 directory.mkdir(); 110 } 111 else if (!directory.isDirectory()) { 112 throw new IllegalArgumentException ( 113 directory.getName() + " is not a directory."); 114 } 115 } 116 117 private void generateCodeFor(Tag tag) { 118 try { 119 String outputPath = this.outputDir + tag.getElHandlerPath() + ".java"; 120 ensureDirectoryExists(new File (outputPath).getParentFile()); 121 122 Properties properties = new Properties (); 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 params = null; 129 130 params = new HashMap (); 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 (); 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 catch (FileNotFoundException fnfe) { 148 throw new RuntimeException (fnfe); 149 } 150 catch (ResourceNotFoundException rnfe) { 151 throw new RuntimeException (rnfe); 152 } 153 catch (MethodInvocationException mie) { 154 throw new RuntimeException (mie); 155 } 156 catch (ParseErrorException pee) { 157 throw new RuntimeException (pee); 158 } 159 catch (Exception e) { 160 throw new RuntimeException (e); 161 } 162 } 163 164 private void printToFile( 165 String outputPath, String velocityTemplatePath, Map velocityParams) 166 throws FileNotFoundException , ResourceNotFoundException, 167 MethodInvocationException, ParseErrorException, Exception { 168 PrintStream out = new PrintStream (new FileOutputStream (outputPath)); 169 StringWriter result = new StringWriter (); 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 iter = this.tagMap.entrySet().iterator(); 181 iter.hasNext(); ) { 182 Map.Entry entry = (Map.Entry ) iter.next(); 183 Tag tag = (Tag) entry.getValue(); 184 getLog().info(tag.getName()); 185 generateCodeFor(tag); 186 } 187 getLog().info("Done!"); 188 } 189 190 194 public static void main(String 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 |