1 23 24 package org.objectweb.jorm.util.lib; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.taskdefs.MatchingTask; 28 import org.apache.tools.ant.taskdefs.UpToDate; 29 import org.apache.tools.ant.taskdefs.Javac; 30 import org.apache.tools.ant.types.DTDLocation; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.types.FileSet; 33 import org.apache.tools.ant.types.Reference; 34 import org.objectweb.jorm.api.PException; 35 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 36 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 37 import org.objectweb.jorm.compiler.lib.JormCompiler; 38 import org.objectweb.jorm.compiler.lib.JormCompilerParameterImpl; 39 import org.objectweb.jorm.compiler.lib.JormCompilerConfiguratorImpl; 40 import org.objectweb.jorm.util.io.api.PathExplorer; 41 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 42 43 import java.io.File ; 44 import java.util.ArrayList ; 45 import java.util.StringTokenizer ; 46 47 50 public class AntJorm extends MatchingTask { 51 private String jormConfFile = "jorm.properties"; 52 private File pdDir = null; 53 54 private boolean antDtdLocationsSet = false; 55 private ArrayList antDtdLocations = new ArrayList (); 56 private boolean antJavacSet = false; 57 private boolean antJavac; 58 private boolean classpathSet = false; 59 private PathExplorer classpath = new DirJavaExplorer(); 60 private boolean antOutputSet = false; 61 private String antOutput; 62 private boolean antLogConfFileSet = false; 63 private String antLogConfFile; 64 private boolean antProjectNameSet = false; 65 private String antProjectName; 66 private boolean antBindingInheritanceSet = false; 67 private String antBindingInheritance; 68 private boolean antClassMappingInheritanceSet = false; 69 private String antClassMappingInheritance; 70 private boolean antVerboseSet = false; 71 private boolean antVerbose; 72 private boolean antKeepSrcSet = false; 73 private boolean antKeepSrc; 74 private boolean antBindingAbstractSet = false; 75 private boolean antBindingAbstract; 76 private boolean generatedPDFiles = false; 77 private String mapperNames = null; 78 79 private JormCompilerParameter compilerParameter = null; 80 private JormCompilerConfigurator compilerConfigurator = null; 81 private JormCompiler compiler = null; 82 83 public AntJorm() { 84 compilerParameter = new JormCompilerParameterImpl(); 85 compilerConfigurator = new JormCompilerConfiguratorImpl(); 86 compiler = new JormCompiler(compilerParameter, compilerConfigurator); 87 } 88 89 93 public void setPdDir(File d) { 94 pdDir = d; 95 classpath.addPath(d.getAbsolutePath()); 96 } 97 98 104 public DTDLocation createDTD() { 105 DTDLocation dtdLocation = new DTDLocation(); 106 antDtdLocations.add(dtdLocation); 107 antDtdLocationsSet = true; 108 return dtdLocation; 109 } 110 111 119 public void setJavac(boolean l) { 120 antJavac = l; 121 antJavacSet = true; 122 } 123 124 131 public void setMapperNames(String mapperNames) { 132 if (mapperNames != null && mapperNames.length() > 0) { 133 this.mapperNames = mapperNames; 134 } 135 } 136 137 142 public void setOtherPdFiles(Path cp) { 143 String [] dl = cp.list(); 144 for (int i = 0; i < dl.length; i++) { 145 classpath.addPath(dl[i]); 146 classpathSet = true; 147 } 148 } 149 150 public void setOtherPdFilesRef(Reference r) { 151 Path p = new Path(project); 152 p.setRefid(r); 153 setOtherPdFiles(p); 154 } 155 156 162 public void setOutput(String s) { 163 antOutput = s; 164 antOutputSet = true; 165 } 166 167 171 public void setJormConf(String s) { 172 jormConfFile = s; 173 } 174 175 179 public void setLogConf(String s) { 180 antLogConfFile = s; 181 antLogConfFileSet = true; 182 } 183 184 190 public void setProjectName(String s) { 191 antProjectName = s; 192 antProjectNameSet = true; 193 } 194 195 202 public void setBindingInheritance(String s) { 203 antBindingInheritance = s; 204 antBindingInheritanceSet = true; 205 } 206 207 214 public void setMappingInheritance(String s) { 215 antClassMappingInheritance = s; 216 antClassMappingInheritanceSet = true; 217 } 218 219 225 public void setVerbose(boolean verbose) { 226 antVerbose = verbose; 227 antVerboseSet = true; 228 } 229 230 237 public void setKeepsrc(boolean keepsrc) { 238 antKeepSrc = keepsrc; 239 antKeepSrcSet = true; 240 } 241 242 250 public void setBindingAbstract(boolean bindingAbstract) { 251 antBindingAbstract = bindingAbstract; 252 antBindingAbstractSet = true; 253 } 254 255 256 public void setGeneratePDFiles(boolean val) { 257 generatedPDFiles = val; 258 } 259 260 263 public void execute() throws BuildException { 264 try { 265 compilerConfigurator.configure(jormConfFile); 266 if (!antLogConfFileSet) { 267 compilerConfigurator.configureLog(jormConfFile); 268 } else { 269 compilerConfigurator.configureLog(antLogConfFile); 270 } 271 if (mapperNames != null) { 272 StringTokenizer st = new StringTokenizer (mapperNames.trim(), ".,", true); 273 if (st.hasMoreTokens()) { 274 compilerConfigurator.removeAllMappers(); 275 } 276 while (st.hasMoreTokens()) { 277 String mn = st.nextToken(), smn; 278 if (mn.equals(".") || mn.equals(",")) { 279 String errmsg = "Malformed mapperNames string (expect mapper name): " + mapperNames; 280 log(errmsg, project.MSG_ERR); 281 throw new BuildException(errmsg); 282 } 283 if (!st.hasMoreTokens()) { 284 String errmsg = "Malformed mapperNames string (expect \".\"): " + mapperNames; 285 log(errmsg, project.MSG_ERR); 286 throw new BuildException(errmsg); 287 } 288 smn = st.nextToken(); 289 if (!smn.equals(".")) { 290 String errmsg = "Malformed mapperNames string (expect \".\"): " + mapperNames; 291 log(errmsg, project.MSG_ERR); 292 throw new BuildException(errmsg); 293 } 294 if (!st.hasMoreTokens()) { 295 String errmsg = "Malformed mapperNames string (expect submapper name): " + mapperNames; 296 log(errmsg, project.MSG_ERR); 297 throw new BuildException(errmsg); 298 } 299 smn = st.nextToken(); 300 compilerConfigurator.addSubMapper(mn, smn); 301 if (st.hasMoreTokens()) { 302 smn = st.nextToken(); 303 if (!smn.equals(",")) { 304 String errmsg = "Malformed mapperNames string (expect \",\"): " + mapperNames; 305 log(errmsg, project.MSG_ERR); 306 throw new BuildException(errmsg); 307 } 308 } 309 } 310 } 311 compilerParameter.loadConfFile(compilerConfigurator.getGlobalJormcOptsFile(), 312 compilerConfigurator.knownMappers()); 313 if (antDtdLocationsSet) { 314 compilerParameter.setDtdLocations(antDtdLocations); 315 } 316 if (antJavacSet) { 317 compilerParameter.setJavac(antJavac); 318 } 319 if (classpathSet) { 320 compilerParameter.setClasspath(classpath); 321 } 322 if (antOutputSet) { 323 compilerParameter.setOutput(antOutput); 324 } 325 if (antLogConfFileSet) { 326 compilerParameter.setLogConfFile(antLogConfFile); 327 } 328 if (antProjectNameSet) { 329 compilerParameter.setProjectName(antProjectName); 330 } 331 if (antBindingInheritanceSet) { 332 compilerParameter.setBindingInheritance(antBindingInheritance); 333 } 334 if (antClassMappingInheritanceSet) { 335 compilerParameter.setClassMappingInheritance(antClassMappingInheritance); 336 } 337 if (antVerboseSet) { 338 compilerParameter.setVerbose(antVerbose); 339 } 340 if (antKeepSrcSet) { 341 compilerParameter.setKeepSrc(antKeepSrc); 342 } 343 if (antBindingAbstractSet) { 344 compilerParameter.setBindingAbstract(antBindingAbstract); 345 } 346 compilerParameter.setGeneratedPDFiles(generatedPDFiles); 347 } catch (PException e) { 348 Exception current = e; 349 while (current instanceof PException 350 && ((PException) current).getNestedException() != null) { 351 current = ((PException) current).getNestedException(); 352 } 353 throw new BuildException(current); 354 } 355 log("Assigns immutable properties", project.MSG_DEBUG); 356 log("pdDir=" + pdDir, project.MSG_DEBUG); 359 String [] pdFiles = super.getDirectoryScanner(pdDir).getIncludedFiles(); 360 log("Generates(" + pdFiles + ")", project.MSG_DEBUG); 361 generates(pdFiles); 362 } 363 364 public void generates(String [] pdFileNames) throws BuildException { 365 ArrayList compileList = new ArrayList (); 367 for (int i = 0; i < pdFileNames.length; i++) { 368 int idx = pdFileNames[i].lastIndexOf(File.separatorChar); 369 String packageName; 370 String className; 371 if (idx == -1) { 372 packageName = null; 373 className = pdFileNames[i]; 374 } else { 375 packageName = pdFileNames[i].substring(0, idx); 376 className = pdFileNames[i].substring(idx + 1); 377 } 378 className = className.substring(0, className.lastIndexOf('.')); 379 log("-packageName=" + packageName, project.MSG_DEBUG); 380 log("-className=" + className, project.MSG_DEBUG); 381 File pdFile = new File (pdDir, pdFileNames[i]); 382 log("-pdfile=" + pdFile, project.MSG_DEBUG); 383 FileSet fs = new FileSet(); 384 fs.setProject(project); 385 fs.setDir(pdDir); 386 fs.setIncludes(pdFileNames[i]); 387 UpToDate u = new UpToDate(); 388 u.setProject(project); 389 u.addSrcfiles(fs); 390 String pngFileName = compilerParameter.getOutput() + File.separator 392 + (packageName == null ? "" : packageName + File.separator) 393 + File.separator + className + "PNG.java"; 394 log("\tpngFileName=" + pngFileName, project.MSG_DEBUG); 395 String accFileName = compilerParameter.getOutput() + File.separator 396 + (packageName == null ? "" : packageName + File.separator) 397 + File.separator + className + "Accessor.java"; 398 log("\taccFileName=" + accFileName, project.MSG_DEBUG); 399 boolean mustGenerate; 400 File pngFile = new File (pngFileName); 401 File accFile = new File (accFileName); 402 if (accFile.exists()) { 403 u.setTargetFile(accFile); 404 mustGenerate = !u.eval(); 405 } else if (pngFile.exists()) { 406 u.setTargetFile(pngFile); 407 mustGenerate = !u.eval(); 408 } else { 409 mustGenerate = true; 411 } 412 if (mustGenerate) 413 compileList.add(pdFileNames[i]); 414 } 415 if (compileList.size() > 0) { 416 project.log("not upToDate", project.MSG_DEBUG); 417 compilerParameter.setInputFiles(compileList); 418 try { 419 boolean javac = compilerParameter.isJavac(); 420 compilerParameter.setJavac(false); 421 compiler.process(); 422 if (javac) { 423 log("javac compilation", project.MSG_DEBUG); 424 Javac javacinst = new Javac(); 426 Path path = new Path(project, compilerParameter.getOutput()); 427 javacinst.setProject(project); 428 javacinst.setSrcdir(path); 429 javacinst.setDestdir(new File (compilerParameter.getOutput())); 430 javacinst.setDebug(true); 431 javacinst.setIncludes("**/*.java"); 432 javacinst.execute(); 434 } 435 } catch (PException e) { 436 Exception current = e; 437 while (current instanceof PException 438 && ((PException) current).getNestedException() != null) { 439 current = ((PException) current).getNestedException(); 440 } 441 current.printStackTrace(); 442 throw new BuildException(current); 443 } 444 } else { 445 project.log("upToDate", project.MSG_DEBUG); 446 } 447 } 448 } 449 | Popular Tags |