ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sbaill...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional ANTLR.java
Date Sun, 18 Nov 2001 15:59:18 GMT
sbailliez    01/11/18 07:59:18

  Modified:    docs/manual/OptionalTasks antlr.html
               src/main/org/apache/tools/ant/taskdefs/optional ANTLR.java
  Log:
  ANTLR task will now work with the jar being in ${ant.home}/lib rather than in the classpath.
  Bug 4899 reported by David Holscher <David.Holscher@nav-international.com>
  
  I copied the code snippet that Stefan did for JUnit. Thanks Stefan.
  I added the possibility to specify a classpath and jvm args to ANTLR and fixed the code
so that the working dir is not mandatory as specified in the docs.
  
  Revision  Changes    Path
  1.3       +26 -0     jakarta-ant/docs/manual/OptionalTasks/antlr.html
  
  Index: antlr.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/OptionalTasks/antlr.html,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- antlr.html	2001/02/13 12:31:55	1.2
  +++ antlr.html	2001/11/18 15:59:18	1.3
  @@ -57,6 +57,32 @@
       <td align="center" valign="top">No</td>
     </tr>
   </table>
  +
  +<h3><a name="nested">Nested Elements</a></h3>
  +
  +<p><code>ANTLR</code> supports a nested <code>&lt;classpath&gt;</code>
  +element, that represents a <a href="../using.html#path">PATH like
  +structure</a>. It is given as a convenience if you have to specify
  +the original ANTLR directory. In most cases, dropping the appropriate
  +ANTLR jar in the normal Ant lib repository will be enough.</p>
  +
  +<h4>jvmarg</h4>
  +
  +<p>If fork is enabled, additional parameters may be passed to the new
  +VM via nested <code>&lt;jvmarg&gt;</code> attributes, for example:</p>
  +
  +<pre>
  +&lt;antlr fork=&quot;yes&quot; target=...&gt;
  +  &lt;jvmarg value=&quot;-Djava.compiler=NONE&quot;/&gt;
  +  ...
  +&lt;/antlr&gt;
  +</pre>
  +
  +<p>would run ANTLR in a VM without JIT.</p>
  +
  +<p><code>&lt;jvmarg&gt;</code> allows all attributes described
in <a
  +href="../using.html#arg">Command line arguments</a>.</p>
  +
   <h3>Example</h3>
   <blockquote><pre>
   &lt;antlr
  
  
  
  1.4       +88 -11    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java
  
  Index: ANTLR.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/ANTLR.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ANTLR.java	2001/10/28 21:30:18	1.3
  +++ ANTLR.java	2001/11/18 15:59:18	1.4
  @@ -58,22 +58,40 @@
   import java.io.BufferedReader;
   import java.io.FileReader;
   import java.io.IOException;
  +import java.net.URL;
  +
   import org.apache.tools.ant.Task;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.taskdefs.Execute;
   import org.apache.tools.ant.taskdefs.LogStreamHandler;
  +import org.apache.tools.ant.taskdefs.ExecuteJava;
   import org.apache.tools.ant.types.CommandlineJava;
  +import org.apache.tools.ant.types.Path;
  +import org.apache.tools.ant.types.Commandline;
  +import org.apache.tools.ant.types.Environment;
  +
   /**
  - * @author Erik Meade, emeade@geekfarm.org
  + * ANTLR task.
  + *
  + * @author <a href="mailto:emeade@geekfarm.org">Erik Meade</a>
  + * @author <a href="mailto:sbailliez@apache.org>Stephane Bailliez</a>
    */
   public class ANTLR extends Task {
   
       private CommandlineJava commandline = new CommandlineJava();
  +
  +    /** the file to process */
       private File target;
  +
  +    /** where to output the result */
       private File outputDirectory;
  +
  +    /** should fork ? */
       private boolean fork = false;
  -    private File dir;
  +
  +    /** working directory */
  +    private File workingdir = null;
   
       public ANTLR() {
           commandline.setVm("java");
  @@ -98,10 +116,67 @@
        * The working directory of the process
        */
       public void setDir(File d) {
  -        this.dir = d;
  +        this.workingdir = d;
  +    }
  +
  +    /**
  +     * <code>&lt;classpath&gt;</code> allows classpath to be set
  +     * because a directory might be given for Antlr debug...
  +     */
  +    public Path createClasspath() {
  +        return commandline.createClasspath(project).createPath();
       }
   
  +    /**
  +     * Create a new JVM argument. Ignored if no JVM is forked.
  +     * @return  create a new JVM argument so that any argument can be passed to the JVM.
  +     * @see #setFork(boolean)
  +     */
  +    public Commandline.Argument createJvmarg() {
  +        return commandline.createVmArgument();
  +    }
   
  +    /**
  +     * Adds the jars or directories containing Antlr
  +     * this should make the forked JVM work without having to
  +     * specify it directly.
  +     */
  +    public void init() throws BuildException {
  +        addClasspathEntry("/antlr/Tool.class");
  +    }
  +
  +    /**
  +     * Search for the given resource and add the directory or archive
  +     * that contains it to the classpath.
  +     *
  +     * <p>Doesn't work for archives in JDK 1.1 as the URL returned by
  +     * getResource doesn't contain the name of the archive.</p>
  +     */
  +    protected void addClasspathEntry(String resource) {
  +        URL url = getClass().getResource(resource);
  +        if (url != null) {
  +            String u = url.toString();
  +            if (u.startsWith("jar:file:")) {
  +                int pling = u.indexOf("!");
  +                String jarName = u.substring(9, pling);
  +                log("Implicitly adding "+jarName+" to classpath",
  +                    Project.MSG_DEBUG);
  +                createClasspath().setLocation(new File((new File(jarName)).getAbsolutePath()));
  +            } else if (u.startsWith("file:")) {
  +                int tail = u.indexOf(resource);
  +                String dirName = u.substring(5, tail);
  +                log("Implicitly adding "+dirName+" to classpath",
  +                    Project.MSG_DEBUG);
  +                createClasspath().setLocation(new File((new File(dirName)).getAbsolutePath()));
  +            } else {
  +                log("Don\'t know how to handle resource URL "+u,
  +                    Project.MSG_DEBUG);
  +            }
  +        } else {
  +            log("Couldn\'t find "+resource, Project.MSG_DEBUG);
  +        }
  +    }
  +
       public void execute() throws BuildException {
           validateAttributes();
   
  @@ -117,9 +192,11 @@
                   if (err == 1) {
                       throw new BuildException("ANTLR returned: "+err, location);
                   }
  -            }
  -            else {
  -                Execute.runCommand(this, commandline.getCommandline());
  +            } else {
  +                ExecuteJava exe = new ExecuteJava();
  +                exe.setJavaCommand(commandline.getJavaCommand());
  +                exe.setClasspath(commandline.getClasspath());
  +                exe.execute(project);
               }
           }
       }
  @@ -137,9 +214,6 @@
           if (!outputDirectory.isDirectory()) {
               throw new BuildException("Invalid output directory: " + outputDirectory);
           }
  -        if (fork && (dir == null || !dir.isDirectory())) {
  -            throw new BuildException("Invalid working directory: " + dir);
  -        }
       }
   
       private File getGeneratedFile() throws BuildException {
  @@ -156,7 +230,7 @@
               }
               in.close();
           } catch (Exception e) {
  -            throw new BuildException("Unable to determine generated class");
  +            throw new BuildException("Unable to determine generated class", e);
           }
           if (generatedFileName == null) {
               throw new BuildException("Unable to determine generated class");
  @@ -164,11 +238,14 @@
           return new File(outputDirectory, generatedFileName + ".java");
       }
   
  +    /** execute in a forked VM */
       private int run(String[] command) throws BuildException {
           Execute exe = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
                                                          Project.MSG_WARN), null);
           exe.setAntRun(project);
  -        exe.setWorkingDirectory(dir);
  +        if (workingdir != null){
  +            exe.setWorkingDirectory(workingdir);
  +        }
           exe.setCommandline(command);
           try {
               return exe.execute();
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message