Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 301   Methods: 11
NCLOC: 124   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
MMetrics.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.FileInputStream;
 58   
 import java.io.FileOutputStream;
 59   
 import java.io.IOException;
 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.EnumeratedAttribute;
 66   
 import org.apache.tools.ant.types.Path;
 67   
 
 68   
 /**
 69   
  * Computes the metrics of a set of Java files and write the results to an XML
 70   
  * file. As a convenience, a stylesheet is given in <tt>etc</tt> directory,
 71   
  * so that an HTML report can be generated from the XML file.
 72   
  * <p>
 73   
  * You will not be able to use this task with the evaluation version since
 74   
  * as of Metamata 2.0, Metrics does not support command line :-(
 75   
  *
 76   
  *
 77   
  * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
 78   
  */
 79   
 public class MMetrics extends AbstractMetamataTask {
 80   
 /*
 81   
     The command line options as of Metamata 2.0 are as follows:
 82   
 
 83   
 Usage
 84   
     mmetrics <option>... <path>...
 85   
 
 86   
 Parameters
 87   
     path              File or directory to measure.
 88   
 
 89   
 Options
 90   
     -arguments   -A   <file>      Includes command line arguments from file.
 91   
     -classpath   -cp  <path>      Sets class path (also source path unless one
 92   
                                   explicitly set). Overrides METAPATH/CLASSPATH.
 93   
     -compilation-units            Measure compilation units.
 94   
     -files                        Measure compilation units.
 95   
     -format      -f   <format>    Sets output format, default output file type.
 96   
     -help        -h               Prints help and exits.
 97   
     -indent      -i   <string>    Sets string used to indent labels one level.
 98   
     -methods                      Measure methods, types, and compilation units.
 99   
     -output      -o   <file>      Sets output file name.
 100   
     -quiet       -q               Suppresses copyright message.
 101   
     -sourcepath       <path>      Sets source path. Overrides SOURCEPATH.
 102   
     -types                        Measure types and compilation units.
 103   
     -verbose     -v               Prints all messages.
 104   
     -version     -V               Prints version and exits.
 105   
 
 106   
 Format Options
 107   
     comma csv                     Format output as comma-separated text.
 108   
     html htm                      Format output as an HTML table.
 109   
     tab tab-separated tsv         Format output as tab-separated text.
 110   
     text txt                      Format output as space-aligned text.
 111   
 */
 112   
 
 113   
     /** the granularity mode. Should be one of 'files', 'methods' and 'types'. */
 114   
     private String granularity = null;
 115   
 
 116   
     /** the XML output file */
 117   
     private File outFile = null;
 118   
 
 119   
     /** the location of the temporary txt report */
 120   
     private File tmpFile;
 121   
 
 122   
     private Path path = null;
 123   
 
 124   
     //--------------------------- PUBLIC METHODS -------------------------------
 125   
 
 126   
     /** default constructor */
 127  0
     public MMetrics() {
 128  0
         super("com.metamata.sc.MMetrics");
 129   
     }
 130   
 
 131   
     /**
 132   
      * Attributes for granularity.
 133   
      */
 134   
     public static class GranularityAttribute extends EnumeratedAttribute {
 135  0
         public String[] getValues() {
 136  0
             return new String[]{"compilation-units", "files", "methods", "types", "packages"};
 137   
         }
 138   
     }
 139   
 
 140   
     /**
 141   
      * set the granularity of the audit. Should be one of 'files', 'methods'
 142   
      * or 'types'.
 143   
      * @param granularity   the audit reporting mode.
 144   
      */
 145  0
     public void setGranularity(GranularityAttribute granularity) {
 146  0
         this.granularity = granularity.getValue();
 147   
     }
 148   
 
 149   
     /**
 150   
      * Set the output XML file
 151   
      * @param file the xml file to write the XML report to.
 152   
      */
 153  0
     public void setTofile(File file) {
 154  0
         this.outFile = file;
 155   
     }
 156   
 
 157   
     /**
 158   
      * Set a new path (directory) to measure metrics from.
 159   
      * @return the path instance to use.
 160   
      */
 161  0
     public Path createPath() {
 162  0
         if (path == null) {
 163  0
             path = new Path(getProject());
 164   
         }
 165  0
         return path;
 166   
 
 167   
     }
 168   
 
 169   
     //------------------- PROTECTED / PRIVATE METHODS --------------------------
 170   
 
 171   
 
 172   
     // check for existing options and outfile, all other are optional
 173  0
     protected void checkOptions() throws BuildException {
 174  0
         super.checkOptions();
 175   
 
 176  0
         if (outFile == null) {
 177  0
             throw new BuildException("Output XML file must be set via 'tofile' attribute.");
 178   
         }
 179  0
         if (path == null && fileSets.size() == 0) {
 180  0
             throw new BuildException("Must set either paths (path element) or files (fileset element)");
 181   
         }
 182   
         // I don't accept dirs and files at the same time, I cannot recognize the semantic in the result
 183  0
         if (path != null && fileSets.size() > 0) {
 184  0
             throw new BuildException("Cannot set paths (path element) and files (fileset element) at the same time");
 185   
         }
 186  0
         tmpFile = createTmpFile();
 187   
     }
 188   
 
 189  0
     protected void execute0(ExecuteStreamHandler handler) throws BuildException {
 190  0
         super.execute0(handler);
 191  0
         transformFile();
 192   
     }
 193   
 
 194   
     /**
 195   
      * transform the generated file via the handler
 196   
      * This function can either be called if the result is written to the output
 197   
      * file via -output or we could use the handler directly on stdout if not.
 198   
      * @see #createStreamHandler()
 199   
      */
 200  0
     protected void transformFile() throws BuildException {
 201  0
         FileInputStream tmpStream = null;
 202  0
         try {
 203  0
             tmpStream = new FileInputStream(tmpFile);
 204   
         } catch (IOException e) {
 205  0
             throw new BuildException("Error reading temporary file: " + tmpFile, e);
 206   
         }
 207  0
         FileOutputStream xmlStream = null;
 208  0
         try {
 209  0
             xmlStream = new FileOutputStream(outFile);
 210  0
             ExecuteStreamHandler xmlHandler = new MMetricsStreamHandler(this, xmlStream);
 211  0
             xmlHandler.setProcessOutputStream(tmpStream);
 212  0
             xmlHandler.start();
 213  0
             xmlHandler.stop();
 214   
         } catch (IOException e) {
 215  0
             throw new BuildException("Error creating output file: " + outFile, e);
 216   
         } finally {
 217  0
             if (xmlStream != null) {
 218  0
                 try {
 219  0
                     xmlStream.close();
 220   
                 } catch (IOException ignored) {
 221   
                 }
 222   
             }
 223  0
             if (tmpStream != null) {
 224  0
                 try {
 225  0
                     tmpStream.close();
 226   
                 } catch (IOException ignored) {
 227   
                 }
 228   
             }
 229   
         }
 230   
     }
 231   
 
 232   
 
 233   
     /** cleanup the temporary txt report */
 234  0
     protected void cleanUp() throws BuildException {
 235  0
         try {
 236  0
             super.cleanUp();
 237   
         } finally {
 238  0
             if (tmpFile != null) {
 239  0
                 tmpFile.delete();
 240  0
                 tmpFile = null;
 241   
             }
 242   
         }
 243   
     }
 244   
 
 245   
     /**
 246   
      * if the report is transform via a temporary txt file we should use a
 247   
      * a normal logger here, otherwise we could use the metrics handler
 248   
      * directly to capture and transform the output on stdout to XML.
 249   
      */
 250  0
     protected ExecuteStreamHandler createStreamHandler() {
 251   
         // write the report directtly to an XML stream
 252   
         // return new MMetricsStreamHandler(this, xmlStream);
 253  0
         return new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_INFO);
 254   
     }
 255   
 
 256   
 
 257  0
     protected Vector getOptions() {
 258  0
         Vector options = new Vector(512);
 259   
         // there is a bug in Metamata 2.0 build 37. The sourcepath argument does
 260   
         // not work. So we will use the sourcepath prepended to classpath. (order
 261   
         // is important since Metamata looks at .class and .java)
 262  0
         if (sourcePath != null) {
 263  0
             sourcePath.append(classPath); // srcpath is prepended
 264  0
             classPath = sourcePath;
 265  0
             sourcePath = null; // prevent from using -sourcepath
 266   
         }
 267   
 
 268   
         // don't forget to modify the pattern if you change the options reporting
 269  0
         if (classPath != null) {
 270  0
             options.addElement("-classpath");
 271  0
             options.addElement(classPath.toString());
 272   
         }
 273  0
         options.addElement("-output");
 274  0
         options.addElement(tmpFile.toString());
 275   
 
 276  0
         options.addElement("-" + granularity);
 277   
 
 278   
         // display the metamata copyright
 279   
         // options.addElement( "-quiet");
 280  0
         options.addElement("-format");
 281   
 
 282   
         // need this because that's what the handler is using, it's
 283   
         // way easier to process than any other separator
 284  0
         options.addElement("tab");
 285   
 
 286   
         // specify a / as the indent character, used by the handler.
 287  0
         options.addElement("-i");
 288  0
         options.addElement("/");
 289   
 
 290   
         // directories
 291  0
         String[] dirs = path.list();
 292  0
         for (int i = 0; i < dirs.length; i++) {
 293  0
             options.addElement(dirs[i]);
 294   
         }
 295   
         // files next.
 296  0
         addAllVector(options, includedFiles.keys());
 297  0
         return options;
 298   
     }
 299   
 
 300   
 }
 301