Package org.apache.commons.jexl3.parser
Class StringParser
java.lang.Object
org.apache.commons.jexl3.parser.StringParser
- Direct Known Subclasses:
JexlParser
Common constant strings utilities.
This package methods read JEXL string literals and handle escaping through the 'backslash' (ie: \) character. Escaping is used to neutralize string delimiters (the single and double quotes) and read Unicode hexadecimal encoded characters.
The only escapable characters are the single and double quotes - ''' and '"' -, a Unicode sequence starting with 'u' followed by 4 hexadecimals and the backslash character - '\' - itself.
A sequence where '\' occurs before any non-escapable character or sequence has no effect, the sequence output being the same as the input.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe base 10 offset used to convert hexa characters to decimal.private static final charThe first printable 7bits ascii character.private static final charThe last 7bits ascii character.private static final intInitial shift value for composing a Unicode char from 4 nibbles (16 - 4).private static final intThe length of an escaped unicode sequence. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbuildRegex(CharSequence str) Builds a regex pattern string, handles escaping '/' through '\/' syntax.static StringbuildString(CharSequence str, boolean eatsep) Builds a string, handles escaping through '\' syntax.private static StringbuildString(CharSequence str, boolean eatsep, boolean esc) Builds a string, handles escaping through '\' syntax.static StringbuildTemplate(CharSequence str, boolean eatsep) Builds a template, does not escape characters.static StringescapeIdentifier(String str) Adds a escape char ('\') where needed in a string form of an idestatic StringescapeString(String str, char delim) Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.private static intread(StringBuilder strb, CharSequence str, int begin, int end, char sep, boolean esc) Read the remainder of a string till a given separator, handles escaping through '\' syntax.static intreadString(StringBuilder strb, CharSequence str, int index, char sep) Read the remainder of a string till a given separator, handles escaping through '\' syntax.private static intreadUnicodeChar(StringBuilder strb, CharSequence str, int begin) Reads a Unicode escape character.static StringunescapeIdentifier(String str) Remove escape char ('\') from an identifier.
-
Field Details
-
UCHAR_LEN
private static final int UCHAR_LENThe length of an escaped unicode sequence.- See Also:
-
SHIFT
private static final int SHIFTInitial shift value for composing a Unicode char from 4 nibbles (16 - 4).- See Also:
-
BASE10
private static final int BASE10The base 10 offset used to convert hexa characters to decimal.- See Also:
-
LAST_ASCII
private static final char LAST_ASCIIThe last 7bits ascii character.- See Also:
-
FIRST_ASCII
private static final char FIRST_ASCIIThe first printable 7bits ascii character.- See Also:
-
-
Constructor Details
-
StringParser
protected StringParser()Default constructor.
-
-
Method Details
-
buildRegex
Builds a regex pattern string, handles escaping '/' through '\/' syntax.- Parameters:
str- the string to build from- Returns:
- the built string
-
buildString
Builds a string, handles escaping through '\' syntax.- Parameters:
str- the string to build fromeatsep- whether the separator, the first character, should be considered- Returns:
- the built string
-
buildString
Builds a string, handles escaping through '\' syntax.- Parameters:
str- the string to build fromeatsep- whether the separator, the first character, should be consideredesc- whether escape characters are interpreted or escaped- Returns:
- the built string
-
buildTemplate
Builds a template, does not escape characters.- Parameters:
str- the string to build fromeatsep- whether the separator, the first character, should be considered- Returns:
- the built string
-
escapeIdentifier
Adds a escape char ('\') where needed in a string form of an ide- Parameters:
str- the identifier un-escaped string- Returns:
- the string with added backslash character before space, quote, double-quote and backslash
-
escapeString
Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.- Parameters:
str- the string to escapedelim- the delimiter character- Returns:
- the escaped representation
-
read
private static int read(StringBuilder strb, CharSequence str, int begin, int end, char sep, boolean esc) Read the remainder of a string till a given separator, handles escaping through '\' syntax.- Parameters:
strb- the destination buffer to copy characters intostr- the originbegin- the relative offset in str to begin readingend- the relative offset in str to end readingsep- the separator, single or double quote, marking end of stringesc- whether escape characters are interpreted or escaped- Returns:
- the last character offset handled in origin
-
readString
Read the remainder of a string till a given separator, handles escaping through '\' syntax.- Parameters:
strb- the destination buffer to copy characters intostr- the originindex- the offset into the originsep- the separator, single or double quote, marking end of string- Returns:
- the offset in origin
-
readUnicodeChar
Reads a Unicode escape character.- Parameters:
strb- the builder to write the character tostr- the sequencebegin- the begin offset in sequence (after the '\\u')- Returns:
- 0 if char could not be read, 4 otherwise
-
unescapeIdentifier
Remove escape char ('\') from an identifier.- Parameters:
str- the identifier escaped string, ie with a backslash before space, quote, double-quote and backslash- Returns:
- the string with no '\\' character
-