Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 476   Methods: 29
NCLOC: 232   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JavaCC.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.javacc;
 56   
 
 57   
 import java.io.File;
 58   
 import java.util.Enumeration;
 59   
 import java.util.Hashtable;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.Task;
 63   
 import org.apache.tools.ant.taskdefs.Execute;
 64   
 import org.apache.tools.ant.types.Commandline;
 65   
 import org.apache.tools.ant.types.CommandlineJava;
 66   
 import org.apache.tools.ant.types.Path;
 67   
 import org.apache.tools.ant.util.JavaEnvUtils;
 68   
 
 69   
 /**
 70   
  * JavaCC compiler compiler task.
 71   
  *
 72   
  * @author thomas.haas@softwired-inc.com
 73   
  * @author Michael Saunders <a href="mailto:michael@amtec.com">michael@amtec.com</a>
 74   
  */
 75   
 public class JavaCC extends Task {
 76   
 
 77   
     // keys to optional attributes
 78   
     private static final String LOOKAHEAD              = "LOOKAHEAD";
 79   
     private static final String CHOICE_AMBIGUITY_CHECK = "CHOICE_AMBIGUITY_CHECK";
 80   
     private static final String OTHER_AMBIGUITY_CHECK  = "OTHER_AMBIGUITY_CHECK";
 81   
 
 82   
     private static final String STATIC                 = "STATIC";
 83   
     private static final String DEBUG_PARSER           = "DEBUG_PARSER";
 84   
     private static final String DEBUG_LOOKAHEAD        = "DEBUG_LOOKAHEAD";
 85   
     private static final String DEBUG_TOKEN_MANAGER    = "DEBUG_TOKEN_MANAGER";
 86   
     private static final String OPTIMIZE_TOKEN_MANAGER = "OPTIMIZE_TOKEN_MANAGER";
 87   
     private static final String ERROR_REPORTING        = "ERROR_REPORTING";
 88   
     private static final String JAVA_UNICODE_ESCAPE    = "JAVA_UNICODE_ESCAPE";
 89   
     private static final String UNICODE_INPUT          = "UNICODE_INPUT";
 90   
     private static final String IGNORE_CASE            = "IGNORE_CASE";
 91   
     private static final String COMMON_TOKEN_ACTION    = "COMMON_TOKEN_ACTION";
 92   
     private static final String USER_TOKEN_MANAGER     = "USER_TOKEN_MANAGER";
 93   
     private static final String USER_CHAR_STREAM       = "USER_CHAR_STREAM";
 94   
     private static final String BUILD_PARSER           = "BUILD_PARSER";
 95   
     private static final String BUILD_TOKEN_MANAGER    = "BUILD_TOKEN_MANAGER";
 96   
     private static final String SANITY_CHECK           = "SANITY_CHECK";
 97   
     private static final String FORCE_LA_CHECK         = "FORCE_LA_CHECK";
 98   
     private static final String CACHE_TOKENS           = "CACHE_TOKENS";
 99   
 
 100   
     private final Hashtable optionalAttrs = new Hashtable();
 101   
 
 102   
     // required attributes
 103   
     private File outputDirectory = null;
 104   
     private File target          = null;
 105   
     private File javaccHome      = null;
 106   
 
 107   
     private CommandlineJava cmdl = new CommandlineJava();
 108   
 
 109   
     protected static final int TASKDEF_TYPE_JAVACC = 1;
 110   
     protected static final int TASKDEF_TYPE_JJTREE = 2;
 111   
     protected static final int TASKDEF_TYPE_JJDOC = 3;
 112   
 
 113   
     protected static final String[] ARCHIVE_LOCATIONS = 
 114   
         new String[] {"JavaCC.zip", "bin/lib/JavaCC.zip", 
 115   
                       "bin/lib/javacc.jar"};
 116   
 
 117   
     protected static final String COM_PACKAGE = "COM.sun.labs.";
 118   
     protected static final String COM_JAVACC_CLASS = "javacc.Main";
 119   
     protected static final String COM_JJTREE_CLASS = "jjtree.Main";
 120   
     protected static final String COM_JJDOC_CLASS = "jjdoc.JJDocMain";
 121   
 
 122   
     protected static final String ORG_PACKAGE = "org.netbeans.javacc.";
 123   
     protected static final String ORG_JAVACC_CLASS = "parser.Main";
 124   
     protected static final String ORG_JJTREE_CLASS = COM_JJTREE_CLASS;
 125   
     protected static final String ORG_JJDOC_CLASS = COM_JJDOC_CLASS;
 126   
 
 127   
     /**
 128   
      * Sets the LOOKAHEAD grammar option.
 129   
      */
 130  0
     public void setLookahead(int lookahead) {
 131  0
         optionalAttrs.put(LOOKAHEAD, new Integer(lookahead));
 132   
     }
 133   
 
 134   
     /**
 135   
      * Sets the CHOICE_AMBIGUITY_CHECK grammar option.
 136   
      */
 137  0
     public void setChoiceambiguitycheck(int choiceAmbiguityCheck) {
 138  0
         optionalAttrs.put(CHOICE_AMBIGUITY_CHECK, new Integer(choiceAmbiguityCheck));
 139   
     }
 140   
 
 141   
     /**
 142   
      * Sets the OTHER_AMBIGUITY_CHECK grammar option.
 143   
      */
 144  0
     public void setOtherambiguityCheck(int otherAmbiguityCheck) {
 145  0
         optionalAttrs.put(OTHER_AMBIGUITY_CHECK, new Integer(otherAmbiguityCheck));
 146   
     }
 147   
 
 148   
     /**
 149   
      * Sets the STATIC grammar option.
 150   
      */
 151  0
     public void setStatic(boolean staticParser) {
 152  0
         optionalAttrs.put(STATIC, new Boolean(staticParser));
 153   
     }
 154   
 
 155   
     /**
 156   
      * Sets the DEBUG_PARSER grammar option.
 157   
      */
 158  0
     public void setDebugparser(boolean debugParser) {
 159  0
         optionalAttrs.put(DEBUG_PARSER, new Boolean(debugParser));
 160   
     }
 161   
 
 162   
     /**
 163   
      * Sets the DEBUG_LOOKAHEAD grammar option.
 164   
      */
 165  0
     public void setDebuglookahead(boolean debugLookahead) {
 166  0
         optionalAttrs.put(DEBUG_LOOKAHEAD, new Boolean(debugLookahead));
 167   
     }
 168   
 
 169   
     /**
 170   
      * Sets the DEBUG_TOKEN_MANAGER grammar option.
 171   
      */
 172  0
     public void setDebugtokenmanager(boolean debugTokenManager) {
 173  0
         optionalAttrs.put(DEBUG_TOKEN_MANAGER, new Boolean(debugTokenManager));
 174   
     }
 175   
 
 176   
     /**
 177   
      * Sets the OPTIMIZE_TOKEN_MANAGER grammar option.
 178   
      */
 179  0
     public void setOptimizetokenmanager(boolean optimizeTokenManager) {
 180  0
         optionalAttrs.put(OPTIMIZE_TOKEN_MANAGER, new Boolean(optimizeTokenManager));
 181   
     }
 182   
 
 183   
     /**
 184   
      * Sets the ERROR_REPORTING grammar option.
 185   
      */
 186  0
     public void setErrorreporting(boolean errorReporting) {
 187  0
         optionalAttrs.put(ERROR_REPORTING, new Boolean(errorReporting));
 188   
     }
 189   
 
 190   
     /**
 191   
      * Sets the JAVA_UNICODE_ESCAPE grammar option.
 192   
      */
 193  0
     public void setJavaunicodeescape(boolean javaUnicodeEscape) {
 194  0
         optionalAttrs.put(JAVA_UNICODE_ESCAPE, new Boolean(javaUnicodeEscape));
 195   
     }
 196   
 
 197   
     /**
 198   
      * Sets the UNICODE_INPUT grammar option.
 199   
      */
 200  0
     public void setUnicodeinput(boolean unicodeInput) {
 201  0
         optionalAttrs.put(UNICODE_INPUT, new Boolean(unicodeInput));
 202   
     }
 203   
 
 204   
     /**
 205   
      * Sets the IGNORE_CASE grammar option.
 206   
      */
 207  0
     public void setIgnorecase(boolean ignoreCase) {
 208  0
         optionalAttrs.put(IGNORE_CASE, new Boolean(ignoreCase));
 209   
     }
 210   
 
 211   
     /**
 212   
      * Sets the COMMON_TOKEN_ACTION grammar option.
 213   
      */
 214  0
     public void setCommontokenaction(boolean commonTokenAction) {
 215  0
         optionalAttrs.put(COMMON_TOKEN_ACTION, new Boolean(commonTokenAction));
 216   
     }
 217   
 
 218   
     /**
 219   
      * Sets the USER_TOKEN_MANAGER grammar option.
 220   
      */
 221  0
     public void setUsertokenmanager(boolean userTokenManager) {
 222  0
         optionalAttrs.put(USER_TOKEN_MANAGER, new Boolean(userTokenManager));
 223   
     }
 224   
 
 225   
     /**
 226   
      * Sets the USER_CHAR_STREAM grammar option.
 227   
      */
 228  0
     public void setUsercharstream(boolean userCharStream) {
 229  0
         optionalAttrs.put(USER_CHAR_STREAM, new Boolean(userCharStream));
 230   
     }
 231   
 
 232   
     /**
 233   
      * Sets the BUILD_PARSER grammar option.
 234   
      */
 235  0
     public void setBuildparser(boolean buildParser) {
 236  0
         optionalAttrs.put(BUILD_PARSER, new Boolean(buildParser));
 237   
     }
 238   
 
 239   
     /**
 240   
      * Sets the BUILD_TOKEN_MANAGER grammar option.
 241   
      */
 242  0
     public void setBuildtokenmanager(boolean buildTokenManager) {
 243  0
         optionalAttrs.put(BUILD_TOKEN_MANAGER, new Boolean(buildTokenManager));
 244   
     }
 245   
 
 246   
     /**
 247   
      * Sets the SANITY_CHECK grammar option.
 248   
      */
 249  0
     public void setSanitycheck(boolean sanityCheck) {
 250  0
         optionalAttrs.put(SANITY_CHECK, new Boolean(sanityCheck));
 251   
     }
 252   
 
 253   
     /**
 254   
      * Sets the FORCE_LA_CHECK grammar option.
 255   
      */
 256  0
     public void setForcelacheck(boolean forceLACheck) {
 257  0
         optionalAttrs.put(FORCE_LA_CHECK, new Boolean(forceLACheck));
 258   
     }
 259   
 
 260   
     /**
 261   
      * Sets the CACHE_TOKENS grammar option.
 262   
      */
 263  0
     public void setCachetokens(boolean cacheTokens) {
 264  0
         optionalAttrs.put(CACHE_TOKENS, new Boolean(cacheTokens));
 265   
     }
 266   
 
 267   
     /**
 268   
      * The directory to write the generated files to.
 269   
      * If not set, the files are written to the directory
 270   
      * containing the grammar file.
 271   
      */
 272  0
     public void setOutputdirectory(File outputDirectory) {
 273  0
         this.outputDirectory = outputDirectory;
 274   
     }
 275   
 
 276   
     /**
 277   
      * The grammar file to process.
 278   
      */
 279  0
     public void setTarget(File target) {
 280  0
         this.target = target;
 281   
     }
 282   
 
 283   
     /**
 284   
      * The directory containing the JavaCC distribution.
 285   
      */
 286  0
     public void setJavacchome(File javaccHome) {
 287  0
         this.javaccHome = javaccHome;
 288   
     }
 289   
 
 290  0
     public JavaCC() {
 291  0
         cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
 292   
     }
 293   
 
 294  0
     public void execute() throws BuildException {
 295   
 
 296   
         // load command line with optional attributes
 297  0
         Enumeration iter = optionalAttrs.keys();
 298  0
         while (iter.hasMoreElements()) {
 299  0
             String name  = (String) iter.nextElement();
 300  0
             Object value = optionalAttrs.get(name);
 301  0
             cmdl.createArgument().setValue("-" + name + ":" + value.toString());
 302   
         }
 303   
 
 304   
         // check the target is a file
 305  0
         if (target == null || !target.isFile()) {
 306  0
             throw new BuildException("Invalid target: " + target);
 307   
         }
 308   
 
 309   
         // use the directory containing the target as the output directory
 310  0
         if (outputDirectory == null) {
 311  0
             outputDirectory = new File(target.getParent());
 312  0
         } else if (!outputDirectory.isDirectory()) {
 313  0
             throw new BuildException("Outputdir not a directory.");
 314   
         }
 315  0
         cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:"
 316   
             + outputDirectory.getAbsolutePath());
 317   
 
 318   
         // determine if the generated java file is up-to-date
 319  0
         final File javaFile = getOutputJavaFile(outputDirectory, target);
 320  0
         if (javaFile.exists() && target.lastModified() < javaFile.lastModified()) {
 321  0
             log("Target is already built - skipping (" + target + ")", Project.MSG_VERBOSE);
 322  0
             return;
 323   
         }
 324  0
         cmdl.createArgument().setValue(target.getAbsolutePath());
 325   
 
 326  0
         cmdl.setClassname(JavaCC.getMainClass(javaccHome,
 327   
                                               JavaCC.TASKDEF_TYPE_JAVACC));
 328   
 
 329  0
         final Path classpath = cmdl.createClasspath(getProject());
 330  0
         final File javaccJar = JavaCC.getArchiveFile(javaccHome);
 331  0
         classpath.createPathElement().setPath(javaccJar.getAbsolutePath());
 332  0
         classpath.addJavaRuntime();
 333   
 
 334  0
         final Commandline.Argument arg = cmdl.createVmArgument();
 335  0
         arg.setValue("-mx140M");
 336  0
         arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath());
 337   
 
 338  0
         Execute.runCommand(this, cmdl.getCommandline());
 339   
     }
 340   
 
 341   
     /**
 342   
      * Helper method to retrieve the path used to store the JavaCC.zip
 343   
      * or javacc.jar which is different from versions.
 344   
      *
 345   
      * @param home the javacc home path directory.
 346   
      * @throws BuildException thrown if the home directory is invalid
 347   
      * or if the archive could not be found despite attempts to do so.
 348   
      * @return the file object pointing to the JavaCC archive.
 349   
      */
 350  0
     protected static File getArchiveFile(File home) throws BuildException {
 351  0
         return new File(home, 
 352   
                         ARCHIVE_LOCATIONS[getMajorVersionNumber(home) - 1]);
 353   
     }
 354   
 
 355   
     /**
 356   
      * Helper method to retrieve main class which is different from versions.
 357   
      * @param home the javacc home path directory.
 358   
      * @param type the taskdef.
 359   
      * @throws BuildException thrown if the home directory is invalid
 360   
      * or if the archive could not be found despite attempts to do so.
 361   
      * @return the main class for the taskdef.
 362   
      */
 363  0
     protected static String getMainClass(File home, int type) 
 364   
         throws BuildException {
 365   
 
 366  0
         int majorVersion = getMajorVersionNumber(home);
 367  0
         String packagePrefix = null;
 368  0
         String mainClass = null;
 369   
 
 370  0
         switch (majorVersion) {
 371   
             case 1:
 372   
             case 2:
 373  0
                 packagePrefix = COM_PACKAGE;
 374   
 
 375  0
                 switch (type) {
 376   
                     case TASKDEF_TYPE_JAVACC:
 377  0
                         mainClass = COM_JAVACC_CLASS;
 378   
 
 379  0
                         break;
 380   
 
 381   
                     case TASKDEF_TYPE_JJTREE:
 382  0
                         mainClass = COM_JJTREE_CLASS;
 383   
 
 384  0
                         break;
 385   
 
 386   
                     case TASKDEF_TYPE_JJDOC:
 387  0
                         mainClass = COM_JJDOC_CLASS;
 388   
 
 389  0
                         break;
 390   
                 }
 391   
 
 392  0
                 break;
 393   
 
 394   
             case 3:
 395  0
                 packagePrefix = ORG_PACKAGE;
 396   
 
 397  0
                 switch (type) {
 398   
                     case TASKDEF_TYPE_JAVACC:
 399  0
                         mainClass = ORG_JAVACC_CLASS;
 400   
 
 401  0
                         break;
 402   
 
 403   
                     case TASKDEF_TYPE_JJTREE:
 404  0
                         mainClass = ORG_JJTREE_CLASS;
 405   
 
 406  0
                         break;
 407   
 
 408   
                     case TASKDEF_TYPE_JJDOC:
 409  0
                         mainClass = ORG_JJDOC_CLASS;
 410   
 
 411  0
                         break;
 412   
                 }
 413   
 
 414  0
                 break;
 415   
         }
 416   
 
 417  0
         return packagePrefix + mainClass;
 418   
     }
 419   
 
 420   
     /**
 421   
      * Helper method to determine the major version number of JavaCC.
 422   
      * @param home the javacc home path directory.
 423   
      * @throws BuildException thrown if the home directory is invalid
 424   
      * or if the archive could not be found despite attempts to do so.
 425   
      * @return the file object pointing to the JavaCC archive.
 426   
      */
 427  0
     protected static int getMajorVersionNumber(File home) 
 428   
         throws BuildException {
 429   
 
 430  0
         if (home == null || !home.isDirectory()) {
 431  0
             throw new BuildException("JavaCC home must be a valid directory.");
 432   
         }
 433   
 
 434  0
         for (int i = 0; i < ARCHIVE_LOCATIONS.length; i++) {
 435  0
             File f = new File(home, ARCHIVE_LOCATIONS[i]);
 436   
 
 437  0
             if (f.exists()){
 438  0
                 return (i + 1);
 439   
             }
 440   
         }
 441   
 
 442  0
         throw new BuildException("Could not find a path to JavaCC.zip "
 443   
                                  + "or javacc.jar from '" + home + "'.");
 444   
     }
 445   
 
 446   
     /**
 447   
      * Determines the output Java file to be generated by the given grammar
 448   
      * file.
 449   
      *
 450   
      */
 451  0
     private File getOutputJavaFile(File outputdir, File srcfile) {
 452  0
         String path = srcfile.getPath();
 453   
 
 454   
         // Extract file's base-name
 455  0
         int startBasename = path.lastIndexOf(File.separator);
 456  0
         if (startBasename != -1) {
 457  0
             path = path.substring(startBasename + 1);
 458   
         }
 459   
 
 460   
         // Replace the file's extension with '.java'
 461  0
         int startExtn = path.lastIndexOf('.');
 462  0
         if (startExtn != -1) {
 463  0
             path = path.substring(0, startExtn) + ".java";
 464   
         } else {
 465  0
             path += ".java";
 466   
         }
 467   
 
 468   
         // Change the directory
 469  0
         if (outputdir != null) {
 470  0
             path = outputdir + File.separator + path;
 471   
         }
 472   
 
 473  0
         return new File(path);
 474   
     }
 475   
 }
 476