1 package ch.paranor.epla.structure; 2 3 import java.io.BufferedOutputStream ; 4 import java.io.File ; 5 import java.io.FileNotFoundException ; 6 import java.io.FileOutputStream ; 7 import java.io.OutputStream ; 8 import java.net.MalformedURLException ; 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.DirectoryScanner; 16 import org.apache.tools.ant.Project; 17 import org.apache.tools.ant.Task; 18 import org.apache.tools.ant.types.FileSet; 19 import org.eclipse.emf.codegen.jet.JETCompiler; 20 import org.eclipse.emf.codegen.jet.JETException; 21 import org.eclipse.emf.codegen.jet.JETSkeleton; 22 23 26 public class JETCTask extends Task { 27 28 private File destDir; 29 30 private List filesets = new ArrayList (); 31 private List templateFiles = new ArrayList (); 32 33 private File templateFile; 34 private String packageName; 35 private String className; 36 37 public void execute() throws BuildException { 38 preCheck(); 39 40 if (templateFile != null) { 42 templateFiles.add(templateFile); 43 } 44 for (Iterator i = filesets.iterator(); i.hasNext();) { 45 FileSet fs = (FileSet) i.next(); 46 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 47 File fromDir = fs.getDir(getProject()); 48 49 String [] includedFiles = ds.getIncludedFiles(); 50 for (int j = 0; j < includedFiles.length; j++) { 51 templateFiles.add(project.resolveFile(includedFiles[j])); 52 } 53 } 54 55 try { 56 compileJETTemplates(); 57 } catch (Exception e) { 58 throw new BuildException(e, location); 59 } 60 } 61 62 public void setDestdir(File destDir) { 63 this.destDir = destDir; 64 } 65 66 public void setTemplate(File templateFile) { 67 this.templateFile = templateFile; 68 } 69 70 public void setPackage(String packageName) { 71 this.packageName = packageName; 72 } 73 74 public void setClass(String className) { 75 this.className = className; 76 } 77 78 public void addFileset(FileSet fileset) { 79 filesets.add(fileset); 80 } 81 82 private void preCheck() throws BuildException { 83 assertTrue(destDir != null, "No destination directory specified"); 84 assertTrue( 85 destDir.isDirectory(), 86 "Specified destination directory does not exist: " + destDir); 87 assertTrue( 88 templateFile != null || filesets.size() > 0, 89 "No template files specified"); 90 if (templateFile != null) { 91 assertTrue( 92 templateFile.isFile(), 93 "Specified template does not exist: " + templateFile); 94 } else { 95 assertTrue( 96 className == null && packageName == null, 97 "Attributes \"class\" and \"package\" can only be used together with attribute \"template\""); 98 } 99 } 100 101 private void compileJETTemplates() 102 throws MalformedURLException , JETException, FileNotFoundException { 103 log( 104 "Compiling " 105 + templateFiles.size() 106 + " JET template(s) to " 107 + destDir); 108 for (Iterator i = templateFiles.iterator(); i.hasNext();) { 109 File template = (File ) i.next(); 110 compileJETTemplate(template, destDir); 111 } 112 } 113 114 private File compileJETTemplate(File template, File outputDir) 115 throws JETException, FileNotFoundException , MalformedURLException { 116 String templateURI = template.getAbsoluteFile().toURL().toString(); 117 JETCompiler jetCompiler = new JETCompiler(templateURI); 118 log("Parsing template " + template, Project.MSG_VERBOSE); 119 jetCompiler.parse(); 120 121 JETSkeleton skeleton = jetCompiler.getSkeleton(); 123 if (skeleton == null) { 124 log("JET directive missing in " + templateFile, Project.MSG_WARN); 125 jetCompiler.handleDirective("jet", null, null, new HashMap ()); 126 skeleton = jetCompiler.getSkeleton(); 127 } 128 if (className != null) { 129 skeleton.setClassName(className); 130 } 131 if (packageName != null) { 132 skeleton.setPackageName(packageName); 133 } 134 135 File packageDir = 136 new File (outputDir, skeleton.getPackageName().replace('.', '/')); 137 packageDir.mkdirs(); 138 File targetFile = 139 new File (packageDir, skeleton.getClassName() + ".java"); 140 141 OutputStream output = 142 new BufferedOutputStream (new FileOutputStream (targetFile)); 143 log("Generating java source " + targetFile, Project.MSG_VERBOSE); 144 jetCompiler.generate(output); 145 return targetFile; 146 } 147 148 private void assertTrue(boolean expr, String message) 149 throws BuildException { 150 if (!expr) { 151 throw new BuildException(message, location); 152 } 153 } 154 155 } 156 | Popular Tags |