Skip to content

Commit

Permalink
Reformat.
Browse files Browse the repository at this point in the history
  • Loading branch information
joehni committed Sep 27, 2023
1 parent c2deef3 commit 63fb8f8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*
* Created on 07. March 2004 by Joe Walnes
*/
package com.thoughtworks.xstream.io.xml;
Expand Down Expand Up @@ -38,14 +38,14 @@
* neither directly nor as entity nor within CDATA. However, this writer works by default in a quirks mode, where it
* will write any character at least as character entity (even a null character). You may switch into XML_1_1 mode
* (which supports most characters) or XML_1_0 that does only support a very limited number of control characters. See
* XML specification for version <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets">1.0</a> or <a
* href="http://www.w3.org/TR/2006/REC-xml11-20060816/#charsets">1.1</a>. If a character is not supported, a
* XML specification for version <a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets">1.0</a> or
* <a href="http://www.w3.org/TR/2006/REC-xml11-20060816/#charsets">1.1</a>. If a character is not supported, a
* {@link StreamException} is thrown. Select a proper parser implementation that respects the version in the XML header
* (the Xpp3 parser will also read character entities of normally invalid characters).
* You may also switch to XML_1_0_REPLACEMENT or XML_1_1_REPLACEMENT mode, which will replace the invalid characters
* with a U+FFFD replacement character.
* (the Xpp3 or MX parsers will also read character entities of normally invalid characters). You may also switch to
* XML_1_0_REPLACEMENT or XML_1_1_REPLACEMENT mode, which will replace the invalid characters with a U+FFFD replacement
* character.
* </p>
*
*
* @author Joe Walnes
* @author J&ouml;rg Schaible
*/
Expand Down Expand Up @@ -80,7 +80,8 @@ public class PrettyPrintWriter extends AbstractXmlWriter {
/**
* @since 1.4
*/
public PrettyPrintWriter(final Writer writer, final int mode, final char[] lineIndenter, final NameCoder nameCoder) {
public PrettyPrintWriter(
final Writer writer, final int mode, final char[] lineIndenter, final NameCoder nameCoder) {
super(nameCoder);
this.writer = new QuickWriter(writer);
this.lineIndenter = lineIndenter;
Expand Down Expand Up @@ -259,8 +260,8 @@ private void writeText(final String text, final boolean isAttribute) {
replaced = true;
} else {
throw new StreamException("Invalid character 0x"
+ Integer.toHexString(c)
+ " in XML stream");
+ Integer.toHexString(c)
+ " in XML stream");
}
}
}
Expand All @@ -270,14 +271,18 @@ private void writeText(final String text, final boolean isAttribute) {
} else {
boolean replaced = false;
if (mode == XML_1_0 || mode == XML_1_0_REPLACEMENT) {
if (c < 9 || c == '\u000b' || c == '\u000c' || c == '\u000e' || c >= '\u000f' && c <= '\u001f') {
if (c < 9
|| c == '\u000b'
|| c == '\u000c'
|| c == '\u000e'
|| c >= '\u000f' && c <= '\u001f') {
if (mode == XML_1_0_REPLACEMENT) {
writer.write(REPLACEMENT);
replaced = true;
} else {
throw new StreamException("Invalid character 0x"
+ Integer.toHexString(c)
+ " in XML 1.0 stream");
+ Integer.toHexString(c)
+ " in XML 1.0 stream");
}
}
}
Expand All @@ -288,8 +293,8 @@ private void writeText(final String text, final boolean isAttribute) {
replaced = true;
} else {
throw new StreamException("Invalid character 0x"
+ Integer.toHexString(c)
+ " in XML stream");
+ Integer.toHexString(c)
+ " in XML stream");
}
}
}
Expand Down Expand Up @@ -355,7 +360,7 @@ public void close() {
/**
* Retrieve the line terminator. This method returns always a line feed, since according the XML specification any
* parser must ignore a carriage return. Overload this method, if you need different behavior.
*
*
* @return the line terminator
* @since 1.3
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,59 +259,60 @@ public void testReplacesInvalidControlCharactersInXml1_0ReplacementMode() {
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_0_REPLACEMENT);
writer.startNode("tag");
final String ctrl = ""
+ "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007"
+ "\u0008\u0009\n\u000b\u000c\r\u000e\u000f"
+ "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"
+ "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"
+ "\u007f"
+ "\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087"
+ "\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f"
+ "\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097"
+ "\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f"
+ "";
+ "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007"
+ "\u0008\u0009\n\u000b\u000c\r\u000e\u000f"
+ "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"
+ "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"
+ "\u007f"
+ "\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087"
+ "\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f"
+ "\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097"
+ "\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f"
+ "";
for (int i = 0; i < ctrl.length(); i++) {
final char c = ctrl.charAt(i);
writer.setValue(new Character(c).toString());
}
writer.endNode();

assertXmlProducedIs("<tag>&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;"
+ "&#xfffd;\t\n&#xfffd;&#xfffd;&#xd;&#xfffd;&#xfffd;"
+ "&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;"
+ "&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;"
+ "&#x7f;"
+ "&#x80;&#x81;&#x82;&#x83;&#x84;&#x85;&#x86;&#x87;"
+ "&#x88;&#x89;&#x8a;&#x8b;&#x8c;&#x8d;&#x8e;&#x8f;"
+ "&#x90;&#x91;&#x92;&#x93;&#x94;&#x95;&#x96;&#x97;"
+ "&#x98;&#x99;&#x9a;&#x9b;&#x9c;&#x9d;&#x9e;&#x9f;</tag>"); }
+ "&#xfffd;\t\n&#xfffd;&#xfffd;&#xd;&#xfffd;&#xfffd;"
+ "&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;"
+ "&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;"
+ "&#x7f;"
+ "&#x80;&#x81;&#x82;&#x83;&#x84;&#x85;&#x86;&#x87;"
+ "&#x88;&#x89;&#x8a;&#x8b;&#x8c;&#x8d;&#x8e;&#x8f;"
+ "&#x90;&#x91;&#x92;&#x93;&#x94;&#x95;&#x96;&#x97;"
+ "&#x98;&#x99;&#x9a;&#x9b;&#x9c;&#x9d;&#x9e;&#x9f;</tag>");
}

public void testReplacesInvalidControlCharactersInXml1_1ReplacementMode() {
writer = new PrettyPrintWriter(buffer, PrettyPrintWriter.XML_1_1_REPLACEMENT);
writer.startNode("tag");
final String ctrl = ""
+ "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007"
+ "\u0008\u0009\n\u000b\u000c\r\u000e\u000f"
+ "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"
+ "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"
+ "\u007f"
+ "\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087"
+ "\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f"
+ "\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097"
+ "\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f"
+ "";
+ "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007"
+ "\u0008\u0009\n\u000b\u000c\r\u000e\u000f"
+ "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"
+ "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"
+ "\u007f"
+ "\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087"
+ "\u0088\u0089\u008a\u008b\u008c\u008d\u008e\u008f"
+ "\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097"
+ "\u0098\u0099\u009a\u009b\u009c\u009d\u009e\u009f"
+ "";
for (int i = 0; i < ctrl.length(); i++) {
final char c = ctrl.charAt(i);
writer.setValue(new Character(c).toString());
}
writer.endNode();
assertXmlProducedIs("<tag>&#xfffd;&#x1;&#x2;&#x3;&#x4;&#x5;&#x6;&#x7;"
+ "&#x8;\t\n&#xb;&#xc;&#xd;&#xe;&#xf;"
+ "&#x10;&#x11;&#x12;&#x13;&#x14;&#x15;&#x16;&#x17;"
+ "&#x18;&#x19;&#x1a;&#x1b;&#x1c;&#x1d;&#x1e;&#x1f;&#x7f;"
+ "&#x80;&#x81;&#x82;&#x83;&#x84;&#x85;&#x86;&#x87;"
+ "&#x88;&#x89;&#x8a;&#x8b;&#x8c;&#x8d;&#x8e;&#x8f;"
+ "&#x90;&#x91;&#x92;&#x93;&#x94;&#x95;&#x96;&#x97;"
+ "&#x98;&#x99;&#x9a;&#x9b;&#x9c;&#x9d;&#x9e;&#x9f;</tag>");
+ "&#x8;\t\n&#xb;&#xc;&#xd;&#xe;&#xf;"
+ "&#x10;&#x11;&#x12;&#x13;&#x14;&#x15;&#x16;&#x17;"
+ "&#x18;&#x19;&#x1a;&#x1b;&#x1c;&#x1d;&#x1e;&#x1f;&#x7f;"
+ "&#x80;&#x81;&#x82;&#x83;&#x84;&#x85;&#x86;&#x87;"
+ "&#x88;&#x89;&#x8a;&#x8b;&#x8c;&#x8d;&#x8e;&#x8f;"
+ "&#x90;&#x91;&#x92;&#x93;&#x94;&#x95;&#x96;&#x97;"
+ "&#x98;&#x99;&#x9a;&#x9b;&#x9c;&#x9d;&#x9e;&#x9f;</tag>");
}

public void testSupportsInvalidUnicodeCharacterslInQuirksMode() {
Expand Down

0 comments on commit 63fb8f8

Please sign in to comment.