Author: mperham
Date: Sun Jan  8 15:01:33 2006
New Revision: 367117

URL: http://svn.apache.org/viewcvs?rev=367117&view=rev
Log:
PR: MPMD-8
Contributer: Patrick O'Shea

Add support for different PMD report output formats.

Added:
    maven/plugins/trunk/maven-pmd-plugin/src/site/
    maven/plugins/trunk/maven-pmd-plugin/src/site/apt/
    maven/plugins/trunk/maven-pmd-plugin/src/site/apt/howto.apt
    maven/plugins/trunk/maven-pmd-plugin/src/site/site.xml   (with props)
Modified:
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.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=367117&r1=367116&r2=367117&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 15:01:33 2006
@@ -19,8 +19,10 @@
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Writer;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
@@ -36,6 +38,11 @@
 import net.sourceforge.pmd.TargetJDK1_3;
 import net.sourceforge.pmd.TargetJDK1_4;
 import net.sourceforge.pmd.TargetJDK1_5;
+import net.sourceforge.pmd.renderers.CSVRenderer;
+import net.sourceforge.pmd.renderers.HTMLRenderer;
+import net.sourceforge.pmd.renderers.Renderer;
+import net.sourceforge.pmd.renderers.TextRenderer;
+import net.sourceforge.pmd.renderers.XMLRenderer;
 
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.project.MavenProject;
@@ -81,6 +88,15 @@
     private String targetJdk;
 
     /**
+     * Set the output format type.  Defaults to "html".  Must be one of:
+     * "html", "csv", "xml", "txt" or the full class name of the PMD renderer 
to use.
+     * See the net.sourceforge.pmd.renderers package javadoc for available 
renderers.
+     * 
+     * @parameter
+     */
+    private String format = "html";
+
+    /**
      * @parameter
      */
     private String[] rulesets = new String[] { "controversial" };
@@ -125,6 +141,11 @@
         return siteRenderer;
     }
 
+    private boolean isHtml()
+    {
+        return "html".equals( format );
+    }
+
     /**
      * @see 
org.apache.maven.reporting.AbstractMavenReport#executeReport(java.util.Locale)
      */
@@ -141,7 +162,6 @@
         PmdReportListener reportSink = new PmdReportListener( sink, 
sourceDirectory, getBundle( locale ) );
         report.addListener( reportSink );
         ruleContext.setReport( report );
-
         reportSink.beginDocument();
 
         List files;
@@ -197,6 +217,23 @@
             }
         }
         reportSink.endDocument();
+        
+        if ( !isHtml() )
+        {
+            // Use the PMD renderers to render in any format aside from HTML.
+            Renderer r = createRenderer();
+            String buffer = r.render( report );
+            try
+            {
+                Writer writer = new FileWriter( new File( 
this.getReportOutputDirectory(), "pmd." + format ) );
+                writer.write( buffer, 0, buffer.length() );
+                writer.close();
+            }
+            catch ( IOException ioe )
+            {
+                throw new MavenReportException( ioe.getMessage(), ioe );
+            }
+        }
     }
 
     /**
@@ -274,5 +311,45 @@
     {
         ArtifactHandler artifactHandler = 
project.getArtifact().getArtifactHandler();
         return ( "java".equals( artifactHandler.getLanguage() ) );
+    }
+
+    /**
+     * Create and return the correct renderer for the output type.
+     * @return the renderer based on the configured output
+     * @throws MavenReportException if no renderer found for the output type
+     */
+    public final Renderer createRenderer()
+        throws MavenReportException
+    {
+        if ( format.equals( "xml" ) )
+        {
+            return new XMLRenderer();
+        }
+        else if ( format.equals( "txt" ) )
+        {
+            return new TextRenderer();
+        }
+        else if ( format.equals( "csv" ) )
+        {
+            return new CSVRenderer();
+        }
+        else if ( format.equals( "html" ) )
+        {
+            return new HTMLRenderer();
+        }
+        if ( !format.equals( "" ) )
+        {
+            try
+            {
+                return (Renderer) Class.forName( format ).newInstance();
+            }
+            catch ( Exception e )
+            {
+                throw new MavenReportException( "Can't find the custom format 
" + format + ": "
+                    + e.getClass().getName() );
+            }
+        }
+
+        throw new MavenReportException( "Can't create report with format of " 
+ format );
     }
 }

Added: maven/plugins/trunk/maven-pmd-plugin/src/site/apt/howto.apt
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/site/apt/howto.apt?rev=367117&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/site/apt/howto.apt (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/site/apt/howto.apt Sun Jan  8 
15:01:33 2006
@@ -0,0 +1,39 @@
+ ------
+ Maven 2 PMD Plugin: configuration examples
+ ------
+ ------
+ January 6, 2006
+
+Introduction
+
+  The {{{http://pmd.sourceforge.net/}PMD}} plugin allows you to automatically 
run the PMD code analysis tool
+  on your project's source code and generate a site report with its results.
+
+   * pmd:pmd (default): run PMD and generate the report
+
+Configuration
+
+  To configure the PMD plugin, you use the standard m2 report plugin 
conventions.  For example, the following
+  tells Maven to run the PMD report as part of the site report generation, use 
the 'basic' and 'controversial'
+  PMD rulesets and output the report in XML format.  The rulesets are assumed 
to reside in /rulesets/[name].xml 
+  in the classpath.  Note that HTML is always generated in addition to any 
other alternate format.  Legal formats
+  are "html", "csv", "xml" and "txt".
+
++--------
+    <reporting>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-pmd-plugin</artifactId>
+                <configuration>
+                    <rulesets>
+                        <ruleset>basic</ruleset>
+                        <ruleset>controversial</ruleset>
+                    </rulesets>
+                    <format>xml</format>
+                </configuration>
+            </plugin>
+        </plugins>
+    </reporting>
++---------
+

Added: maven/plugins/trunk/maven-pmd-plugin/src/site/site.xml
URL: 
http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-pmd-plugin/src/site/site.xml?rev=367117&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/site/site.xml (added)
+++ maven/plugins/trunk/maven-pmd-plugin/src/site/site.xml Sun Jan  8 15:01:33 
2006
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+
+<project name="Maven PMD Plugin">
+  <bannerLeft>
+    <name>Maven PMD Plugin</name>
+    <src>http://maven.apache.org/images/apache-maven-project.png</src>
+    <href>http://maven.apache.org/</href>
+  </bannerLeft>
+  <bannerRight>
+    <src>http://maven.apache.org/images/maven-small.gif</src>
+  </bannerRight>
+  <body>
+    <links>
+      <item name="Maven 2" href="http://maven.apache.org/maven2/"/>
+    </links>
+
+    <menu name="Overview">
+      <item name="How to Use" href="howto.html"/>
+    </menu>
+    ${reports}
+  </body>
+</project>

Propchange: maven/plugins/trunk/maven-pmd-plugin/src/site/site.xml
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to