maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From micha...@apache.org
Subject [maven-help-plugin] 01/01: [MPH-87] help:effective-pom/effective-settings uses platform encoding and garbles non-ASCII characters, emits invalid XML
Date Tue, 20 Feb 2018 23:22:09 GMT
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 658a0bb94f9952262d8654b57eb560303cd12306
Author: Michael Osipov <michaelo@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.
    * PrettyPrintFormat default to \r\n line ending which produces
      inconsistent endings on Unix.
---
 .../maven/plugins/help/AbstractEffectiveMojo.java       |  9 +--------
 .../org/apache/maven/plugins/help/EffectivePomMojo.java | 17 +++++++++++------
 .../maven/plugins/help/EffectiveSettingsMojo.java       | 16 +++++++++-------
 3 files changed, 21 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..c459cbc 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,8 @@ public class EffectivePomMojo
 
             StringWriter w = new StringWriter();
             Format format = Format.getPrettyFormat();
+            format.setEncoding( encoding );
+            format.setLineSeparator( System.lineSeparator() );
             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.

Mime
View raw message