From 5b06633a6f47084c7ffadffc52b0eac76c3358c5 Mon Sep 17 00:00:00 2001 From: Hua Zhang Date: Sat, 25 Feb 2023 14:58:57 +0100 Subject: [PATCH] add xml decimal attribute escaper --- .../com/google/common/xml/XmlEscapers.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/guava/src/com/google/common/xml/XmlEscapers.java b/guava/src/com/google/common/xml/XmlEscapers.java index 33241f3f3299..58d6243c0fac 100644 --- a/guava/src/com/google/common/xml/XmlEscapers.java +++ b/guava/src/com/google/common/xml/XmlEscapers.java @@ -98,9 +98,34 @@ public static Escaper xmlAttributeEscaper() { return XML_ATTRIBUTE_ESCAPER; } + /** + * Returns an {@link Escaper} instance that escapes special characters in a string so it can + * safely be included in XML document as an attribute value. See section 3.3.3 of the XML + * specification. + * + *

This escaper substitutes {@code 0xFFFD} for non-whitespace control characters and the + * character values {@code 0xFFFE} and {@code 0xFFFF} which are not permitted in XML. For more + * detail see section 2.2 of + * the XML specification. + * + *

This escaper does not escape non-ASCII characters to their numeric character references + * (NCR). However, horizontal tab {@code '\t'}, line feed {@code '\n'} and carriage return {@code + * '\r'} are escaped to a corresponding NCR {@code " "}, {@code " "}, and {@code " "} + * respectively. Any other non-ASCII characters appearing in the input will be preserved in the + * output. + * + *

This escaper does not treat surrogate pairs specially and does not perform Unicode + * validation on its input. + */ + public static Escaper xmlDecimalAttributeEscaper() { + return XML_DECIMAL_ATTRIBUTE_ESCAPER; + } + private static final Escaper XML_ESCAPER; private static final Escaper XML_CONTENT_ESCAPER; private static final Escaper XML_ATTRIBUTE_ESCAPER; + private static final Escaper XML_DECIMAL_ATTRIBUTE_ESCAPER; static { Escapers.Builder builder = Escapers.builder(); @@ -139,5 +164,9 @@ public static Escaper xmlAttributeEscaper() { builder.addEscape('\n', " "); builder.addEscape('\r', " "); XML_ATTRIBUTE_ESCAPER = builder.build(); + builder.addEscape('\t', " "); + builder.addEscape('\n', " "); + builder.addEscape('\r', " "); + XML_DECIMAL_ATTRIBUTE_ESCAPER = builder.build(); } }