Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.ReportNormalizerAntTask

LineHitsNoteSource
1  /*
2   * $Id: ReportNormalizerAntTask.java 1047 2008-06-27 19:04:30Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.phoenix.report;
34  
35  import java.io.File;
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  import org.apache.tools.ant.BuildException;
42  import org.apache.tools.ant.Project;
43  import org.apache.tools.ant.taskdefs.Execute;
44  import org.apache.tools.ant.taskdefs.LogStreamHandler;
45  import org.apache.tools.ant.taskdefs.MatchingTask;
46  import org.apache.tools.ant.types.Commandline;
47  import org.apache.tools.ant.types.CommandlineJava;
48  import org.apache.tools.ant.types.Environment;
49  import org.apache.tools.ant.types.Path;
50  
51  /**
52   * Report Normalizer Ant Task.
53   *
54   * This Task takes none, one or more input reports such as Checkstyle or PMD
55   * and generates a normalized report file (XML) for any Java source under
56   * <code>srcdir</code>.
57   * This version of the class JcoderzReportAntTask just creates the combined XML
58   * report. No further processing into a HTML report is done at this time.
59   *
60   * @author Michael Griffel (Michael.Griffel@jcoderz.org)
61   * @author Michael Rumpf (Michael.Rumpf@jcoderz.org)
62   */
630 public final class ReportNormalizerAntTask
64        extends MatchingTask
65  {
66     /** the current working directory when forking JVM */
670    private File mWorkingDir = null;
68  
69     /** Output directory for XML/HTML report */
700    private File mOut = new File(".");
71     /** The Filter Filename */
720    private File mFilter = null;
73     /** The project's name */
740    private String mName = "Not defined";
75     /** Flag: exit build process if an error occurred */
760    private boolean mFailOnError = false;
77     /** The level of the report */
780    private ReportLevel mLevel = ReportLevel.PROD;
79     /** List of input report of type JcoderzReportAntTask.Report */
800    private final List mReportFiles = new ArrayList();
81     /** The Java Commandline */
820    private final CommandlineJava mCommandline = new CommandlineJava();
83     /**
84      * List of source directories of type JcoderzReportAntTask.SourceDirectory.
85      */
860    private final List mSourceDirectories = new ArrayList();
87  
88     /** Debug output flag */
890    private boolean mDebug = false;
90  
91  
92     /**
93      * Sets the output directory to given <code>dir</code>.
94      * This directory is used to store the XML/HTML report file(s).
95      * @param dir The output directory to set.
96      */
97     public void setOut (File dir)
98     {
990       mOut = dir;
1000    }
101  
102     /**
103      * Sets the filter file to <code>f</code>.
104      * This file is used to filter to raw jcoderz XML report.
105      *
106      * @param f The filter file.
107      */
108     public void setFilter (File f)
109     {
1100       mFilter = f;
1110    }
112  
113     /**
114      * Sets the projectName to given <code>projectName</code>.
115      * @param projectName The projectName to set.
116      */
117     public void setName (String projectName)
118     {
1190       mName = projectName;
1200    }
121  
122     /**
123      * Sets the level to given <code>level</code>.
124      * @param level The level to set.
125      */
126     public void setLevel (String level)
127     {
1280       mLevel = ReportLevel.fromString(level);
1290    }
130  
131     /**
132      * The directory to invoke the VM in. Ignored if no JVM is forked.
133      * @param dir the directory to invoke the JVM from.
134      */
135     public void setDir (File dir)
136     {
1370       mWorkingDir = dir;
1380    }
139  
140     /**
141      * Set whether we should fail on an error.
142      *
143      * @param b Whether we should fail on an error.
144      */
145     public void setFailonerror (boolean b)
146     {
1470       mFailOnError = b;
1480(1)   }
149  
150 (2)   public void setDebug (boolean b)
151     {
1520       mDebug = b;
1530    }
154  
155     /**
156      * Adds a classpath to the task.
157      * @return The newly created classpath.
158      */
159     public Path createClasspath ()
160     {
1610       return mCommandline.createClasspath(getProject()).createPath();
162     }
163  
164     /**
165      * Adds a bootclasspath to the task.
166      * @return The newly created bootclasspath.
167      */
168     public Path createBootclasspath ()
169     {
1700       return mCommandline.createBootclasspath(getProject()).createPath();
171     }
172  
173     /**
174      * Adds a system property.
175      *
176      * @param sysp system property
177      */
178     public void addSysproperty (Environment.Variable sysp)
179     {
1800       mCommandline.addSysproperty(sysp);
1810    }
182  
183     /**
184      * Adds a JVM argument.
185      *
186      * @return The newly created Command line argument.
187      */
188     public Commandline.Argument createJvmarg ()
189     {
1900       return mCommandline.createVmArgument();
191     }
192  
193     /**
194      * Adds a Report file such as PMD report or checkstyle report.
195      * @return The newly create report file.
196      */
197     public Report createReport ()
198     {
1990       final Report ret = new Report();
2000(3)      mReportFiles.add(ret);
2010       return ret;
202     }
203  
204     /**
205      * Adds a source folder  file such as PMD report or checkstyle report.
206      * @return The newly create report file.
207      */
208     public SourceDirectory createSrcDir ()
209     {
2100       final SourceDirectory ret = new SourceDirectory();
2110(4)      mSourceDirectories.add(ret);
2120       return ret;
213     }
214  
215     /**
216      * Execute this task.
217      *
218      * @throws BuildException An building exception occurred.
219      */
220     public void execute ()
221           throws BuildException
222     {
2230       checkParameters();
224  
225        try
226        {
227           int exitValue;
228  
2290          exitValue = executeAsForked();
230  
2310          if (exitValue != 0)
232           {
2330             final String msg = "ReportNormalizer returned with exit code '"
234                 + exitValue + "'";
2350             log(msg, Project.MSG_WARN);
2360             throw new BuildException(msg, getLocation());
237           }
238        }
2390       catch (BuildException e)
240        {
2410          if (mFailOnError)
242           {
2430             throw e;
244           }
245           else
246           {
2470             log(e.getMessage(), Project.MSG_ERR);
248           }
2490       }
2500    }
251  
252  
253     /**
254      * Create a Java command line for executing the ReportNormalizer.
255      *
256      * @param suite The suite to run
257      * @return The Java command line.
258      */
259     private CommandlineJava createCommandline ()
260     {
261         final CommandlineJava cmd;
262         try
263         {
2640            cmd = (CommandlineJava) mCommandline.clone();
265         }
2660        catch (CloneNotSupportedException unexpected)
267         {
2680            throw new RuntimeException(
269                     "Ups, CommandLineJava doesn't support the method clone()",
270                     unexpected);
2710        }
272  
2730       cmd.setClassname(ReportNormalizer.class.getName());
274  
275  // WOW      cmd.createVmArgument().setValue("-Xmx1500m");
276  
2770       cmd.createArgument().setValue("-out");
2780       cmd.createArgument().setFile(mOut);
279  
2800       cmd.createArgument().setValue("-projectName");
2810       cmd.createArgument().setValue(mName);
282  
2830       cmd.createArgument().setValue("-level");
2840(5)      cmd.createArgument().setValue(mLevel.toString());
285  
2860       cmd.createArgument().setValue("-loglevel");
2870       if (mDebug)
288        {
2890          cmd.createArgument().setValue("ALL");
290        }
291        else
292        {
2930          cmd.createArgument().setValue("INFO");
294        }
295  
2960       if (mFilter != null)
297        {
2980          cmd.createArgument().setValue("-filter");
2990          cmd.createArgument().setFile(mFilter);
300        }
301  
3020       for (final Iterator iterator = mSourceDirectories.iterator();
3030             iterator.hasNext();)
304        {
3050          final SourceDirectory sourceDir = (SourceDirectory) iterator.next();
3060          cmd.createArgument().setValue("-srcDir");
3070          cmd.createArgument().setPath(
308                    new Path(getProject(), sourceDir.getDir()));
3090       }
310  
3110       for (final Iterator iterator = mReportFiles.iterator();
3120             iterator.hasNext();)
313        {
3140          final Report reportFile = (Report) iterator.next();
3150          if (reportFile.testIfCondition())
316           {
3170             cmd.createArgument().setValue("-" + reportFile.getFormat());
3180             cmd.createArgument().setFile(reportFile.getFile());
319           }
3200       }
321  
3220       return cmd;
323     }
324  
325     private int executeAsForked ()
326           throws BuildException
327     {
3280       final Execute execute
329           = new Execute(new LogStreamHandler(
330                    this, Project.MSG_INFO, Project.MSG_WARN));
331  
3320       final CommandlineJava cmd = createCommandline();
3330       execute.setCommandline(cmd.getCommandline());
334  
3350       if (mWorkingDir != null)
336        {
3370          log("Setting working directory to : "
338                 + mWorkingDir, Project.MSG_VERBOSE);
3390          execute.setWorkingDirectory(mWorkingDir);
3400          execute.setAntRun(getProject());
341        }
342  
3430       final Path classpath = mCommandline.getClasspath();
3440       if (classpath != null)
345        {
3460          log("Using CLASSPATH " + classpath, Project.MSG_VERBOSE);
347        }
348  
3490       log("Executing: [fork] " + cmd.toString(), Project.MSG_VERBOSE);
350  
351        try
352        {
3530          return execute.execute();
354        }
3550       catch (IOException e)
356        {
3570          throw new BuildException("Process fork failed.", e, getLocation());
358        }
359     }
360  
361     private void checkParameters ()
362     {
3630(6)      if (mSourceDirectories.size() == 0)
364        {
3650          throw new BuildException(
366                 "at least one srcdir element must be specified!", getLocation());
367        }
3680    }
369  
370     /** This class represents an input report with a format and filename. */
3710    public final class Report
372     {
373        private ReportFormat mReportFormat;
374        private File mReportFilename;
3750       private String mIfCondition = "";
376  
377        /**
378         * Returns the reportFilename.
379         * @return the reportFilename.
380         */
381        public File getFile ()
382        {
3830          return mReportFilename;
384        }
385  
386        /**
387         * Sets the reportFilename to given <code>reportFilename</code>.
388         * @param reportFilename The reportFilename to set.
389         */
390        public void setFile (File reportFilename)
391        {
3920          mReportFilename = reportFilename;
3930       }
394  
395        /**
396         * Returns the reportFormat.
397         * @return the reportFormat.
398         */
399        public ReportFormat getFormat ()
400        {
4010          return mReportFormat;
402        }
403  
404        /**
405         * Sets the reportFormat to given <code>reportFormat</code>.
406         * @param reportFormat The reportFormat to set.
407         */
408        public void setFormat (String reportFormat)
409        {
4100          mReportFormat = ReportFormat.fromString(reportFormat);
4110       }
412  
413        /**
414         * Only use report file if the property is set to <code>true</code>.
415         * @param property the property name to check.
416         */
417        public void setif (String property)
418        {
4190          mIfCondition = (property == null) ? "" : property;
4200       }
421  
422        /**
423         * Tests whether or not the "if" condition is satisfied.
424         *
425         * @return whether or not the "if" condition is satisfied. If no
426         *         condition (or an empty condition) has been set,
427         *         <code>true</code> is returned.
428         */
429        private boolean testIfCondition ()
430        {
431           final boolean result;
4320          if ("".equals(mIfCondition))
433           {
4340              result = true;
435           }
436           else
437           {
4380              final String test = getProject().replaceProperties(mIfCondition);
4390              result = getProject().getProperty(test) != null;
440           }
4410          return result;
442        }
443     }
444  
4450    public static final class SourceDirectory
446     {
447        private String mSourceDir;
448        /**
449         * Returns the sourceDir.
450         * @return the sourceDir.
451         */
452        public String getDir ()
453        {
4540          return mSourceDir;
455        }
456  
457        /**
458         * Sets the sourceDir to given <code>sourceDir</code>.
459         * @param sourceDir The sourceDir to set.
460         */
461        public void setDir (String sourceDir)
462        {
4630          mSourceDir = sourceDir;
4640       }
465     }
466  }

Findings in this File

c (7) Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag 'BuildException'.
c (1) 148 : 0 Copied and pasted code. 201 equal tokens (97 lines) found in 2 locations. See also: org.jcoderz.phoenix.report.JcoderzReportAntTask:159
c (2) 150 : 4 Missing a Javadoc comment.
d (3) 200 : 23 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
d (4) 211 : 29 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
c (5) 284 : 0 Copied and pasted code. 533 equal tokens (169 lines) found in 2 locations. See also: org.jcoderz.phoenix.report.JcoderzReportAntTask:297
d (6) 363 : 11 Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()