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

Class XmlEscapers

corpus/java/training/guava/xml/XmlEscapers.java:42–145  ·  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

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

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