Skip to content

Commit

Permalink
Use "unicode-" rather than "ctrl-" for invalid XML characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
baron1405 committed Mar 23, 2024
1 parent d33dd8c commit 29bcb69
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +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 in hexidecimal (e.g. `ctrl-0xFFFE`) 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
- Changed JSR-305 dependency from `implementation` to `api`

## [2.0.1] - 2023-12-23
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cthing/xmlwriter/XmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2657,7 +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 ctrl-0xN} where {@code N}
* numeric character reference. Invalid XML control characters are written as {code unicode-0xN} where {@code N}
* is the hexidecimal value of the invalid character.
*
* @param c Character to write
Expand All @@ -2681,7 +2681,7 @@ void writeEscaped(final int c) throws SAXException {
writeRaw(Integer.toHexString(c).toUpperCase());
writeRaw(';');
} else {
writeRaw("ctrl-0x");
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;ctrl-0x1Actrl-0xFFFE&#x1F603;\t\n";
final String testStringOut = "&lt;Hello &amp;&lt;&gt;\" World&#xA9;unicode-0x1Aunicode-0xFFFE&#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, "ctrl-0x0"),
arguments(0x1F, "ctrl-0x1F"),
arguments(0xFFFF, "ctrl-0xFFFF"),
arguments(0x0, "unicode-0x0"),
arguments(0x1F, "unicode-0x1F"),
arguments(0xFFFF, "unicode-0xFFFF"),
arguments(0x7F, "&#x7F;"),
arguments(0xD7FF, "&#xD7FF;"),
arguments(0xE000, "&#xE000;"),
Expand Down

0 comments on commit 29bcb69

Please sign in to comment.