Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 274   Methods: 16
NCLOC: 130   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JJTree.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.javacc;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.IOException;
 59   
 import java.util.Enumeration;
 60   
 import java.util.Hashtable;
 61   
 import org.apache.tools.ant.BuildException;
 62   
 import org.apache.tools.ant.Project;
 63   
 import org.apache.tools.ant.Task;
 64   
 import org.apache.tools.ant.taskdefs.Execute;
 65   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 66   
 import org.apache.tools.ant.types.Commandline;
 67   
 import org.apache.tools.ant.types.CommandlineJava;
 68   
 import org.apache.tools.ant.types.Path;
 69   
 import org.apache.tools.ant.util.JavaEnvUtils;
 70   
 
 71   
 /**
 72   
  * Runs the JJTree compiler compiler.
 73   
  *
 74   
  * @author thomas.haas@softwired-inc.com
 75   
  * @author Michael Saunders <a href="mailto:michael@amtec.com">michael@amtec.com</a>
 76   
  */
 77   
 public class JJTree extends Task {
 78   
 
 79   
     // keys to optional attributes
 80   
     private static final String BUILD_NODE_FILES  = "BUILD_NODE_FILES";
 81   
     private static final String MULTI             = "MULTI";
 82   
     private static final String NODE_DEFAULT_VOID = "NODE_DEFAULT_VOID";
 83   
     private static final String NODE_FACTORY      = "NODE_FACTORY";
 84   
     private static final String NODE_SCOPE_HOOK   = "NODE_SCOPE_HOOK";
 85   
     private static final String NODE_USES_PARSER  = "NODE_USES_PARSER";
 86   
     private static final String STATIC            = "STATIC";
 87   
     private static final String VISITOR           = "VISITOR";
 88   
 
 89   
     private static final String NODE_PACKAGE      = "NODE_PACKAGE";
 90   
     private static final String VISITOR_EXCEPTION = "VISITOR_EXCEPTION";
 91   
     private static final String NODE_PREFIX       = "NODE_PREFIX";
 92   
 
 93   
     private final Hashtable optionalAttrs = new Hashtable();
 94   
 
 95   
     // required attributes
 96   
     private File outputDirectory = null;
 97   
     private File target          = null;
 98   
     private File javaccHome      = null;
 99   
 
 100   
     private CommandlineJava cmdl = new CommandlineJava();
 101   
 
 102   
 
 103   
     /**
 104   
      * Sets the BUILD_NODE_FILES grammar option.
 105   
      */
 106  0
     public void setBuildnodefiles(boolean buildNodeFiles) {
 107  0
         optionalAttrs.put(BUILD_NODE_FILES, new Boolean(buildNodeFiles));
 108   
     }
 109   
 
 110   
     /**
 111   
      * Sets the MULTI grammar option.
 112   
      */
 113  0
     public void setMulti(boolean multi) {
 114  0
         optionalAttrs.put(MULTI, new Boolean(multi));
 115   
     }
 116   
 
 117   
     /**
 118   
      * Sets the NODE_DEFAULT_VOID grammar option.
 119   
      */
 120  0
     public void setNodedefaultvoid(boolean nodeDefaultVoid) {
 121  0
         optionalAttrs.put(NODE_DEFAULT_VOID, new Boolean(nodeDefaultVoid));
 122   
     }
 123   
 
 124   
     /**
 125   
      * Sets the NODE_FACTORY grammar option.
 126   
      */
 127  0
     public void setNodefactory(boolean nodeFactory) {
 128  0
         optionalAttrs.put(NODE_FACTORY, new Boolean(nodeFactory));
 129   
     }
 130   
 
 131   
     /**
 132   
      * Sets the NODE_SCOPE_HOOK grammar option.
 133   
      */
 134  0
     public void setNodescopehook(boolean nodeScopeHook) {
 135  0
         optionalAttrs.put(NODE_SCOPE_HOOK, new Boolean(nodeScopeHook));
 136   
     }
 137   
 
 138   
     /**
 139   
      * Sets the NODE_USES_PARSER grammar option.
 140   
      */
 141  0
     public void setNodeusesparser(boolean nodeUsesParser) {
 142  0
         optionalAttrs.put(NODE_USES_PARSER, new Boolean(nodeUsesParser));
 143   
     }
 144   
 
 145   
     /**
 146   
      * Sets the STATIC grammar option.
 147   
      */
 148  0
     public void setStatic(boolean staticParser) {
 149  0
         optionalAttrs.put(STATIC, new Boolean(staticParser));
 150   
     }
 151   
 
 152   
     /**
 153   
      * Sets the VISITOR grammar option.
 154   
      */
 155  0
     public void setVisitor(boolean visitor) {
 156  0
         optionalAttrs.put(VISITOR, new Boolean(visitor));
 157   
     }
 158   
 
 159   
     /**
 160   
      * Sets the NODE_PACKAGE grammar option.
 161   
      */
 162  0
     public void setNodepackage(String nodePackage) {
 163  0
         optionalAttrs.put(NODE_PACKAGE, new String(nodePackage));
 164   
     }
 165   
 
 166   
     /**
 167   
      * Sets the VISITOR_EXCEPTION grammar option.
 168   
      */
 169  0
     public void setVisitorException(String visitorException) {
 170  0
         optionalAttrs.put(VISITOR_EXCEPTION, new String(visitorException));
 171   
     }
 172   
 
 173   
     /**
 174   
      * Sets the NODE_PREFIX grammar option.
 175   
      */
 176  0
     public void setNodeprefix(String nodePrefix) {
 177  0
         optionalAttrs.put(NODE_PREFIX, new String(nodePrefix));
 178   
     }
 179   
 
 180   
     /**
 181   
      * The directory to write the generated file to.
 182   
      * If not set, the files are written to the directory
 183   
      * containing the grammar file.
 184   
      */
 185  0
     public void setOutputdirectory(File outputDirectory) {
 186  0
         this.outputDirectory = outputDirectory;
 187   
     }
 188   
 
 189   
     /**
 190   
      * The jjtree grammar file to process.
 191   
      */
 192  0
     public void setTarget(File target) {
 193  0
         this.target = target;
 194   
     }
 195   
 
 196   
     /**
 197   
      * The directory containing the JavaCC distribution.
 198   
      */
 199  0
     public void setJavacchome(File javaccHome) {
 200  0
         this.javaccHome = javaccHome;
 201   
     }
 202   
 
 203  0
     public JJTree() {
 204  0
         cmdl.setVm(JavaEnvUtils.getJreExecutable("java"));
 205   
     }
 206   
 
 207  0
     public void execute() throws BuildException {
 208   
 
 209   
         // load command line with optional attributes
 210  0
         Enumeration iter = optionalAttrs.keys();
 211  0
         while (iter.hasMoreElements()) {
 212  0
             String name  = (String) iter.nextElement();
 213  0
             Object value = optionalAttrs.get(name);
 214  0
             cmdl.createArgument().setValue("-" + name + ":" + value.toString());
 215   
         }
 216   
 
 217  0
         if (target == null || !target.isFile()) {
 218  0
             throw new BuildException("Invalid target: " + target);
 219   
         }
 220   
 
 221   
         // use the directory containing the target as the output directory
 222  0
         if (outputDirectory == null) {
 223  0
             outputDirectory = new File(target.getParent());
 224   
         }
 225  0
         if (!outputDirectory.isDirectory()) {
 226  0
             throw new BuildException("'outputdirectory' " + outputDirectory
 227   
                 + " is not a directory.");
 228   
         }
 229   
         // convert backslashes to slashes, otherwise jjtree will put this as
 230   
         // comments and this seems to confuse javacc
 231  0
         cmdl.createArgument().setValue("-OUTPUT_DIRECTORY:"
 232   
             + outputDirectory.getAbsolutePath().replace('\\', '/'));
 233   
 
 234  0
         String targetName = target.getName();
 235  0
         final File javaFile = new File(outputDirectory,
 236   
             targetName.substring(0, targetName.indexOf(".jjt")) + ".jj");
 237  0
         if (javaFile.exists()
 238   
              && target.lastModified() < javaFile.lastModified()) {
 239  0
             log("Target is already built - skipping (" + target + ")",
 240   
                 Project.MSG_VERBOSE);
 241  0
             return;
 242   
         }
 243  0
         cmdl.createArgument().setValue(target.getAbsolutePath());
 244   
 
 245  0
         cmdl.setClassname(JavaCC.getMainClass(javaccHome, 
 246   
                                               JavaCC.TASKDEF_TYPE_JJTREE));
 247   
 
 248  0
         final Path classpath = cmdl.createClasspath(getProject());
 249  0
         final File javaccJar = JavaCC.getArchiveFile(javaccHome);
 250  0
         classpath.createPathElement().setPath(javaccJar.getAbsolutePath());
 251  0
         classpath.addJavaRuntime();
 252   
 
 253  0
         final Commandline.Argument arg = cmdl.createVmArgument();
 254  0
         arg.setValue("-mx140M");
 255  0
         arg.setValue("-Dinstall.root=" + javaccHome.getAbsolutePath());
 256   
 
 257  0
         final Execute process =
 258   
             new Execute(new LogStreamHandler(this,
 259   
                                              Project.MSG_INFO,
 260   
                                              Project.MSG_INFO),
 261   
                         null);
 262  0
         log(cmdl.describeCommand(), Project.MSG_VERBOSE);
 263  0
         process.setCommandline(cmdl.getCommandline());
 264   
 
 265  0
         try {
 266  0
             if (process.execute() != 0) {
 267  0
                 throw new BuildException("JJTree failed.");
 268   
             }
 269   
         } catch (IOException e) {
 270  0
             throw new BuildException("Failed to launch JJTree", e);
 271   
         }
 272   
     }
 273   
 }
 274