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/21 23:57:12 UTC

svn commit: r658890 - /maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java

Author: bentmann
Date: Wed May 21 14:57:11 2008
New Revision: 658890

URL: http://svn.apache.org/viewvc?rev=658890&view=rev
Log:
[MCHECKSTYLE-95] Allow to configure file encoding for source files

Modified:
    maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java

Modified: maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java?rev=658890&r1=658889&r2=658890&view=diff
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java (original)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleReport.java Wed May 21 14:57:11 2008
@@ -21,6 +21,7 @@
 
 import com.puppycrawl.tools.checkstyle.Checker;
 import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
+import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
 import com.puppycrawl.tools.checkstyle.DefaultLogger;
 import com.puppycrawl.tools.checkstyle.ModuleFactory;
 import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
@@ -470,6 +471,16 @@
     private File xrefLocation;
 
     /**
+     * The file encoding to use when reading the source files. If the property <code>project.build.sourceEncoding</code>
+     * is not set, the platform default encoding is used. <strong>Note:</strong> This parameter always overrides the
+     * property <code>charset</code> from Checkstyle's <code>TreeWalker</code> module.
+     * 
+     * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
+     * @since 2.2
+     */
+    private String encoding;
+
+    /**
      * @component
      * @required
      * @readonly
@@ -584,6 +595,32 @@
 
                 config = ConfigurationLoader.loadConfiguration( configFile,
                                                                 new PropertiesExpander( overridingProperties ) );
+                String effectiveEncoding =
+                    StringUtils.isNotEmpty( encoding ) ? encoding : System.getProperty( "file.encoding", "UTF-8" );
+                if ( StringUtils.isEmpty( encoding ) )
+                {
+                    getLog().warn(
+                                   "File encoding has not been set, using platform encoding " + effectiveEncoding
+                                       + ", i.e. build is platform dependent!" );
+                }
+                Configuration[] modules = config.getChildren();
+                for ( int i = 0; i < modules.length; i++ )
+                {
+                    Configuration module = modules[i];
+                    if ( "TreeWalker".equals( module.getName() )
+                        || "com.puppycrawl.tools.checkstyle.TreeWalker".equals( module.getName() ) )
+                    {
+                        if ( module instanceof DefaultConfiguration )
+                        {
+                            ( (DefaultConfiguration) module ).addAttribute( "charset", effectiveEncoding );
+                        }
+                        else
+                        {
+                            getLog().warn( "Failed to configure file encoding on module " + module );
+                        }
+                    }
+                }
+
                 results = executeCheckstyle( config, moduleFactory );
 
                 ResourceBundle bundle = getBundle( locale );