public final class XmlEscape extends Object
Utility class for performing XML escape/unescape operations.
Configuration of escape/unescape operationsEscape operations can be (optionally) configured by means of:
XmlEscapeLevel
enum.XmlEscapeType
enum.Unescape operations need no configuration parameters. Unescape operations will always perform complete unescape of CERs, decimal and hexadecimal references.
FeaturesThis class supports both XML 1.0 and XML 1.1 escape/unescape operations. Whichever the XML version used, only the five predefined XML character entities are supported: <, >, &, " and '. This means there is no support for DTD-defined or user-defined entities.
Each version of XML establishes a series of characters that are considered not-valid, even when escaped —for example, the \u0000 (null byte)—. Escape operations will automatically remove these chars.
Also, each version of XML establishes a series of control characters that, even if allowed as valid characters, should always appear escaped. For example: \u0001 to \u0008 in XML 1.1.
This class supports the whole Unicode character set: \u0000 to \u10FFFF, including characters not representable by only one char in Java (>\uFFFF).
Input/OutputThere are two different input/output modes that can be used in escape/unescape operations:
The following references apply:
Modifier and Type | Method and Description |
---|---|
static void |
escapeXml10(char[] text,
int offset,
int len,
Writer writer)
Perform an XML 1.0 level 2 (markup-significant and all non-ASCII chars) escape operation
on a char[] input.
|
static void |
escapeXml10(char[] text,
int offset,
int len,
Writer writer,
XmlEscapeType type,
XmlEscapeLevel level)
Perform a (configurable) XML 1.0 escape operation on a char[] input.
|
static String |
escapeXml10(String text)
Perform an XML 1.0 level 2 (markup-significant and all non-ASCII chars) escape operation
on a String input.
|
static String |
escapeXml10(String text,
XmlEscapeType type,
XmlEscapeLevel level)
Perform a (configurable) XML 1.0 escape operation on a String input.
|
static void |
escapeXml10Minimal(char[] text,
int offset,
int len,
Writer writer)
Perform an XML 1.0 level 1 (only markup-significant chars) escape operation
on a char[] input.
|
static String |
escapeXml10Minimal(String text)
Perform an XML 1.0 level 1 (only markup-significant chars) escape operation
on a String input.
|
static void |
escapeXml11(char[] text,
int offset,
int len,
Writer writer)
Perform an XML 1.1 level 2 (markup-significant and all non-ASCII chars) escape operation
on a char[] input.
|
static void |
escapeXml11(char[] text,
int offset,
int len,
Writer writer,
XmlEscapeType type,
XmlEscapeLevel level)
Perform a (configurable) XML 1.1 escape operation on a char[] input.
|
static String |
escapeXml11(String text)
Perform an XML 1.1 level 2 (markup-significant and all non-ASCII chars) escape operation
on a String input.
|
static String |
escapeXml11(String text,
XmlEscapeType type,
XmlEscapeLevel level)
Perform a (configurable) XML 1.1 escape operation on a String input.
|
static void |
escapeXml11Minimal(char[] text,
int offset,
int len,
Writer writer)
Perform an XML 1.1 level 1 (only markup-significant chars) escape operation
on a char[] input.
|
static String |
escapeXml11Minimal(String text)
Perform an XML 1.1 level 1 (only markup-significant chars) escape operation
on a String input.
|
static void |
unescapeXml(char[] text,
int offset,
int len,
Writer writer)
Perform an XML unescape operation on a char[] input.
|
static String |
unescapeXml(String text)
Perform an XML unescape operation on a String input.
|
public static String escapeXml10Minimal(String text)
Perform an XML 1.0 level 1 (only markup-significant chars) escape operation on a String input.
Level 1 means this method will only escape the five markup-significant characters which are predefined as Character Entity References in XML: <, >, &, " and '.
This method calls escapeXml10(String, XmlEscapeType, XmlEscapeLevel)
with the following
preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the String to be escaped.public static String escapeXml11Minimal(String text)
Perform an XML 1.1 level 1 (only markup-significant chars) escape operation on a String input.
Level 1 means this method will only escape the five markup-significant characters which are predefined as Character Entity References in XML: <, >, &, " and '.
This method calls escapeXml11(String, XmlEscapeType, XmlEscapeLevel)
with the following
preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the String to be escaped.public static String escapeXml10(String text)
Perform an XML 1.0 level 2 (markup-significant and all non-ASCII chars) escape operation on a String input.
Level 2 means this method will escape:
This escape will be performed by replacing those chars by the corresponding XML Character Entity References (e.g. '<') when such CER exists for the replaced character, and replacing by a hexadecimal character reference (e.g. '␰') when there there is no CER for the replaced character.
This method calls escapeXml10(String, XmlEscapeType, XmlEscapeLevel)
with the following
preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the String to be escaped.public static String escapeXml11(String text)
Perform an XML 1.1 level 2 (markup-significant and all non-ASCII chars) escape operation on a String input.
Level 2 means this method will escape:
This escape will be performed by replacing those chars by the corresponding XML Character Entity References (e.g. '<') when such CER exists for the replaced character, and replacing by a hexadecimal character reference (e.g. '␰') when there there is no CER for the replaced character.
This method calls escapeXml11(String, XmlEscapeType, XmlEscapeLevel)
with the following
preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the String to be escaped.public static String escapeXml10(String text, XmlEscapeType type, XmlEscapeLevel level)
Perform a (configurable) XML 1.0 escape operation on a String input.
This method will perform an escape operation according to the specified
XmlEscapeType
and XmlEscapeLevel
argument values.
All other String-based escapeXml10*(...) methods call this one with preconfigured type and level values.
This method is thread-safe.
text
- the String to be escaped.type
- the type of escape operation to be performed, see XmlEscapeType
.level
- the escape level to be applied, see XmlEscapeLevel
.public static String escapeXml11(String text, XmlEscapeType type, XmlEscapeLevel level)
Perform a (configurable) XML 1.1 escape operation on a String input.
This method will perform an escape operation according to the specified
XmlEscapeType
and XmlEscapeLevel
argument values.
All other String-based escapeXml11*(...) methods call this one with preconfigured type and level values.
This method is thread-safe.
text
- the String to be escaped.type
- the type of escape operation to be performed, see XmlEscapeType
.level
- the escape level to be applied, see XmlEscapeLevel
.public static void escapeXml10Minimal(char[] text, int offset, int len, Writer writer) throws IOException
Perform an XML 1.0 level 1 (only markup-significant chars) escape operation on a char[] input.
Level 1 means this method will only escape the five markup-significant characters which are predefined as Character Entity References in XML: <, >, &, " and '.
This method calls
escapeXml10(char[], int, int, java.io.Writer, XmlEscapeType, XmlEscapeLevel)
with the following preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeXml11Minimal(char[] text, int offset, int len, Writer writer) throws IOException
Perform an XML 1.1 level 1 (only markup-significant chars) escape operation on a char[] input.
Level 1 means this method will only escape the five markup-significant characters which are predefined as Character Entity References in XML: <, >, &, " and '.
This method calls
escapeXml10(char[], int, int, java.io.Writer, XmlEscapeType, XmlEscapeLevel)
with the following preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeXml10(char[] text, int offset, int len, Writer writer) throws IOException
Perform an XML 1.0 level 2 (markup-significant and all non-ASCII chars) escape operation on a char[] input.
Level 2 means this method will escape:
This escape will be performed by replacing those chars by the corresponding XML Character Entity References (e.g. '<') when such CER exists for the replaced character, and replacing by a hexadecimal character reference (e.g. '␰') when there there is no CER for the replaced character.
This method calls escapeXml10(char[], int, int, java.io.Writer, XmlEscapeType, XmlEscapeLevel)
with the following preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeXml11(char[] text, int offset, int len, Writer writer) throws IOException
Perform an XML 1.1 level 2 (markup-significant and all non-ASCII chars) escape operation on a char[] input.
Level 2 means this method will escape:
This escape will be performed by replacing those chars by the corresponding XML Character Entity References (e.g. '<') when such CER exists for the replaced character, and replacing by a hexadecimal character reference (e.g. '␰') when there there is no CER for the replaced character.
This method calls escapeXml11(char[], int, int, java.io.Writer, XmlEscapeType, XmlEscapeLevel)
with the following preconfigured values:
XmlEscapeType.CHARACTER_ENTITY_REFERENCES_DEFAULT_TO_HEXA
XmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeXml10(char[] text, int offset, int len, Writer writer, XmlEscapeType type, XmlEscapeLevel level) throws IOException
Perform a (configurable) XML 1.0 escape operation on a char[] input.
This method will perform an escape operation according to the specified
XmlEscapeType
and XmlEscapeLevel
argument values.
All other char[]-based escapeXml10*(...) methods call this one with preconfigured type and level values.
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.type
- the type of escape operation to be performed, see XmlEscapeType
.level
- the escape level to be applied, see XmlEscapeLevel
.IOException
- if an input/output exception occurspublic static void escapeXml11(char[] text, int offset, int len, Writer writer, XmlEscapeType type, XmlEscapeLevel level) throws IOException
Perform a (configurable) XML 1.1 escape operation on a char[] input.
This method will perform an escape operation according to the specified
XmlEscapeType
and XmlEscapeLevel
argument values.
All other char[]-based escapeXml11*(...) methods call this one with preconfigured type and level values.
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.type
- the type of escape operation to be performed, see XmlEscapeType
.level
- the escape level to be applied, see XmlEscapeLevel
.IOException
- if an input/output exception occurspublic static String unescapeXml(String text)
Perform an XML unescape operation on a String input.
No additional configuration arguments are required. Unescape operations will always perform complete XML 1.0/1.1 unescape of CERs, decimal and hexadecimal references.
This method is thread-safe.
text
- the String to be unescaped.public static void unescapeXml(char[] text, int offset, int len, Writer writer) throws IOException
Perform an XML unescape operation on a char[] input.
No additional configuration arguments are required. Unescape operations will always perform complete XML 1.0/1.1 unescape of CERs, decimal and hexadecimal references.
This method is thread-safe.
text
- the char[] to be unescaped.offset
- the position in text at which the unescape operation should start.len
- the number of characters in text that should be unescaped.writer
- the java.io.Writer to which the unescaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occursCopyright © 2015 The UNBESCAPE team. All rights reserved.