Class StringParser

java.lang.Object
org.apache.commons.jexl3.parser.StringParser
Direct Known Subclasses:
JexlParser

public class StringParser extends Object
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 Details

    • UCHAR_LEN

      private static final int UCHAR_LEN
      The length of an escaped unicode sequence.
      See Also:
    • SHIFT

      private static final int SHIFT
      Initial shift value for composing a Unicode char from 4 nibbles (16 - 4).
      See Also:
    • BASE10

      private static final int BASE10
      The base 10 offset used to convert hexa characters to decimal.
      See Also:
    • LAST_ASCII

      private static final char LAST_ASCII
      The last 7bits ascii character.
      See Also:
    • FIRST_ASCII

      private static final char FIRST_ASCII
      The first printable 7bits ascii character.
      See Also:
  • Constructor Details

    • StringParser

      protected StringParser()
      Default constructor.
  • Method Details

    • buildRegex

      public static String buildRegex(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 String buildString(CharSequence str, boolean eatsep)
      Builds a string, handles escaping through '\' syntax.
      Parameters:
      str - the string to build from
      eatsep - whether the separator, the first character, should be considered
      Returns:
      the built string
    • buildString

      private static String buildString(CharSequence str, boolean eatsep, boolean esc)
      Builds a string, handles escaping through '\' syntax.
      Parameters:
      str - the string to build from
      eatsep - whether the separator, the first character, should be considered
      esc - whether escape characters are interpreted or escaped
      Returns:
      the built string
    • buildTemplate

      public static String buildTemplate(CharSequence str, boolean eatsep)
      Builds a template, does not escape characters.
      Parameters:
      str - the string to build from
      eatsep - whether the separator, the first character, should be considered
      Returns:
      the built string
    • escapeIdentifier

      public static String escapeIdentifier(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 String escapeString(String str, char delim)
      Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.
      Parameters:
      str - the string to escape
      delim - 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 into
      str - the origin
      begin - the relative offset in str to begin reading
      end - the relative offset in str to end reading
      sep - the separator, single or double quote, marking end of string
      esc - whether escape characters are interpreted or escaped
      Returns:
      the last character offset handled in origin
    • readString

      public static int readString(StringBuilder strb, CharSequence str, int index, char sep)
      Read the remainder of a string till a given separator, handles escaping through '\' syntax.
      Parameters:
      strb - the destination buffer to copy characters into
      str - the origin
      index - the offset into the origin
      sep - the separator, single or double quote, marking end of string
      Returns:
      the offset in origin
    • readUnicodeChar

      private static int readUnicodeChar(StringBuilder strb, CharSequence str, int begin)
      Reads a Unicode escape character.
      Parameters:
      strb - the builder to write the character to
      str - the sequence
      begin - the begin offset in sequence (after the '\\u')
      Returns:
      0 if char could not be read, 4 otherwise
    • unescapeIdentifier

      public static String unescapeIdentifier(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