[MSHARED-985] XmlWriterUtil platform independent and consistent (#91)
* make XmlWriterUtil platform independent and consistent with its documentation
diff --git a/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java b/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java
index d9dd49e..7d84d91 100644
--- a/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java
+++ b/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java
@@ -32,6 +32,9 @@
{
/** The vm line separator */
public static final String LS = System.getProperty( "line.separator" );
+
+ /** Platform independent line separator */
+ private static final String CRLF = "\r\n";
/** The default line indenter size i.e. 2. */
public static final int DEFAULT_INDENTATION_SIZE = 2;
@@ -43,7 +46,7 @@
* Convenience method to write one <code>CRLF</code>.
*
* @param writer not null writer
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeLineBreak( XMLWriter writer ) throws IOException
{
@@ -55,13 +58,13 @@
*
* @param writer not null
* @param repeat positive number
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeLineBreak( XMLWriter writer, int repeat ) throws IOException
{
for ( int i = 0; i < repeat; i++ )
{
- writer.writeMarkup( LS );
+ writer.writeMarkup( CRLF );
}
}
@@ -73,7 +76,7 @@
* @param indent positive number
* @see #DEFAULT_INDENTATION_SIZE
* @see #writeLineBreak(XMLWriter, int, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeLineBreak( XMLWriter writer, int repeat, int indent ) throws IOException
{
@@ -87,7 +90,7 @@
* @param repeat The number of repetitions of the indent
* @param indent positive number
* @param indentSize positive number
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeLineBreak( XMLWriter writer, int repeat, int indent, int indentSize ) throws IOException
{
@@ -112,7 +115,7 @@
* @param writer not null
* @see #DEFAULT_COLUMN_LINE
* @see #writeCommentLineBreak(XMLWriter, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeCommentLineBreak( XMLWriter writer ) throws IOException
{
@@ -124,7 +127,7 @@
*
* @param writer not null
* @param columnSize positive number
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeCommentLineBreak( XMLWriter writer, int columnSize ) throws IOException
{
@@ -133,18 +136,18 @@
columnSize = DEFAULT_COLUMN_LINE;
}
- writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS );
+ writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + CRLF );
}
/**
- * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of
+ * Convenience method to write XML comment line. The <code>comment</code> is split to have a size of
* <code>80</code>.
*
* @param writer not null
* @param comment The comment to write
* @see #DEFAULT_INDENTATION_SIZE
* @see #writeComment(XMLWriter, String, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeComment( XMLWriter writer, String comment ) throws IOException
{
@@ -160,7 +163,7 @@
* @param indent positive number
* @see #DEFAULT_INDENTATION_SIZE
* @see #writeComment(XMLWriter, String, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeComment( XMLWriter writer, String comment, int indent ) throws IOException
{
@@ -177,7 +180,7 @@
* @param indentSize positive number
* @see #DEFAULT_COLUMN_LINE
* @see #writeComment(XMLWriter, String, int, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize ) throws IOException
{
@@ -193,7 +196,7 @@
* @param indent positive number
* @param indentSize positive number
* @param columnSize positive number
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
throws IOException
@@ -220,7 +223,7 @@
String indentation = StringUtils.repeat( " ", indent * indentSize );
int magicNumber = indentation.length() + columnSize - "-->".length() - 1;
- String[] sentences = StringUtils.split( comment, LS );
+ String[] sentences = StringUtils.split( comment, CRLF );
StringBuffer line = new StringBuffer( indentation + "<!-- " );
for ( String sentence : sentences )
@@ -239,7 +242,7 @@
line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
}
- line.append( "-->" ).append( LS );
+ line.append( "-->" ).append( CRLF );
writer.writeMarkup( line.toString() );
}
line = new StringBuffer( indentation + "<!-- " );
@@ -262,7 +265,7 @@
line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
}
- line.append( "-->" ).append( LS );
+ line.append( "-->" ).append( CRLF );
writer.writeMarkup( line.toString() );
}
@@ -275,7 +278,7 @@
* @param comment The comment to write
* @see #DEFAULT_INDENTATION_SIZE
* @see #writeCommentText(XMLWriter, String, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeCommentText( XMLWriter writer, String comment ) throws IOException
{
@@ -292,7 +295,7 @@
* @param indent positive number
* @see #DEFAULT_INDENTATION_SIZE
* @see #writeCommentText(XMLWriter, String, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeCommentText( XMLWriter writer, String comment, int indent ) throws IOException
{
@@ -309,7 +312,7 @@
* @param indentSize positive number
* @see #DEFAULT_COLUMN_LINE
* @see #writeCommentText(XMLWriter, String, int, int, int)
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize )
throws IOException
@@ -327,7 +330,7 @@
* @param indent positive number
* @param indentSize positive number
* @param columnSize positive number
- * @throws IOException if writing fails.
+ * @throws IOException if writing fails
*/
public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
throws IOException
diff --git a/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java b/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java
index 9a050da..433e5fe 100644
--- a/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java
@@ -62,7 +62,7 @@
{
XmlWriterUtil.writeLineBreak( xmlWriter );
writer.close();
- assertEquals( 1, StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) );
+ assertEquals( 1, StringUtils.countMatches( output.toString(), "\r\n" ) );
}
/**
@@ -75,7 +75,7 @@
{
XmlWriterUtil.writeLineBreak( xmlWriter, 10 );
writer.close();
- assertEquals( 10, StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) );
+ assertEquals( 10, StringUtils.countMatches( output.toString(), "\r\n" ) );
}
/**
@@ -88,7 +88,7 @@
{
XmlWriterUtil.writeLineBreak( xmlWriter, 10, 2 );
writer.close();
- assertEquals( 10, StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) );
+ assertEquals( 10, StringUtils.countMatches( output.toString(), "\r\n" ) );
assertEquals( 1, StringUtils.countMatches( output.toString(), StringUtils
.repeat( " ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE ) ) );
}
@@ -103,7 +103,7 @@
{
XmlWriterUtil.writeLineBreak( xmlWriter, 10, 2, 4 );
writer.close();
- assertEquals( 10, StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) );
+ assertEquals( 10, StringUtils.countMatches( output.toString(), "\r\n" ) );
assertEquals( 1, StringUtils.countMatches( output.toString(), StringUtils.repeat( " ", 2 * 4 ) ) );
}
@@ -118,9 +118,9 @@
XmlWriterUtil.writeCommentLineBreak( xmlWriter );
writer.close();
StringBuilder sb = new StringBuilder();
- sb.append( "<!-- ====================================================================== -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- ====================================================================== -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() );
+ assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() );
}
/**
@@ -133,13 +133,13 @@
{
XmlWriterUtil.writeCommentLineBreak( xmlWriter, 20 );
writer.close();
- assertEquals( output.toString(), "<!-- ========== -->" + XmlWriterUtil.LS );
+ assertEquals( output.toString(), "<!-- ========== -->" + "\r\n" );
}
public void testWriteCommentLineBreak() throws IOException {
XmlWriterUtil.writeCommentLineBreak( xmlWriter, 10 );
writer.close();
- assertEquals( output.toString(), output.toString(), "<!-- -->" + XmlWriterUtil.LS );
+ assertEquals( output.toString(), output.toString(), "<!-- -->" + "\r\n" );
}
/**
@@ -153,9 +153,9 @@
XmlWriterUtil.writeComment( xmlWriter, "hello" );
writer.close();
StringBuffer sb = new StringBuffer();
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() );
+ assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() );
}
@@ -165,7 +165,7 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( "<!-- hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
assertTrue( output.toString().length() >= XmlWriterUtil.DEFAULT_COLUMN_LINE );
}
@@ -174,10 +174,10 @@
XmlWriterUtil.writeComment( xmlWriter, "hello\nworld" );
writer.close();
StringBuffer sb = new StringBuffer();
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
- sb.append( "<!-- world -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
+ sb.append( "<!-- world -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() ) );
+ assertEquals( output.toString().length(), 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() ) );
}
/**
@@ -194,9 +194,9 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( indent );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() + 2
+ assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() + 2
* XmlWriterUtil.DEFAULT_INDENTATION_SIZE );
}
@@ -207,11 +207,11 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( indent );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
sb.append( indent );
- sb.append( "<!-- world -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- world -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() ) + 2 * indent.length() );
+ assertEquals( output.toString().length(), 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() ) + 2 * indent.length() );
}
/**
@@ -228,9 +228,9 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( repeat );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() + 2 * 4 );
+ assertEquals( output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() + 2 * 4 );
}
@@ -240,11 +240,11 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( repeat );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
sb.append( repeat );
- sb.append( "<!-- world -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- world -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertTrue( output.toString().length() == 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() ) + 2 * repeat.length() );
+ assertTrue( output.toString().length() == 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() ) + 2 * repeat.length() );
}
/**
@@ -261,9 +261,9 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( indent );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertTrue( output.toString().length() == 50 - 1 + XmlWriterUtil.LS.length() + 2 * 4 );
+ assertTrue( output.toString().length() == 50 - 1 + "\r\n".length() + 2 * 4 );
}
public void testWriteCommentXMLWriterStringIntIntInt_2() throws IOException
@@ -273,7 +273,7 @@
writer.close();
StringBuffer sb = new StringBuffer();
sb.append( indent );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
assertTrue( output.toString().length() >= 10 + 2 * 4 );
}
@@ -289,13 +289,13 @@
XmlWriterUtil.writeCommentText( xmlWriter, "hello", 0 );
writer.close();
StringBuffer sb = new StringBuffer();
- sb.append( XmlWriterUtil.LS );
- sb.append( "<!-- ====================================================================== -->" ).append( XmlWriterUtil.LS );
- sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
- sb.append( "<!-- ====================================================================== -->" ).append( XmlWriterUtil.LS );
- sb.append( XmlWriterUtil.LS );
+ sb.append( "\r\n" );
+ sb.append( "<!-- ====================================================================== -->" ).append( "\r\n" );
+ sb.append( "<!-- hello -->" ).append( "\r\n" );
+ sb.append( "<!-- ====================================================================== -->" ).append( "\r\n" );
+ sb.append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), 3 * ( 80 - 1 + XmlWriterUtil.LS.length() ) + 2 * XmlWriterUtil.LS.length() );
+ assertEquals( output.toString().length(), 3 * ( 80 - 1 + "\r\n".length() ) + 2 * "\r\n".length() );
}
public void testWriteCommentTextXMLWriterStringInt_2() throws IOException {
@@ -305,20 +305,20 @@
+ "loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnong line", 2 );
writer.close();
StringBuffer sb = new StringBuffer();
- sb.append( XmlWriterUtil.LS );
+ sb.append( "\r\n" );
sb.append( indent ).append( "<!-- ====================================================================== -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- hello world with end of line -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- and -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnong -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- line -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- ====================================================================== -->" )
- .append( XmlWriterUtil.LS );
- sb.append( XmlWriterUtil.LS );
+ .append( "\r\n" );
+ sb.append( "\r\n" );
sb.append( indent );
assertEquals( output.toString(), sb.toString() );
}
@@ -337,17 +337,17 @@
XmlWriterUtil.writeCommentText( xmlWriter, "hello", 2, 4 );
writer.close();
StringBuilder sb = new StringBuilder();
- sb.append( XmlWriterUtil.LS );
+ sb.append( "\r\n" );
sb.append( indent ).append( "<!-- ====================================================================== -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- hello -->" )
- .append( XmlWriterUtil.LS );
+ .append( "\r\n" );
sb.append( indent ).append( "<!-- ====================================================================== -->" )
- .append( XmlWriterUtil.LS );
- sb.append( XmlWriterUtil.LS );
+ .append( "\r\n" );
+ sb.append( "\r\n" );
sb.append( indent );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), 3 * ( 80 - 1 + XmlWriterUtil.LS.length() ) + 4 * 2 * 4 + 2 * XmlWriterUtil.LS.length() );
+ assertEquals( output.toString().length(), 3 * ( 80 - 1 + "\r\n".length() ) + 4 * 2 * 4 + 2 * "\r\n".length() );
}
/**
@@ -363,14 +363,14 @@
XmlWriterUtil.writeCommentText( xmlWriter, "hello", 2, 4, 50 );
writer.close();
StringBuilder sb = new StringBuilder();
- sb.append( XmlWriterUtil.LS );
- sb.append( indent ).append( "<!-- ======================================== -->" ).append( XmlWriterUtil.LS );
- sb.append( indent ).append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
- sb.append( indent ).append( "<!-- ======================================== -->" ).append( XmlWriterUtil.LS );
- sb.append( XmlWriterUtil.LS );
+ sb.append( "\r\n" );
+ sb.append( indent ).append( "<!-- ======================================== -->" ).append( "\r\n" );
+ sb.append( indent ).append( "<!-- hello -->" ).append( "\r\n" );
+ sb.append( indent ).append( "<!-- ======================================== -->" ).append( "\r\n" );
+ sb.append( "\r\n" );
sb.append( indent );
assertEquals( output.toString(), sb.toString() );
- assertEquals( output.toString().length(), 3 * ( 50 - 1 + XmlWriterUtil.LS.length() ) + 4 * 2 * 4 + 2 * XmlWriterUtil.LS.length() );
+ assertEquals( output.toString().length(), 3 * ( 50 - 1 + "\r\n".length() ) + 4 * 2 * 4 + 2 * "\r\n".length() );
}
/**
@@ -384,7 +384,7 @@
XmlWriterUtil.writeComment( xmlWriter, null );
writer.close();
StringBuilder sb = new StringBuilder();
- sb.append( "<!-- null -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- null -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
}
@@ -399,7 +399,7 @@
XmlWriterUtil.writeComment( xmlWriter, "This is a short text" );
writer.close();
StringBuilder sb = new StringBuilder();
- sb.append( "<!-- This is a short text -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- This is a short text -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
}
@@ -416,10 +416,10 @@
+ "and documentation from a central piece of information." );
writer.close();
StringBuilder sb = new StringBuilder();
- sb.append( "<!-- Maven is a software project management and comprehension tool. Based -->" ).append( XmlWriterUtil.LS );
- sb.append( "<!-- on the concept of a project object model (POM), Maven can manage a -->" ).append( XmlWriterUtil.LS );
- sb.append( "<!-- project's build, reporting and documentation from a central piece of -->" ).append( XmlWriterUtil.LS );
- sb.append( "<!-- information. -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- Maven is a software project management and comprehension tool. Based -->" ).append( "\r\n" );
+ sb.append( "<!-- on the concept of a project object model (POM), Maven can manage a -->" ).append( "\r\n" );
+ sb.append( "<!-- project's build, reporting and documentation from a central piece of -->" ).append( "\r\n" );
+ sb.append( "<!-- information. -->" ).append( "\r\n" );
assertEquals( output.toString(), sb.toString() );
}
}