Package org.apache.commons.jexl3.parser
Class StringParser
- java.lang.Object
-
- org.apache.commons.jexl3.parser.StringParser
-
- Direct Known Subclasses:
JexlParser
public class StringParser extends java.lang.ObjectCommon 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
Fields Modifier and Type Field Description private static intBASE10The base 10 offset used to convert hexa characters to decimal.private static charFIRST_ASCIIThe first printable 7bits ASCII character.private static charLAST_ASCIIThe last 7bits ASCII character.private static intSHIFTInitial shift value for composing a Unicode char from 4 nibbles (16 - 4).private static intUCHAR_LENThe length of an escaped unicode sequence.
-
Constructor Summary
Constructors Modifier Constructor Description protectedStringParser()Default constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringbuildRegex(java.lang.CharSequence str)Builds a regex pattern string, handles escaping '/' through '\/' syntax.static java.lang.StringbuildString(java.lang.CharSequence str, boolean eatsep)Builds a string, handles escaping through '\' syntax.private static java.lang.StringbuildString(java.lang.CharSequence str, boolean eatsep, boolean esc)Builds a string, handles escaping through '\' syntax.static java.lang.StringbuildTemplate(java.lang.CharSequence str, boolean eatsep)Builds a template, does not escape characters.static java.lang.StringescapeIdentifier(java.lang.String str)Adds a escape char ('\') where needed in a string form of an idestatic java.lang.StringescapeString(java.lang.CharSequence str, char delim)Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.private static intread(java.lang.StringBuilder strb, java.lang.CharSequence str, int begin, int end, char sep, boolean esc)Reads the remainder of a string till a given separator, handles escaping through '\' syntax.static intreadString(java.lang.StringBuilder strb, java.lang.CharSequence str, int index, char sep)Reads the remainder of a string till a given separator, handles escaping through '\' syntax.private static intreadUnicodeChar(java.lang.StringBuilder strb, java.lang.CharSequence str, int begin)Reads a Unicode escape character.static java.lang.StringunescapeIdentifier(java.lang.String str)Remove escape char ('\') from an identifier.
-
-
-
Field Detail
-
UCHAR_LEN
private static final int UCHAR_LEN
The length of an escaped unicode sequence.- See Also:
- Constant Field Values
-
SHIFT
private static final int SHIFT
Initial shift value for composing a Unicode char from 4 nibbles (16 - 4).- See Also:
- Constant Field Values
-
BASE10
private static final int BASE10
The base 10 offset used to convert hexa characters to decimal.- See Also:
- Constant Field Values
-
LAST_ASCII
private static final char LAST_ASCII
The last 7bits ASCII character.- See Also:
- Constant Field Values
-
FIRST_ASCII
private static final char FIRST_ASCII
The first printable 7bits ASCII character.- See Also:
- Constant Field Values
-
-
Method Detail
-
buildRegex
public static java.lang.String buildRegex(java.lang.CharSequence str)
Builds a regex pattern string, handles escaping '/' through '\/' syntax.- Parameters:
str- the string to build from- Returns:
- the built string
-
buildString
public static java.lang.String buildString(java.lang.CharSequence str, boolean eatsep)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
private static java.lang.String buildString(java.lang.CharSequence str, boolean eatsep, boolean esc)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
public static java.lang.String buildTemplate(java.lang.CharSequence str, boolean eatsep)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
public static java.lang.String escapeIdentifier(java.lang.String str)
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
public static java.lang.String escapeString(java.lang.CharSequence str, char delim)Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.- Parameters:
delim- the delimiter character (if 0, no delimiter is added)str- the string to escape- Returns:
- the escaped representation
-
read
private static int read(java.lang.StringBuilder strb, java.lang.CharSequence str, int begin, int end, char sep, boolean esc)Reads 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
public static int readString(java.lang.StringBuilder strb, java.lang.CharSequence str, int index, char sep)Reads 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
private static int readUnicodeChar(java.lang.StringBuilder strb, java.lang.CharSequence str, int begin)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
public static java.lang.String unescapeIdentifier(java.lang.String str)
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
-
-