diff --git a/xstream/src/java/com/thoughtworks/xstream/io/xml/PrettyPrintWriter.java b/xstream/src/java/com/thoughtworks/xstream/io/xml/PrettyPrintWriter.java index 5d6f752fd..95c2b4555 100644 --- a/xstream/src/java/com/thoughtworks/xstream/io/xml/PrettyPrintWriter.java +++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/PrettyPrintWriter.java @@ -206,9 +206,7 @@ protected void writeText(final QuickWriter writer, final String text) { } private void writeText(final String text, final boolean isAttribute) { - final int length = text.length(); - for (int i = 0; i < length; i++) { - final char c = text.charAt(i); + text.codePoints().forEach(c -> { switch (c) { case '\0': if (mode == XML_QUIRKS) { @@ -238,7 +236,7 @@ private void writeText(final String text, final boolean isAttribute) { case '\t': case '\n': if (!isAttribute) { - writer.write(c); + writer.write(Character.toChars(c)); break; } //$FALL-THROUGH$ @@ -251,7 +249,7 @@ private void writeText(final String text, final boolean isAttribute) { + " in XML stream"); } } - writer.write(c); + writer.write(Character.toChars(c)); } else { if (mode == XML_1_0) { if (c < 9 || c == '\u000b' || c == '\u000c' || c == '\u000e' || c >= '\u000f' && c <= '\u001f') { @@ -272,7 +270,7 @@ private void writeText(final String text, final boolean isAttribute) { writer.write(';'); } } - } + }); } @Override diff --git a/xstream/src/test/com/thoughtworks/xstream/io/xml/PrettyPrintWriterTest.java b/xstream/src/test/com/thoughtworks/xstream/io/xml/PrettyPrintWriterTest.java index fe64c1c1a..a0c327502 100644 --- a/xstream/src/test/com/thoughtworks/xstream/io/xml/PrettyPrintWriterTest.java +++ b/xstream/src/test/com/thoughtworks/xstream/io/xml/PrettyPrintWriterTest.java @@ -295,6 +295,33 @@ public void testThrowsForInvalidUnicodeCharacterslInXml1_1Mode() { assertXmlProducedIs("퟿\ue000\ufffd"); } + public void testSupportsSupplementaryMultilingualPlaneInQuirks_Mode() { + writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_QUIRKS); + writer.startNode("tag"); + writer.setValue("\uD83E\uDD8A"); + writer.endNode(); + + assertXmlProducedIs("\uD83E\uDD8A"); + } + + public void testSupportsSupplementaryMultilingualPlaneInXml1_0Mode() { + writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_0); + writer.startNode("tag"); + writer.setValue("\uD83E\uDD8A"); + writer.endNode(); + + assertXmlProducedIs("\uD83E\uDD8A"); + } + + public void testSupportsSupplementaryMultilingualPlaneInXml1_1Mode() { + writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_1); + writer.startNode("tag"); + writer.setValue("\uD83E\uDD8A"); + writer.endNode(); + + assertXmlProducedIs("\uD83E\uDD8A"); + } + private String replace(final String in, final char what, final String with) { final int pos = in.indexOf(what); if (pos == -1) {