Fields Summary |
---|
private org.apache.tools.ant.types.CommandlineJava | commandline |
private File | targetFilethe file to process |
private File | outputDirectorywhere to output the result |
private File | superGrammaran optional super grammar file |
private boolean | htmloptional flag to enable html output |
private boolean | diagnosticoptional flag to print out a diagnostic file |
private boolean | traceoptional flag to add trace methods |
private boolean | traceParseroptional flag to add trace methods to the parser only |
private boolean | traceLexeroptional flag to add trace methods to the lexer only |
private boolean | traceTreeWalkeroptional flag to add trace methods to the tree walker only |
private File | workingdirworking directory |
private ByteArrayOutputStream | boscaptures ANTLR's output |
private boolean | debugThe debug attribute |
private static final org.apache.tools.ant.util.FileUtils | FILE_UTILSInstance of a utility class to use for file operations. |
Methods Summary |
---|
protected void | addClasspathEntry(java.lang.String resource)Search for the given resource and add the directory or archive
that contains it to the classpath.
Doesn't work for archives in JDK 1.1 as the URL returned by
getResource doesn't contain the name of the archive.
/*
* pre Ant 1.6 this method used to call getClass().getResource
* while Ant 1.6 will call ClassLoader.getResource().
*
* The difference is that Class.getResource expects a leading
* slash for "absolute" resources and will strip it before
* delegating to ClassLoader.getResource - so we now have to
* emulate Class's behavior.
*/
if (resource.startsWith("/")) {
resource = resource.substring(1);
} else {
resource = "org/apache/tools/ant/taskdefs/optional/"
+ resource;
}
File f = LoaderUtils.getResourceSource(getClass().getClassLoader(),
resource);
if (f != null) {
log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG);
createClasspath().setLocation(f);
} else {
log("Couldn\'t find " + resource, Project.MSG_VERBOSE);
}
|
public org.apache.tools.ant.types.Path | createClasspath()Adds a classpath to be set
because a directory might be given for Antlr debug.
return commandline.createClasspath(getProject()).createPath();
|
public Commandline.Argument | createJvmarg()Adds a new JVM argument.
return commandline.createVmArgument();
|
public void | execute()Execute the task.
validateAttributes();
//TODO: use ANTLR to parse the grammar file to do this.
File generatedFile = getGeneratedFile();
boolean targetIsOutOfDate =
targetFile.lastModified() > generatedFile.lastModified();
boolean superGrammarIsOutOfDate = superGrammar != null
&& (superGrammar.lastModified() > generatedFile.lastModified());
if (targetIsOutOfDate || superGrammarIsOutOfDate) {
if (targetIsOutOfDate) {
log("Compiling " + targetFile + " as it is newer than "
+ generatedFile, Project.MSG_VERBOSE);
} else if (superGrammarIsOutOfDate) {
log("Compiling " + targetFile + " as " + superGrammar
+ " is newer than " + generatedFile, Project.MSG_VERBOSE);
}
populateAttributes();
commandline.createArgument().setValue(targetFile.toString());
log(commandline.describeCommand(), Project.MSG_VERBOSE);
int err = run(commandline.getCommandline());
if (err != 0) {
throw new BuildException("ANTLR returned: " + err, getLocation());
} else {
String output = bos.toString();
if (output.indexOf("error:") > -1) {
throw new BuildException("ANTLR signaled an error: "
+ output, getLocation());
}
}
} else {
log("Skipped grammar file. Generated file " + generatedFile
+ " is newer.", Project.MSG_VERBOSE);
}
|
private java.io.File | getGeneratedFile()
String generatedFileName = null;
try {
BufferedReader in = new BufferedReader(new FileReader(targetFile));
String line;
while ((line = in.readLine()) != null) {
int extendsIndex = line.indexOf(" extends ");
if (line.startsWith("class ") && extendsIndex > -1) {
generatedFileName = line.substring(6, extendsIndex).trim();
break;
}
}
in.close();
} catch (Exception e) {
throw new BuildException("Unable to determine generated class", e);
}
if (generatedFileName == null) {
throw new BuildException("Unable to determine generated class");
}
return new File(outputDirectory, generatedFileName
+ (html ? ".html" : ".java"));
|
public void | init()Adds the jars or directories containing Antlr
this should make the forked JVM work without having to
specify it directly.
addClasspathEntry("/antlr/ANTLRGrammarParseBehavior.class");
|
protected boolean | is272()Whether the antlr version is 2.7.2 (or higher).
AntClassLoader l = null;
try {
l = getProject().createClassLoader(commandline.getClasspath());
l.loadClass("antlr.Version");
return true;
} catch (ClassNotFoundException e) {
return false;
} finally {
if (l != null) {
l.cleanup();
}
}
|
private void | populateAttributes()A refactored method for populating all the command line arguments based
on the user-specified attributes.
commandline.createArgument().setValue("-o");
commandline.createArgument().setValue(outputDirectory.toString());
if (superGrammar != null) {
commandline.createArgument().setValue("-glib");
commandline.createArgument().setValue(superGrammar.toString());
}
if (html) {
commandline.createArgument().setValue("-html");
}
if (diagnostic) {
commandline.createArgument().setValue("-diagnostic");
}
if (trace) {
commandline.createArgument().setValue("-trace");
}
if (traceParser) {
commandline.createArgument().setValue("-traceParser");
}
if (traceLexer) {
commandline.createArgument().setValue("-traceLexer");
}
if (traceTreeWalker) {
if (is272()) {
commandline.createArgument().setValue("-traceTreeParser");
} else {
commandline.createArgument().setValue("-traceTreeWalker");
}
}
if (debug) {
commandline.createArgument().setValue("-debug");
}
|
private int | run(java.lang.String[] command)execute in a forked VM
PumpStreamHandler psh =
new PumpStreamHandler(new LogOutputStream(this, Project.MSG_INFO),
new TeeOutputStream(
new LogOutputStream(this,
Project.MSG_WARN),
bos)
);
Execute exe = new Execute(psh, null);
exe.setAntRun(getProject());
if (workingdir != null) {
exe.setWorkingDirectory(workingdir);
}
exe.setCommandline(command);
try {
return exe.execute();
} catch (IOException e) {
throw new BuildException(e, getLocation());
} finally {
FileUtils.close(bos);
}
|
public void | setDebug(boolean enable)Sets a flag to enable ParseView debugging
this.debug = enable;
|
public void | setDiagnostic(boolean enable)Sets a flag to emit diagnostic text
diagnostic = enable;
|
public void | setDir(java.io.File d)The working directory of the process
this.workingdir = d;
|
public void | setFork(boolean s)
//this.fork = s;
|
public void | setGlib(java.lang.String superGrammar)Sets an optional super grammar file.
Use setGlib(File superGrammar) instead.
String sg = null;
if (Os.isFamily("dos")) {
sg = superGrammar.replace('\\", '/");
} else {
sg = superGrammar;
}
setGlib(FILE_UTILS.resolveFile(getProject().getBaseDir(), sg));
|
public void | setGlib(java.io.File superGrammar)Sets an optional super grammar file
this.superGrammar = superGrammar;
|
public void | setHtml(boolean enable)If true, emit html
html = enable;
|
public void | setOutputdirectory(java.io.File outputDirectory)The directory to write the generated files to.
log("Setting output directory to: " + outputDirectory.toString(), Project.MSG_VERBOSE);
this.outputDirectory = outputDirectory;
|
public void | setTarget(java.io.File target)The grammar file to process.
log("Setting target to: " + target.toString(), Project.MSG_VERBOSE);
this.targetFile = target;
|
public void | setTrace(boolean enable)If true, enables all tracing.
trace = enable;
|
public void | setTraceLexer(boolean enable)If true, enables lexer tracing.
traceLexer = enable;
|
public void | setTraceParser(boolean enable)If true, enables parser tracing.
traceParser = enable;
|
public void | setTraceTreeWalker(boolean enable)Sets a flag to allow the user to enable tree walker tracing
traceTreeWalker = enable;
|
private void | validateAttributes()
if (targetFile == null || !targetFile.isFile()) {
throw new BuildException("Invalid target: " + targetFile);
}
// if no output directory is specified, used the target's directory
if (outputDirectory == null) {
setOutputdirectory(new File(targetFile.getParent()));
}
if (!outputDirectory.isDirectory()) {
throw new BuildException("Invalid output directory: " + outputDirectory);
}
|