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/03/18 20:40:21 UTC

[maven-help-plugin] branch master updated: [MPH-137] Use JDOM's PrettyFormatter throughout

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 7cba2ff  [MPH-137] Use JDOM's PrettyFormatter throughout
7cba2ff is described below

commit 7cba2ff9f6342cb51655f6efde3adf29b43b796e
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Mar 18 21:37:00 2018 +0100

    [MPH-137] Use JDOM's PrettyFormatter throughout
    
    While this seems unnecessary, it adds the following values:
    
    * consistent code between single module and multi module projects as well
      as settings
    * character escapes if the output encoding does not support the source
      encoding's input
    * drop superfluous comments line which are optimized away by JDOM anyway
---
 .../maven/plugins/help/AbstractEffectiveMojo.java  | 36 +++++++++++++++--
 .../maven/plugins/help/EffectivePomMojo.java       | 46 +---------------------
 .../maven/plugins/help/EffectiveSettingsMojo.java  |  2 +-
 3 files changed, 35 insertions(+), 49 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 597d594..7c98b9e 100644
--- a/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/AbstractEffectiveMojo.java
@@ -112,8 +112,6 @@ public abstract class AbstractEffectiveMojo
         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
         XmlWriterUtil.writeComment( writer, " " );
         XmlWriterUtil.writeCommentLineBreak( writer );
-
-        XmlWriterUtil.writeLineBreak( writer );
     }
 
     /**
@@ -129,8 +127,6 @@ public abstract class AbstractEffectiveMojo
         XmlWriterUtil.writeComment( writer, comment );
         XmlWriterUtil.writeComment( writer, " " );
         XmlWriterUtil.writeCommentLineBreak( writer );
-
-        XmlWriterUtil.writeLineBreak( writer );
     }
 
     /**
@@ -189,6 +185,38 @@ public abstract class AbstractEffectiveMojo
     }
 
     /**
+     * @param effectiveModel not null
+     * @param encoding not null
+     * @return pretty format of the xml or the original {@code effectiveModel} if an error occurred.
+     */
+    protected static String prettyFormat( String effectiveModel, String encoding )
+    {
+        SAXBuilder builder = new SAXBuilder();
+
+        try
+        {
+            Document effectiveDocument = builder.build( new StringReader( effectiveModel ) );
+
+            StringWriter w = new StringWriter();
+            Format format = Format.getPrettyFormat();
+            format.setEncoding( encoding );
+            format.setLineSeparator( System.lineSeparator() );
+            XMLOutputter out = new XMLOutputter( format );
+            out.output( effectiveDocument, w );
+
+            return w.toString();
+        }
+        catch ( JDOMException e )
+        {
+            return effectiveModel;
+        }
+        catch ( IOException e )
+        {
+            return effectiveModel;
+        }
+    }
+
+    /**
      * Properties which provides a sorted keySet().
      */
     protected static class SortedProperties
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 c459cbc..35df117 100644
--- a/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java
@@ -31,14 +31,8 @@ import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 import org.codehaus.plexus.util.xml.XMLWriter;
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
-import org.jdom.Document;
-import org.jdom.JDOMException;
-import org.jdom.input.SAXBuilder;
-import org.jdom.output.Format;
-import org.jdom.output.XMLOutputter;
 
 import java.io.IOException;
-import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.List;
 import java.util.Properties;
@@ -111,7 +105,6 @@ public class EffectivePomMojo
 
         writeHeader( writer );
 
-        String effectivePom;
         if ( shouldWriteAllEffectivePOMsInReactor() )
         {
             // outer root element
@@ -121,17 +114,14 @@ public class EffectivePomMojo
                 writeEffectivePom( subProject, writer );
             }
             writer.endElement();
-
-            effectivePom = w.toString();
-            effectivePom = prettyFormat( effectivePom, encoding );
         }
         else
         {
             writeEffectivePom( project, writer );
-
-            effectivePom = w.toString();
         }
 
+        String effectivePom = prettyFormat( w.toString(), encoding );
+
         if ( output != null )
         {
             try
@@ -222,36 +212,4 @@ public class EffectivePomMojo
         properties.putAll( pom.getProperties() );
         pom.setProperties( properties );
     }
-
-    /**
-     * @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, String encoding )
-    {
-        SAXBuilder builder = new SAXBuilder();
-
-        try
-        {
-            Document effectiveDocument = builder.build( new StringReader( effectivePom ) );
-
-            StringWriter w = new StringWriter();
-            Format format = Format.getPrettyFormat();
-            format.setEncoding( encoding );
-            format.setLineSeparator( System.lineSeparator() );
-            XMLOutputter out = new XMLOutputter( format );
-            out.output( effectiveDocument, w );
-
-            return w.toString();
-        }
-        catch ( JDOMException e )
-        {
-            return effectivePom;
-        }
-        catch ( IOException e )
-        {
-            return effectivePom;
-        }
-    }
 }
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 a960f82..2c3c669 100644
--- a/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
+++ b/src/main/java/org/apache/maven/plugins/help/EffectiveSettingsMojo.java
@@ -101,7 +101,7 @@ public class EffectiveSettingsMojo
 
         writeEffectiveSettings( copySettings, writer );
 
-        String effectiveSettings = w.toString();
+        String effectiveSettings = prettyFormat( w.toString(), encoding );
 
         if ( output != null )
         {

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