Skip to content

Commit

Permalink
Allow nulls for write elements in MXSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory authored and slawekjaranowski committed May 21, 2024
1 parent af8cbad commit b55ce82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,9 @@ public void flush() throws IOException {
// --- utility methods

protected void writeAttributeValue(String value, Writer out) throws IOException {
if (value == null) {
return;
}
// .[apostrophe and <, & escaped],
final char quot = attributeUseApostrophe ? '\'' : '"';
final String quotEntity = attributeUseApostrophe ? "&apos;" : "&quot;";
Expand Down Expand Up @@ -908,6 +911,9 @@ protected void writeAttributeValue(String value, Writer out) throws IOException
}

protected void writeElementContent(String text, Writer out) throws IOException {
if (text == null) {
return;
}
// escape '<', '&', ']]>', <32 if necessary
int pos = 0;
for (int i = 0; i < text.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.codehaus.plexus.util.xml.pull;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
Expand Down Expand Up @@ -55,4 +56,19 @@ private String expectedOutput() {
out.append("</root>");
return out.toString();
}

/**
* Tests MJAVADOC-793.
*/
@Test
public void testWriteNullValues() throws IOException {
// should be no-ops
new MXSerializer().writeElementContent(null, null);
new MXSerializer().writeAttributeValue(null, null);
final StringWriter stringWriter = new StringWriter();
new MXSerializer().writeElementContent(null, stringWriter);
assertEquals("", stringWriter.toString());
new MXSerializer().writeAttributeValue(null, stringWriter);
assertEquals("", stringWriter.toString());
}
}

0 comments on commit b55ce82

Please sign in to comment.