Skip to content

Commit e03c11f

Browse files
committed
1 parent c9d72af commit e03c11f

File tree

2 files changed

+103
-85
lines changed

2 files changed

+103
-85
lines changed

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

+89-85
Original file line numberDiff line numberDiff line change
@@ -854,110 +854,114 @@ public void flush() throws IOException {
854854
// --- utility methods
855855

856856
protected void writeAttributeValue(String value, Writer out) throws IOException {
857-
// .[apostrophe and <, & escaped],
858-
final char quot = attributeUseApostrophe ? '\'' : '"';
859-
final String quotEntity = attributeUseApostrophe ? "&apos;" : "&quot;";
860-
861-
int pos = 0;
862-
for (int i = 0; i < value.length(); i++) {
863-
char ch = value.charAt(i);
864-
if (ch == '&') {
865-
if (i > pos) out.write(value.substring(pos, i));
866-
out.write("&amp;");
867-
pos = i + 1;
868-
}
869-
if (ch == '<') {
870-
if (i > pos) out.write(value.substring(pos, i));
871-
out.write("&lt;");
872-
pos = i + 1;
873-
} else if (ch == quot) {
874-
if (i > pos) out.write(value.substring(pos, i));
875-
out.write(quotEntity);
876-
pos = i + 1;
877-
} else if (ch < 32) {
878-
// in XML 1.0 only legal character are #x9 | #xA | #xD
879-
// and they must be escaped otherwise in attribute value they are normalized to spaces
880-
if (ch == 13 || ch == 10 || ch == 9) {
881-
if (i > pos) out.write(value.substring(pos, i));
882-
out.write("&#");
883-
out.write(Integer.toString(ch));
884-
out.write(';');
885-
pos = i + 1;
886-
} else {
887-
throw new IllegalStateException(
888-
"character " + Integer.toString(ch) + " is not allowed in output" + getLocation());
889-
// in XML 1.1 legal are [#x1-#xD7FF]
890-
// if(ch > 0) {
891-
// if(i > pos) out.write(text.substring(pos, i));
892-
// out.write("&#");
893-
// out.write(Integer.toString(ch));
894-
// out.write(';');
895-
// pos = i + 1;
896-
// } else {
897-
// throw new IllegalStateException(
898-
// "character zero is not allowed in XML 1.1 output"+getLocation());
899-
// }
900-
}
901-
}
902-
}
903-
if (pos > 0) {
904-
out.write(value.substring(pos));
905-
} else {
906-
out.write(value); // this is shortcut to the most common case
907-
}
908-
}
909-
910-
protected void writeElementContent(String text, Writer out) throws IOException {
911-
// escape '<', '&', ']]>', <32 if necessary
912-
int pos = 0;
913-
for (int i = 0; i < text.length(); i++) {
914-
// TODO: check if doing char[] text.getChars() would be faster than getCharAt(i) ...
915-
char ch = text.charAt(i);
916-
if (ch == ']') {
917-
if (seenBracket) {
918-
seenBracketBracket = true;
919-
} else {
920-
seenBracket = true;
921-
}
922-
} else {
857+
if (value != null) {
858+
// .[apostrophe and <, & escaped],
859+
final char quot = attributeUseApostrophe ? '\'' : '"';
860+
final String quotEntity = attributeUseApostrophe ? "&apos;" : "&quot;";
861+
862+
int pos = 0;
863+
for (int i = 0; i < value.length(); i++) {
864+
char ch = value.charAt(i);
923865
if (ch == '&') {
924-
if (i > pos) out.write(text.substring(pos, i));
866+
if (i > pos) out.write(value.substring(pos, i));
925867
out.write("&amp;");
926868
pos = i + 1;
927-
} else if (ch == '<') {
928-
if (i > pos) out.write(text.substring(pos, i));
869+
}
870+
if (ch == '<') {
871+
if (i > pos) out.write(value.substring(pos, i));
929872
out.write("&lt;");
930873
pos = i + 1;
931-
} else if (seenBracketBracket && ch == '>') {
932-
if (i > pos) out.write(text.substring(pos, i));
933-
out.write("&gt;");
874+
} else if (ch == quot) {
875+
if (i > pos) out.write(value.substring(pos, i));
876+
out.write(quotEntity);
934877
pos = i + 1;
935878
} else if (ch < 32) {
936879
// in XML 1.0 only legal character are #x9 | #xA | #xD
937-
if (ch == 9 || ch == 10 || ch == 13) {
938-
// pass through
939-
940-
// } else if(ch == 13) { //escape
880+
// and they must be escaped otherwise in attribute value they are normalized to spaces
881+
if (ch == 13 || ch == 10 || ch == 9) {
882+
if (i > pos) out.write(value.substring(pos, i));
883+
out.write("&#");
884+
out.write(Integer.toString(ch));
885+
out.write(';');
886+
pos = i + 1;
887+
} else {
888+
throw new IllegalStateException(
889+
"character " + Integer.toString(ch) + " is not allowed in output" + getLocation());
890+
// in XML 1.1 legal are [#x1-#xD7FF]
891+
// if(ch > 0) {
941892
// if(i > pos) out.write(text.substring(pos, i));
942893
// out.write("&#");
943894
// out.write(Integer.toString(ch));
944895
// out.write(';');
945896
// pos = i + 1;
897+
// } else {
898+
// throw new IllegalStateException(
899+
// "character zero is not allowed in XML 1.1 output"+getLocation());
900+
// }
901+
}
902+
}
903+
}
904+
if (pos > 0) {
905+
out.write(value.substring(pos));
906+
} else {
907+
out.write(value); // this is shortcut to the most common case
908+
}
909+
}
910+
}
911+
912+
protected void writeElementContent(String text, Writer out) throws IOException {
913+
if (text != null) {
914+
// escape '<', '&', ']]>', <32 if necessary
915+
int pos = 0;
916+
for (int i = 0; i < text.length(); i++) {
917+
// TODO: check if doing char[] text.getChars() would be faster than getCharAt(i) ...
918+
char ch = text.charAt(i);
919+
if (ch == ']') {
920+
if (seenBracket) {
921+
seenBracketBracket = true;
946922
} else {
947-
// skip special char
923+
seenBracket = true;
924+
}
925+
} else {
926+
if (ch == '&') {
927+
if (i > pos) out.write(text.substring(pos, i));
928+
out.write("&amp;");
929+
pos = i + 1;
930+
} else if (ch == '<') {
948931
if (i > pos) out.write(text.substring(pos, i));
932+
out.write("&lt;");
949933
pos = i + 1;
934+
} else if (seenBracketBracket && ch == '>') {
935+
if (i > pos) out.write(text.substring(pos, i));
936+
out.write("&gt;");
937+
pos = i + 1;
938+
} else if (ch < 32) {
939+
// in XML 1.0 only legal character are #x9 | #xA | #xD
940+
if (ch == 9 || ch == 10 || ch == 13) {
941+
// pass through
942+
943+
// } else if(ch == 13) { //escape
944+
// if(i > pos) out.write(text.substring(pos, i));
945+
// out.write("&#");
946+
// out.write(Integer.toString(ch));
947+
// out.write(';');
948+
// pos = i + 1;
949+
} else {
950+
// skip special char
951+
if (i > pos) out.write(text.substring(pos, i));
952+
pos = i + 1;
953+
}
954+
}
955+
if (seenBracket) {
956+
seenBracketBracket = seenBracket = false;
950957
}
951-
}
952-
if (seenBracket) {
953-
seenBracketBracket = seenBracket = false;
954958
}
955959
}
956-
}
957-
if (pos > 0) {
958-
out.write(text.substring(pos));
959-
} else {
960-
out.write(text); // this is shortcut to the most common case
960+
if (pos > 0) {
961+
out.write(text.substring(pos));
962+
} else {
963+
out.write(text); // this is shortcut to the most common case
964+
}
961965
}
962966
}
963967

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

+14
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,17 @@ private String expectedOutput() {
5556
out.append("</root>");
5657
return out.toString();
5758
}
59+
60+
/**
61+
* Tests MJAVADOC-793.
62+
*/
63+
@Test
64+
public void testMJAVADOC793() 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+
new MXSerializer().writeAttributeValue(null, stringWriter);
71+
}
5872
}

0 commit comments

Comments
 (0)