Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 360   Methods: 16
NCLOC: 154   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MAudit.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 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   
 package org.apache.tools.ant.taskdefs.optional.metamata;
 55   
 
 56   
 import java.io.File;
 57   
 import java.io.FileOutputStream;
 58   
 import java.io.IOException;
 59   
 import java.io.OutputStream;
 60   
 import java.util.Vector;
 61   
 import org.apache.tools.ant.BuildException;
 62   
 import org.apache.tools.ant.Project;
 63   
 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
 64   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 65   
 import org.apache.tools.ant.types.FileSet;
 66   
 import org.apache.tools.ant.types.Path;
 67   
 
 68   
 /**
 69   
  * Invokes the Metamata Audit/ Webgain Quality Analyzer on a set of Java files.
 70   
  * <p>
 71   
  * <i>maudit</i> performs static analysis of the Java source code and byte code files to find and report
 72   
  * errors of style and potential problems related to performance, maintenance and robustness.
 73   
  *  As a convenience, a stylesheet is given in <tt>etc</tt> directory, so that an HTML report 
 74   
  * can be generated from the XML file.
 75   
  *
 76   
  * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
 77   
  */
 78   
 public class MAudit extends AbstractMetamataTask {
 79   
 
 80   
     /* As of Metamata 2.0, the command line of MAudit is as follows:
 81   
     Usage
 82   
         maudit <option>... <path>... [-unused <search-path>...]
 83   
 
 84   
     Parameters
 85   
         path               File or directory to audit.
 86   
         search-path        File or directory to search for declaration uses.
 87   
 
 88   
     Options
 89   
         -arguments  -A     <file>     Includes command line arguments from file.
 90   
         -classpath  -cp    <path>     Sets class path (also source path unless one
 91   
                                       explicitly set). Overrides METAPATH/CLASSPATH.
 92   
         -exit       -x                Exits after the first error.
 93   
         -fix        -f                Automatically fixes certain errors.
 94   
         -fullpath                     Prints full path for locations.
 95   
         -help       -h                Prints help and exits.
 96   
         -list       -l                Creates listing file for each audited file.
 97   
         -offsets    -off              Offset and length for locations.
 98   
         -output     -o     <file>     Prints output to file.
 99   
         -quiet      -q                Suppresses copyright and summary messages.
 100   
         -sourcepath        <path>     Sets source path. Overrides SOURCEPATH.
 101   
         -tab        -t                Prints a tab character after first argument.
 102   
         -unused     -u                Finds declarations unused in search paths.
 103   
         -verbose    -v                Prints all messages.
 104   
         -version    -V                Prints version and exits.
 105   
     */
 106   
 
 107   
     //---------------------- PUBLIC METHODS ------------------------------------
 108   
 
 109   
     /** pattern used by maudit to report the error for a file */
 110   
     /** RE does not seems to support regexp pattern with comments so i'm stripping it*/
 111   
     // (?:file:)?((?#filepath).+):((?#line)\\d+)\\s*:\\s+((?#message).*)
 112   
     static final String AUDIT_PATTERN = "(?:file:)?(.+):(\\d+)\\s*:\\s+(.*)";
 113   
 
 114   
     private File outFile = null;
 115   
 
 116   
     private Path searchPath = null;
 117   
 
 118   
     private Path rulesPath = null;
 119   
 
 120   
     private boolean fix = false;
 121   
 
 122   
     private boolean list = false;
 123   
 
 124   
     private boolean unused = false;
 125   
 
 126   
 //  add a bunch of undocumented options for the task
 127   
     private boolean quiet = false;
 128   
     private boolean exit = false;
 129   
     private boolean offsets = false;
 130   
     private boolean verbose = false;
 131   
     private boolean fullsemanticize = false;
 132   
 
 133   
     /** default constructor */
 134  0
     public MAudit() {
 135  0
         super("com.metamata.gui.rc.MAudit");
 136   
     }
 137   
 
 138   
     /** 
 139   
      * The XML file to which the Audit result should be written to; required
 140   
      */
 141   
      
 142  0
     public void setTofile(File outFile) {
 143  0
         this.outFile = outFile;
 144   
     }
 145   
 
 146   
     /**
 147   
      * Automatically fix certain errors 
 148   
      * (those marked as fixable in the manual);
 149   
      * optional, default=false
 150   
      */
 151  0
     public void setFix(boolean flag) {
 152  0
         this.fix = flag;
 153   
     }
 154   
 
 155   
     /**
 156   
      * Creates listing file for each audited file; optional, default false. 
 157   
      * When set, a .maudit file will be generated in the
 158   
      * same location as the source file.
 159   
      */
 160  0
     public void setList(boolean flag) {
 161  0
         this.list = flag;
 162   
     }
 163   
 
 164   
     /**
 165   
      * Finds declarations unused in search paths; optional, default false. 
 166   
      * It will look for unused global declarations
 167   
      * in the source code within a use domain specified by the 
 168   
      * <tt>searchpath</tt> element.
 169   
      */
 170  0
     public void setUnused(boolean flag) {
 171  0
         this.unused = flag;
 172   
     }
 173   
 
 174   
     /**
 175   
      * flag to suppress copyright and summary messages; default false.
 176   
      * internal/testing only
 177   
      * @ant.attribute ignore="true"
 178   
      */
 179  0
     public void setQuiet(boolean flag) {
 180  0
         this.quiet = flag;
 181   
     }
 182   
 
 183   
     /**
 184   
      * flag to tell the task to exit after the first error. 
 185   
      * internal/testing only
 186   
      * @ant.attribute ignore="true"
 187   
      */
 188  0
     public void setExit(boolean flag) {
 189  0
         this.exit = flag;
 190   
     }
 191   
 
 192   
     /**
 193   
      * internal/testing only
 194   
      * @ant.attribute ignore="true"
 195   
      */
 196  0
     public void setOffsets(boolean flag) {
 197  0
         this.offsets = flag;
 198   
     }
 199   
 
 200   
     /**
 201   
      * flag to print all messages; optional, default false.
 202   
      * internal/testing only
 203   
      * @ant.attribute ignore="true"
 204   
      */
 205  0
     public void setVerbose(boolean flag) {
 206  0
         this.verbose = flag;
 207   
     }
 208   
 
 209   
     /**
 210   
      * internal/testing only
 211   
      * @ant.attribute ignore="true"
 212   
      */
 213  0
     public void setFullsemanticize(boolean flag) {
 214  0
         this.fullsemanticize = flag;
 215   
     }
 216   
 
 217   
     /** 
 218   
      * classpath for additional audit rules
 219   
      * these must be placed before metamata.jar !! 
 220   
      */
 221  0
     public Path createRulespath() {
 222  0
         if (rulesPath == null) {
 223  0
             rulesPath = new Path(getProject());
 224   
         }
 225  0
         return rulesPath;
 226   
     }
 227   
 
 228   
     /** 
 229   
      * search path to use for unused global declarations; 
 230   
      * required when <tt>unused</tt> is set. 
 231   
      */
 232  0
     public Path createSearchpath() {
 233  0
         if (searchPath == null) {
 234  0
             searchPath = new Path(getProject());
 235   
         }
 236  0
         return searchPath;
 237   
     }
 238   
 
 239   
     /**
 240   
      * create the option vector for the command
 241   
      */
 242  0
     protected Vector getOptions() {
 243  0
         Vector options = new Vector(512);
 244   
         // add the source path automatically from the fileset.
 245   
         // to avoid redundancy...
 246  0
         for (int i = 0; i < fileSets.size(); i++) {
 247  0
             FileSet fs = (FileSet) fileSets.elementAt(i);
 248  0
             Path path = createSourcepath();
 249  0
             File dir = fs.getDir(getProject());
 250  0
             path.setLocation(dir);
 251   
         }
 252   
 
 253   
         // there is a bug in Metamata 2.0 build 37. The sourcepath argument does
 254   
         // not work. So we will use the sourcepath prepended to classpath. (order
 255   
         // is important since Metamata looks at .class and .java)
 256  0
         if (sourcePath != null) {
 257  0
             sourcePath.append(classPath); // srcpath is prepended
 258  0
             classPath = sourcePath;
 259  0
             sourcePath = null; // prevent from using -sourcepath
 260   
         }
 261   
 
 262   
         // don't forget to modify the pattern if you change the options reporting
 263  0
         if (classPath != null) {
 264  0
             options.addElement("-classpath");
 265  0
             options.addElement(classPath.toString());
 266   
         }
 267   
         // suppress copyright msg when running, we will let it so that this
 268   
         // will be the only output to the console if in xml mode
 269  0
         if (quiet) {
 270  0
             options.addElement("-quiet");
 271   
         }
 272  0
         if (fullsemanticize) {
 273  0
             options.addElement("-full-semanticize");
 274   
         }
 275  0
         if (verbose) {
 276  0
             options.addElement("-verbose");
 277   
         }
 278  0
         if (offsets) {
 279  0
             options.addElement("-offsets");
 280   
         }
 281  0
         if (exit) {
 282  0
             options.addElement("-exit");
 283   
         }
 284  0
         if (fix) {
 285  0
             options.addElement("-fix");
 286   
         }
 287  0
         options.addElement("-fullpath");
 288   
 
 289   
         // generate .maudit files much more detailed than the report
 290   
         // I don't like it very much, I think it could be interesting
 291   
         // to get all .maudit files and include them in the XML.
 292  0
         if (list) {
 293  0
             options.addElement("-list");
 294   
         }
 295  0
         if (sourcePath != null) {
 296  0
             options.addElement("-sourcepath");
 297  0
             options.addElement(sourcePath.toString());
 298   
         }
 299  0
         addAllVector(options, includedFiles.keys());
 300  0
         if (unused) {
 301  0
             options.addElement("-unused");
 302  0
             options.addElement(searchPath.toString());
 303   
         }
 304  0
         return options;
 305   
     }
 306   
 
 307   
     /**
 308   
      * validate the settings
 309   
      */
 310  0
     protected void checkOptions() throws BuildException {
 311  0
         super.checkOptions();
 312  0
         if (unused && searchPath == null) {
 313  0
             throw new BuildException("'searchpath' element must be set when looking for 'unused' declarations.");
 314   
         }
 315  0
         if (!unused && searchPath != null) {
 316  0
             log("'searchpath' element ignored. 'unused' attribute is disabled.", Project.MSG_WARN);
 317   
         }
 318  0
         if (rulesPath != null) {
 319  0
             cmdl.createClasspath(getProject()).addExisting(rulesPath);
 320   
         }
 321   
     }
 322   
 
 323  0
     protected ExecuteStreamHandler createStreamHandler() throws BuildException {
 324   
         // if we didn't specify a file, then use a screen report
 325  0
         if (outFile == null) {
 326  0
             return new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_ERR);
 327   
         }
 328  0
         ExecuteStreamHandler handler = null;
 329  0
         OutputStream out = null;
 330  0
         try {
 331  0
             out = new FileOutputStream(outFile);
 332  0
             handler = new MAuditStreamHandler(this, out);
 333   
         } catch (IOException e) {
 334  0
             throw new BuildException(e);
 335   
         } finally {
 336  0
             if (out == null){
 337  0
                 try {
 338  0
                     out.close();
 339   
                 } catch (IOException e){
 340   
                 }
 341   
             }
 342   
         }
 343  0
         return handler;
 344   
     }
 345   
 
 346  0
     protected void cleanUp() throws BuildException {
 347  0
         super.cleanUp();
 348   
         // at this point if -list is used, we should move
 349   
         // the .maudit file since we cannot choose their location :(
 350   
         // the .maudit files match the .java files
 351   
         // we'll use includedFiles to get the .maudit files.
 352   
 
 353   
         /*if (out != null){
 354   
             // close it if not closed by the handler...
 355   
         }*/
 356   
     }
 357   
 
 358   
 }
 359   
 
 360