FileDocCategorySizeDatePackage
JavaCC.javaAPI DocApache Ant 1.7019956Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs.optional.javacc

JavaCC

public class JavaCC extends org.apache.tools.ant.Task
JavaCC compiler compiler task.

Fields Summary
private static final String
LOOKAHEAD
private static final String
CHOICE_AMBIGUITY_CHECK
private static final String
OTHER_AMBIGUITY_CHECK
private static final String
STATIC
private static final String
DEBUG_PARSER
private static final String
DEBUG_LOOKAHEAD
private static final String
DEBUG_TOKEN_MANAGER
private static final String
OPTIMIZE_TOKEN_MANAGER
private static final String
ERROR_REPORTING
private static final String
JAVA_UNICODE_ESCAPE
private static final String
UNICODE_INPUT
private static final String
IGNORE_CASE
private static final String
COMMON_TOKEN_ACTION
private static final String
USER_TOKEN_MANAGER
private static final String
USER_CHAR_STREAM
private static final String
BUILD_PARSER
private static final String
BUILD_TOKEN_MANAGER
private static final String
SANITY_CHECK
private static final String
FORCE_LA_CHECK
private static final String
CACHE_TOKENS
private static final String
KEEP_LINE_COLUMN
private static final String
JDK_VERSION
private final Hashtable
optionalAttrs
private File
outputDirectory
private File
targetFile
private File
javaccHome
private org.apache.tools.ant.types.CommandlineJava
cmdl
protected static final int
TASKDEF_TYPE_JAVACC
protected static final int
TASKDEF_TYPE_JJTREE
protected static final int
TASKDEF_TYPE_JJDOC
protected static final String[]
ARCHIVE_LOCATIONS
protected static final int[]
ARCHIVE_LOCATIONS_VS_MAJOR_VERSION
protected static final String
COM_PACKAGE
protected static final String
COM_JAVACC_CLASS
protected static final String
COM_JJTREE_CLASS
protected static final String
COM_JJDOC_CLASS
protected static final String
ORG_PACKAGE_3_0
protected static final String
ORG_PACKAGE_3_1
protected static final String
ORG_JAVACC_CLASS
protected static final String
ORG_JJTREE_CLASS
protected static final String
ORG_JJDOC_CLASS
Constructors Summary
public JavaCC()
Constructor

        cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
    
Methods Summary
public voidexecute()
Run the task.

throws
BuildException on error.


        // load command line with optional attributes
        Enumeration iter = optionalAttrs.keys();
        while (iter.hasMoreElements()) {
            String name  = (String) iter.nextElement();
            Object value = optionalAttrs.get(name);
            cmdl.createArgument().setValue("-" + name + ":" + value.toString());
        }

        // check the target is a file
        if (targetFile == null || !targetFile.isFile()) {
            throw new BuildException("Invalid target: " + targetFile);
        }

        // use the directory containing the target as the output directory
        if (outputDirectory == null) {
            outputDirectory = new File(targetFile.getParent());
        } else if (!outputDirectory.isDirectory()) {
            throw new BuildException("Outputdir not a directory.");
        }
        cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:"
                                       + outputDirectory.getAbsolutePath());

        // determine if the generated java file is up-to-date
        final File javaFile = getOutputJavaFile(outputDirectory, targetFile);
        if (javaFile.exists() && targetFile.lastModified() < javaFile.lastModified()) {
            log("Target is already built - skipping (" + targetFile + ")", Project.MSG_VERBOSE);
            return;
        }
        cmdl.createArgument().setValue(targetFile.getAbsolutePath());

        final Path classpath = cmdl.createClasspath(getProject());
        final File javaccJar = JavaCC.getArchiveFile(javaccHome);
        classpath.createPathElement().setPath(javaccJar.getAbsolutePath());
        classpath.addJavaRuntime();

        cmdl.setClassname(JavaCC.getMainClass(classpath,
                                              JavaCC.TASKDEF_TYPE_JAVACC));

        final Commandline.Argument arg = cmdl.createVmArgument();
        arg.setValue("-mx140M");
        arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath());

        Execute.runCommand(this, cmdl.getCommandline());
    
protected static java.io.FilegetArchiveFile(java.io.File home)
Helper method to retrieve the path used to store the JavaCC.zip or javacc.jar which is different from versions.

param
home the javacc home path directory.
throws
BuildException thrown if the home directory is invalid or if the archive could not be found despite attempts to do so.
return
the file object pointing to the JavaCC archive.

        return new File(home,
                        ARCHIVE_LOCATIONS[getArchiveLocationIndex(home)]);
    
private static intgetArchiveLocationIndex(java.io.File home)
Helper method to determine the archive location index.

param
home the javacc home path directory.
throws
BuildException thrown if the home directory is invalid or if the archive could not be found despite attempts to do so.
return
the archive location index


        if (home == null || !home.isDirectory()) {
            throw new BuildException("JavaCC home must be a valid directory.");
        }

        for (int i = 0; i < ARCHIVE_LOCATIONS.length; i++) {
            File f = new File(home, ARCHIVE_LOCATIONS[i]);

            if (f.exists()) {
                return i;
            }
        }

        throw new BuildException("Could not find a path to JavaCC.zip "
                                 + "or javacc.jar from '" + home + "'.");
    
protected static java.lang.StringgetMainClass(java.io.File home, int type)
Helper method to retrieve main class which is different from versions.

param
home the javacc home path directory.
param
type the taskdef.
throws
BuildException thrown if the home directory is invalid or if the archive could not be found despite attempts to do so.
return
the main class for the taskdef.


        Path p = new Path(null);
        p.createPathElement().setLocation(getArchiveFile(home));
        p.addJavaRuntime();
        return getMainClass(p, type);
    
protected static java.lang.StringgetMainClass(org.apache.tools.ant.types.Path path, int type)
Helper method to retrieve main class which is different from versions.

param
path classpath to search in.
param
type the taskdef.
throws
BuildException thrown if the home directory is invalid or if the archive could not be found despite attempts to do so.
return
the main class for the taskdef.
since
Ant 1.7

        String packagePrefix = null;
        String mainClass = null;

        AntClassLoader l = new AntClassLoader();
        l.setClassPath(path.concatSystemClasspath("ignore"));
        String javaccClass = COM_PACKAGE + COM_JAVACC_CLASS;
        InputStream is = l.getResourceAsStream(javaccClass.replace('.", '/")
                                               + ".class");
        if (is != null) {
            packagePrefix = COM_PACKAGE;
            switch (type) {
            case TASKDEF_TYPE_JAVACC:
                mainClass = COM_JAVACC_CLASS;

                break;

            case TASKDEF_TYPE_JJTREE:
                mainClass = COM_JJTREE_CLASS;

                break;

            case TASKDEF_TYPE_JJDOC:
                mainClass = COM_JJDOC_CLASS;

                break;
            default:
                // Fall Through
            }
        } else {
            javaccClass = ORG_PACKAGE_3_1 + ORG_JAVACC_CLASS;
            is = l.getResourceAsStream(javaccClass.replace('.", '/")
                                       + ".class");
            if (is != null) {
                packagePrefix = ORG_PACKAGE_3_1;
            } else {
                javaccClass = ORG_PACKAGE_3_0 + ORG_JAVACC_CLASS;
                is = l.getResourceAsStream(javaccClass.replace('.", '/")
                                           + ".class");
                if (is != null) {
                    packagePrefix = ORG_PACKAGE_3_0;
                }
            }

            if (is != null) {
                switch (type) {
                case TASKDEF_TYPE_JAVACC:
                    mainClass = ORG_JAVACC_CLASS;

                break;

                case TASKDEF_TYPE_JJTREE:
                    mainClass = ORG_JJTREE_CLASS;

                    break;

                case TASKDEF_TYPE_JJDOC:
                    mainClass = ORG_JJDOC_CLASS;

                    break;
                default:
                    // Fall Through
                }
            }
        }

        if (packagePrefix == null) {
            throw new BuildException("failed to load JavaCC");
        }
        if (mainClass == null) {
            throw new BuildException("unknown task type " + type);
        }
        return packagePrefix + mainClass;
    
protected static intgetMajorVersionNumber(java.io.File home)
Helper method to determine the major version number of JavaCC.

param
home the javacc home path directory.
throws
BuildException thrown if the home directory is invalid or if the archive could not be found despite attempts to do so.
return
a the major version number


        return
            ARCHIVE_LOCATIONS_VS_MAJOR_VERSION[getArchiveLocationIndex(home)];
    
private java.io.FilegetOutputJavaFile(java.io.File outputdir, java.io.File srcfile)
Determines the output Java file to be generated by the given grammar file.

        String path = srcfile.getPath();

        // Extract file's base-name
        int startBasename = path.lastIndexOf(File.separator);
        if (startBasename != -1) {
            path = path.substring(startBasename + 1);
        }

        // Replace the file's extension with '.java'
        int startExtn = path.lastIndexOf('.");
        if (startExtn != -1) {
            path = path.substring(0, startExtn) + ".java";
        } else {
            path += ".java";
        }

        // Change the directory
        if (outputdir != null) {
            path = outputdir + File.separator + path;
        }

        return new File(path);
    
public voidsetBuildparser(boolean buildParser)
Sets the BUILD_PARSER grammar option.

param
buildParser a boolean value.

        optionalAttrs.put(BUILD_PARSER, buildParser ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetBuildtokenmanager(boolean buildTokenManager)
Sets the BUILD_TOKEN_MANAGER grammar option.

param
buildTokenManager a boolean value.

        optionalAttrs.put(BUILD_TOKEN_MANAGER, buildTokenManager ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetCachetokens(boolean cacheTokens)
Sets the CACHE_TOKENS grammar option.

param
cacheTokens a boolean value.

        optionalAttrs.put(CACHE_TOKENS, cacheTokens ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetChoiceambiguitycheck(int choiceAmbiguityCheck)
Sets the CHOICE_AMBIGUITY_CHECK grammar option.

param
choiceAmbiguityCheck an int value.

        optionalAttrs.put(CHOICE_AMBIGUITY_CHECK, new Integer(choiceAmbiguityCheck));
    
public voidsetCommontokenaction(boolean commonTokenAction)
Sets the COMMON_TOKEN_ACTION grammar option.

param
commonTokenAction a boolean value.

        optionalAttrs.put(COMMON_TOKEN_ACTION, commonTokenAction ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetDebuglookahead(boolean debugLookahead)
Sets the DEBUG_LOOKAHEAD grammar option.

param
debugLookahead a boolean value.

        optionalAttrs.put(DEBUG_LOOKAHEAD, debugLookahead ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetDebugparser(boolean debugParser)
Sets the DEBUG_PARSER grammar option.

param
debugParser a boolean value.

        optionalAttrs.put(DEBUG_PARSER, debugParser ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetDebugtokenmanager(boolean debugTokenManager)
Sets the DEBUG_TOKEN_MANAGER grammar option.

param
debugTokenManager a boolean value.

        optionalAttrs.put(DEBUG_TOKEN_MANAGER, debugTokenManager ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetErrorreporting(boolean errorReporting)
Sets the ERROR_REPORTING grammar option.

param
errorReporting a boolean value.

        optionalAttrs.put(ERROR_REPORTING, errorReporting ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetForcelacheck(boolean forceLACheck)
Sets the FORCE_LA_CHECK grammar option.

param
forceLACheck a boolean value.

        optionalAttrs.put(FORCE_LA_CHECK, forceLACheck ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetIgnorecase(boolean ignoreCase)
Sets the IGNORE_CASE grammar option.

param
ignoreCase a boolean value.

        optionalAttrs.put(IGNORE_CASE, ignoreCase ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetJDKversion(java.lang.String jdkVersion)
Sets the JDK_VERSION option.

param
jdkVersion the version to use.
since
Ant1.7

        optionalAttrs.put(JDK_VERSION, jdkVersion);
    
public voidsetJavacchome(java.io.File javaccHome)
The directory containing the JavaCC distribution.

param
javaccHome the directory.

        this.javaccHome = javaccHome;
    
public voidsetJavaunicodeescape(boolean javaUnicodeEscape)
Sets the JAVA_UNICODE_ESCAPE grammar option.

param
javaUnicodeEscape a boolean value.

        optionalAttrs.put(JAVA_UNICODE_ESCAPE, javaUnicodeEscape ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetKeeplinecolumn(boolean keepLineColumn)
Sets the KEEP_LINE_COLUMN grammar option.

param
keepLineColumn a boolean value.

        optionalAttrs.put(KEEP_LINE_COLUMN, keepLineColumn ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetLookahead(int lookahead)
Sets the LOOKAHEAD grammar option.

param
lookahead an int value.


                   
        
        optionalAttrs.put(LOOKAHEAD, new Integer(lookahead));
    
public voidsetOptimizetokenmanager(boolean optimizeTokenManager)
Sets the OPTIMIZE_TOKEN_MANAGER grammar option.

param
optimizeTokenManager a boolean value.

        optionalAttrs.put(OPTIMIZE_TOKEN_MANAGER,
                          optimizeTokenManager ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetOtherambiguityCheck(int otherAmbiguityCheck)
Sets the OTHER_AMBIGUITY_CHECK grammar option.

param
otherAmbiguityCheck an int value.

        optionalAttrs.put(OTHER_AMBIGUITY_CHECK, new Integer(otherAmbiguityCheck));
    
public voidsetOutputdirectory(java.io.File outputDirectory)
The directory to write the generated files to. If not set, the files are written to the directory containing the grammar file.

param
outputDirectory the output directory.

        this.outputDirectory = outputDirectory;
    
public voidsetSanitycheck(boolean sanityCheck)
Sets the SANITY_CHECK grammar option.

param
sanityCheck a boolean value.

        optionalAttrs.put(SANITY_CHECK, sanityCheck ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetStatic(boolean staticParser)
Sets the STATIC grammar option.

param
staticParser a boolean value.

        optionalAttrs.put(STATIC, staticParser ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetTarget(java.io.File targetFile)
The grammar file to process.

param
targetFile the grammar file.

        this.targetFile = targetFile;
    
public voidsetUnicodeinput(boolean unicodeInput)
Sets the UNICODE_INPUT grammar option.

param
unicodeInput a boolean value.

        optionalAttrs.put(UNICODE_INPUT, unicodeInput ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetUsercharstream(boolean userCharStream)
Sets the USER_CHAR_STREAM grammar option.

param
userCharStream a boolean value.

        optionalAttrs.put(USER_CHAR_STREAM, userCharStream ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetUsertokenmanager(boolean userTokenManager)
Sets the USER_TOKEN_MANAGER grammar option.

param
userTokenManager a boolean value.

        optionalAttrs.put(USER_TOKEN_MANAGER, userTokenManager ? Boolean.TRUE : Boolean.FALSE);