Skip to content

Commit

Permalink
Fix for MJAVADOC-793
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed May 20, 2024
1 parent 4ee6e63 commit 5a5581a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 85 deletions.
174 changes: 89 additions & 85 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? "&apos;" : "&quot;";

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("&amp;");
pos = i + 1;
}
if (ch == '<') {
if (i > pos) out.write(value.substring(pos, i));
out.write("&lt;");
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 ? "&apos;" : "&quot;";

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("&amp;");
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("&lt;");
pos = i + 1;
} else if (seenBracketBracket && ch == '>') {
if (i > pos) out.write(text.substring(pos, i));
out.write("&gt;");
} 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("&amp;");
pos = i + 1;
} else if (ch == '<') {
if (i > pos) out.write(text.substring(pos, i));
out.write("&lt;");
pos = i + 1;
} else if (seenBracketBracket && ch == '>') {
if (i > pos) out.write(text.substring(pos, i));
out.write("&gt;");
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
}
}
}

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,17 @@ private String expectedOutput() {
out.append("</root>");
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);
}
}

0 comments on commit 5a5581a

Please sign in to comment.