Top Description Inners Fields Constructors Methods
com.google.common.escape

public final Class Escapers

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

Static utility methods pertaining to Escaper instances.
Authors
Sven Mawson, David Beaumont
Since
15.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Escapers.Builder

A builder for simple, fast escapers.

Field Summary

Modifier and TypeField and Description
private static final Escaper

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static UnicodeEscaper

Returns:

a UnicodeEscaper with the same behavior as the given instance
asUnicodeEscaper
(Escaper
the instance to be wrapped
escaper
)

Returns a UnicodeEscaper equivalent to the given escaper instance.

public static Escapers.Builder
builder()

Returns a builder for creating simple, fast escapers.

public static String

Returns:

the replacement string, or null if no escaping was needed
computeReplacement
(CharEscaper escaper, char
the character to escape if necessary
c
)

Returns a string that would replace the given character in the specified escaper, or null if no replacement should be made.

public static String

Returns:

the replacement string, or null if no escaping was needed
computeReplacement
(UnicodeEscaper escaper, int
the Unicode code point to escape if necessary
cp
)

Returns a string that would replace the given character in the specified escaper, or null if no replacement should be made.

public static Escaper
nullEscaper()

Returns an Escaper that does no escaping, passing all character data through unchanged.

private static String
stringOrNull(char[] in)

private static UnicodeEscaper
wrap(CharEscaper escaper)

Private helper to wrap a CharEscaper as a UnicodeEscaper.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NULL_ESCAPERback to summary
private static final Escaper NULL_ESCAPER

Constructor Detail

Escapersback to summary
private Escapers()

Method Detail

asUnicodeEscaperback to summary
pack-priv static UnicodeEscaper asUnicodeEscaper(Escaper escaper)

Returns a UnicodeEscaper equivalent to the given escaper instance. If the escaper is already a UnicodeEscaper then it is simply returned, otherwise it is wrapped in a UnicodeEscaper.

When a CharEscaper escaper is wrapped by this method it acquires extra behavior with respect to the well-formedness of Unicode character sequences and will throw IllegalArgumentException when given bad input.

Parameters
escaper:Escaper

the instance to be wrapped

Returns:UnicodeEscaper

a UnicodeEscaper with the same behavior as the given instance

Exceptions
NullPointerException:
if escaper is null
IllegalArgumentException:
if escaper is not a UnicodeEscaper or a CharEscaper
builderback to summary
public static Escapers.Builder builder()

Returns a builder for creating simple, fast escapers. A builder instance can be reused and each escaper that is created will be a snapshot of the current builder state. Builders are not thread safe.

The initial state of the builder is such that:

  • There are no replacement mappings
  • safeMin == Character.MIN_VALUE
  • safeMax == Character.MAX_VALUE
  • unsafeReplacement == null

For performance reasons escapers created by this builder are not Unicode aware and will not validate the well-formedness of their input.

computeReplacementback to summary
public static String computeReplacement(CharEscaper escaper, char c)

Returns a string that would replace the given character in the specified escaper, or null if no replacement should be made. This method is intended for use in tests through the EscaperAsserts class; production users of CharEscaper should limit themselves to its public interface.

Parameters
c:char

the character to escape if necessary

Returns:String

the replacement string, or null if no escaping was needed

Annotations
@CheckForNull
computeReplacementback to summary
public static String computeReplacement(UnicodeEscaper escaper, int cp)

Returns a string that would replace the given character in the specified escaper, or null if no replacement should be made. This method is intended for use in tests through the EscaperAsserts class; production users of UnicodeEscaper should limit themselves to its public interface.

Parameters
cp:int

the Unicode code point to escape if necessary

Returns:String

the replacement string, or null if no escaping was needed

Annotations
@CheckForNull
nullEscaperback to summary
public static Escaper nullEscaper()

Returns an Escaper that does no escaping, passing all character data through unchanged.

stringOrNullback to summary
private static String stringOrNull(char[] in)
Annotations
@CheckForNull
wrapback to summary
private static UnicodeEscaper wrap(CharEscaper escaper)

Private helper to wrap a CharEscaper as a UnicodeEscaper.

com.google.common.escape back to summary

public final Class Escapers.Builder

extends Object
Class Inheritance

A builder for simple, fast escapers.

Typically an escaper needs to deal with the escaping of high valued characters or code points. In these cases it is necessary to extend either ArrayBasedCharEscaper or ArrayBasedUnicodeEscaper to provide the desired behavior. However this builder is suitable for creating escapers that replace a relative small set of characters.

Author
David Beaumont
Since
15.0

Field Summary

Modifier and TypeField and Description
private final Map<Character, String>
private char
private char
private String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Escapers.Builder

Returns:

the builder instance
addEscape
(char
the character to be replaced
c
,
String
the string to replace the given character
replacement
)

Adds a replacement string for the given input character.

public Escaper
build()

Returns a new escaper based on the current state of the builder.

public Escapers.Builder

Returns:

the builder instance
setSafeRange
(char
the lowest 'safe' character
safeMin
,
char
the highest 'safe' character
safeMax
)

Sets the safe range of characters for the escaper.

public Escapers.Builder

Returns:

the builder instance
setUnsafeReplacement
(@Nullable String
the string to replace unsafe characters
unsafeReplacement
)

Sets the replacement string for any characters outside the 'safe' range that have no explicit replacement.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

replacementMapback to summary
private final Map<Character, String> replacementMap
safeMaxback to summary
private char safeMax
safeMinback to summary
private char safeMin
unsafeReplacementback to summary
private String unsafeReplacement
Annotations
@CheckForNull

Constructor Detail

Builderback to summary
private Builder()

Method Detail

addEscapeback to summary
public Escapers.Builder addEscape(char c, String replacement)

Adds a replacement string for the given input character. The specified character will be replaced by the given string whenever it occurs in the input, irrespective of whether it lies inside or outside the 'safe' range.

Parameters
c:char

the character to be replaced

replacement:String

the string to replace the given character

Returns:Escapers.Builder

the builder instance

Annotations
@CanIgnoreReturnValue
Exceptions
NullPointerException:
if replacement is null
buildback to summary
public Escaper build()

Returns a new escaper based on the current state of the builder.

setSafeRangeback to summary
public Escapers.Builder setSafeRange(char safeMin, char safeMax)

Sets the safe range of characters for the escaper. Characters in this range that have no explicit replacement are considered 'safe' and remain unescaped in the output. If safeMax < safeMin then the safe range is empty.

Parameters
safeMin:char

the lowest 'safe' character

safeMax:char

the highest 'safe' character

Returns:Escapers.Builder

the builder instance

Annotations
@CanIgnoreReturnValue
setUnsafeReplacementback to summary
public Escapers.Builder setUnsafeReplacement(@Nullable String unsafeReplacement)

Sets the replacement string for any characters outside the 'safe' range that have no explicit replacement. If unsafeReplacement is null then no replacement will occur, if it is "" then the unsafe characters are removed from the output.

Parameters
unsafeReplacement:@Nullable String

the string to replace unsafe characters

Returns:Escapers.Builder

the builder instance

Annotations
@CanIgnoreReturnValue