MCPcopy Create free account
hub / github.com/antlr/codebuff / XmlEscapers

Class XmlEscapers

output/java_guava/1.4.16/XmlEscapers.java:44–151  ·  view source on GitHub ↗

Escaper instances suitable for strings to be included in XML attribute values and elements' text contents. When possible, avoid manual escaping by using templating systems and high-level APIs that provide autoescaping. For example, consider XOM or <a href="ht

Source from the content-addressed store, hash-verified

42
43
44@Beta
45@GwtCompatible
46public class XmlEscapers {
47 private XmlEscapers() {}
48
49 private static final char MIN_ASCII_CONTROL_CHAR = 0x00;
50 private static final char MAX_ASCII_CONTROL_CHAR = 0x1F;
51
52 // For each xxxEscaper() method, please add links to external reference pages
53 // that are considered authoritative for the behavior of that escaper.
54
55 /**
56 * Returns an {@link Escaper} instance that escapes special characters in a string so it can
57 * safely be included in an XML document as element content. See section
58 * <a href="http://www.w3.org/TR/2008/REC-xml-20081126/#syntax">2.4</a> of the XML specification.
59 *
60 * <p><b>Note:</b> Double and single quotes are not escaped, so it is <b>not safe</b> to use this
61 * escaper to escape attribute values. Use {@link #xmlContentEscaper} if the output can appear in
62 * element content or {@link #xmlAttributeEscaper} in attribute values.
63 *
64 * <p>This escaper substitutes {@code 0xFFFD} for non-whitespace control characters and the
65 * character values {@code 0xFFFE} and {@code 0xFFFF} which are not permitted in XML. For more
66 * detail see section <a href="http://www.w3.org/TR/2008/REC-xml-20081126/#charsets">2.2</a> of
67 * the XML specification.
68 *
69 * <p>This escaper does not escape non-ASCII characters to their numeric character references
70 * (NCR). Any non-ASCII characters appearing in the input will be preserved in the output.
71 * Specifically "\r" (carriage return) is preserved in the output, which may result in it being
72 * silently converted to "\n" when the XML is parsed.
73 *
74 * <p>This escaper does not treat surrogate pairs specially and does not perform Unicode
75 * validation on its input.
76 */
77
78
79 public static Escaper xmlContentEscaper() {
80 return XML_CONTENT_ESCAPER;
81 }
82
83 /**
84 * Returns an {@link Escaper} instance that escapes special characters in a string so it can
85 * safely be included in XML document as an attribute value. See section
86 * <a href="http://www.w3.org/TR/2008/REC-xml-20081126/#AVNormalize">3.3.3</a> of the XML
87 * specification.
88 *
89 * <p>This escaper substitutes {@code 0xFFFD} for non-whitespace control characters and the
90 * character values {@code 0xFFFE} and {@code 0xFFFF} which are not permitted in XML. For more
91 * detail see section <a href="http://www.w3.org/TR/2008/REC-xml-20081126/#charsets">2.2</a> of
92 * the XML specification.
93 *
94 * <p>This escaper does not escape non-ASCII characters to their numeric character references
95 * (NCR). However, horizontal tab {@code '\t'}, line feed {@code '\n'} and carriage return
96 * {@code '\r'} are escaped to a corresponding NCR {@code "&#x9;"}, {@code "&#xA;"}, and
97 * {@code "&#xD;"} respectively. Any other non-ASCII characters appearing in the input will be
98 * preserved in the output.
99 *
100 * <p>This escaper does not treat surrogate pairs specially and does not perform Unicode
101 * validation on its input.

Callers

nothing calls this directly

Calls 5

builderMethod · 0.95
setSafeRangeMethod · 0.45
setUnsafeReplacementMethod · 0.45
addEscapeMethod · 0.45
buildMethod · 0.45

Tested by

no test coverage detected