Skip to content

Commit

Permalink
Naive solution
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed May 18, 2024
1 parent 5cf5e7a commit 386daf0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void writeText(String text, boolean escapeXml) {
finishTag();

if (escapeXml) {
text = escapeXml(text);
text = escapeXmlText(text);
}

write(StringUtils.unifyLineSeparators(text, lineSeparator));
Expand Down Expand Up @@ -228,6 +228,8 @@ private static String escapeXml(String text) {

private static final Pattern lowers = Pattern.compile("([\000-\037])");

private static final Pattern lowersText = Pattern.compile("([\000-\010\013-\014\016-\037])");

private static String escapeXmlAttribute(String text) {
text = escapeXml(text);

Expand All @@ -247,6 +249,19 @@ private static String escapeXmlAttribute(String text) {
return b.toString();
}

private static String escapeXmlText(String text) {
text = escapeXml(text);

Matcher m = lowersText.matcher(text);
StringBuffer b = new StringBuffer();
while (m.find()) {
m = m.appendReplacement(b, "&#" + Integer.toString(m.group(1).charAt(0)) + ";");
}
m.appendTail(b);

return b.toString();
}

/** {@inheritDoc} */
@Override
public void addAttribute(String key, String value) {
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -943,19 +943,11 @@ protected void writeElementContent(String text, Writer out) throws IOException {
// 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 (i > pos) out.write(text.substring(pos, i));
out.write("&#");
out.write(Integer.toString(ch));
out.write(';');
pos = i + 1;
}
}
if (seenBracket) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ private String createExpectedXML(boolean escape) {
buf.append(LS);
buf.append(" </el6>");
buf.append(LS);
buf.append(" <el8>special-char-" + (char) 7 + "</el8>");
if (escape) {
buf.append(" <el8>special-char-&#7;</el8>");
} else {
buf.append(" <el8>special-char-" + (char) 7 + "</el8>");
}
buf.append(LS);
buf.append("</root>");

Expand Down

0 comments on commit 386daf0

Please sign in to comment.