Skip to content

Commit

Permalink
PrettyPrintWriter fails to serialize characters in the Unicode Supple…
Browse files Browse the repository at this point in the history
…mentary Multilingual Plane in XML 1.0 mode and XML 1.1 mode. Closes #337.
  • Loading branch information
basil authored and joehni committed Nov 11, 2024
1 parent ab4a172 commit d987316
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ <h2>Major changes</h2>
<ul>
<li>Support for Record types (contributed by Julia Boes and Chris Hegarty, both of Oracle).</li>
<li>Usage of Java generic types. Most code bases will still compile without change.</li>
<li>GPR:#337: PrettyPrintWriter supports now characters of the Unicode Supplementary Multilingual Plane (by
Basil Crow).</li>
<li>Support for fail-safe deserialization unless the parser is not directly involved (I/O or syntax error) as
combination of new method getLevel() of HierarchicalStreamReader and GHPR:#91.</li>
<li>GHI:#120: Add <em>Automatic-Module-Name</em> entry to XStream's manifests.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,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) {
Expand Down Expand Up @@ -267,7 +265,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$
Expand All @@ -287,7 +285,7 @@ private void writeText(final String text, final boolean isAttribute) {
}
}
if (!replaced) {
writer.write(c);
writer.write(Character.toChars(c));
}
} else {
boolean replaced = false;
Expand Down Expand Up @@ -326,7 +324,7 @@ private void writeText(final String text, final boolean isAttribute) {
}
}
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2004, 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2008, 2013, 2018, 2023 XStream Committers.
* Copyright (C) 2006, 2007, 2008, 2013, 2018, 2023, 2024 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -397,6 +397,33 @@ public void testReplacesInvalidUnicodeCharactersInXml1_1ReplacementMode() {
assertXmlProducedIs("<tag>&#xd7ff;&#xfffd;&#xfffd;\ue000\ufffd&#xfffd;&#xfffd;</tag>");
}

public void testSupportsSupplementaryMultilingualPlaneInQuirks_Mode() {
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_QUIRKS);
writer.startNode("tag");
writer.setValue("\uD83E\uDD8A");
writer.endNode();

assertXmlProducedIs("<tag>\uD83E\uDD8A</tag>");
}

public void testSupportsSupplementaryMultilingualPlaneInXml1_0Mode() {
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_0);
writer.startNode("tag");
writer.setValue("\uD83E\uDD8A");
writer.endNode();

assertXmlProducedIs("<tag>\uD83E\uDD8A</tag>");
}

public void testSupportsSupplementaryMultilingualPlaneInXml1_1Mode() {
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_1);
writer.startNode("tag");
writer.setValue("\uD83E\uDD8A");
writer.endNode();

assertXmlProducedIs("<tag>\uD83E\uDD8A</tag>");
}

private String replace(final String in, final char what, final String with) {
final int pos = in.indexOf(what);
if (pos == -1) {
Expand Down

0 comments on commit d987316

Please sign in to comment.