Skip to content

Commit 43dbdca

Browse files
garydgregoryslawekjaranowski
authored andcommitted
Allow nulls for write elements in MXSerializer
https://issues.apache.org/jira/browse/MJAVADOC-793
1 parent 6bccd34 commit 43dbdca

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

‎src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@ public void flush() throws IOException{
854854
// --- utility methods
855855

856856
protectedvoidwriteAttributeValue(Stringvalue, Writerout) throwsIOException{
857+
if (value == null){
858+
return;
859+
}
857860
// .[apostrophe and <, & escaped],
858861
finalcharquot = attributeUseApostrophe ? '\'' : '"';
859862
finalStringquotEntity = attributeUseApostrophe ? "&apos;" : "&quot;";
@@ -908,6 +911,9 @@ protected void writeAttributeValue(String value, Writer out) throws IOException
908911
}
909912

910913
protectedvoidwriteElementContent(Stringtext, Writerout) throwsIOException{
914+
if (text == null){
915+
return;
916+
}
911917
// escape '<', '&', ']]>', <32 if necessary
912918
intpos = 0;
913919
for (inti = 0; i < text.length(); i++){

‎src/test/java/org/codehaus/plexus/util/xml/pull/MXSerializerTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packageorg.codehaus.plexus.util.xml.pull;
22

3+
importjava.io.IOException;
34
importjava.io.StringReader;
45
importjava.io.StringWriter;
56
importjava.util.Arrays;
@@ -55,4 +56,19 @@ private String expectedOutput(){
5556
out.append("</root>");
5657
returnout.toString();
5758
}
59+
60+
/**
61+
* Tests MJAVADOC-793.
62+
*/
63+
@Test
64+
publicvoidtestWriteNullValues() throwsIOException{
65+
// should be no-ops
66+
newMXSerializer().writeElementContent(null, null);
67+
newMXSerializer().writeAttributeValue(null, null);
68+
finalStringWriterstringWriter = newStringWriter();
69+
newMXSerializer().writeElementContent(null, stringWriter);
70+
assertEquals("", stringWriter.toString());
71+
newMXSerializer().writeAttributeValue(null, stringWriter);
72+
assertEquals("", stringWriter.toString());
73+
}
5874
}

0 commit comments

Comments
(0)