Skip to content

Commit

Permalink
No longer escape and write characters considered to be invalid accord…
Browse files Browse the repository at this point in the history
…ing to the XML 1.0 spec.
  • Loading branch information
baron1405 committed Mar 29, 2024
1 parent 29bcb69 commit 1e48ed4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Numeric character entities are now written in hexidecimal (e.g. `©`) rather than decimal
- Invalid XML characters are now written using the prefix `unicode-` (e.g. `unicode-0xFFFE`) rather
than `ctrl-` and using hexidecimal (e.g. `unicode-0xFFFE`) rather than decimal for the character code
- Invalid XML characters are no longer written. In previous versions, they were written in decimal with the prefix
"ctrl-".
- Changed JSR-305 dependency from `implementation` to `api`

## [2.0.1] - 2023-12-23
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/cthing/xmlwriter/XmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2657,8 +2657,7 @@ void writeEscaped(final char[] carr, final int start, final int length) throws S
/**
* Writes the specified character to the output escaping the '&', '<', and '>' characters using the
* standard XML escape sequences. Control characters and characters outside the ASCII range are escaped using a
* numeric character reference. Invalid XML control characters are written as {code unicode-0xN} where {@code N}
* is the hexidecimal value of the invalid character.
* numeric character reference. Invalid XML characters are not written.
*
* @param c Character to write
* @throws SAXException If there is an error writing the character. The SAXException wraps an IOException.
Expand All @@ -2680,9 +2679,6 @@ void writeEscaped(final int c) throws SAXException {
writeRaw("&#x");
writeRaw(Integer.toHexString(c).toUpperCase());
writeRaw(';');
} else {
writeRaw("unicode-0x");
writeRaw(Integer.toHexString(c).toUpperCase());
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/cthing/xmlwriter/XmlWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void testNeedsEscaping(final String str, final boolean needsEscaping) {
@DisplayName("Write an array with escaping")
void testWriteEscapedArray() throws Exception {
final String testStringIn = "<Hello &<>\" World\u00A9\u001A\uFFFE\uD83D\uDE03\t\n";
final String testStringOut = "&lt;Hello &amp;&lt;&gt;\" World&#xA9;unicode-0x1Aunicode-0xFFFE&#x1F603;\t\n";
final String testStringOut = "&lt;Hello &amp;&lt;&gt;\" World&#xA9;&#x1F603;\t\n";

this.xmlWriter.writeEscaped(testStringIn.toCharArray(), 0, testStringIn.length());

Expand All @@ -133,9 +133,9 @@ public static Stream<Arguments> writeEscapedProvider() {
arguments('\n', "\n"),
arguments('\t', "\t"),
arguments('\r', "\r"),
arguments(0x0, "unicode-0x0"),
arguments(0x1F, "unicode-0x1F"),
arguments(0xFFFF, "unicode-0xFFFF"),
arguments(0x0, ""),
arguments(0x1F, ""),
arguments(0xFFFF, ""),
arguments(0x7F, "&#x7F;"),
arguments(0xD7FF, "&#xD7FF;"),
arguments(0xE000, "&#xE000;"),
Expand Down

0 comments on commit 1e48ed4

Please sign in to comment.