001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.xml;
016
017import com.google.common.annotations.GwtCompatible;
018import com.google.common.escape.Escaper;
019import com.google.common.escape.Escapers;
020
021/**
022 * {@code Escaper} instances suitable for strings to be included in XML attribute values and
023 * elements' text contents. When possible, avoid manual escaping by using templating systems and
024 * high-level APIs that provide autoescaping. For example, consider <a
025 * href="https://www.xom.nu/">XOM</a>.
026 *
027 * <p><b>Note:</b> Currently the escapers provided by this class do not escape any characters
028 * outside the ASCII character range. Unlike HTML escaping the XML escapers will not escape
029 * non-ASCII characters to their numeric entity replacements. These XML escapers provide the minimal
030 * level of escaping to ensure that the output can be safely included in a Unicode XML document.
031 *
032 * <p>For details on the behavior of the escapers in this class, see sections <a
033 * href="https://www.w3.org/TR/2008/REC-xml-20081126/#charsets">2.2</a> and <a
034 * href="https://www.w3.org/TR/2008/REC-xml-20081126/#syntax">2.4</a> of the XML specification.
035 *
036 * @author Alex Matevossian
037 * @author David Beaumont
038 * @since 15.0
039 */
040@GwtCompatible
041public final class XmlEscapers {
042  private XmlEscapers() {}
043
044  private static final char MIN_ASCII_CONTROL_CHAR = 0x00;
045  private static final char MAX_ASCII_CONTROL_CHAR = 0x1F;
046
047  // For each xxxEscaper() method, please add links to external reference pages
048  // that are considered authoritative for the behavior of that escaper.
049
050  /**
051   * Returns an {@link Escaper} instance that escapes special characters in a string so it can
052   * safely be included in an XML document as element content. See section <a
053   * href="https://www.w3.org/TR/2008/REC-xml-20081126/#syntax">2.4</a> of the XML specification.
054   *
055   * <p><b>Note:</b> Double and single quotes are not escaped, so it is <b>not safe</b> to use this
056   * escaper to escape attribute values. Use {@link #xmlContentEscaper} if the output can appear in
057   * element content or {@link #xmlAttributeEscaper} in attribute values.
058   *
059   * <p>This escaper substitutes {@code U+FFFD} for non-whitespace control characters and the
060   * noncharacters {@code U+FFFE} and {@code U+FFFF}, which are not permitted in XML. For more
061   * detail, see section <a href="https://www.w3.org/TR/2008/REC-xml-20081126/#charsets">2.2</a> of
062   * the XML specification.
063   *
064   * <p>This escaper does not escape non-ASCII characters to their numeric character references
065   * (NCR). Any non-ASCII characters appearing in the input will be preserved in the output.
066   * Specifically "\r" (carriage return) is preserved in the output, which may result in it being
067   * silently converted to "\n" when the XML is parsed.
068   *
069   * <p>This escaper does not treat surrogate pairs specially and does not perform Unicode
070   * validation on its input.
071   */
072  public static Escaper xmlContentEscaper() {
073    return XML_CONTENT_ESCAPER;
074  }
075
076  /**
077   * Returns an {@link Escaper} instance that escapes special characters in a string so it can
078   * safely be included in XML document as an attribute value. See section <a
079   * href="https://www.w3.org/TR/2008/REC-xml-20081126/#AVNormalize">3.3.3</a> of the XML
080   * specification.
081   *
082   * <p>This escaper substitutes {@code U+FFFD} for non-whitespace control characters and the
083   * noncharacters {@code U+FFFE} and {@code U+FFFF}, which are not permitted in XML. For more
084   * detail, see section <a href="https://www.w3.org/TR/2008/REC-xml-20081126/#charsets">2.2</a> of
085   * the XML specification.
086   *
087   * <p>This escaper does not escape non-ASCII characters to their numeric character references
088   * (NCR). However, horizontal tab {@code '\t'}, line feed {@code '\n'} and carriage return {@code
089   * '\r'} are escaped to a corresponding NCR {@code "&#x9;"}, {@code "&#xA;"}, and {@code "&#xD;"}
090   * respectively. Any other non-ASCII characters appearing in the input will be preserved in the
091   * output.
092   *
093   * <p>This escaper does not treat surrogate pairs specially and does not perform Unicode
094   * validation on its input.
095   */
096  @SuppressWarnings("EscapedEntity") // We do mean for the user to see &#x9;" etc.
097  public static Escaper xmlAttributeEscaper() {
098    return XML_ATTRIBUTE_ESCAPER;
099  }
100  private static final Escaper XML_CONTENT_ESCAPER;
101  private static final Escaper XML_ATTRIBUTE_ESCAPER;
102
103  static {
104    Escapers.Builder builder = Escapers.builder();
105    // The noncharacters U+FFFE and U+FFFF are explicitly not allowed in XML
106    // (Unicode code points above U+FFFF are represented via surrogate pairs
107    // which means they are treated as pairs of safe characters).
108    builder.setSafeRange(Character.MIN_VALUE, '\uFFFD');
109    // Unsafe characters are replaced with the Unicode replacement character.
110    builder.setUnsafeReplacement("\uFFFD");
111
112    /*
113     * Except for \n, \t, and \r, all ASCII control characters are replaced with the Unicode
114     * replacement character.
115     *
116     * Implementation note: An alternative to the following would be to make a map that simply
117     * replaces the allowed ASCII whitespace characters with themselves and to set the minimum safe
118     * character to 0x20. However this would slow down the escaping of simple strings that contain
119     * \t, \n, or \r.
120     */
121    for (char c = MIN_ASCII_CONTROL_CHAR; c <= MAX_ASCII_CONTROL_CHAR; c++) {
122      if (c != '\t' && c != '\n' && c != '\r') {
123        builder.addEscape(c, "\uFFFD");
124      }
125    }
126
127    // Build the content escaper first and then add quote escaping for the
128    // general escaper.
129    builder.addEscape('&', "&amp;");
130    builder.addEscape('<', "&lt;");
131    builder.addEscape('>', "&gt;");
132    XML_CONTENT_ESCAPER = builder.build();
133    builder.addEscape('\'', "&apos;");
134    builder.addEscape('"', "&quot;");
135    builder.addEscape('\t', "&#x9;");
136    builder.addEscape('\n', "&#xA;");
137    builder.addEscape('\r', "&#xD;");
138    XML_ATTRIBUTE_ESCAPER = builder.build();
139  }
140}