Skip to content

Commit 386daf0

Browse files
committed
Naive solution
1 parent 5cf5e7a commit 386daf0

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private void writeText(String text, boolean escapeXml) {
186186
finishTag();
187187

188188
if (escapeXml) {
189-
text = escapeXml(text);
189+
text = escapeXmlText(text);
190190
}
191191

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

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

231+
private static final Pattern lowersText = Pattern.compile("([\000-\010\013-\014\016-\037])");
232+
231233
private static String escapeXmlAttribute(String text) {
232234
text = escapeXml(text);
233235

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

252+
private static String escapeXmlText(String text) {
253+
text = escapeXml(text);
254+
255+
Matcher m = lowersText.matcher(text);
256+
StringBuffer b = new StringBuffer();
257+
while (m.find()) {
258+
m = m.appendReplacement(b, "&#" + Integer.toString(m.group(1).charAt(0)) + ";");
259+
}
260+
m.appendTail(b);
261+
262+
return b.toString();
263+
}
264+
250265
/** {@inheritDoc} */
251266
@Override
252267
public void addAttribute(String key, String value) {

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

+5-13
Original file line numberDiff line numberDiff line change
@@ -943,19 +943,11 @@ protected void writeElementContent(String text, Writer out) throws IOException {
943943
// out.write(';');
944944
// pos = i + 1;
945945
} else {
946-
throw new IllegalStateException(
947-
"character " + Integer.toString(ch) + " is not allowed in output" + getLocation());
948-
// in XML 1.1 legal are [#x1-#xD7FF]
949-
// if(ch > 0) {
950-
// if(i > pos) out.write(text.substring(pos, i));
951-
// out.write("&#");
952-
// out.write(Integer.toString(ch));
953-
// out.write(';');
954-
// pos = i + 1;
955-
// } else {
956-
// throw new IllegalStateException(
957-
// "character zero is not allowed in XML 1.1 output"+getLocation());
958-
// }
946+
if (i > pos) out.write(text.substring(pos, i));
947+
out.write("&#");
948+
out.write(Integer.toString(ch));
949+
out.write(';');
950+
pos = i + 1;
959951
}
960952
}
961953
if (seenBracket) {

src/test/java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ private String createExpectedXML(boolean escape) {
8282
buf.append(LS);
8383
buf.append(" </el6>");
8484
buf.append(LS);
85-
buf.append(" <el8>special-char-" + (char) 7 + "</el8>");
85+
if (escape) {
86+
buf.append(" <el8>special-char-&#7;</el8>");
87+
} else {
88+
buf.append(" <el8>special-char-" + (char) 7 + "</el8>");
89+
}
8690
buf.append(LS);
8791
buf.append("</root>");
8892

0 commit comments

Comments
 (0)