You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mi...@apache.org on 2018/02/20 23:10:38 UTC

[maven-help-plugin] 01/01: [MPH-87] help:effective-pom/effective-settings uses platform encoding and garbles non-ASCII characters, emits invalid XML

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch MPH-87
in repository https://gitbox.apache.org/repos/asf/maven-help-plugin.git

commit 58dd63b9d7cf509fc05b6dd2d8d19bd3d63c328d
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Wed Feb 21 00:10:00 2018 +0100

    [MPH-87] help:effective-pom/effective-settings uses platform encoding and garbles non-ASCII characters, emits invalid XML
    
    The fix for this issues requires a series of changes:
    * When writing XML content to file, don't provide an encoding, let the
    * writer read the prolog by itself. This avoids mismatches when encoding
    * is not provided (UTF-8), but platform encoding is not UTF-8.
    * Rather than feeding the model encoding unconditionally to
    * PrettyPrintXMLWriter provide the encoding of the output stream written
    * to.
    * PrettyPrintFormat does not replay the input encoding of a file, but
    * always uses UTF-8 unless told otherwise. This meant that the prolog did
    * not match to the actual file encoding.
---
 .../apache/maven/plugins/help/AbstractEffectiveMojo.java |  9 +--------
 .../org/apache/maven/plugins/help/EffectivePomMojo.java  | 16 ++++++++++------
 .../apache/maven/plugins/help/EffectiveSettingsMojo.java | 16 +++++++++-------
 3 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java b/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
index eaf216c..597d594 100644
--- a/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
@@ -34,7 +34,6 @@ import java.util.Set;
 
 import org.apache.commons.lang3.time.DateFormatUtils;
 import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.WriterFactory;
 import org.codehaus.plexus.util.xml.XMLWriter;
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
@@ -72,7 +71,7 @@ public abstract class AbstractEffectiveMojo
      * @throws IOException if any
      * @see AbstractHelpMojo#writeFile(File, String) if encoding is null.
      */
-    protected static void writeXmlFile( File output, String content, String encoding )
+    protected static void writeXmlFile( File output, String content )
         throws IOException
     {
         if ( output == null )
@@ -80,12 +79,6 @@ public abstract class AbstractEffectiveMojo
             return;
         }
 
-        if ( StringUtils.isEmpty( encoding ) )
-        {
-            writeFile( output, content );
-            return;
-        }
-
         Writer out = null;
         try
         {
diff --git a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
index f030ea7..0faa837 100644
--- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
@@ -83,7 +83,7 @@ public class EffectivePomMojo
      * <br>
      * <b>Note</b>: Should respect the Maven format, i.e. <code>groupId:artifactId[:version]</code>. The
      * latest version of the artifact will be used when no version is specified.
-     * 
+     *
      * @since 3.0.0
      */
     @Parameter( property = "artifact" )
@@ -103,9 +103,11 @@ public class EffectivePomMojo
         }
 
         StringWriter w = new StringWriter();
+        String encoding = output != null ? project.getModel().getModelEncoding()
+                                : System.getProperty( "file.encoding" );
         XMLWriter writer =
             new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
-                                      project.getModel().getModelEncoding(), null );
+                                      encoding, null );
 
         writeHeader( writer );
 
@@ -121,7 +123,7 @@ public class EffectivePomMojo
             writer.endElement();
 
             effectivePom = w.toString();
-            effectivePom = prettyFormat( effectivePom );
+            effectivePom = prettyFormat( effectivePom, encoding );
         }
         else
         {
@@ -134,7 +136,7 @@ public class EffectivePomMojo
         {
             try
             {
-                writeXmlFile( output, effectivePom, project.getModel().getModelEncoding() );
+                writeXmlFile( output, effectivePom );
             }
             catch ( IOException e )
             {
@@ -161,7 +163,7 @@ public class EffectivePomMojo
      * Determines if all effective POMs of all the projects in the reactor should be written. When this goal is started
      * on the command-line, it is always the case. However, when it is bound to a phase in the lifecycle, it is only the
      * case when the current project being built is the head project in the reactor.
-     * 
+     *
      * @return <code>true</code> if all effective POMs should be written, <code>false</code> otherwise.
      */
     private boolean shouldWriteAllEffectivePOMsInReactor()
@@ -223,9 +225,10 @@ public class EffectivePomMojo
 
     /**
      * @param effectivePom not null
+     * @param encoding not null
      * @return pretty format of the xml  or the original <code>effectivePom</code> if an error occurred.
      */
-    private static String prettyFormat( String effectivePom )
+    private static String prettyFormat( String effectivePom, String encoding )
     {
         SAXBuilder builder = new SAXBuilder();
 
@@ -235,6 +238,7 @@ public class EffectivePomMojo
 
             StringWriter w = new StringWriter();
             Format format = Format.getPrettyFormat();
+            format.setEncoding( encoding );
             XMLOutputter out = new XMLOutputter( format );
             out.output( effectiveDocument, w );
 
diff --git a/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java b/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
index bbcac03..a960f82 100644
--- a/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
@@ -91,9 +91,11 @@ public class EffectiveSettingsMojo
         }
 
         StringWriter w = new StringWriter();
+        String encoding = output != null ? copySettings.getModelEncoding()
+                                : System.getProperty( "file.encoding" );
         XMLWriter writer =
             new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
-                                      copySettings.getModelEncoding(), null );
+                                      encoding, null );
 
         writeHeader( writer );
 
@@ -105,7 +107,7 @@ public class EffectiveSettingsMojo
         {
             try
             {
-                writeXmlFile( output, effectiveSettings, copySettings.getModelEncoding() );
+                writeXmlFile( output, effectiveSettings );
             }
             catch ( IOException e )
             {
@@ -172,7 +174,7 @@ public class EffectiveSettingsMojo
         {
             return null;
         }
-        
+
         // Not a deep copy in M2.2.1 !!!
         Settings clone = SettingsUtils.copySettings( settings );
 
@@ -189,11 +191,11 @@ public class EffectiveSettingsMojo
             clonedServer.setPrivateKey( server.getPrivateKey() );
             clonedServer.setSourceLevel( server.getSourceLevel() );
             clonedServer.setUsername( server.getUsername() );
-            
+
             clonedServers.add( clonedServer );
         }
         clone.setServers( clonedServers );
-        
+
         List<Proxy> clonedProxies = new ArrayList<Proxy>( settings.getProxies().size() );
         for ( Proxy proxy : settings.getProxies() )
         {
@@ -207,11 +209,11 @@ public class EffectiveSettingsMojo
             clonedProxy.setProtocol( proxy.getProtocol() );
             clonedProxy.setSourceLevel( proxy.getSourceLevel() );
             clonedProxy.setUsername( proxy.getUsername() );
-            
+
             clonedProxies.add( clonedProxy );
         }
         clone.setProxies( clonedProxies );
-        
+
         return clone;
     }
 

-- 
To stop receiving notification emails like this one, please contact
michaelo@apache.org.