Skip to content

Commit b55ce82

Browse files
garydgregoryslawekjaranowski
authored andcommitted
Allow nulls for write elements in MXSerializer
https://issues.apache.org/jira/browse/MJAVADOC-793 (cherry picked from commit 43dbdca)
1 parent af8cbad commit b55ce82

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

+6
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@ public void flush() throws IOException {
854854
// --- utility methods
855855

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

910913
protected void writeElementContent(String text, Writer out) throws IOException {
914+
if (text == null) {
915+
return;
916+
}
911917
// escape '<', '&', ']]>', <32 if necessary
912918
int pos = 0;
913919
for (int i = 0; i < text.length(); i++) {

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

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.codehaus.plexus.util.xml.pull;
22

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

0 commit comments

Comments
 (0)