You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2008/04/19 00:50:53 UTC

svn commit: r649719 - in /maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd: CpdReport.java PmdReport.java

Author: bentmann
Date: Fri Apr 18 15:50:49 2008
New Revision: 649719

URL: http://svn.apache.org/viewvc?rev=649719&view=rev
Log:
[MPMD-76] use ${project.build.sourceEncoding} as default value for "sourceEncoding" parameter

Modified:
    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

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=649719&r1=649718&r2=649719&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 Fri Apr 18 15:50:49 2008
@@ -39,6 +39,7 @@
 import net.sourceforge.pmd.cpd.XMLRenderer;
 
 import org.apache.maven.reporting.MavenReportException;
+import org.codehaus.plexus.util.ReaderFactory;
 
 /**
  * Creates a report for PMD's CPD tool.  See
@@ -70,9 +71,10 @@
     private boolean skip;
 
     /**
-     * The file encoding to use when reading the java source.
-     *
-     * @parameter
+     * The file encoding to use when reading the java source. <strong>Note:</strong> Prior to version 2.4, this
+     * parameter defaulted to the JVM's default encoding which led to platform-dependent builds.
+     * 
+     * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
      * @since 2.3
      */
     private String sourceEncoding;
@@ -110,13 +112,11 @@
                 Map files = null;
                 try
                 {
-                    if ( sourceEncoding != null )
-                    {
-                        cpd.setEncoding( sourceEncoding );
+                    cpd.setEncoding( getSourceEncoding() );
+
+                    // test encoding as CPD will convert exception into a RuntimeException
+                    new OutputStreamWriter( new ByteArrayOutputStream() , getSourceEncoding() );
 
-                        // test encoding as CPD will convert exception into a RuntimeException
-                        new OutputStreamWriter( new ByteArrayOutputStream() , sourceEncoding );
-                    }
                     files = getFilesToProcess( );
                     for ( Iterator it = files.keySet().iterator(); it.hasNext(); ) 
                     {
@@ -125,7 +125,7 @@
                 }
                 catch ( UnsupportedEncodingException e )
                 {
-                    throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e );
+                    throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e );
                 }
                 catch ( IOException e )
                 {
@@ -148,6 +148,16 @@
             }
 
         }
+    }
+
+    /**
+     * Gets the effective source file encoding.
+     * 
+     * @return The effective source file encoding, never <code>null</code>.
+     */
+    private String getSourceEncoding()
+    {
+        return ( sourceEncoding == null ) ? ReaderFactory.ISO_8859_1 : sourceEncoding;
     }
 
     void writeNonHtml( CPD cpd ) throws MavenReportException

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=649719&r1=649718&r2=649719&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 Fri Apr 18 15:50:49 2008
@@ -57,6 +57,7 @@
 import org.codehaus.plexus.resource.loader.FileResourceCreationException;
 import org.codehaus.plexus.resource.loader.FileResourceLoader;
 import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
+import org.codehaus.plexus.util.ReaderFactory;
 
 /**
  * Creates a PMD report.
@@ -107,9 +108,10 @@
     private String[] rulesets = new String[]{"rulesets/basic.xml", "rulesets/unusedcode.xml", "rulesets/imports.xml", };
 
     /**
-     * The file encoding to use when reading the java source.
-     *
-     * @parameter
+     * The file encoding to use when reading the java source. <strong>Note:</strong> Prior to version 2.4, this
+     * parameter defaulted to the JVM's default encoding which led to platform-dependent builds.
+     * 
+     * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
      */
     private String sourceEncoding;
 
@@ -199,8 +201,6 @@
                     throw new MavenReportException( e.getMessage(), e );
                 }
     
-                boolean hasEncoding = sourceEncoding != null;
-    
                 Map files;
                 try
                 {
@@ -227,14 +227,12 @@
                         {
                             // PMD closes this Reader even though it did not open it so we have
                             // to open a new one with every call to processFile().
-                            Reader reader = hasEncoding
-                                ? new InputStreamReader( new FileInputStream( file ), sourceEncoding )
-                                : new FileReader( file );
+                            Reader reader = new InputStreamReader( new FileInputStream( file ), getSourceEncoding() );
                             pmd.processFile( reader, sets[idx], ruleContext );
                         }
                         catch ( UnsupportedEncodingException e1 )
                         {
-                            throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e1 );
+                            throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e1 );
                         }
                         catch ( PMDException pe )
                         {
@@ -304,6 +302,16 @@
         }
     }
 
+    /**
+     * Gets the effective source file encoding.
+     * 
+     * @return The effective source file encoding, never <code>null</code>.
+     */
+    private String getSourceEncoding()
+    {
+        return ( sourceEncoding == null ) ? ReaderFactory.ISO_8859_1 : sourceEncoding;
+    }
+    
     /**
      * Convenience method to get the location of the specified file name.
      *