public final class

CharEscapers

extends Object
java.lang.Object
   ↳ com.google.gdata.util.common.base.CharEscapers

Class Overview

Utility functions for dealing with CharEscapers, and some commonly used CharEscaper instances.

Summary

Fields
private static final CharEscaper ASCII_HTML_ESCAPER Escapes special characters from a string so it can safely be included in an HTML document in either element content or attribute values.
private static final Escaper CPP_URI_ESCAPER
private static final char[] HEX_DIGITS
private static final CharEscaper JAVASCRIPT_ESCAPER CharEscaper to escape javascript strings.
private static final CharEscaper JAVA_CHAR_ESCAPER Escapes special characters from a string so it can safely be included in a Java char literal or string literal.
private static final CharEscaper JAVA_STRING_ESCAPER Escapes special characters from a string so it can safely be included in a Java string literal.
private static final CharEscaper JAVA_STRING_UNICODE_ESCAPER Escapes each non-ASCII character in with its Unicode escape sequence \\uxxxx where xxxx is a hex number.
private static final CharEscaper NULL_ESCAPER Performs no escaping.
private static final CharEscaper PYTHON_ESCAPER Escapes special characters in a string so it can safely be included in a Python string literal.
private static final Escaper URI_ESCAPER
private static final Escaper URI_ESCAPER_NO_PLUS
private static final Escaper URI_PATH_ESCAPER
private static final Escaper URI_QUERY_STRING_ESCAPER
private static final CharEscaper XML_CONTENT_ESCAPER Escapes special characters from a string so it can safely be included in an XML document in element content.
private static final CharEscaper XML_ESCAPER Escapes special characters from a string so it can safely be included in an XML document in either element content or attribute values.
Public Methods
static CharEscaper asciiHtmlEscaper()
Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an HTML document in either element content or attribute values.
static Escaper cppUriEscaper()
Returns a Escaper instance that escapes Java characters in a manner compatible with the C++ webutil/url URL class (the kGoogle1Escape set).
static CharEscaper fallThrough(CharEscaper primary, CharEscaper secondary)
Returns a composite CharEscaper instance that tries to escape characters using a primary CharEscaper first and falls back to a secondary one if there is no escaping.
static CharEscaper htmlEscaper()
Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an HTML document in either element content or attribute values.
static CharEscaper javaCharEscaper()
Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in a Java char or string literal.
static CharEscaper javaStringEscaper()
Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in a Java string literal.
static CharEscaper javaStringUnicodeEscaper()
Returns a CharEscaper instance that replaces non-ASCII characters in a string with their Unicode escape sequences (\\uxxxx where xxxx is a hex number).
static CharEscaper javascriptEscaper()
Returns a CharEscaper instance that escapes non-ASCII characters in a string so it can safely be included in a Javascript string literal.
static CharEscaper nullEscaper()
Returns a CharEscaper that does no escaping.
static CharEscaper pythonEscaper()
Returns a CharEscaper instance that escapes special characters from a string so it can safely be included in a Python string literal.
static Escaper uriEscaper(boolean plusForSpace)
Returns a Escaper instance that escapes Java characters so they can be safely included in URIs.
static Escaper uriEscaper()
Returns an Escaper instance that escapes Java chars so they can be safely included in URIs.
static Escaper uriPathEscaper()
Returns an Escaper instance that escapes Java chars so they can be safely included in URI path segments.
static Escaper uriQueryStringEscaper()
Returns an Escaper instance that escapes Java chars so they can be safely included in URI query string segments.
static CharEscaper xmlContentEscaper()
Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an XML document in element content.
static CharEscaper xmlEscaper()
Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an XML document in either element content or attribute values.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

private static final CharEscaper ASCII_HTML_ESCAPER

Escapes special characters from a string so it can safely be included in an HTML document in either element content or attribute values. Does not alter non-ASCII characters or control characters.

private static final Escaper CPP_URI_ESCAPER

private static final char[] HEX_DIGITS

private static final CharEscaper JAVASCRIPT_ESCAPER

CharEscaper to escape javascript strings. Turns all non-ASCII characters into ASCII javascript escape sequences (e.g., \\uhhhh or \xhh).

private static final CharEscaper JAVA_CHAR_ESCAPER

Escapes special characters from a string so it can safely be included in a Java char literal or string literal.

Note that non-ASCII characters will be octal or Unicode escaped.

This is the same as JAVA_STRING_ESCAPER, except that it escapes single quotes.

private static final CharEscaper JAVA_STRING_ESCAPER

Escapes special characters from a string so it can safely be included in a Java string literal. Does not escape single-quotes, so use JAVA_CHAR_ESCAPE if you are generating char literals, or if you are unsure.

Note that non-ASCII characters will be octal or Unicode escaped.

private static final CharEscaper JAVA_STRING_UNICODE_ESCAPER

Escapes each non-ASCII character in with its Unicode escape sequence \\uxxxx where xxxx is a hex number. Existing escape sequences won't be affected.

private static final CharEscaper NULL_ESCAPER

Performs no escaping.

private static final CharEscaper PYTHON_ESCAPER

Escapes special characters in a string so it can safely be included in a Python string literal. Does not have any special handling for non-ASCII characters.

private static final Escaper URI_ESCAPER

private static final Escaper URI_ESCAPER_NO_PLUS

private static final Escaper URI_PATH_ESCAPER

private static final Escaper URI_QUERY_STRING_ESCAPER

private static final CharEscaper XML_CONTENT_ESCAPER

Escapes special characters from a string so it can safely be included in an XML document in element content. Note that quotes are not escaped, so this is not safe for use in attribute values. Use XML_ESCAPER for attribute values, or if you are unsure. Also removes non-whitespace control characters, as there is no way to represent them in XML.

private static final CharEscaper XML_ESCAPER

Escapes special characters from a string so it can safely be included in an XML document in either element content or attribute values. Also removes null-characters and control characters, as there is no way to represent them in XML.

Public Methods

public static CharEscaper asciiHtmlEscaper ()

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an HTML document in either element content or attribute values.

Note

: does not alter non-ASCII and control characters.

public static Escaper cppUriEscaper ()

Returns a Escaper instance that escapes Java characters in a manner compatible with the C++ webutil/url URL class (the kGoogle1Escape set).

When encoding a String, the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters "!", "(", ")", "*", "-", ".", "_", "~", ",", "/" and ":" remain the same.
  • The space character " " is converted into a plus sign "+".
  • All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, uppercase, hexadecimal representation of the byte value.

Note: Unlike other escapers, URI escapers produce uppercase hexidecimal sequences. From RFC 3986:
"URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings."

Note: This escaper is a special case and is not compliant with RFC 2396. Specifically it will not escape "/", ":" and ",". This is only provided for certain limited use cases and you should favor using uriEscaper() whenever possible.

public static CharEscaper fallThrough (CharEscaper primary, CharEscaper secondary)

Returns a composite CharEscaper instance that tries to escape characters using a primary CharEscaper first and falls back to a secondary one if there is no escaping.

The returned escaper will attempt to escape each character using the primary escaper, and if the primary escaper has no escaping for that character, it will use the secondary escaper. If the secondary escaper has no escaping for a character either, the original character will be used. If the primary escaper has an escape for a character, the secondary escaper will not be used at all for that character; the escaped output of the primary is not run through the secondary. For a case where you would like to first escape with one escaper, and then with another, it is recommended that you call each escaper in order.

Parameters
primary The primary CharEscaper to use
secondary The secondary CharEscaper to use if the first one has no escaping rule for a character
Throws
NullPointerException if any of the arguments is null

public static CharEscaper htmlEscaper ()

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an HTML document in either element content or attribute values.

Note

: alters non-ASCII and control characters. The entity list was taken from: here

public static CharEscaper javaCharEscaper ()

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in a Java char or string literal. The behavior of this escaper is the same as that of the javaStringEscaper(), except it also escapes single quotes.

public static CharEscaper javaStringEscaper ()

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in a Java string literal.

Note

: does not escape single quotes, so use the escaper returned by javaCharEscaper() if you are generating char literals or if you are unsure.

public static CharEscaper javaStringUnicodeEscaper ()

Returns a CharEscaper instance that replaces non-ASCII characters in a string with their Unicode escape sequences (\\uxxxx where xxxx is a hex number). Existing escape sequences won't be affected.

public static CharEscaper javascriptEscaper ()

Returns a CharEscaper instance that escapes non-ASCII characters in a string so it can safely be included in a Javascript string literal. Non-ASCII characters are replaced with their ASCII javascript escape sequences (e.g., \\uhhhh or \xhh).

public static CharEscaper nullEscaper ()

Returns a CharEscaper that does no escaping.

public static CharEscaper pythonEscaper ()

Returns a CharEscaper instance that escapes special characters from a string so it can safely be included in a Python string literal. Does not have any special handling for non-ASCII characters.

public static Escaper uriEscaper (boolean plusForSpace)

Returns a Escaper instance that escapes Java characters so they can be safely included in URIs. For details on escaping URIs, see section 2.4 of RFC 2396.

When encoding a String, the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • If plusForSpace was specified, the space character " " is converted into a plus sign "+". Otherwise it is converted into "%20".
  • All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, uppercase, hexadecimal representation of the byte value.

Note: Unlike other escapers, URI escapers produce uppercase hexidecimal sequences. From RFC 3986:
"URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings."

Parameters
plusForSpace If true space is escaped to + otherwise it is escaped to %20. Although common, the escaping of spaces as plus signs has a very ambiguous status in the relevant specifications. You should prefer %20 unless you are doing exact character-by-character comparisons of URLs and backwards compatibility requires you to use plus signs.
See Also

public static Escaper uriEscaper ()

Returns an Escaper instance that escapes Java chars so they can be safely included in URIs. For details on escaping URIs, see section 2.4 of RFC 2396.

When encoding a String, the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The special characters ".", "-", "*", and "_" remain the same.
  • The space character " " is converted into a plus sign "+".
  • All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, uppercase, hexadecimal representation of the byte value.

public static Escaper uriPathEscaper ()

Returns an Escaper instance that escapes Java chars so they can be safely included in URI path segments. For details on escaping URIs, see section 2.4 of RFC 3986.

When encoding a String, the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The unreserved characters ".", "-", "~", and "_" remain the same.
  • The general delimiters "@" and ":" remain the same.
  • The subdelimiters "!", "$", "&", "'", "(", ")", "*", ",", ";", and "=" remain the same.
  • The space character " " is converted into %20.
  • All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, uppercase, hexadecimal representation of the byte value.

Note: Unlike other escapers, URI escapers produce uppercase hexidecimal sequences. From RFC 3986:
"URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings."

public static Escaper uriQueryStringEscaper ()

Returns an Escaper instance that escapes Java chars so they can be safely included in URI query string segments. When the query string consists of a sequence of name=value pairs separated by &, the names and values should be individually encoded. If you escape an entire query string in one pass with this escaper, then the "=" and "&" characters used as separators will also be escaped.

This escaper is also suitable for escaping fragment identifiers.

For details on escaping URIs, see section 2.4 of RFC 3986.

When encoding a String, the following rules apply:

  • The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
  • The unreserved characters ".", "-", "~", and "_" remain the same.
  • The general delimiters "@" and ":" remain the same.
  • The path delimiters "/" and "?" remain the same.
  • The subdelimiters "!", "$", "'", "(", ")", "*", ",", and ";", remain the same.
  • The space character " " is converted into %20.
  • The equals sign "=" is converted into %3D.
  • The ampersand "&" is converted into %26.
  • All other characters are converted into one or more bytes using UTF-8 encoding and each byte is then represented by the 3-character string "%XY", where "XY" is the two-digit, uppercase, hexadecimal representation of the byte value.

Note: Unlike other escapers, URI escapers produce uppercase hexidecimal sequences. From RFC 3986:
"URI producers and normalizers should use uppercase hexadecimal digits for all percent-encodings."

public static CharEscaper xmlContentEscaper ()

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an XML document in element content.

Note

: double and single quotes are not escaped, so it is not safe to use this escaper to escape attribute values. Use the xmlEscaper() escaper to escape attribute values or if you are unsure. Also silently removes non-whitespace control characters, as there is no way to represent them in XML.

public static CharEscaper xmlEscaper ()

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an XML document in either element content or attribute values.

Note

: silently removes null-characters and control characters, as there is no way to represent them in XML.