1 17 18 package org.apache.tools.ant.taskdefs.optional.metamata; 19 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.Vector ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.taskdefs.Execute; 28 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 29 import org.apache.tools.ant.taskdefs.LogStreamHandler; 30 import org.apache.tools.ant.types.Commandline; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.util.JavaEnvUtils; 33 34 44 public class MParse extends AbstractMetamataTask { 45 46 private File target = null; 47 private boolean verbose = false; 48 private boolean debugparser = false; 49 private boolean debugscanner = false; 50 private boolean cleanup = false; 51 52 53 public void setTarget(File target) { 54 this.target = target; 55 } 56 57 58 public void setVerbose(boolean flag) { 59 verbose = flag; 60 } 61 62 63 public void setDebugscanner(boolean flag) { 64 debugscanner = flag; 65 } 66 67 68 public void setDebugparser(boolean flag) { 69 debugparser = flag; 70 } 71 72 75 public void setCleanup(boolean value) { 76 cleanup = value; 77 } 78 79 public MParse() { 80 cmdl.setVm(JavaEnvUtils.getJreExecutable("java")); 81 cmdl.setClassname("com.metamata.jj.MParse"); 82 } 83 84 85 86 public void execute() throws BuildException { 87 try { 88 setUp(); 89 ExecuteStreamHandler handler = createStreamHandler(); 90 _execute(handler); 91 } finally { 92 cleanUp(); 93 } 94 } 95 96 97 protected ExecuteStreamHandler createStreamHandler() { 98 return new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_INFO); 99 } 100 101 104 protected void setUp() throws BuildException { 105 checkOptions(); 106 107 File [] jars = getMetamataLibs(); 109 final Path classPath = cmdl.createClasspath(getProject()); 110 for (int i = 0; i < jars.length; i++) { 111 classPath.createPathElement().setLocation(jars[i]); 112 } 113 114 final Commandline.Argument vmArgs = cmdl.createVmArgument(); 116 vmArgs.setValue("-Dmetamata.home=" + metamataHome.getAbsolutePath()); 117 118 119 Vector opts = getOptions(); 121 String [] options = new String [ opts.size() ]; 122 opts.copyInto(options); 123 124 optionsFile = createTmpFile(); 125 generateOptionsFile(optionsFile, options); 126 Commandline.Argument args = cmdl.createArgument(); 127 args.setLine("-arguments " + optionsFile.getAbsolutePath()); 128 } 129 130 131 132 protected void _execute(ExecuteStreamHandler handler) throws BuildException { 133 String pathname = target.getAbsolutePath(); 136 int pos = pathname.length() - ".jj".length(); 137 pathname = pathname.substring(0, pos) + ".java"; 138 File javaFile = new File (pathname); 139 if (javaFile.exists() && target.lastModified() < javaFile.lastModified()) { 140 getProject().log("Target is already build - skipping (" + target + ")"); 141 return; 142 } 143 144 final Execute process = new Execute(handler); 145 log(cmdl.describeCommand(), Project.MSG_VERBOSE); 146 process.setCommandline(cmdl.getCommandline()); 147 try { 148 if (process.execute() != 0) { 149 throw new BuildException("Metamata task failed."); 150 } 151 } catch (IOException e) { 152 throw new BuildException("Failed to launch Metamata task: ", e); 153 } 154 } 155 156 157 protected void cleanUp() { 158 if (optionsFile != null) { 159 optionsFile.delete(); 160 optionsFile = null; 161 } 162 if (cleanup) { 163 String name = target.getName(); 164 int pos = name.length() - ".jj".length(); 165 name = "__jj" + name.substring(0, pos) + ".sunjj"; 166 final File sunjj = new File (target.getParent(), name); 167 if (sunjj.exists()) { 168 getProject().log("Removing stale file: " + sunjj.getName()); 169 sunjj.delete(); 170 } 171 } 172 } 173 174 181 protected File [] getMetamataLibs() { 182 Vector files = new Vector (); 183 files.addElement(new File (metamataHome, "lib/metamata.jar")); 184 files.addElement(new File (metamataHome, "bin/lib/JavaCC.zip")); 185 186 File [] array = new File [ files.size() ]; 187 files.copyInto(array); 188 return array; 189 } 190 191 192 196 protected void checkOptions() throws BuildException { 197 if (metamataHome == null || !metamataHome.exists()) { 199 throw new BuildException("'metamatahome' must point to Metamata home directory."); 200 } 201 metamataHome = getProject().resolveFile(metamataHome.getPath()); 202 203 File [] jars = getMetamataLibs(); 205 for (int i = 0; i < jars.length; i++) { 206 if (!jars[i].exists()) { 207 throw new BuildException(jars[i] 208 + " does not exist. Check your metamata installation."); 209 } 210 } 211 212 if (target == null || !target.isFile() 214 || !target.getName().endsWith(".jj")) { 215 throw new BuildException("Invalid target: " + target); 216 } 217 target = getProject().resolveFile(target.getPath()); 218 } 219 220 224 protected Vector getOptions() { 225 Vector options = new Vector (); 226 if (verbose) { 227 options.addElement("-verbose"); 228 } 229 if (debugscanner) { 230 options.addElement("-ds"); 231 } 232 if (debugparser) { 233 options.addElement("-dp"); 234 } 235 if (classPath != null) { 236 options.addElement("-classpath"); 237 options.addElement(classPath.toString()); 238 } 239 if (sourcePath != null) { 240 options.addElement("-sourcepath"); 241 options.addElement(sourcePath.toString()); 242 } 243 options.addElement(target.getAbsolutePath()); 244 return options; 245 } 246 247 254 protected void generateOptionsFile(File tofile, String [] options) throws BuildException { 255 FileWriter fw = null; 256 try { 257 fw = new FileWriter (tofile); 258 PrintWriter pw = new PrintWriter (fw); 259 for (int i = 0; i < options.length; i++) { 260 pw.println(options[i]); 261 } 262 pw.flush(); 263 } catch (IOException e) { 264 throw new BuildException("Error while writing options file " + tofile, e); 265 } finally { 266 if (fw != null) { 267 try { 268 fw.close(); 269 } catch (IOException ignored) { 270 } 272 } 273 } 274 } 275 } 276 | Popular Tags |