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 |
Methods Summary |
---|
private java.lang.String | createOutputFileName(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 void | execute()Run the task.
// 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.String | getDefaultOutputDirectory()
return getProject().getBaseDir().getAbsolutePath().replace('\\", '/");
|
private java.io.File | getRoot(java.io.File file)Determine root directory for a given file.
File root = file.getAbsoluteFile();
while (root.getParent() != null) {
root = root.getParentFile();
}
return root;
|
private java.lang.String | makeOutputFileRelative(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 void | setBuildnodefiles(boolean buildNodeFiles)Sets the BUILD_NODE_FILES grammar option.
optionalAttrs.put(BUILD_NODE_FILES, buildNodeFiles ? Boolean.TRUE : Boolean.FALSE);
|
public void | setJavacchome(java.io.File javaccHome)The directory containing the JavaCC distribution.
this.javaccHome = javaccHome;
|
public void | setMulti(boolean multi)Sets the MULTI grammar option.
optionalAttrs.put(MULTI, multi ? Boolean.TRUE : Boolean.FALSE);
|
public void | setNodedefaultvoid(boolean nodeDefaultVoid)Sets the NODE_DEFAULT_VOID grammar option.
optionalAttrs.put(NODE_DEFAULT_VOID, nodeDefaultVoid ? Boolean.TRUE : Boolean.FALSE);
|
public void | setNodefactory(boolean nodeFactory)Sets the NODE_FACTORY grammar option.
optionalAttrs.put(NODE_FACTORY, nodeFactory ? Boolean.TRUE : Boolean.FALSE);
|
public void | setNodepackage(java.lang.String nodePackage)Sets the NODE_PACKAGE grammar option.
optionalAttrs.put(NODE_PACKAGE, nodePackage);
|
public void | setNodeprefix(java.lang.String nodePrefix)Sets the NODE_PREFIX grammar option.
optionalAttrs.put(NODE_PREFIX, nodePrefix);
|
public void | setNodescopehook(boolean nodeScopeHook)Sets the NODE_SCOPE_HOOK grammar option.
optionalAttrs.put(NODE_SCOPE_HOOK, nodeScopeHook ? Boolean.TRUE : Boolean.FALSE);
|
public void | setNodeusesparser(boolean nodeUsesParser)Sets the NODE_USES_PARSER grammar option.
optionalAttrs.put(NODE_USES_PARSER, nodeUsesParser ? Boolean.TRUE : Boolean.FALSE);
|
public void | setOutputdirectory(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.
this.outputDirectory = outputDirectory;
|
public void | setOutputfile(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.
this.outputFile = outputFile;
|
public void | setStatic(boolean staticParser)Sets the STATIC grammar option.
optionalAttrs.put(STATIC, staticParser ? Boolean.TRUE : Boolean.FALSE);
|
public void | setTarget(java.io.File targetFile)The jjtree grammar file to process.
this.targetFile = targetFile;
|
public void | setVisitor(boolean visitor)Sets the VISITOR grammar option.
optionalAttrs.put(VISITOR, visitor ? Boolean.TRUE : Boolean.FALSE);
|
public void | setVisitorException(java.lang.String visitorException)Sets the VISITOR_EXCEPTION grammar option.
optionalAttrs.put(VISITOR_EXCEPTION, visitorException);
|
private java.lang.String | validateOutputFile(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.
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;
|