Package org.cthing.escapers
Class XmlEscaper
java.lang.Object
org.cthing.escapers.XmlEscaper
Escapes strings and character arrays using XML 1.0 entities. For example,
calling the
escape(CharSequence, Option...) with the string "this & that" produces the string
"this & that". Characters are escaped according to the following table:
| Code Point | Escape | Description |
|---|---|---|
| 0x09 | Unchanged | Tab |
| 0x0A | Unchanged | Line feed |
| 0x0D | Unchanged | Carriage return |
| 0x20 - 0x21 | Unchanged | Printable ASCII characters |
| 0x22 | " | Double quote |
| 0x23 - 0x25 | Unchanged | Printable ASCII characters |
| 0x26 | & | Ampersand |
| 0x27 | ' | Single quote |
| 0x28 - 0x3B | Unchanged | Printable ASCII characters |
| 0x3C | < | Less than |
| 0x3D | Unchanged | Printable ASCII characters |
| 0x3E | > | Greater than |
| 0x3F - 0x7E | Unchanged | Printable ASCII characters |
| 0x7F | Numeric character entity (e.g. ) | ASCII control character |
| 0x80 - 0xFF |
Unchanged by default or numeric character entity (e.g. „)
if XmlEscaper.Option.ESCAPE_NON_ASCII specified
|
ISO Latin-1 |
| 0x100 - 0xD7FF |
Unchanged by default or numeric character entity (e.g. )
if XmlEscaper.Option.ESCAPE_NON_ASCII specified
|
Unicode BMP |
| 0xE000 - 0xFFFD |
Unchanged by default or numeric character entity (e.g. )
if XmlEscaper.Option.ESCAPE_NON_ASCII specified
|
Unicode BMP |
| 0x10000 - 0x10FFFF |
Unchanged by default or numeric character entity (e.g. 𐀀)
if XmlEscaper.Option.ESCAPE_NON_ASCII specified
|
Unicode surrogate pairs |
The characters in the above table correspond to the valid XML characters. Invalid characters are not included in the output (e.g. the null character 0x00 is dropped).
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic voidescape(char[] charArr, int offset, int length, Writer writer, Set<XmlEscaper.Option> options) Applies XML escaping to the specified character array and writes the result to the specified writer.static voidescape(char[] charArr, int offset, int length, Writer writer, XmlEscaper.Option... options) Applies XML escaping to the specified character array and writes the result to the specified writer.static Stringescape(char[] charArr, int offset, int length, Set<XmlEscaper.Option> options) Applies XML escaping to the specified character array.static Stringescape(char[] charArr, int offset, int length, XmlEscaper.Option... options) Applies XML escaping to the specified character array.static voidescape(char[] charArr, Writer writer, Set<XmlEscaper.Option> options) Applies XML escaping to the specified character array and writes the result to the specified writer.static voidescape(char[] charArr, Writer writer, XmlEscaper.Option... options) Applies XML escaping to the specified character array and writes the result to the specified writer.static Stringescape(char[] charArr, Set<XmlEscaper.Option> options) Applies XML escaping to the specified character array.static Stringescape(char[] charArr, XmlEscaper.Option... options) Applies XML escaping to the specified character array.static voidescape(CharSequence charSequence, Writer writer, Set<XmlEscaper.Option> options) Applies XML escaping to the specified string and writes the result to the specified writer.static voidescape(CharSequence charSequence, Writer writer, XmlEscaper.Option... options) Applies XML escaping to the specified string and writes the result to the specified writer.static Stringescape(CharSequence charSequence, Set<XmlEscaper.Option> options) Applies XML escaping to the specified string.static Stringescape(CharSequence charSequence, XmlEscaper.Option... options) Applies XML escaping to the specified string.
-
Method Details
-
escape
Applies XML escaping to the specified string.- Parameters:
charSequence- String to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
Applies XML escaping to the specified string.- Parameters:
charSequence- String to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
@WillNotClose public static void escape(CharSequence charSequence, Writer writer, XmlEscaper.Option... options) throws IOException Applies XML escaping to the specified string and writes the result to the specified writer. Note that invalid XML characters are not included in the output.- Parameters:
charSequence- String to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(CharSequence charSequence, Writer writer, Set<XmlEscaper.Option> options) throws IOException Applies XML escaping to the specified string and writes the result to the specified writer. Note that invalid XML characters are not included in the output.- Parameters:
charSequence- String to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
Applies XML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
Applies XML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arrayoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
Applies XML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
Applies XML escaping to the specified character array.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arrayoptions- Escaping options- Returns:
- Escaped string or
nullifnullwas passed in. Note that invalid XML characters are not included in the output.
-
escape
@WillNotClose public static void escape(char[] charArr, Writer writer, XmlEscaper.Option... options) throws IOException Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.- Parameters:
charArr- Character array to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, XmlEscaper.Option... options) throws IOException Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arraywriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(char[] charArr, Writer writer, Set<XmlEscaper.Option> options) throws IOException Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.- Parameters:
charArr- Character array to escapewriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-
escape
@WillNotClose public static void escape(char[] charArr, int offset, int length, Writer writer, Set<XmlEscaper.Option> options) throws IOException Applies XML escaping to the specified character array and writes the result to the specified writer. Note that invalid XML characters are not included in the output.- Parameters:
charArr- Character array to escapeoffset- Start index in arraylength- Number of characters in arraywriter- Writer to which the escaped string is writtenoptions- Escaping options- Throws:
IOException- if there was a problem writing the escaped stringIllegalArgumentException- if the writer isnull
-