Class StringParser

  • Direct Known Subclasses:
    JexlParser

    public class StringParser
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      private static int BASE10
      The base 10 offset used to convert hexa characters to decimal.
      private static char FIRST_ASCII
      The first printable 7bits ascii character.
      private static char LAST_ASCII
      The last 7bits ascii character.
      private static int SHIFT
      Initial shift value for composing a Unicode char from 4 nibbles (16 - 4).
      private static int UCHAR_LEN
      The length of an escaped unicode sequence.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected StringParser()
      Default constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String buildRegex​(java.lang.CharSequence str)
      Builds a regex pattern string, handles escaping '/' through '\/' syntax.
      static java.lang.String buildString​(java.lang.CharSequence str, boolean eatsep)
      Builds a string, handles escaping through '\' syntax.
      private static java.lang.String buildString​(java.lang.CharSequence str, boolean eatsep, boolean esc)
      Builds a string, handles escaping through '\' syntax.
      static java.lang.String buildTemplate​(java.lang.CharSequence str, boolean eatsep)
      Builds a template, does not escape characters.
      static java.lang.String escapeIdentifier​(java.lang.String str)
      Adds a escape char ('\') where needed in a string form of an ide
      static java.lang.String escapeString​(java.lang.String str, char delim)
      Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.
      private static int read​(java.lang.StringBuilder strb, java.lang.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 int readString​(java.lang.StringBuilder strb, java.lang.CharSequence str, int index, char sep)
      Read the remainder of a string till a given separator, handles escaping through '\' syntax.
      private static int readUnicodeChar​(java.lang.StringBuilder strb, java.lang.CharSequence str, int begin)
      Reads a Unicode escape character.
      static java.lang.String unescapeIdentifier​(java.lang.String str)
      Remove escape char ('\') from an identifier.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Constructor Detail

      • StringParser

        protected StringParser()
        Default constructor.
    • 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 from
        eatsep - 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 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 java.lang.String buildTemplate​(java.lang.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 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.String str,
                                                    char delim)
        Escapes a String representation, expand non-ASCII characters as Unicode escape sequence.
        Parameters:
        delim - the delimiter character
        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)
        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​(java.lang.StringBuilder strb,
                                     java.lang.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​(java.lang.StringBuilder strb,
                                           java.lang.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 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