[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
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 @@
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 @@
XmlWriterUtil.writeComment( writer, comment );
XmlWriterUtil.writeComment( writer, " " );
XmlWriterUtil.writeCommentLineBreak( writer );
-
- XmlWriterUtil.writeLineBreak( writer );
}
/**
@@ -189,6 +185,38 @@
}
/**
+ * @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.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 @@
writeHeader( writer );
- String effectivePom;
if ( shouldWriteAllEffectivePOMsInReactor() )
{
// outer root element
@@ -121,17 +114,14 @@
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 @@
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 @@
writeEffectiveSettings( copySettings, writer );
- String effectiveSettings = w.toString();
+ String effectiveSettings = prettyFormat( w.toString(), encoding );
if ( output != null )
{