GeezerVison is showing github.com — Remove special chars from xml output · codehaus-plexus/plexus-xml@be6885b
File treeExpand file treeCollapse file tree Open diff view settings main/java/org/codehaus/plexus/util/xml test/java/org/codehaus/plexus/util/xml
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/util/xml/PrettyPrintXMLWriter.java +20-5Lines changed: 20 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | import java.util.regex.Pattern; | 24 | 24 |
| 25 | 25 | /** | 26 | | - * Implementation of XMLWriter which emits nicely formatted documents. | 27 | | - * | | 26 | + * <p>Implementation of XMLWriter which emits nicely formatted documents.</p> | 28 | 27 | * | | 28 | + * <p>C0n control characters except <code>\n</code>, <code>\r</code>, and <code>\t</code> are omitted from output</p> | 29 | 29 | */ | 30 | 30 | public class PrettyPrintXMLWriter implements XMLWriter { | 31 | 31 | /** Line separator ("\n" on UNIX) */ | @@ -185,7 +185,7 @@ private void writeText(String text, boolean escapeXml) { | 185 | 185 | finishTag(); | 186 | 186 |
| 187 | 187 | if (escapeXml) { | 188 | | - text = escapeXml(text); | | 188 | + text = escapeXmlText(text); | 189 | 189 | } | 190 | 190 |
| 191 | 191 | write(StringUtils.unifyLineSeparators(text, lineSeparator)); | @@ -225,10 +225,12 @@ private static String escapeXml(String text) { | 225 | 225 |
| 226 | 226 | private static final Pattern crlf = Pattern.compile(crlf_str); | 227 | 227 |
| 228 | | - private static final Pattern lowers = Pattern.compile("([\000-\037])"); | | 228 | + private static final Pattern lowers = Pattern.compile("([\\x00-\\x1F])"); | | 229 | + | | 230 | + private static final Pattern illegalC0Characters = Pattern.compile("([\\x00-\\x08\\x0B-\\x0C\\x0E-\\x1F])"); | 229 | 231 |
| 230 | 232 | private static String escapeXmlAttribute(String text) { | 231 | | - text = escapeXml(text); | | 233 | + text = escapeXmlText(text); | 232 | 234 |
| 233 | 235 | // Windows | 234 | 236 | Matcher crlfmatcher = crlf.matcher(text); | @@ -246,6 +248,19 @@ private static String escapeXmlAttribute(String text) { | 246 | 248 | return b.toString(); | 247 | 249 | } | 248 | 250 |
| | 251 | + private static String escapeXmlText(String text) { | | 252 | + text = escapeXml(text); | | 253 | + | | 254 | + Matcher matcher = illegalC0Characters.matcher(text); | | 255 | + StringBuffer b = new StringBuffer(); | | 256 | + while (matcher.find()) { | | 257 | + matcher = matcher.appendReplacement(b, ""); | | 258 | + } | | 259 | + matcher.appendTail(b); | | 260 | + | | 261 | + return b.toString(); | | 262 | + } | | 263 | + | 249 | 264 | /** {@inheritDoc} */ | 250 | 265 | @Override | 251 | 266 | public void addAttribute(String key, String value) { | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/codehaus/plexus/util/xml/pull/MXSerializer.java +4-13Lines changed: 4 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | * <li>PROPERTY_SERIALIZER_INDENTATION | 25 | 25 | * <li>PROPERTY_SERIALIZER_LINE_SEPARATOR | 26 | 26 | * </ul> | | 27 | + * <p>C0n control characters except <code>\n</code>, <code>\r</code>, and <code>\t</code> are omitted from output</p> | 27 | 28 | */ | 28 | 29 | public class MXSerializer implements XmlSerializer { | 29 | 30 | protected static final String XML_URI = "http://www.w3.org/XML/1998/namespace"; | @@ -943,19 +944,9 @@ protected void writeElementContent(String text, Writer out) throws IOException { | 943 | 944 | // out.write(';'); | 944 | 945 | // pos = i + 1; | 945 | 946 | } 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 | | - // } | | 947 | + // skip special char | | 948 | + if (i > pos) out.write(text.substring(pos, i)); | | 949 | + pos = i + 1; | 959 | 950 | } | 960 | 951 | } | 961 | 952 | if (seenBracket) { | |
Collapse file Copy file name to clipboardExpand all lines: src/test/java/org/codehaus/plexus/util/xml/Xpp3DomWriterTest.java +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,6 +82,12 @@ private String createExpectedXML(boolean escape) { | 82 | 82 | buf.append(LS); | 83 | 83 | buf.append(" </el6>"); | 84 | 84 | buf.append(LS); | | 85 | + if (escape) { | | 86 | + buf.append(" <el8>special-char-</el8>"); | | 87 | + } else { | | 88 | + buf.append(" <el8>special-char-" + (char) 7 + "</el8>"); | | 89 | + } | | 90 | + buf.append(LS); | 85 | 91 | buf.append("</root>"); | 86 | 92 |
| 87 | 93 | return buf.toString(); | @@ -95,7 +101,7 @@ private Xpp3Dom createXpp3Dom() { | 95 | 101 | dom.addChild(el1); | 96 | 102 |
| 97 | 103 | Xpp3Dom el2 = new Xpp3Dom("el2"); | 98 | | - el2.setAttribute("att2", "attribute2\nnextline"); | | 104 | + el2.setAttribute("att2", "attribute2\nnextline" + (char) 7); | 99 | 105 | dom.addChild(el2); | 100 | 106 |
| 101 | 107 | Xpp3Dom el3 = new Xpp3Dom("el3"); | @@ -119,6 +125,10 @@ private Xpp3Dom createXpp3Dom() { | 119 | 125 | el7.setValue("element7\n&\"\'<>"); | 120 | 126 | el6.addChild(el7); | 121 | 127 |
| | 128 | + Xpp3Dom el8 = new Xpp3Dom("el8"); | | 129 | + el8.setValue("special-char-" + (char) 7); | | 130 | + | | 131 | + dom.addChild(el8); | 122 | 132 | return dom; | 123 | 133 | } | 124 | 134 | } |
Collapse file Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package org.codehaus.plexus.util.xml.pull; | | 2 | + | | 3 | +import java.io.StringReader; | | 4 | +import java.io.StringWriter; | | 5 | +import java.util.Arrays; | | 6 | + | | 7 | +import org.junit.jupiter.api.Test; | | 8 | + | | 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; | | 10 | + | | 11 | +class MXSerializerTest { | | 12 | + | | 13 | + @Test | | 14 | + void testSerialize() throws Exception { | | 15 | + | | 16 | + StringWriter writer = new StringWriter(); | | 17 | + | | 18 | + MXSerializer sr = new MXSerializer(); | | 19 | + sr.setOutput(writer); | | 20 | + | | 21 | + sr.startDocument(null, Boolean.TRUE); | | 22 | + sr.startTag(null, "root"); | | 23 | + for (int i : Arrays.asList(8, 9, 10, 11, 13, 15)) { | | 24 | + sr.startTag(null, "char"); | | 25 | + sr.text(Character.getName(i) + ": " + ((char) i)); | | 26 | + sr.endTag(null, "char"); | | 27 | + } | | 28 | + | | 29 | + sr.endTag(null, "root"); | | 30 | + sr.endDocument(); | | 31 | + assertEquals(expectedOutput(), writer.toString()); | | 32 | + } | | 33 | + | | 34 | + @Test | | 35 | + void testDeserialize() throws Exception { | | 36 | + MXParser parser = new MXParser(); | | 37 | + parser.setInput(new StringReader(expectedOutput())); | | 38 | + int eventType = parser.getEventType(); | | 39 | + | | 40 | + while (eventType != XmlPullParser.END_DOCUMENT) { | | 41 | + eventType = parser.next(); | | 42 | + } | | 43 | + } | | 44 | + | | 45 | + private String expectedOutput() { | | 46 | + StringBuilder out = new StringBuilder(); | | 47 | + out.append("<?xml version=\"1.0\" standalone=\"yes\"?>"); | | 48 | + out.append("<root>"); | | 49 | + out.append("<char>BACKSPACE: </char>"); | | 50 | + out.append("<char>CHARACTER TABULATION: \t</char>"); | | 51 | + out.append("<char>LINE FEED (LF): \n</char>"); | | 52 | + out.append("<char>LINE TABULATION: </char>"); | | 53 | + out.append("<char>CARRIAGE RETURN (CR): \r</char>"); | | 54 | + out.append("<char>SHIFT IN: </char>"); | | 55 | + out.append("</root>"); | | 56 | + return out.toString(); | | 57 | + } | | 58 | +} |
|