1 18 19 package org.apache.tools.ant.taskdefs.optional.javacc; 20 21 import java.io.File ; 22 import java.io.InputStream ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 26 import org.apache.tools.ant.AntClassLoader; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.Task; 30 import org.apache.tools.ant.taskdefs.Execute; 31 import org.apache.tools.ant.types.Commandline; 32 import org.apache.tools.ant.types.CommandlineJava; 33 import org.apache.tools.ant.types.Path; 34 import org.apache.tools.ant.util.JavaEnvUtils; 35 36 40 public class JavaCC extends Task { 41 42 private static final String LOOKAHEAD = "LOOKAHEAD"; 44 private static final String CHOICE_AMBIGUITY_CHECK = "CHOICE_AMBIGUITY_CHECK"; 45 private static final String OTHER_AMBIGUITY_CHECK = "OTHER_AMBIGUITY_CHECK"; 46 47 private static final String STATIC = "STATIC"; 48 private static final String DEBUG_PARSER = "DEBUG_PARSER"; 49 private static final String DEBUG_LOOKAHEAD = "DEBUG_LOOKAHEAD"; 50 private static final String DEBUG_TOKEN_MANAGER = "DEBUG_TOKEN_MANAGER"; 51 private static final String OPTIMIZE_TOKEN_MANAGER = "OPTIMIZE_TOKEN_MANAGER"; 52 private static final String ERROR_REPORTING = "ERROR_REPORTING"; 53 private static final String JAVA_UNICODE_ESCAPE = "JAVA_UNICODE_ESCAPE"; 54 private static final String UNICODE_INPUT = "UNICODE_INPUT"; 55 private static final String IGNORE_CASE = "IGNORE_CASE"; 56 private static final String COMMON_TOKEN_ACTION = "COMMON_TOKEN_ACTION"; 57 private static final String USER_TOKEN_MANAGER = "USER_TOKEN_MANAGER"; 58 private static final String USER_CHAR_STREAM = "USER_CHAR_STREAM"; 59 private static final String BUILD_PARSER = "BUILD_PARSER"; 60 private static final String BUILD_TOKEN_MANAGER = "BUILD_TOKEN_MANAGER"; 61 private static final String SANITY_CHECK = "SANITY_CHECK"; 62 private static final String FORCE_LA_CHECK = "FORCE_LA_CHECK"; 63 private static final String CACHE_TOKENS = "CACHE_TOKENS"; 64 private static final String KEEP_LINE_COLUMN = "KEEP_LINE_COLUMN"; 65 private static final String JDK_VERSION = "JDK_VERSION"; 66 67 private final Hashtable optionalAttrs = new Hashtable (); 68 69 private File outputDirectory = null; 71 private File targetFile = null; 72 private File javaccHome = null; 73 74 private CommandlineJava cmdl = new CommandlineJava(); 75 76 protected static final int TASKDEF_TYPE_JAVACC = 1; 77 protected static final int TASKDEF_TYPE_JJTREE = 2; 78 protected static final int TASKDEF_TYPE_JJDOC = 3; 79 80 protected static final String [] ARCHIVE_LOCATIONS = 81 new String [] { 82 "JavaCC.zip", 83 "bin/lib/JavaCC.zip", 84 "bin/lib/javacc.jar", 85 "javacc.jar", }; 87 88 protected static final int[] ARCHIVE_LOCATIONS_VS_MAJOR_VERSION = 89 new int[] { 90 1, 91 2, 92 3, 93 3, 94 }; 95 96 protected static final String COM_PACKAGE = "COM.sun.labs."; 97 protected static final String COM_JAVACC_CLASS = "javacc.Main"; 98 protected static final String COM_JJTREE_CLASS = "jjtree.Main"; 99 protected static final String COM_JJDOC_CLASS = "jjdoc.JJDocMain"; 100 101 protected static final String ORG_PACKAGE_3_0 = "org.netbeans.javacc."; 102 protected static final String ORG_PACKAGE_3_1 = "org.javacc."; 103 protected static final String ORG_JAVACC_CLASS = "parser.Main"; 104 protected static final String ORG_JJTREE_CLASS = COM_JJTREE_CLASS; 105 protected static final String ORG_JJDOC_CLASS = COM_JJDOC_CLASS; 106 107 111 public void setLookahead(int lookahead) { 112 optionalAttrs.put(LOOKAHEAD, new Integer (lookahead)); 113 } 114 115 119 public void setChoiceambiguitycheck(int choiceAmbiguityCheck) { 120 optionalAttrs.put(CHOICE_AMBIGUITY_CHECK, new Integer (choiceAmbiguityCheck)); 121 } 122 123 127 public void setOtherambiguityCheck(int otherAmbiguityCheck) { 128 optionalAttrs.put(OTHER_AMBIGUITY_CHECK, new Integer (otherAmbiguityCheck)); 129 } 130 131 135 public void setStatic(boolean staticParser) { 136 optionalAttrs.put(STATIC, staticParser ? Boolean.TRUE : Boolean.FALSE); 137 } 138 139 143 public void setDebugparser(boolean debugParser) { 144 optionalAttrs.put(DEBUG_PARSER, debugParser ? Boolean.TRUE : Boolean.FALSE); 145 } 146 147 151 public void setDebuglookahead(boolean debugLookahead) { 152 optionalAttrs.put(DEBUG_LOOKAHEAD, debugLookahead ? Boolean.TRUE : Boolean.FALSE); 153 } 154 155 159 public void setDebugtokenmanager(boolean debugTokenManager) { 160 optionalAttrs.put(DEBUG_TOKEN_MANAGER, debugTokenManager ? Boolean.TRUE : Boolean.FALSE); 161 } 162 163 167 public void setOptimizetokenmanager(boolean optimizeTokenManager) { 168 optionalAttrs.put(OPTIMIZE_TOKEN_MANAGER, 169 optimizeTokenManager ? Boolean.TRUE : Boolean.FALSE); 170 } 171 172 176 public void setErrorreporting(boolean errorReporting) { 177 optionalAttrs.put(ERROR_REPORTING, errorReporting ? Boolean.TRUE : Boolean.FALSE); 178 } 179 180 184 public void setJavaunicodeescape(boolean javaUnicodeEscape) { 185 optionalAttrs.put(JAVA_UNICODE_ESCAPE, javaUnicodeEscape ? Boolean.TRUE : Boolean.FALSE); 186 } 187 188 192 public void setUnicodeinput(boolean unicodeInput) { 193 optionalAttrs.put(UNICODE_INPUT, unicodeInput ? Boolean.TRUE : Boolean.FALSE); 194 } 195 196 200 public void setIgnorecase(boolean ignoreCase) { 201 optionalAttrs.put(IGNORE_CASE, ignoreCase ? Boolean.TRUE : Boolean.FALSE); 202 } 203 204 208 public void setCommontokenaction(boolean commonTokenAction) { 209 optionalAttrs.put(COMMON_TOKEN_ACTION, commonTokenAction ? Boolean.TRUE : Boolean.FALSE); 210 } 211 212 216 public void setUsertokenmanager(boolean userTokenManager) { 217 optionalAttrs.put(USER_TOKEN_MANAGER, userTokenManager ? Boolean.TRUE : Boolean.FALSE); 218 } 219 220 224 public void setUsercharstream(boolean userCharStream) { 225 optionalAttrs.put(USER_CHAR_STREAM, userCharStream ? Boolean.TRUE : Boolean.FALSE); 226 } 227 228 232 public void setBuildparser(boolean buildParser) { 233 optionalAttrs.put(BUILD_PARSER, buildParser ? Boolean.TRUE : Boolean.FALSE); 234 } 235 236 240 public void setBuildtokenmanager(boolean buildTokenManager) { 241 optionalAttrs.put(BUILD_TOKEN_MANAGER, buildTokenManager ? Boolean.TRUE : Boolean.FALSE); 242 } 243 244 248 public void setSanitycheck(boolean sanityCheck) { 249 optionalAttrs.put(SANITY_CHECK, sanityCheck ? Boolean.TRUE : Boolean.FALSE); 250 } 251 252 256 public void setForcelacheck(boolean forceLACheck) { 257 optionalAttrs.put(FORCE_LA_CHECK, forceLACheck ? Boolean.TRUE : Boolean.FALSE); 258 } 259 260 264 public void setCachetokens(boolean cacheTokens) { 265 optionalAttrs.put(CACHE_TOKENS, cacheTokens ? Boolean.TRUE : Boolean.FALSE); 266 } 267 268 272 public void setKeeplinecolumn(boolean keepLineColumn) { 273 optionalAttrs.put(KEEP_LINE_COLUMN, keepLineColumn ? Boolean.TRUE : Boolean.FALSE); 274 } 275 276 281 public void setJDKversion(String jdkVersion) { 282 optionalAttrs.put(JDK_VERSION, jdkVersion); 283 } 284 285 291 public void setOutputdirectory(File outputDirectory) { 292 this.outputDirectory = outputDirectory; 293 } 294 295 299 public void setTarget(File targetFile) { 300 this.targetFile = targetFile; 301 } 302 303 307 public void setJavacchome(File javaccHome) { 308 this.javaccHome = javaccHome; 309 } 310 311 314 public JavaCC() { 315 cmdl.setVm(JavaEnvUtils.getJreExecutable("java")); 316 } 317 318 322 public void execute() throws BuildException { 323 324 Enumeration iter = optionalAttrs.keys(); 326 while (iter.hasMoreElements()) { 327 String name = (String ) iter.nextElement(); 328 Object value = optionalAttrs.get(name); 329 cmdl.createArgument().setValue("-" + name + ":" + value.toString()); 330 } 331 332 if (targetFile == null || !targetFile.isFile()) { 334 throw new BuildException("Invalid target: " + targetFile); 335 } 336 337 if (outputDirectory == null) { 339 outputDirectory = new File (targetFile.getParent()); 340 } else if (!outputDirectory.isDirectory()) { 341 throw new BuildException("Outputdir not a directory."); 342 } 343 cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:" 344 + outputDirectory.getAbsolutePath()); 345 346 final File javaFile = getOutputJavaFile(outputDirectory, targetFile); 348 if (javaFile.exists() && targetFile.lastModified() < javaFile.lastModified()) { 349 log("Target is already built - skipping (" + targetFile + ")", Project.MSG_VERBOSE); 350 return; 351 } 352 cmdl.createArgument().setValue(targetFile.getAbsolutePath()); 353 354 final Path classpath = cmdl.createClasspath(getProject()); 355 final File javaccJar = JavaCC.getArchiveFile(javaccHome); 356 classpath.createPathElement().setPath(javaccJar.getAbsolutePath()); 357 classpath.addJavaRuntime(); 358 359 cmdl.setClassname(JavaCC.getMainClass(classpath, 360 JavaCC.TASKDEF_TYPE_JAVACC)); 361 362 final Commandline.Argument arg = cmdl.createVmArgument(); 363 arg.setValue("-mx140M"); 364 arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath()); 365 366 Execute.runCommand(this, cmdl.getCommandline()); 367 } 368 369 378 protected static File getArchiveFile(File home) throws BuildException { 379 return new File (home, 380 ARCHIVE_LOCATIONS[getArchiveLocationIndex(home)]); 381 } 382 383 391 protected static String getMainClass(File home, int type) 392 throws BuildException { 393 394 Path p = new Path(null); 395 p.createPathElement().setLocation(getArchiveFile(home)); 396 p.addJavaRuntime(); 397 return getMainClass(p, type); 398 } 399 400 409 protected static String getMainClass(Path path, int type) 410 throws BuildException { 411 String packagePrefix = null; 412 String mainClass = null; 413 414 AntClassLoader l = new AntClassLoader(); 415 l.setClassPath(path.concatSystemClasspath("ignore")); 416 String javaccClass = COM_PACKAGE + COM_JAVACC_CLASS; 417 InputStream is = l.getResourceAsStream(javaccClass.replace('.', '/') 418 + ".class"); 419 if (is != null) { 420 packagePrefix = COM_PACKAGE; 421 switch (type) { 422 case TASKDEF_TYPE_JAVACC: 423 mainClass = COM_JAVACC_CLASS; 424 425 break; 426 427 case TASKDEF_TYPE_JJTREE: 428 mainClass = COM_JJTREE_CLASS; 429 430 break; 431 432 case TASKDEF_TYPE_JJDOC: 433 mainClass = COM_JJDOC_CLASS; 434 435 break; 436 default: 437 } 439 } else { 440 javaccClass = ORG_PACKAGE_3_1 + ORG_JAVACC_CLASS; 441 is = l.getResourceAsStream(javaccClass.replace('.', '/') 442 + ".class"); 443 if (is != null) { 444 packagePrefix = ORG_PACKAGE_3_1; 445 } else { 446 javaccClass = ORG_PACKAGE_3_0 + ORG_JAVACC_CLASS; 447 is = l.getResourceAsStream(javaccClass.replace('.', '/') 448 + ".class"); 449 if (is != null) { 450 packagePrefix = ORG_PACKAGE_3_0; 451 } 452 } 453 454 if (is != null) { 455 switch (type) { 456 case TASKDEF_TYPE_JAVACC: 457 mainClass = ORG_JAVACC_CLASS; 458 459 break; 460 461 case TASKDEF_TYPE_JJTREE: 462 mainClass = ORG_JJTREE_CLASS; 463 464 break; 465 466 case TASKDEF_TYPE_JJDOC: 467 mainClass = ORG_JJDOC_CLASS; 468 469 break; 470 default: 471 } 473 } 474 } 475 476 if (packagePrefix == null) { 477 throw new BuildException("failed to load JavaCC"); 478 } 479 if (mainClass == null) { 480 throw new BuildException("unknown task type " + type); 481 } 482 return packagePrefix + mainClass; 483 } 484 485 493 private static int getArchiveLocationIndex(File home) 494 throws BuildException { 495 496 if (home == null || !home.isDirectory()) { 497 throw new BuildException("JavaCC home must be a valid directory."); 498 } 499 500 for (int i = 0; i < ARCHIVE_LOCATIONS.length; i++) { 501 File f = new File (home, ARCHIVE_LOCATIONS[i]); 502 503 if (f.exists()) { 504 return i; 505 } 506 } 507 508 throw new BuildException("Could not find a path to JavaCC.zip " 509 + "or javacc.jar from '" + home + "'."); 510 } 511 512 520 protected static int getMajorVersionNumber(File home) 521 throws BuildException { 522 523 return 524 ARCHIVE_LOCATIONS_VS_MAJOR_VERSION[getArchiveLocationIndex(home)]; 525 } 526 527 532 private File getOutputJavaFile(File outputdir, File srcfile) { 533 String path = srcfile.getPath(); 534 535 int startBasename = path.lastIndexOf(File.separator); 537 if (startBasename != -1) { 538 path = path.substring(startBasename + 1); 539 } 540 541 int startExtn = path.lastIndexOf('.'); 543 if (startExtn != -1) { 544 path = path.substring(0, startExtn) + ".java"; 545 } else { 546 path += ".java"; 547 } 548 549 if (outputdir != null) { 551 path = outputdir + File.separator + path; 552 } 553 554 return new File (path); 555 } 556 } 557 | Popular Tags |