Skip to content

Commit d987316

Browse files
basiljoehni
authored andcommitted
PrettyPrintWriter fails to serialize characters in the Unicode Supplementary Multilingual Plane in XML 1.0 mode and XML 1.1 mode. Closes #337.
1 parent ab4a172 commit d987316

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

xstream-distribution/src/content/changes.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ <h2>Major changes</h2>
3838
<ul>
3939
<li>Support for Record types (contributed by Julia Boes and Chris Hegarty, both of Oracle).</li>
4040
<li>Usage of Java generic types. Most code bases will still compile without change.</li>
41+
<li>GPR:#337: PrettyPrintWriter supports now characters of the Unicode Supplementary Multilingual Plane (by
42+
Basil Crow).</li>
4143
<li>Support for fail-safe deserialization unless the parser is not directly involved (I/O or syntax error) as
4244
combination of new method getLevel() of HierarchicalStreamReader and GHPR:#91.</li>
4345
<li>GHI:#120: Add <em>Automatic-Module-Name</em> entry to XStream's manifests.</li>

xstream/src/java/com/thoughtworks/xstream/io/xml/PrettyPrintWriter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,7 @@ protected void writeText(final QuickWriter writer, final String text) {
233233
}
234234

235235
private void writeText(final String text, final boolean isAttribute) {
236-
final int length = text.length();
237-
for (int i = 0; i < length; i++) {
238-
final char c = text.charAt(i);
236+
text.codePoints().forEach(c -> {
239237
switch (c) {
240238
case '\0':
241239
if (mode == XML_QUIRKS) {
@@ -267,7 +265,7 @@ private void writeText(final String text, final boolean isAttribute) {
267265
case '\t':
268266
case '\n':
269267
if (!isAttribute) {
270-
writer.write(c);
268+
writer.write(Character.toChars(c));
271269
break;
272270
}
273271
//$FALL-THROUGH$
@@ -287,7 +285,7 @@ private void writeText(final String text, final boolean isAttribute) {
287285
}
288286
}
289287
if (!replaced) {
290-
writer.write(c);
288+
writer.write(Character.toChars(c));
291289
}
292290
} else {
293291
boolean replaced = false;
@@ -326,7 +324,7 @@ private void writeText(final String text, final boolean isAttribute) {
326324
}
327325
}
328326
}
329-
}
327+
});
330328
}
331329

332330
@Override

xstream/src/test/com/thoughtworks/xstream/io/xml/PrettyPrintWriterTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2004, 2005 Joe Walnes.
3-
* Copyright (C) 2006, 2007, 2008, 2013, 2018, 2023 XStream Committers.
3+
* Copyright (C) 2006, 2007, 2008, 2013, 2018, 2023, 2024 XStream Committers.
44
* All rights reserved.
55
*
66
* The software in this package is published under the terms of the BSD
@@ -397,6 +397,33 @@ public void testReplacesInvalidUnicodeCharactersInXml1_1ReplacementMode() {
397397
assertXmlProducedIs("<tag>&#xd7ff;&#xfffd;&#xfffd;\ue000\ufffd&#xfffd;&#xfffd;</tag>");
398398
}
399399

400+
public void testSupportsSupplementaryMultilingualPlaneInQuirks_Mode() {
401+
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_QUIRKS);
402+
writer.startNode("tag");
403+
writer.setValue("\uD83E\uDD8A");
404+
writer.endNode();
405+
406+
assertXmlProducedIs("<tag>\uD83E\uDD8A</tag>");
407+
}
408+
409+
public void testSupportsSupplementaryMultilingualPlaneInXml1_0Mode() {
410+
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_0);
411+
writer.startNode("tag");
412+
writer.setValue("\uD83E\uDD8A");
413+
writer.endNode();
414+
415+
assertXmlProducedIs("<tag>\uD83E\uDD8A</tag>");
416+
}
417+
418+
public void testSupportsSupplementaryMultilingualPlaneInXml1_1Mode() {
419+
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_1);
420+
writer.startNode("tag");
421+
writer.setValue("\uD83E\uDD8A");
422+
writer.endNode();
423+
424+
assertXmlProducedIs("<tag>\uD83E\uDD8A</tag>");
425+
}
426+
400427
private String replace(final String in, final char what, final String with) {
401428
final int pos = in.indexOf(what);
402429
if (pos == -1) {

0 commit comments

Comments
 (0)