FileDocCategorySizeDatePackage
JJTree.javaAPI DocApache Ant 1.7014158Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs.optional.javacc

JJTree

public class JJTree extends org.apache.tools.ant.Task
Runs the JJTree compiler compiler.

Fields Summary
private static final String
OUTPUT_FILE
private static final String
BUILD_NODE_FILES
private static final String
MULTI
private static final String
NODE_DEFAULT_VOID
private static final String
NODE_FACTORY
private static final String
NODE_SCOPE_HOOK
private static final String
NODE_USES_PARSER
private static final String
STATIC
private static final String
VISITOR
private static final String
NODE_PACKAGE
private static final String
VISITOR_EXCEPTION
private static final String
NODE_PREFIX
private final Hashtable
optionalAttrs
private String
outputFile
private static final String
DEFAULT_SUFFIX
private File
outputDirectory
private File
targetFile
private File
javaccHome
private org.apache.tools.ant.types.CommandlineJava
cmdl
Constructors Summary
public JJTree()
Constructor

        cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
    
Methods Summary
private java.lang.StringcreateOutputFileName(java.io.File destFile, java.lang.String optionalOutputFile, java.lang.String outputDir)

        optionalOutputFile = validateOutputFile(optionalOutputFile,
                                                outputDir);
        String jjtreeFile = destFile.getAbsolutePath().replace('\\", '/");

        if ((optionalOutputFile == null) || optionalOutputFile.equals("")) {
            int filePos = jjtreeFile.lastIndexOf("/");

            if (filePos >= 0) {
                jjtreeFile = jjtreeFile.substring(filePos + 1);
            }

            int suffixPos = jjtreeFile.lastIndexOf('.");

            if (suffixPos == -1) {
                optionalOutputFile = jjtreeFile + DEFAULT_SUFFIX;
            } else {
                String currentSuffix = jjtreeFile.substring(suffixPos);

                if (currentSuffix.equals(DEFAULT_SUFFIX)) {
                    optionalOutputFile = jjtreeFile + DEFAULT_SUFFIX;
                } else {
                    optionalOutputFile = jjtreeFile.substring(0, suffixPos)
                        + DEFAULT_SUFFIX;
                }
            }
        }

        if ((outputDir == null) || outputDir.equals("")) {
            outputDir = getDefaultOutputDirectory();
        }

        return (outputDir + "/" + optionalOutputFile).replace('\\", '/");
    
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());
        }

        if (targetFile == null || !targetFile.isFile()) {
            throw new BuildException("Invalid target: " + targetFile);
        }

        File javaFile = null;

        // use the directory containing the target as the output directory
        if (outputDirectory == null) {
            // convert backslashes to slashes, otherwise jjtree will
            // put this as comments and this seems to confuse javacc
            cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:"
                                           + getDefaultOutputDirectory());

            javaFile = new File(createOutputFileName(targetFile, outputFile,
                                                     null));
        } else {
            if (!outputDirectory.isDirectory()) {
                throw new BuildException("'outputdirectory' " + outputDirectory
                                         + " is not a directory.");
            }

            // convert backslashes to slashes, otherwise jjtree will
            // put this as comments and this seems to confuse javacc
            cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:"
                                           + outputDirectory.getAbsolutePath()
                                             .replace('\\", '/"));

            javaFile = new File(createOutputFileName(targetFile, outputFile,
                                                     outputDirectory
                                                     .getPath()));
        }

        if (javaFile.exists()
            && targetFile.lastModified() < javaFile.lastModified()) {
            log("Target is already built - skipping (" + targetFile + ")",
                Project.MSG_VERBOSE);
            return;
        }

        if (outputFile != null) {
            cmdl.createArgument().setValue("-" + OUTPUT_FILE + ":"
                                           + outputFile.replace('\\", '/"));
        }

        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_JJTREE));

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

        final Execute process =
            new Execute(new LogStreamHandler(this,
                                             Project.MSG_INFO,
                                             Project.MSG_INFO),
                        null);
        log(cmdl.describeCommand(), Project.MSG_VERBOSE);
        process.setCommandline(cmdl.getCommandline());

        try {
            if (process.execute() != 0) {
                throw new BuildException("JJTree failed.");
            }
        } catch (IOException e) {
            throw new BuildException("Failed to launch JJTree", e);
        }
    
private java.lang.StringgetDefaultOutputDirectory()

        return getProject().getBaseDir().getAbsolutePath().replace('\\", '/");
    
private java.io.FilegetRoot(java.io.File file)
Determine root directory for a given file.

param
file
return
file's root directory

        File root = file.getAbsoluteFile();

        while (root.getParent() != null) {
            root = root.getParentFile();
        }

        return root;
    
private java.lang.StringmakeOutputFileRelative(java.lang.String destFile)

        StringBuffer relativePath = new StringBuffer();
        String defaultOutputDirectory = getDefaultOutputDirectory();
        int nextPos = defaultOutputDirectory.indexOf('/");
        int startPos = nextPos + 1;

        while (startPos > -1 && startPos < defaultOutputDirectory.length()) {
            relativePath.append("/..");
            nextPos = defaultOutputDirectory.indexOf('/", startPos);

            if (nextPos == -1) {
                startPos = nextPos;
            } else {
                startPos = nextPos + 1;
            }
        }

        relativePath.append(destFile);

        return relativePath.toString();
    
public voidsetBuildnodefiles(boolean buildNodeFiles)
Sets the BUILD_NODE_FILES grammar option.

param
buildNodeFiles a boolean value.



                   
        
        optionalAttrs.put(BUILD_NODE_FILES, buildNodeFiles ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetJavacchome(java.io.File javaccHome)
The directory containing the JavaCC distribution.

param
javaccHome the directory containing JavaCC.

        this.javaccHome = javaccHome;
    
public voidsetMulti(boolean multi)
Sets the MULTI grammar option.

param
multi a boolean value.

        optionalAttrs.put(MULTI, multi ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetNodedefaultvoid(boolean nodeDefaultVoid)
Sets the NODE_DEFAULT_VOID grammar option.

param
nodeDefaultVoid a boolean value.

        optionalAttrs.put(NODE_DEFAULT_VOID, nodeDefaultVoid ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetNodefactory(boolean nodeFactory)
Sets the NODE_FACTORY grammar option.

param
nodeFactory a boolean value.

        optionalAttrs.put(NODE_FACTORY, nodeFactory ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetNodepackage(java.lang.String nodePackage)
Sets the NODE_PACKAGE grammar option.

param
nodePackage the option to use.

        optionalAttrs.put(NODE_PACKAGE, nodePackage);
    
public voidsetNodeprefix(java.lang.String nodePrefix)
Sets the NODE_PREFIX grammar option.

param
nodePrefix the option to use.

        optionalAttrs.put(NODE_PREFIX, nodePrefix);
    
public voidsetNodescopehook(boolean nodeScopeHook)
Sets the NODE_SCOPE_HOOK grammar option.

param
nodeScopeHook a boolean value.

        optionalAttrs.put(NODE_SCOPE_HOOK, nodeScopeHook ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetNodeusesparser(boolean nodeUsesParser)
Sets the NODE_USES_PARSER grammar option.

param
nodeUsesParser a boolean value.

        optionalAttrs.put(NODE_USES_PARSER, nodeUsesParser ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetOutputdirectory(java.io.File outputDirectory)
The directory to write the generated JavaCC grammar and node 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 voidsetOutputfile(java.lang.String outputFile)
The outputfile to write the generated JavaCC grammar file to. If not set, the file is written with the same name as the JJTree grammar file with a suffix .jj.

param
outputFile the output file name.

        this.outputFile = outputFile;
    
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 jjtree grammar file to process.

param
targetFile the grammar file.

        this.targetFile = targetFile;
    
public voidsetVisitor(boolean visitor)
Sets the VISITOR grammar option.

param
visitor a boolean value.

        optionalAttrs.put(VISITOR, visitor ? Boolean.TRUE : Boolean.FALSE);
    
public voidsetVisitorException(java.lang.String visitorException)
Sets the VISITOR_EXCEPTION grammar option.

param
visitorException the option to use.

        optionalAttrs.put(VISITOR_EXCEPTION, visitorException);
    
private java.lang.StringvalidateOutputFile(java.lang.String destFile, java.lang.String outputDir)
When running JJTree from an Ant taskdesk the -OUTPUT_DIRECTORY must always be set. But when -OUTPUT_DIRECTORY is set, -OUTPUT_FILE is handled as if relative of this -OUTPUT_DIRECTORY. Thus when the -OUTPUT_FILE is absolute or contains a drive letter we have a problem.

param
destFile
param
outputDir
return
throws
BuildException

        if (destFile == null) {
            return null;
        }

        if ((outputDir == null)
            && (destFile.startsWith("/") || destFile.startsWith("\\"))) {
            String relativeOutputFile = makeOutputFileRelative(destFile);
            setOutputfile(relativeOutputFile);

            return relativeOutputFile;
        }

        String root = getRoot(new File(destFile)).getAbsolutePath();

        if ((root.length() > 1)
            && destFile.startsWith(root.substring(0, root.length() - 1))) {
            throw new BuildException("Drive letter in 'outputfile' not "
                                     + "supported: " + destFile);
        }

        return destFile;