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/05/03 14:40:22 UTC

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

Author: bentmann
Date: Sat May  3 05:40:22 2008
New Revision: 653065

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

o Reverted default encoding to platform encoding (http://www.nabble.com/-POLL--Default-Value-for-File-Encoding-td16958386.html)

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=653065&r1=653064&r2=653065&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 May  3 05:40:22 2008
@@ -39,7 +39,8 @@
 import net.sourceforge.pmd.cpd.XMLRenderer;
 
 import org.apache.maven.reporting.MavenReportException;
-import org.codehaus.plexus.util.ReaderFactory;
+import org.codehaus.plexus.util.WriterFactory;
+import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Creates a report for PMD's CPD tool.  See
@@ -71,8 +72,7 @@
     private boolean skip;
 
     /**
-     * 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.
+     * The file encoding to use when reading the Java sources.
      * 
      * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
      * @since 2.3
@@ -112,10 +112,13 @@
                 Map files = null;
                 try
                 {
-                    cpd.setEncoding( getSourceEncoding() );
+                    if ( StringUtils.isNotEmpty( sourceEncoding ) )
+                    {
+                        cpd.setEncoding( sourceEncoding );
 
-                    // 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
+                        WriterFactory.newWriter( new ByteArrayOutputStream(), sourceEncoding );
+                    }
 
                     files = getFilesToProcess( );
                     for ( Iterator it = files.keySet().iterator(); it.hasNext(); ) 
@@ -125,7 +128,7 @@
                 }
                 catch ( UnsupportedEncodingException e )
                 {
-                    throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e );
+                    throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e );
                 }
                 catch ( IOException e )
                 {
@@ -150,16 +153,6 @@
         }
     }
 
-    /**
-     * 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
     {
         Renderer r = createRenderer();

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=653065&r1=653064&r2=653065&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 May  3 05:40:22 2008
@@ -22,11 +22,9 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
@@ -58,6 +56,7 @@
 import org.codehaus.plexus.resource.loader.FileResourceLoader;
 import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
 import org.codehaus.plexus.util.ReaderFactory;
+import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Creates a PMD report.
@@ -108,8 +107,7 @@
     private String[] rulesets = new String[]{"rulesets/basic.xml", "rulesets/unusedcode.xml", "rulesets/imports.xml", };
 
     /**
-     * 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.
+     * The file encoding to use when reading the Java sources.
      * 
      * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
      */
@@ -227,12 +225,20 @@
                         {
                             // 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 = new InputStreamReader( new FileInputStream( file ), getSourceEncoding() );
+                            Reader reader;
+                            if ( StringUtils.isNotEmpty( sourceEncoding ) )
+                            {
+                                reader = ReaderFactory.newReader( file, sourceEncoding );
+                            }
+                            else
+                            {
+                                reader = ReaderFactory.newPlatformReader( file );
+                            }
                             pmd.processFile( reader, sets[idx], ruleContext );
                         }
                         catch ( UnsupportedEncodingException e1 )
                         {
-                            throw new MavenReportException( "Encoding '" + getSourceEncoding() + "' is not supported.", e1 );
+                            throw new MavenReportException( "Encoding '" + sourceEncoding + "' is not supported.", e1 );
                         }
                         catch ( PMDException pe )
                         {
@@ -303,16 +309,6 @@
     }
 
     /**
-     * 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.
      *
      * @param name the name of the file whose location is to be resolved