From b55ce82f298d5658d45a61611d4dad297ee2d524 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 20 May 2024 10:33:23 -0400 Subject: [PATCH] Allow nulls for write elements in MXSerializer https://issues.apache.org/jira/browse/MJAVADOC-793 (cherry picked from commit 43dbdca6780fd60371e63f7e94d938b482e2edc9) --- .../plexus/util/xml/pull/MXSerializer.java | 6 ++++++ .../plexus/util/xml/pull/MXSerializerTest.java | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java b/src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java index ffd0ede..54e48dc 100644 --- a/src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java +++ b/src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java @@ -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 ? "'" : """; @@ -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++) { diff --git a/src/test/java/org/codehaus/plexus/util/xml/pull/MXSerializerTest.java b/src/test/java/org/codehaus/plexus/util/xml/pull/MXSerializerTest.java index 8b66c23..0b1d3d7 100644 --- a/src/test/java/org/codehaus/plexus/util/xml/pull/MXSerializerTest.java +++ b/src/test/java/org/codehaus/plexus/util/xml/pull/MXSerializerTest.java @@ -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; @@ -55,4 +56,19 @@ private String expectedOutput() { out.append(""); 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()); + } }