Author: mperham
Date: Sun Jan  8 13:39:55 2006
New Revision: 367100

URL: http://svn.apache.org/viewcvs?rev=367100&view=rev
Log:
PR: MPMD-2  Add support for configurable rulesets

Modified:
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java?rev=367100&r1=367099&r2=367100&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
 Sun Jan  8 13:39:55 2006
@@ -16,6 +16,17 @@
  * limitations under the License.
  */
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
 import net.sourceforge.pmd.PMD;
 import net.sourceforge.pmd.PMDException;
 import net.sourceforge.pmd.Report;
@@ -35,17 +46,6 @@
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.StringUtils;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.ResourceBundle;
-
 /**
  * Implement the PMD report.
  *
@@ -81,6 +81,11 @@
     private String targetJdk;
 
     /**
+     * @parameter
+     */
+    private String[] rulesets = new String[] { "controversial" };
+
+    /**
      * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
      */
     public String getName( Locale locale )
@@ -137,10 +142,6 @@
         report.addListener( reportSink );
         ruleContext.setReport( report );
 
-        RuleSetFactory ruleSetFactory = new RuleSetFactory();
-        InputStream rulesInput = pmd.getClass().getResourceAsStream( 
"/rulesets/controversial.xml" );
-        RuleSet ruleSet = ruleSetFactory.createRuleSet( rulesInput );
-
         reportSink.beginDocument();
 
         List files;
@@ -153,18 +154,20 @@
             throw new MavenReportException( "Can't parse " + sourceDirectory, 
e );
         }
 
+        RuleSetFactory ruleSetFactory = new RuleSetFactory();
+        RuleSet[] sets = new RuleSet[rulesets.length];
+        for ( int idx = 0; idx < rulesets.length; idx++ )
+        {
+            String set = rulesets[idx];
+            String location = "/rulesets/" + set + ".xml";
+            getLog().debug( "Preparing " + set + " ruleset found in 
classpath:" + location );
+            InputStream rulesInput = pmd.getClass().getResourceAsStream( 
location );
+            sets[idx] = ruleSetFactory.createRuleSet( rulesInput );
+        }
+
         for ( Iterator i = files.iterator(); i.hasNext(); )
         {
             File file = (File) i.next();
-            FileReader fileReader;
-            try
-            {
-                fileReader = new FileReader( file );
-            }
-            catch ( FileNotFoundException e )
-            {
-                throw new MavenReportException( "Error opening source file: " 
+ file, e );
-            }
 
             try
             {
@@ -172,7 +175,11 @@
 
                 reportSink.beginFile( file );
                 ruleContext.setSourceCodeFilename( file.getAbsolutePath() );
-                pmd.processFile( fileReader, ruleSet, ruleContext );
+                for ( int idx = 0; idx < rulesets.length; idx++ )
+                {
+                    // PMD closes this Reader even though it did not open it.
+                    pmd.processFile( new FileReader( file ), sets[idx], 
ruleContext );
+                }
                 reportSink.endFile( file );
             }
             catch ( PMDException e )
@@ -184,16 +191,9 @@
                 }
                 throw new MavenReportException( "Failure executing PMD for: " 
+ file, ex );
             }
-            finally
+            catch ( FileNotFoundException e )
             {
-                try
-                {
-                    fileReader.close();
-                }
-                catch ( IOException e )
-                {
-                    throw new MavenReportException( "Error closing source 
file: " + file, e );
-                }
+                throw new MavenReportException( "Error opening source file: " 
+ file, e );
             }
         }
         reportSink.endDocument();

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java?rev=367100&r1=367099&r2=367100&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReportListener.java
 Sun Jan  8 13:39:55 2006
@@ -16,15 +16,21 @@
  * limitations under the License.
  */
 
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ResourceBundle;
+
 import net.sourceforge.pmd.ReportListener;
 import net.sourceforge.pmd.RuleViolation;
 import net.sourceforge.pmd.stat.Metric;
+
 import org.codehaus.doxia.sink.Sink;
 import org.codehaus.plexus.util.StringUtils;
 
-import java.io.File;
-import java.util.ResourceBundle;
-
 /**
  * Handle events from PMD, converting them into Doxia events.
  *
@@ -44,6 +50,10 @@
 
     private ResourceBundle bundle;
 
+    private List violations = new ArrayList();
+
+    private List metrics = new ArrayList();
+
     public PmdReportListener( Sink sink, String sourceDirectory, 
ResourceBundle bundle )
     {
         this.sink = sink;
@@ -58,7 +68,7 @@
 
     public void ruleViolationAdded( RuleViolation ruleViolation )
     {
-        if ( ! fileInitialized )
+        if ( !fileInitialized )
         {
             sink.section2();
             sink.sectionTitle2();
@@ -77,20 +87,44 @@
 
             fileInitialized = true;
         }
-        sink.tableRow();
-        sink.tableCell();
-        sink.text( ruleViolation.getDescription() );
-        sink.tableCell_();
-        sink.tableCell();
-        // TODO: xref link the line number
-        sink.text( String.valueOf( ruleViolation.getLine() ) );
-        sink.tableCell_();
-        sink.tableRow_();
+        violations.add( ruleViolation );
+    }
+
+    // When dealing with multiple rulesets, the violations will get out of 
order
+    // wrt their source line number.  We re-sort them before writing them to 
the report.
+    private void processViolations()
+    {
+        Collections.sort( violations, new Comparator()
+        {
+            public int compare( Object o1, Object o2 )
+            {
+                return ( (RuleViolation) o1 ).getLine() - ( (RuleViolation) o2 
).getLine();
+            }
+        } );
+
+        for ( Iterator it = violations.iterator(); it.hasNext(); )
+        {
+            RuleViolation ruleViolation = (RuleViolation) it.next();
+
+            sink.tableRow();
+            sink.tableCell();
+            sink.text( ruleViolation.getDescription() );
+            sink.tableCell_();
+            sink.tableCell();
+            // TODO: xref link the line number
+            sink.text( String.valueOf( ruleViolation.getLine() ) );
+            sink.tableCell_();
+            sink.tableRow_();
+        }
+        violations.clear();
     }
 
     public void metricAdded( Metric metric )
     {
-        // TODO: metrics
+        if (metric.getCount() != 0) {
+            // Skip metrics which have no data
+            metrics.add(metric);
+        }
     }
 
     public void beginDocument()
@@ -118,6 +152,7 @@
         // TODO overall summary
 
         sink.section1_();
+        sink.section1();
         sink.sectionTitle1();
         sink.text( bundle.getString( "report.pmd.files" ) );
         sink.sectionTitle1_();
@@ -136,14 +171,68 @@
     {
         if ( fileInitialized )
         {
+            processViolations();
             sink.table_();
             sink.section2_();
         }
     }
 
+    private void processMetrics()
+    {
+        sink.section1();
+        sink.sectionTitle1();
+        sink.text( "Metrics" );
+        sink.sectionTitle1_();
+
+        sink.table();
+        sink.tableRow();
+        sink.tableHeaderCell();
+        sink.text( "Name" );
+        sink.tableHeaderCell_();
+        sink.tableHeaderCell();
+        sink.text( "Count" );
+        sink.tableHeaderCell_();
+        sink.tableHeaderCell();
+        sink.text( "High" );
+        sink.tableHeaderCell_();
+        sink.tableHeaderCell();
+        sink.text( "Low" );
+        sink.tableHeaderCell_();
+        sink.tableHeaderCell();
+        sink.text( "Average" );
+        sink.tableHeaderCell_();
+        sink.tableRow_();
+
+        for ( Iterator iter = metrics.iterator(); iter.hasNext(); )
+        {
+            Metric met = (Metric) iter.next();
+            sink.tableRow();
+            sink.tableCell();
+            sink.text( met.getMetricName() );
+            sink.tableCell_();
+            sink.tableCell();
+            sink.text( "" + met.getCount() );
+            sink.tableCell_();
+            sink.tableCell();
+            sink.text( "" + met.getHighValue() );
+            sink.tableCell_();
+            sink.tableCell();
+            sink.text( "" + met.getLowValue() );
+            sink.tableCell_();
+            sink.tableCell();
+            sink.text( "" + met.getAverage() );
+            sink.tableCell_();
+            sink.tableRow_();
+        }
+        sink.table_();
+        sink.section1_();
+    }
+
     public void endDocument()
     {
         sink.section1_();
+
+        processMetrics();
 
         sink.body_();
 


Reply via email to