UnicodeEscaper that uses an array to quickly look up replacement characters for a given
code point. An additional safe range is provided that determines whether code points without
specific replacements are to be considered safe and left unescaped or should be escaped in a
general way.
A good example of usage of this class is for HTML escaping where the replacement array
contains information about the named HTML entities such as & and " while
escapeUnsafe is overridden to handle general escaping of the form NNNNN;.
The size of the data structure used by ArrayBasedUnicodeEscaper is proportional to the
highest valued code point that requires escaping. For example a replacement map containing the
single character '\u1000' will require approximately 16K of memory. If you need
to create multiple escaper instances that have the same character replacement mapping consider
using ArrayBasedEscaperMap.
| Modifier and Type | Field and Description |
|---|---|
| private final char[][] | |
| private final int | |
| private final int | |
| private final char | |
| private final int | |
| private final char |
| Access | Constructor and Description |
|---|---|
| protected | ArrayBasedUnicodeEscaper(Map<Character, String>
a map of characters to their escaped representations replacementMap, int the lowest character value in the safe range safeMin, int the highest character value in the safe range safeMax, @Nullable String the default replacement for unsafe characters or null if no default
replacement is required unsafeReplacement)Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range. |
| protected | ArrayBasedUnicodeEscaper(ArrayBasedEscaperMap
the map of replacements escaperMap, int the lowest character value in the safe range safeMin, int the highest character value in the safe range safeMax, @Nullable String the default replacement for unsafe characters or null if no default
replacement is required unsafeReplacement)Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified safe range. |
| Modifier and Type | Method and Description |
|---|---|
| public final String | |
| protected final char[] | Returns: the replacement characters, ornull if no escaping was requiredthe Unicode code point to escape if necessary cp)Implements abstract com. |
| protected abstract char[] | Returns: the replacement characters, ornull if no escaping was requiredthe Unicode code point to escape cp)Escapes a code point that has no direct explicit value in the replacement array and lies outside the stated safe range. |
| protected final int | nextEscapeIndex(CharSequence
a sequence of characters csq, int the index of the first character to be scanned index, int the index immediately after the last character to be scanned end)Overrides com. CharSequence, returning the index of
the next character that requires escaping.
|
| replacements | back to summary |
|---|---|
| private final char[][] replacements | |
| replacementsLength | back to summary |
|---|---|
| private final int replacementsLength | |
| safeMax | back to summary |
|---|---|
| private final int safeMax | |
| safeMaxChar | back to summary |
|---|---|
| private final char safeMaxChar | |
| safeMin | back to summary |
|---|---|
| private final int safeMin | |
| safeMinChar | back to summary |
|---|---|
| private final char safeMinChar | |
| ArrayBasedUnicodeEscaper | back to summary |
|---|---|
| protected ArrayBasedUnicodeEscaper(Map<Character, String> replacementMap, int safeMin, int safeMax, @Nullable String unsafeReplacement) Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified
safe range. If If a code point has no mapped replacement then it is checked against the safe range. If it
lies outside that, then
| |
| ArrayBasedUnicodeEscaper | back to summary |
|---|---|
| protected ArrayBasedUnicodeEscaper(ArrayBasedEscaperMap escaperMap, int safeMin, int safeMax, @Nullable String unsafeReplacement) Creates a new ArrayBasedUnicodeEscaper instance with the given replacement map and specified
safe range. If If a code point has no mapped replacement then it is checked against the safe range. If it
lies outside that, then
| |
| escape | back to summary |
|---|---|
| public final String escape(String s) Overrides com. Doc from com. Returns the escaped form of a given literal string. If you are escaping input in arbitrary successive chunks, then it is not generally safe to
use this method. If an input string ends with an unmatched high surrogate character, then this
method will throw Note When implementing an escaper it is a good idea to override this method for
efficiency by inlining the implementation of | |
| escape | back to summary |
|---|---|
| protected final char[] escape(int cp) Implements abstract com. Escapes a single Unicode code point using the replacement array and safe range values. If the
given character does not have an explicit replacement and lies outside the safe range then
| |
| escapeUnsafe | back to summary |
|---|---|
| protected abstract char[] escapeUnsafe(int cp) Escapes a code point that has no direct explicit value in the replacement array and lies outside the stated safe range. Subclasses should override this method to provide generalized escaping for code points if required. Note that arrays returned by this method must not be modified once they have been returned. However it is acceptable to return the same array multiple times (even for different input characters).
| |
| nextEscapeIndex | back to summary |
|---|---|
| protected final int nextEscapeIndex(CharSequence csq, int index, int end) Overrides com. Doc from com. Scans a sub-sequence of characters from a given Note When implementing an escaper, it is a good idea to override this method for
efficiency. The base class implementation determines successive Unicode code points and invokes
Note however that if your escaper does not escape characters in the supplementary range, you should either continue to validate the correctness of any surrogate characters encountered or provide a clear warning to users that your escaper does not validate its input. See
| |