Hi,

This is a small patch to the JavaCC task to fix checking whether the
generated Java files are up-to-date.  Was checking the wrong path for the
output file if the outputdirectory attribute was set.

It looks like this task writes the generated files to the current directory
if an output directory is not specified.  Coupled with the change to search
the current directory's ancestors for the build.xml files, this could lead
to some rather strange behaviour if ant is not invoked from the correct
directory.  If no-one has any issues with it, I'll change the javacc task to
write the generated file to the same directory as the grammar file, if the
outputdirectory attribute is not set.
Index: src/main/org/apache/tools/ant/taskdefs/optional/javacc/JavaCC.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/javacc/JavaCC.java,v
retrieving revision 1.5
diff -u -r1.5 JavaCC.java
--- src/main/org/apache/tools/ant/taskdefs/optional/javacc/JavaCC.java  
2000/08/17 16:02:21     1.5
+++ src/main/org/apache/tools/ant/taskdefs/optional/javacc/JavaCC.java  
2000/09/25 03:28:36
@@ -228,8 +228,9 @@
         if (target == null || !target.isFile()) {
             throw new BuildException("Invalid target: " + target);
         }
-        final File javaFile = new File(
-            target.toString().substring(0, target.toString().indexOf(".jj")) + 
".java");
+
+        // determine if the generated java file is up-to-date
+        final File javaFile = getOutputJavaFile(outputDirectory, target);
         if (javaFile.exists() && target.lastModified() < 
javaFile.lastModified()) {
             project.log("Target is already built - skipping (" + target + ")");
             return;
@@ -263,5 +264,37 @@
         catch (IOException e) {
             throw new BuildException("Failed to launch JavaCC: " + e);
         }
+    }
+
+    /**
+     * Determines the output Java file to be generated by the given grammar
+     * file.
+     * 
+     */
+    private File getOutputJavaFile(File outputdir, File srcfile)
+    {
+        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);
     }
 }

Reply via email to