Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-952] make PrettyPrintXmlWriter platform independent #62

Merged
merged 6 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import org.apache.maven.shared.utils.Os;

/**
* XMLWriter with nice indentation.
Expand Down Expand Up @@ -101,7 +100,7 @@ public PrettyPrintXMLWriter( Writer writer )
*/
public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String encoding, String doctype )
{
this( writer, lineIndent.toCharArray(), Os.LINE_SEP.toCharArray(), encoding, doctype );
this( writer, lineIndent.toCharArray(), "\n".toCharArray(), encoding, doctype );
}

/**
Expand All @@ -122,7 +121,7 @@ public PrettyPrintXMLWriter( Writer writer, String lineIndent, String encoding,
*/
public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
{
this( writer, DEFAULT_LINE_INDENT, Os.LINE_SEP.toCharArray(), encoding, doctype );
this( writer, DEFAULT_LINE_INDENT, "\n".toCharArray(), encoding, doctype );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ public void startElement( String uri, String localName, String qName, Attributes
attachToParent( child );
pushOnStack( child );

// Todo: Detecting tags that close immediately seem to be impossible in sax ?
// http://stackoverflow.com/questions/12968390/detecting-self-closing-tags-in-sax
values.add( new StringBuilder() );

int size = attributes.getLength();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.swing.text.html.HTML;
import java.io.StringWriter;

import org.apache.maven.shared.utils.Os;
import org.apache.maven.shared.utils.StringUtils;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -51,7 +50,7 @@ public void testDefaultPrettyPrintXMLWriter() throws IOException

writer.endElement(); // Tag.HTML

Assert.assertEquals( expectedResult( Os.LINE_SEP ), w.toString() );
Assert.assertEquals( expectedResult(), w.toString() );
}

@Test
Expand All @@ -67,7 +66,7 @@ public void testPrettyPrintXMLWriterWithGivenLineSeparator() throws IOException

writer.endElement(); // Tag.HTML

Assert.assertEquals( expectedResult( "\n" ), w.toString() );
Assert.assertEquals( expectedResult(), w.toString() );
}

@Test
Expand All @@ -83,7 +82,7 @@ public void testPrettyPrintXMLWriterWithGivenLineIndenter() throws IOException

writer.endElement(); // Tag.HTML

Assert.assertEquals( expectedResult( " ", Os.LINE_SEP ), w.toString() );
Assert.assertEquals( expectedResult( " " ), w.toString() );
}

@Test
Expand Down Expand Up @@ -158,13 +157,15 @@ private void writeXhtmlBody( XMLWriter writer ) throws IOException
writer.endElement(); // Tag.BODY
}

private static String expectedResult( String lineSeparator )
private static String expectedResult()
{
return expectedResult( " ", lineSeparator );
return expectedResult( " " );
}

private static String expectedResult( String lineIndenter, String lineSeparator )
private static String expectedResult( String lineIndenter )
{

String lineSeparator = "\n";
StringBuilder expected = new StringBuilder();

expected.append( "<html>" ).append( lineSeparator );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
* under the License.
*/

import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.xml.pull.XmlPullParserException;

import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
Expand All @@ -43,16 +42,13 @@
public class Xpp3DomBuilderTest
{

private static final String LS = System.getProperty( "line.separator" );

private static final String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

@Test
public void selfClosingTag()
throws Exception
{

// Todo: http://stackoverflow.com/questions/12968390/detecting-self-closing-tags-in-sax
String domString = selfClosingTagSource();

Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( domString ) );
Expand Down Expand Up @@ -97,11 +93,15 @@ public void trimming()
assertEquals( " preserve space ", dom.getChild( "element6" ).getValue() );
}

@Test(expected = XmlPullParserException.class)
public void malformedXml()
@Test
public void testMalformedXml()
{
Xpp3DomBuilder.build( new StringReader( "<newRoot>" + createDomString() ) );
fail( "We're supposed to fail" );
try {
Xpp3DomBuilder.build( new StringReader( "<newRoot>" + createDomString() ) );
fail( "We're supposed to fail" );
} catch (XmlPullParserException ex) {
Assert.assertNotNull( ex.getMessage() );
}
}

@Test
Expand Down Expand Up @@ -137,9 +137,9 @@ private static String getAttributeEncodedString()
{
StringBuilder domString = new StringBuilder();
domString.append( "<root>" );
domString.append( LS );
domString.append( "\n" );
domString.append( " <el att=\"&lt;foo&gt;\">bar</el>" );
domString.append( LS );
domString.append( "\n" );
domString.append( "</root>" );

return domString.toString();
Expand All @@ -161,13 +161,13 @@ private static String getExpectedString()
{
StringBuilder domString = new StringBuilder();
domString.append( "<root>" );
domString.append( LS );
domString.append( "\n" );
domString.append( " <a1>\"msg\"</a1>" );
domString.append( LS );
domString.append( "\n" );
domString.append( " <a2>&lt;b&gt;\"msg\"&lt;/b&gt;</a2>" );
domString.append( LS );
domString.append( "\n" );
domString.append( " <a3>&lt;b&gt;\"msg\"&lt;/b&gt;</a3>" );
domString.append( LS );
domString.append( "\n" );
domString.append( "</root>" );
return domString.toString();
}
Expand Down Expand Up @@ -195,12 +195,12 @@ private static String selfClosingTagSource()
buf.append( " <el4></el4>\n" );
buf.append( " <el5></el5>\n" );
buf.append( "</root>" );
return StringUtils.unifyLineSeparators( buf.toString() );
return buf.toString();
}

private static String expectedSelfClosingTag()
{
return StringUtils.unifyLineSeparators( xmlDeclaration + selfClosingTagSource() );
return XML_DECLARATION + selfClosingTagSource();
}

}