--- ANTLR.java.orig Wed Sep 18 13:58:42 2002 +++ ANTLR.java Wed Sep 18 13:58:40 2002 @@ -59,14 +59,18 @@ import java.io.FileReader; import java.io.IOException; import java.net.URL; +import java.util.Vector; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.Execute; import org.apache.tools.ant.taskdefs.LogStreamHandler; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.CommandlineJava; +import org.apache.tools.ant.types.FileList; +import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Path; /** @@ -83,6 +87,9 @@ /** the file to process */ private File target; + /** the FileSets / FileLists to process */ + protected Vector filesets = new Vector(); + /** where to output the result */ private File outputDirectory; @@ -130,6 +137,20 @@ } /** + * Adds a set of files to be ANTLR'ed. + */ + public void addFileset(FileSet set) { + filesets.addElement(set); + } + + /** + * Adds a list of files to be ANTLR'ed. + */ + public void addFilelist(FileList list) { + filesets.addElement(list); + } + + /** * The directory to write the generated files to. */ public void setOutputdirectory(File outputDirectory) { @@ -272,17 +293,71 @@ public void execute() throws BuildException { validateAttributes(); //TODO: use ANTLR to parse the grammer file to do this. - if (target.lastModified() > getGeneratedFile().lastModified()) { - populateAttributes(); - commandline.createArgument().setValue(target.toString()); - log(commandline.describeCommand(), Project.MSG_VERBOSE); - int err = run(commandline.getCommandline()); + // Compile the target file + if (target != null) { + runAntlr(target); + + return; + } + + // Compile the files in the filesets / filelists + populateAttributes(); + for (int i = 0; i < filesets.size(); i++) { + Object next = filesets.elementAt(i); + + if (next instanceof FileSet) { + FileSet fs = (FileSet)next; + DirectoryScanner ds = fs.getDirectoryScanner(project); + String[] files = ds.getIncludedFiles(); + + File directory = fs.getDir(project); + for (int j = 0; j < files.length; j++) { + File grammar = new File(directory, files[j]); + + runAntlr(grammar); + } + } + else if (next instanceof FileList) { + FileList fileList = (FileList)next; + + // Get the base directory + File directory = fileList.getDir(project); + + // Get the String array of files + String[] files = fileList.getFiles(project); + + for (int j = 0; j < files.length; j++) { + File grammar = new File(directory, files[j]); + + runAntlr(grammar); + } + } + } + } + + private void runAntlr(File grammar) + throws BuildException + { + if (grammar == null) + return; + + // Do build avoidance if we can + if (grammar.lastModified() > getGeneratedFile(grammar).lastModified()) { + // Make a private commandline + CommandlineJava newCommandline = (CommandlineJava)commandline.clone(); + newCommandline.createArgument().setValue(grammar.toString()); + + // Run ANTLR + log(newCommandline.describeCommand(), Project.MSG_VERBOSE); + int err = run(newCommandline.getCommandline()); if (err == 1) { throw new BuildException("ANTLR returned: " + err, location); } - } else { - log("Skipped grammar file. Generated file is newer.", Project.MSG_VERBOSE); + } + else { + log("Skipped grammar file '" + grammar + "'. Generated file is newer.", + Project.MSG_VERBOSE); } } @@ -318,7 +393,14 @@ } private void validateAttributes() throws BuildException { - if (target == null || !target.isFile()) { + // Complain if both target and filesets are empty + if (target == null && filesets.size() == 0) + throw new BuildException("This task requires either a 'target' attribute or a 'fileset' or 'filelist' nested element."); + + if (target != null && filesets.size() > 0) + throw new BuildException("This task does not support using both the 'target' attribute and the 'fileset' or 'filelist' nested element."); + + if (target != null && !target.isFile()) { throw new BuildException("Invalid target: " + target); } @@ -328,19 +410,22 @@ } // if no output directory is specified, used the target's directory + // We only default the output dir when target is used if (outputDirectory == null) { - String fileName = target.toString(); - setOutputdirectory(new File(target.getParent())); + if (target != null) { + String fileName = target.toString(); + setOutputdirectory(new File(target.getParent())); + } } - if (!outputDirectory.isDirectory()) { + if (outputDirectory == null || !outputDirectory.isDirectory()) { throw new BuildException("Invalid output directory: " + outputDirectory); } } - private File getGeneratedFile() throws BuildException { + private File getGeneratedFile(File grammar) throws BuildException { String generatedFileName = null; try { - BufferedReader in = new BufferedReader(new FileReader(target)); + BufferedReader in = new BufferedReader(new FileReader(grammar)); String line; while ((line = in.readLine()) != null) { int extendsIndex = line.indexOf(" extends ");