From commits-return-70771-archive-asf-public=cust-asf.ponee.io@maven.apache.org Wed Feb 21 00:22:12 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 43E14180654 for ; Wed, 21 Feb 2018 00:22:12 +0100 (CET) Received: (qmail 66475 invoked by uid 500); 20 Feb 2018 23:22:11 -0000 Mailing-List: contact commits-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list commits@maven.apache.org Received: (qmail 66466 invoked by uid 99); 20 Feb 2018 23:22:11 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Feb 2018 23:22:11 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id F376481BE6; Tue, 20 Feb 2018 23:22:08 +0000 (UTC) Date: Tue, 20 Feb 2018 23:22:09 +0000 To: "commits@maven.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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: michaelo@apache.org In-Reply-To: <151916892893.7355.5673731353722927632@gitbox.apache.org> References: <151916892893.7355.5673731353722927632@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: maven-help-plugin X-Git-Refname: refs/heads/MPH-87 X-Git-Reftype: branch X-Git-Rev: 658a0bb94f9952262d8654b57eb560303cd12306 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20180220232208.F376481BE6@gitbox.apache.org> 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 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 *
* Note: Should respect the Maven format, i.e. groupId:artifactId[:version]. 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 true if all effective POMs should be written, false 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 effectivePom 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 clonedProxies = new ArrayList( 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.