Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.JcoderzReportAntTask

LineHitsNoteSource
1  /*
2   * $Id: JcoderzReportAntTask.java 1011 2008-06-16 17:57:36Z 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   * jCoderZ Report 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   *
58   * @author Michael Griffel (Michael.Griffel@jcoderz.com)
59   */
600 public final class JcoderzReportAntTask
61        extends MatchingTask
62  {
63     /** the current working directory when forking JVM. */
640    private File mWorkingDir = null;
65  
66     /** Output directory for XML/HTML report. */
670    private File mOut = new File(".");
68     /** The Filter Filename. */
690    private File mFilter = null;
70     /** The project's name. */
710    private String mName = "Not defined";
72     /** The output format. */
730    private OutputFormat mOutputFormat = OutputFormat.XML;
74     /** Flag: exit build process if an error occurred. */
750    private boolean mFailOnError = false;
76     /** List of input report of type JcoderzReportAntTask.Report. */
770    private final List mReportFiles = new ArrayList();
78     /** The Java Commandline. */
790    private final CommandlineJava mCommandline = new CommandlineJava();
80     /**
81      * List of source directories of type JcoderzReportAntTask.SourceDirectory.
82      */
830    private final List mSourceDirectories = new ArrayList();
84  
85     /** Debug output flag. */
860    private boolean mDebug = false;
87  
88  
89     /**
90      * Sets the output directory.
91      * This directory is used to store the report file(s).
92      *
93      * @param dir the output directory
94      */
95 (1)   public final void setOut (File dir)
96     {
970       mOut = dir;
980    }
99  
100     /**
101      * Sets the filter file.
102      *
103      * @param f the filter file
104      */
105 (2)   public final void setFilter (File f)
106     {
1070       mFilter = f;
1080    }
109  
110     /**
111      * Sets the projectName to given <code>projectName</code>.
112      *
113      * @param projectName the project name
114      */
115 (3)   public final void setName (String projectName)
116     {
1170       mName = projectName;
1180    }
119  
120     /**
121      * The directory to invoke the VM in. Ignored if no JVM is forked.
122      *
123      * @param dir the directory to invoke the JVM from
124      */
125     public void setDir (File dir)
126     {
1270       mWorkingDir = dir;
1280    }
129  
130     /**
131      * Set whether we should fail on an error.
132      *
133      * @param b whether we should fail on an error
134      */
135     public void setFailonerror (boolean b)
136     {
1370       mFailOnError = b;
1380    }
139  
140     /**
141      * Sets the output format.
142      *
143      * @param s the output format
144      * @throws BuildException throws a BuildException when the format
145      *       name is not valid
146      */
147     public void setFormat (String s)
148           throws BuildException
149     {
150        try
151        {
1520          mOutputFormat = OutputFormat.fromString(s);
153        }
1540       catch (IllegalArgumentException e)
155        {
1560          throw new BuildException("Unsupported output format '"
157                 + s + "'", e, getLocation());
1580       }
1590(4)   }
160  
161     /**
162      * Sets the debug flag.
163      *
164      * @param b the debug mode
165      */
166     public void setDebug (boolean b)
167     {
1680       mDebug = b;
1690    }
170  
171     /**
172      * Adds a classpath to the command line.
173      *
174      * @return the created classpath
175      */
176     public Path createClasspath ()
177     {
1780       return mCommandline.createClasspath(getProject()).createPath();
179     }
180  
181     /**
182      * Adds a bootclasspath to the command line.
183      *
184      * @return the created bootclasspath
185      */
186     public Path createBootclasspath ()
187     {
1880       return mCommandline.createBootclasspath(getProject()).createPath();
189     }
190  
191     /**
192      * Adds a system property.
193      *
194      * @param sysp system property
195      */
196     public void addSysproperty (Environment.Variable sysp)
197     {
1980       mCommandline.addSysproperty(sysp);
1990    }
200  
201     /**
202      * Adds a JVM argument.
203      *
204      * @return the created command line argument
205      */
206     public Commandline.Argument createJvmarg ()
207     {
2080       return mCommandline.createVmArgument();
209     }
210  
211     /**
212      * Adds a report to the list of reports.
213      *
214      * @return the created report
215      */
216     public Report createReport ()
217     {
2180       final Report ret = new Report();
2190(5)      mReportFiles.add(ret);
2200       return ret;
221     }
222  
223     /**
224      * Adds a source directory to the list of directories.
225      *
226      * @return the created source directory
227      */
228     public SourceDirectory createSrcDir ()
229     {
2300       final SourceDirectory ret = new SourceDirectory();
2310(6)      mSourceDirectories.add(ret);
2320       return ret;
233     }
234  
235     /**
236      * Executes this task.
237      *
238      * @throws BuildException An building exception occurred.
239      */
240     public void execute ()
241           throws BuildException
242     {
2430       checkParameters();
244  
245        try
246        {
247           int exitValue;
248  
2490          exitValue = executeAsForked();
250  
2510          if (exitValue != 0)
252           {
2530             final String msg = "ReportNormalizer returned with exit code '"
254                 + exitValue + "'";
2550             log(msg, Project.MSG_WARN);
2560             throw new BuildException(msg, getLocation());
257           }
258        }
2590       catch (BuildException e)
260        {
2610          if (mFailOnError)
262           {
2630             throw e;
264           }
2650          log(e.getMessage(), e, Project.MSG_ERR);
2660       }
2670    }
268  
269     /**
270      * Create a Java command line for executing the ReportNormalizer.
271      *
272      * @return the Java command line
273      */
274     private CommandlineJava createCommandline ()
275     {
276         final CommandlineJava cmd;
277         try
278         {
2790            cmd = (CommandlineJava) mCommandline.clone();
280         }
2810        catch (CloneNotSupportedException unexpected)
282         {
2830            throw new RuntimeException(
284                     "Ups, CommandLineJava doesn't support the method clone()",
285                     unexpected);
2860        }
287  
2880       cmd.setClassname(ReportNormalizer.class.getName());
289  
2900       cmd.createArgument().setValue("-out");
2910       cmd.createArgument().setFile(mOut);
292  
2930       cmd.createArgument().setValue("-projectName");
2940       cmd.createArgument().setValue(mName);
295  
2960       cmd.createArgument().setValue("-format");
2970(7)      cmd.createArgument().setValue(mOutputFormat.toString());
298  
2990       cmd.createArgument().setValue("-loglevel");
3000       if (mDebug)
301        {
3020          cmd.createArgument().setValue("ALL");
303        }
304        else
305        {
3060          cmd.createArgument().setValue("INFO");
307        }
308  
3090       if (mFilter != null)
310        {
3110          cmd.createArgument().setValue("-filter");
3120          cmd.createArgument().setFile(mFilter);
313        }
314  
3150       for (final Iterator iterator = mSourceDirectories.iterator();
3160             iterator.hasNext();)
317        {
3180          final SourceDirectory sourceDir = (SourceDirectory) iterator.next();
3190          cmd.createArgument().setValue("-srcDir");
3200          cmd.createArgument().setPath(
321                    new Path(getProject(), sourceDir.getDir()));
3220       }
323  
3240       for (final Iterator iterator = mReportFiles.iterator();
3250             iterator.hasNext();)
326        {
3270          final Report reportFile = (Report) iterator.next();
3280          if (reportFile.testIfCondition())
329           {
3300             cmd.createArgument().setValue("-" + reportFile.getFormat());
3310             cmd.createArgument().setFile(reportFile.getFile());
332           }
3330       }
334  
3350       return cmd;
336     }
337  
338     private int executeAsForked ()
339           throws BuildException
340     {
3410       final Execute execute
342           = new Execute(new LogStreamHandler(
343                    this, Project.MSG_INFO, Project.MSG_WARN));
344  
3450       final CommandlineJava cmd = createCommandline();
3460       execute.setCommandline(cmd.getCommandline());
347  
3480       if (mWorkingDir != null)
349        {
3500          log("Setting working directory to : "
351                 + mWorkingDir, Project.MSG_VERBOSE);
3520          execute.setWorkingDirectory(mWorkingDir);
3530          execute.setAntRun(getProject());
354        }
355  
3560       final Path classpath = mCommandline.getClasspath();
3570       if (classpath != null)
358        {
3590          log("Using CLASSPATH " + classpath, Project.MSG_VERBOSE);
360        }
361  
3620       log("Executing: [fork] " + cmd.toString(), Project.MSG_VERBOSE);
363  
364        try
365        {
3660          return execute.execute();
367        }
3680       catch (IOException e)
369        {
3700          throw new BuildException("Process fork failed.", e, getLocation());
371        }
372     }
373  
374     private void checkParameters ()
375     {
3760(8)      if (mSourceDirectories.size() == 0)
377        {
3780          throw new BuildException(
379                 "at least one srcdir element must be specified!", getLocation());
380        }
3810    }
382  
383     /** This class represents input report with a format and filename. */
3840    public final class Report
385     {
386        private ReportFormat mReportFormat;
387        private File mReportFilename;
3880       private String mIfCondition = "";
389  
390        /**
391         * Returns the report filename.
392         *
393         * @return the report filename
394         */
395        public File getFile ()
396        {
3970          return mReportFilename;
398        }
399  
400        /**
401         * Sets the report filename.
402         *
403         * @param reportFilename the report filename
404         */
405        public void setFile (File reportFilename)
406        {
4070          mReportFilename = reportFilename;
4080       }
409  
410        /**
411         * Returns the report format.
412         *
413         * @return the report format
414         */
415        public ReportFormat getFormat ()
416        {
4170          return mReportFormat;
418        }
419  
420        /**
421         * Sets the report format.
422         *
423         * @param reportFormat the report format
424         */
425        public void setFormat (String reportFormat)
426        {
4270          mReportFormat = ReportFormat.fromString(reportFormat);
4280       }
429  
430        /**
431         * Only use report file if the property is set to <code>true</code>.
432         *
433         * @param property the property name to check
434         */
435        public void setif (String property)
436        {
4370          mIfCondition = (property == null) ? "" : property;
4380       }
439  
440        /**
441         * Tests whether or not the "if" condition is satisfied.
442         *
443         * @return whether or not the "if" condition is satisfied. If no
444         *         condition (or an empty condition) has been set,
445         *         <code>true</code> is returned
446         */
447        private boolean testIfCondition ()
448        {
449           final boolean result;
4500          if ("".equals(mIfCondition))
451           {
4520              result = true;
453           }
454           else
455           {
4560              final String test = getProject().replaceProperties(mIfCondition);
4570              result = getProject().getProperty(test) != null;
458           }
4590          return result;
460        }
461     }
462  
4630    public static final class SourceDirectory
464     {
465        private String mSourceDir;
466  
467        /**
468         * Returns the sourceDir.
469         *
470         * @return the source directory
471         */
472 (9)      public final String getDir ()
473        {
4740          return mSourceDir;
475        }
476  
477        /**
478         * Sets the source directory.
479         *
480         * @param sourceDir the source directory
481         */
482 (10)      public final void setDir (String sourceDir)
483        {
4840          mSourceDir = sourceDir;
4850       }
486     }
487  }

Findings in this File

c (11) Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag 'BuildException'.
c (1) 95 : 17 Unnecessary final modifier in final class
c (2) 105 : 17 Unnecessary final modifier in final class
c (3) 115 : 17 Unnecessary final modifier in final class
c (4) 159 : 0 Copied and pasted code. 201 equal tokens (97 lines) found in 2 locations. See also: org.jcoderz.phoenix.report.ReportNormalizerAntTask:148
d (5) 219 : 23 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
d (6) 231 : 29 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
c (7) 297 : 0 Copied and pasted code. 533 equal tokens (169 lines) found in 2 locations. See also: org.jcoderz.phoenix.report.ReportNormalizerAntTask:284
d (8) 376 : 11 Substitute calls to size() == 0 (or size() != 0) with calls to isEmpty()
c (9) 472 : 20 Unnecessary final modifier in final class
c (10) 482 : 20 Unnecessary final modifier in final class