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..dcacfee 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,110 +854,114 @@ public void flush() throws IOException { // --- utility methods protected void writeAttributeValue(String value, Writer out) throws IOException { - // .[apostrophe and <, & escaped], - final char quot = attributeUseApostrophe ? '\'' : '"'; - final String quotEntity = attributeUseApostrophe ? "'" : """; - - int pos = 0; - for (int i = 0; i < value.length(); i++) { - char ch = value.charAt(i); - if (ch == '&') { - if (i > pos) out.write(value.substring(pos, i)); - out.write("&"); - pos = i + 1; - } - if (ch == '<') { - if (i > pos) out.write(value.substring(pos, i)); - out.write("<"); - pos = i + 1; - } else if (ch == quot) { - if (i > pos) out.write(value.substring(pos, i)); - out.write(quotEntity); - pos = i + 1; - } else if (ch < 32) { - // in XML 1.0 only legal character are #x9 | #xA | #xD - // and they must be escaped otherwise in attribute value they are normalized to spaces - if (ch == 13 || ch == 10 || ch == 9) { - if (i > pos) out.write(value.substring(pos, i)); - out.write("&#"); - out.write(Integer.toString(ch)); - out.write(';'); - pos = i + 1; - } else { - throw new IllegalStateException( - "character " + Integer.toString(ch) + " is not allowed in output" + getLocation()); - // in XML 1.1 legal are [#x1-#xD7FF] - // if(ch > 0) { - // if(i > pos) out.write(text.substring(pos, i)); - // out.write("&#"); - // out.write(Integer.toString(ch)); - // out.write(';'); - // pos = i + 1; - // } else { - // throw new IllegalStateException( - // "character zero is not allowed in XML 1.1 output"+getLocation()); - // } - } - } - } - if (pos > 0) { - out.write(value.substring(pos)); - } else { - out.write(value); // this is shortcut to the most common case - } - } - - protected void writeElementContent(String text, Writer out) throws IOException { - // escape '<', '&', ']]>', <32 if necessary - int pos = 0; - for (int i = 0; i < text.length(); i++) { - // TODO: check if doing char[] text.getChars() would be faster than getCharAt(i) ... - char ch = text.charAt(i); - if (ch == ']') { - if (seenBracket) { - seenBracketBracket = true; - } else { - seenBracket = true; - } - } else { + if (value != null) { + // .[apostrophe and <, & escaped], + final char quot = attributeUseApostrophe ? '\'' : '"'; + final String quotEntity = attributeUseApostrophe ? "'" : """; + + int pos = 0; + for (int i = 0; i < value.length(); i++) { + char ch = value.charAt(i); if (ch == '&') { - if (i > pos) out.write(text.substring(pos, i)); + if (i > pos) out.write(value.substring(pos, i)); out.write("&"); pos = i + 1; - } else if (ch == '<') { - if (i > pos) out.write(text.substring(pos, i)); + } + if (ch == '<') { + if (i > pos) out.write(value.substring(pos, i)); out.write("<"); pos = i + 1; - } else if (seenBracketBracket && ch == '>') { - if (i > pos) out.write(text.substring(pos, i)); - out.write(">"); + } else if (ch == quot) { + if (i > pos) out.write(value.substring(pos, i)); + out.write(quotEntity); pos = i + 1; } else if (ch < 32) { // in XML 1.0 only legal character are #x9 | #xA | #xD - if (ch == 9 || ch == 10 || ch == 13) { - // pass through - - // } else if(ch == 13) { //escape + // and they must be escaped otherwise in attribute value they are normalized to spaces + if (ch == 13 || ch == 10 || ch == 9) { + if (i > pos) out.write(value.substring(pos, i)); + out.write("&#"); + out.write(Integer.toString(ch)); + out.write(';'); + pos = i + 1; + } else { + throw new IllegalStateException( + "character " + Integer.toString(ch) + " is not allowed in output" + getLocation()); + // in XML 1.1 legal are [#x1-#xD7FF] + // if(ch > 0) { // if(i > pos) out.write(text.substring(pos, i)); // out.write("&#"); // out.write(Integer.toString(ch)); // out.write(';'); // pos = i + 1; + // } else { + // throw new IllegalStateException( + // "character zero is not allowed in XML 1.1 output"+getLocation()); + // } + } + } + } + if (pos > 0) { + out.write(value.substring(pos)); + } else { + out.write(value); // this is shortcut to the most common case + } + } + } + + protected void writeElementContent(String text, Writer out) throws IOException { + if (text != null) { + // escape '<', '&', ']]>', <32 if necessary + int pos = 0; + for (int i = 0; i < text.length(); i++) { + // TODO: check if doing char[] text.getChars() would be faster than getCharAt(i) ... + char ch = text.charAt(i); + if (ch == ']') { + if (seenBracket) { + seenBracketBracket = true; } else { - // skip special char + seenBracket = true; + } + } else { + if (ch == '&') { + if (i > pos) out.write(text.substring(pos, i)); + out.write("&"); + pos = i + 1; + } else if (ch == '<') { if (i > pos) out.write(text.substring(pos, i)); + out.write("<"); pos = i + 1; + } else if (seenBracketBracket && ch == '>') { + if (i > pos) out.write(text.substring(pos, i)); + out.write(">"); + pos = i + 1; + } else if (ch < 32) { + // in XML 1.0 only legal character are #x9 | #xA | #xD + if (ch == 9 || ch == 10 || ch == 13) { + // pass through + + // } else if(ch == 13) { //escape + // if(i > pos) out.write(text.substring(pos, i)); + // out.write("&#"); + // out.write(Integer.toString(ch)); + // out.write(';'); + // pos = i + 1; + } else { + // skip special char + if (i > pos) out.write(text.substring(pos, i)); + pos = i + 1; + } + } + if (seenBracket) { + seenBracketBracket = seenBracket = false; } - } - if (seenBracket) { - seenBracketBracket = seenBracket = false; } } - } - if (pos > 0) { - out.write(text.substring(pos)); - } else { - out.write(text); // this is shortcut to the most common case + if (pos > 0) { + out.write(text.substring(pos)); + } else { + out.write(text); // this is shortcut to the most common case + } } } 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..77deba9 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,17 @@ private String expectedOutput() { out.append(""); return out.toString(); } + + /** + * Tests MJAVADOC-793. + */ + @Test + public void testMJAVADOC793() 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); + new MXSerializer().writeAttributeValue(null, stringWriter); + } }