Author: mperham
Date: Sat Aug 12 11:02:29 2006
New Revision: 431062

URL: http://svn.apache.org/viewvc?rev=431062&view=rev
Log:
PR: MPMD-38
Refactor excludes to work with both PMD and CPD

Modified:
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
    
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
    maven/plugins/trunk/maven-pmd-plugin/src/site/apt/usage.apt
    
maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
    
maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java?rev=431062&r1=431061&r2=431062&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/AbstractPmdReport.java
 Sat Aug 12 11:02:29 2006
@@ -23,9 +23,13 @@
 import org.codehaus.doxia.site.renderer.SiteRenderer;
 import org.codehaus.plexus.util.PathTool;
 import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.FileUtils;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Collections;
 
 /**
  * Base class for the PMD reports.
@@ -93,6 +97,13 @@
     private File xrefLocation;
 
     /**
+     * A list of files to exclude from checking. Can contain ant-style 
wildcards and double wildcards.
+     *
+     * @parameter
+     */
+    private String[] excludes;
+
+    /**
      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
      */
     protected MavenProject getProject()
@@ -146,6 +157,69 @@
         }
         return location;
     }
+
+    /**
+     * Convenience method to get the list of files where the PMD tool will be 
executed
+     *
+     * @param includes contains the concatenated list of files to be included
+     * @return a List of the files where the PMD tool will be executed
+     * @throws java.io.IOException
+     */
+    protected List getFilesToProcess( String includes )
+        throws IOException
+    {
+        String excluding = getExclusionsString( excludes );
+        List files = Collections.EMPTY_LIST;
+
+        File dir = new File( project.getBuild().getSourceDirectory() );
+        if ( dir.exists() )
+        {
+
+            StringBuffer excludesStr = new StringBuffer();
+            if ( StringUtils.isNotEmpty( excluding ) )
+            {
+                excludesStr.append( excluding );
+            }
+            String[] defaultExcludes = FileUtils.getDefaultExcludes();
+            for ( int i = 0; i < defaultExcludes.length; i++ )
+            {
+                if ( excludesStr.length() > 0 )
+                {
+                    excludesStr.append( "," );
+                }
+                excludesStr.append( defaultExcludes[i] );
+            }
+            getLog().debug( "Excluded files: '" + excludesStr + "'" );
+            files = FileUtils.getFiles( dir, includes, excludesStr.toString() 
);
+        }
+        return files;
+    }
+
+    /**
+     * Convenience method that concatenates the files to be excluded into the 
appropriate format
+     *
+     * @param exclude the array of Strings that contains the files to be 
excluded
+     * @return a String that contains the concatenates file names
+     */
+    private String getExclusionsString( String[] exclude )
+    {
+        StringBuffer excludes = new StringBuffer();
+
+        if ( exclude != null )
+        {
+            for ( int index = 0; index < exclude.length; index++ )
+            {
+                if ( excludes.length() > 0 )
+                {
+                    excludes.append( ',' );
+                }
+                excludes.append( exclude[index] );
+            }
+        }
+
+        return excludes.toString();
+    }
+
 
     protected boolean isHtml()
     {

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java?rev=431062&r1=431061&r2=431062&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/CpdReport.java
 Sat Aug 12 11:02:29 2006
@@ -16,20 +16,24 @@
  * limitations under the License.
  */
 
-import net.sourceforge.pmd.cpd.CPD;
-import net.sourceforge.pmd.cpd.CSVRenderer;
-import net.sourceforge.pmd.cpd.JavaLanguage;
-import net.sourceforge.pmd.cpd.Renderer;
-import net.sourceforge.pmd.cpd.XMLRenderer;
-import org.apache.maven.reporting.MavenReportException;
-
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 import java.util.ResourceBundle;
 
+import net.sourceforge.pmd.cpd.CPD;
+import net.sourceforge.pmd.cpd.CSVRenderer;
+import net.sourceforge.pmd.cpd.JavaLanguage;
+import net.sourceforge.pmd.cpd.Renderer;
+import net.sourceforge.pmd.cpd.XMLRenderer;
+
+import org.apache.maven.reporting.MavenReportException;
+import org.codehaus.plexus.util.DirectoryScanner;
+
 /**
  * Report for PMD's CPD tool.  See <a 
href="http://pmd.sourceforge.net/cpd.html";>http://pmd.sourceforge.net/cpd.html</a>
  * for more detail.
@@ -80,21 +84,23 @@
         throws MavenReportException
     {
         if ( !skip && canGenerateReport() )
-        {
-            CPD cpd = new CPD( minimumTokens, new JavaLanguage() );
-            String src = getProject().getBuild().getSourceDirectory();
-
+        {         
+            CPD cpd = new CPD(minimumTokens, new JavaLanguage());
             try
             {
-                // TODO: use source roots instead
-                cpd.addRecursively( src );
+                List files = getFilesToProcess( "**/*.java" );
+                for ( int i = 0; i < files.size(); i++ )
+                {
+                    cpd.add( (File) files.get( i ) );
+                }
             }
-            catch ( IOException e )
+            catch (IOException e)
             {
-                throw new MavenReportException( e.getMessage(), e );
+                throw new MavenReportException(e.getMessage(), e);
             }
             cpd.go();
 
+            String src = getProject().getBuild().getSourceDirectory();
             CpdReportGenerator gen =
                 new CpdReportGenerator( getSink(), src, getBundle( locale ), 
constructXRefLocation() );
             gen.generate( cpd.getMatches() );

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdReport.java?rev=431062&r1=431061&r2=431062&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
 Sat Aug 12 11:02:29 2006
@@ -103,13 +103,6 @@
     private String sourceEncoding;
 
     /**
-     * A list of files to exclude from checking. Can contain wildcards and 
double wildcards.
-     *
-     * @parameter
-     */
-    private String[] excludes;
-
-    /**
      * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
      */
     public String getName( Locale locale )
@@ -151,16 +144,6 @@
             ruleContext.setReport( report );
             reportSink.beginDocument();
 
-            List files;
-            try
-            {
-                files = getFilesToProcess( "**/*.java", getExclusionsString( 
excludes ) );
-            }
-            catch ( IOException e )
-            {
-                throw new MavenReportException( "Can't parse " + 
sourceDirectory, e );
-            }
-
             Locator locator = new Locator( getLog() );
             RuleSetFactory ruleSetFactory = new RuleSetFactory();
             ruleSetFactory.setMinimumPriority( this.minimumPriority );
@@ -183,6 +166,16 @@
 
             boolean hasEncoding = sourceEncoding != null;
 
+            List files;
+            try
+            {
+                files = getFilesToProcess( "**/*.java" );
+            }
+            catch ( IOException e )
+            {
+                throw new MavenReportException( "Can't parse " + 
sourceDirectory, e );
+            }
+
             for ( Iterator i = files.iterator(); i.hasNext(); )
             {
                 File file = (File) i.next();
@@ -217,7 +210,6 @@
                         getLog().warn( "Failure executing PMD for: " + file, 
e3 );
                         reportSink.ruleViolationAdded(new 
ProcessingErrorRuleViolation(file, e3.getLocalizedMessage()) );
                     }
-                       
                 }
                 reportSink.endFile( file );
             }
@@ -298,71 +290,9 @@
         return "pmd";
     }
 
-    /**
-     * Convenience method to get the list of files where the PMD tool will be 
executed
-     *
-     * @param includes contains the concatenated list of files to be included
-     * @param excludes contains the concatenated list of files to be excluded
-     * @return a List of the files where the PMD tool will be executed
-     * @throws IOException
-     */
-    private List getFilesToProcess( String includes, String excludes )
-        throws IOException
-    {
-        List files = Collections.EMPTY_LIST;
-
-        File dir = new File( project.getBuild().getSourceDirectory() );
-        if ( dir.exists() )
-        {
-
-            StringBuffer excludesStr = new StringBuffer();
-            if ( StringUtils.isNotEmpty( excludes ) )
-            {
-                excludesStr.append( excludes );
-            }
-            String[] defaultExcludes = FileUtils.getDefaultExcludes();
-            for ( int i = 0; i < defaultExcludes.length; i++ )
-            {
-                if ( excludesStr.length() > 0 )
-                {
-                    excludesStr.append( "," );
-                }
-                excludesStr.append( defaultExcludes[i] );
-            }
-            getLog().debug( "Excluded files: '" + excludesStr + "'" );
-            files = FileUtils.getFiles( dir, includes, excludesStr.toString() 
);
-        }
-        return files;
-    }
-
     private static ResourceBundle getBundle( Locale locale )
     {
         return ResourceBundle.getBundle( "pmd-report", locale, 
PmdReport.class.getClassLoader() );
-    }
-
-    /**
-     * Convenience method that concatenates the files to be excluded into the 
appropriate format
-     *
-     * @param exclude the array of Strings that contains the files to be 
excluded
-     * @return a String that contains the concatenates file names
-     */
-    private String getExclusionsString( String[] exclude )
-    {
-        StringBuffer excludes = new StringBuffer();
-
-        if ( exclude != null )
-        {
-            for ( int index = 0; index < exclude.length; index++ )
-            {
-                if ( excludes.length() > 0 )
-                {
-                    excludes.append( ',' );
-                }
-                excludes.append( exclude[index] );
-            }
-        }
-
-        return excludes.toString();
     }
 
     /**

Modified: maven/plugins/trunk/maven-pmd-plugin/src/site/apt/usage.apt
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/site/apt/usage.apt?rev=431062&r1=431061&r2=431062&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pmd-plugin/src/site/apt/usage.apt (original)
+++ maven/plugins/trunk/maven-pmd-plugin/src/site/apt/usage.apt Sat Aug 12 
11:02:29 2006
@@ -49,7 +49,8 @@
   See the {{{http://maven.apache.org/plugins/maven-jxr-plugin/}JXR}} plugin 
for more details.
 
   If your source uses a non-default encoding, you can use the sourceEncoding 
parameter to tell Maven which
-  encoding to use when reading the java source.
+  encoding to use when reading the java source.  Note also the ability to 
exclude source which you want
+  to ignore.
 
   You can configure the minimum code size which trips the CPD.  The default of 
100 tokens corresponds
   to approxiamately 5-10 lines of code.
@@ -68,6 +69,10 @@
         <sourceEncoding>utf-8</sourceEncoding>
         <minimumTokens>100</minimumTokens>
         <targetJdk>1.5</targetJdk>
+        <excludes>
+          <exclude>**/*Bean.java</exclude>
+          <exclude>**/generated/*.java</exclude>
+        </excludes>
       </configuration>
     </plugin>
   </plugins>

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java?rev=431062&r1=431061&r2=431062&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/CpdReportTest.java
 Sat Aug 12 11:02:29 2006
@@ -94,15 +94,16 @@
         generatedFile = new File( getBasedir(), 
"target/test/unit/custom-configuration/target/site/cpd.html" );
         assertTrue( FileUtils.fileExists( generatedFile.getAbsolutePath() ) );
 
-        //check the contents of cpd.html
+        // Contents that should NOT be in the report
         String str = readFile( new File( getBasedir(), 
"target/test/unit/custom-configuration/target/site/cpd.html" ) );
-        assertTrue( str.toLowerCase().indexOf( "Sample.java".toLowerCase() ) 
!= -1 );
+        assertTrue( str.toLowerCase().indexOf( "/Sample.java".toLowerCase() ) 
== -1 );
 
         str = readFile( new File( getBasedir(), 
"target/test/unit/custom-configuration/target/site/cpd.html" ) );
-        assertTrue( str.toLowerCase().indexOf( 
"AnotherSample.java".toLowerCase() ) != -1 );
+        assertTrue( str.toLowerCase().indexOf( "public void duplicateMethod( 
int i )".toLowerCase() ) == -1 );
 
+        // Contents that should be in the report
         str = readFile( new File( getBasedir(), 
"target/test/unit/custom-configuration/target/site/cpd.html" ) );
-        assertTrue( str.toLowerCase().indexOf( "public void duplicateMethod( 
int i )".toLowerCase() ) != -1 );
+        assertTrue( str.toLowerCase().indexOf( 
"AnotherSample.java".toLowerCase() ) != -1 );
 
         str = readFile( new File( getBasedir(), 
"target/test/unit/custom-configuration/target/site/cpd.html" ) );
         assertTrue( str.toLowerCase().indexOf( "public static void main( 
String[] args )".toLowerCase() ) != -1 );

Modified: 
maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml?rev=431062&r1=431061&r2=431062&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml
 (original)
+++ 
maven/plugins/trunk/maven-pmd-plugin/src/test/resources/unit/custom-configuration/cpd-custom-configuration-plugin-config.xml
 Sat Aug 12 11:02:29 2006
@@ -21,6 +21,9 @@
           <linkXRef>false</linkXRef>
           
<xrefLocation>${basedir}/target/test/unit/custom-configuration/target/site/xref</xrefLocation>
           <minimumTokens>25</minimumTokens>
+          <excludes>
+            <exclude>**/Sample.java</exclude>
+          </excludes>
         </configuration>
       </plugin>
     </plugins>


Reply via email to