Class WordParser


  • public final class WordParser
    extends java.lang.Object
    This "parser/scanner" holds onto the string version of the JPQL query that is parsed into a parsed tree. It uses a cursor that lets the current Expression object to parse its fragment of the query.

    Provisional API: This interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.

    Version:
    2.5.1
    Author:
    Pascal Filion
    Since:
    2.3
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  WordParser.WordType
      This enumeration determines the type of word that was scanned.
    • Constructor Summary

      Constructors 
      Constructor Description
      WordParser​(java.lang.CharSequence text)
      Creates a new WordParser.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char character()
      Retrieves the character at the current cursor position.
      char character​(int position)
      Retrieves the character at the given cursor position.
      boolean endsWith​(int endPosition, java.lang.String suffix)
      Determines whether the query ends with the given suffix and the end position is the end of the range for testing.
      boolean endsWithIgnoreCase​(int endPosition, java.lang.String suffix)
      Determines whether the query ends with the given suffix and the end position is the end of the range for testing.
      java.lang.String entireWord()
      Retrieves a word starting at the current position.
      java.lang.String entireWord​(int position)
      Retrieves a word starting at the given position.
      WordParser.WordType getWordType()
      Returns what the type of word word() returns.
      boolean isArithmeticSymbol​(char character)
      Determines whether the given character is an arithmetic symbol, which is one of the following: { '>', '<', '/', '*', '-', '+', '=', '{'} .
      boolean isDelimiter​(char character)
      Determines whether the given character is a delimiter.
      boolean isDigit​(char character)
      Determines whether the given character is a character that can be used in a number.
      boolean isTail()
      Determines whether the position of the cursor is at the end of the text.
      boolean isWordSeparator​(char character)
      Determines whether the given character is not considered to be part of a word (which is usually comprise of alphanumeric characters).
      int length()
      Returns the length of the string value.
      void moveBackward​(int position)
      Moves backward the position of the cursor by the given amount.
      void moveBackward​(java.lang.CharSequence word)
      Moves the position of the cursor by the length of the given word.
      java.lang.String moveForward​(int position)
      Moves forward the position of the cursor by the given amount.
      java.lang.String moveForward​(java.lang.CharSequence word)
      Moves the position of the cursor by the length of the given word.
      java.lang.String moveForwardIgnoreWhitespace​(java.lang.CharSequence word)
      Moves the position of the cursor by the length of the given word and ignore any different in whitespace count.
      java.lang.String numericLiteral()
      Retrieves the numeric literal that should be the current word to parse.
      java.lang.String partialWord()
      Retrieves a word before the current position of the cursor, which determines when the parsing stop.
      java.lang.String partialWord​(int position)
      Retrieves a word before the specified position, which determines when the parsing stop.
      int partialWordStartPosition​(int position)
      Finds the beginning of the word and the given position is within that word.
      int position()
      Returns the current position of the cursor.
      void setPosition​(int position)
      Manually sets the position of the cursor within the string.
      int skipLeadingWhitespace()
      Removes the whitespace that starts the given text.
      boolean startsWith​(char possibleCharacter)
      Determines whether the text starts with the given character.
      boolean startsWith​(java.lang.CharSequence prefix)
      Tests whether the query starts with the specified prefix from the current position.
      boolean startsWith​(java.lang.CharSequence prefix, int startIndex)
      Tests whether the substring of the query beginning at the specified index starts with the specified prefix.
      boolean startsWithArithmeticOperator()
      Determines whether the character at the current position is one of the arithmetic operators: { '+', '-', '*', '/' },
      java.lang.Boolean startsWithDigit()
      Determines if the text starts with a digit (true), an arithmetic term (false) or anything else (null).
      boolean startsWithIdentifier​(java.lang.CharSequence identifier)
      Determines whether the text at the current position start with the following identifier.
      boolean startsWithIdentifier​(java.lang.CharSequence identifier, int position)
      Determines whether the text at the current position start with the following identifier.
      boolean startsWithIgnoreCase​(char possibleCharacter)
      Determines whether the text starts with the given character.
      boolean startsWithIgnoreCase​(java.lang.CharSequence prefix)
      Tests if the string starts with the specified prefix.
      boolean startsWithIgnoreCase​(java.lang.CharSequence prefix, int offset)
      Tests if the string starts with the specified prefix.
      java.lang.String substring()
      Returns a substring that is within the current position of the cursor and the end of the text.
      java.lang.String substring​(int startIndex)
      Returns a substring that is within the given position and the end of the text.
      java.lang.String substring​(int startIndex, int endIndex)
      Returns a substring that is within the given positions.
      java.lang.String toString()
      int whitespaceCount()
      Calculates the number of whitespace that are in the query.
      int whitespaceCount​(int position)
      Calculates the number of whitespace that are in the query.
      java.lang.String word()
      Retrieves the first word starting at the current position.
      int wordEndPosition()
      Returns the position a word would end based on the current cursor position.
      int wordEndPosition​(int position)
      Returns the position a word would end based on the given start position.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • WordParser

        public WordParser​(java.lang.CharSequence text)
        Creates a new WordParser.
        Parameters:
        text - The string representation of the JPQL query
    • Method Detail

      • character

        public char character()
        Retrieves the character at the current cursor position.
        Returns:
        The character retrieved from the string at the current cursor position or '\0' if the position is beyond the end of the text
      • character

        public char character​(int position)
        Retrieves the character at the given cursor position.
        Parameters:
        position - The position of the character to return
        Returns:
        The character retrieved from the string at the given position or '\0' if the position is beyond the end of the text
      • endsWith

        public boolean endsWith​(int endPosition,
                                java.lang.String suffix)
        Determines whether the query ends with the given suffix and the end position is the end of the range for testing.
        Parameters:
        endPosition - The position where the check stops
        suffix - The suffix is the text that is used to match it with the substring within the text
        Returns:
        true if the character sequence represented by the argument is a suffix of the query; false otherwise
      • endsWithIgnoreCase

        public boolean endsWithIgnoreCase​(int endPosition,
                                          java.lang.String suffix)
        Determines whether the query ends with the given suffix and the end position is the end of the range for testing. The case of the character is ignored.
        Parameters:
        endPosition - The position where the check stops
        suffix - The suffix is the text that is used to match it with the substring within the text
        Returns:
        true if the character sequence represented by the argument is a suffix of the query; false otherwise
        Since:
        2.5
      • entireWord

        public java.lang.String entireWord()
        Retrieves a word starting at the current position. The text before and after the position will be part of the returned value.

        For instance, "SELECT AVG(e.age) FROM Employee e":

        • Position 3, result is "SELECT";
        • Position 6, result is "SELECT";
        • Position 7, result is an empty string.
        • Position 11, result is an empty string.
        • Position 13, result is "e.".
        Returns:
        The word in which the cursor is
      • entireWord

        public java.lang.String entireWord​(int position)
        Retrieves a word starting at the given position. The text before and after the position will be part of the returned value.

        For instance, "SELECT AVG(e.age) FROM Employee e":

        • Position 3, result is "SELECT";
        • Position 6, result is "SELECT";
        • Position 7, result is an empty string.
        • Position 11, result is an empty string.
        • Position 13, result is "e.".
        Parameters:
        position - The position where to retrieve the word
        Returns:
        The word in which the cursor is
      • getWordType

        public WordParser.WordType getWordType()
        Returns what the type of word word() returns.
        Returns:
        The category of the word returned by word()
        Since:
        2.4
      • isArithmeticSymbol

        public boolean isArithmeticSymbol​(char character)
        Determines whether the given character is an arithmetic symbol, which is one of the following: { '>', '<', '/', '*', '-', '+', '=', '{'} .
        Parameters:
        character - The character to test if it's a math symbol
        Returns:
        true if the given character is one of the valid math symbols; false otherwise
      • isDelimiter

        public boolean isDelimiter​(char character)
        Determines whether the given character is a delimiter. The delimiter are '(', ')' and ','.
        Parameters:
        character - The character to test
        Returns:
        true if the given character is a delimiter; false otherwise
      • isDigit

        public boolean isDigit​(char character)
        Determines whether the given character is a character that can be used in a number. This only includes the numeric characters [0, 9] and the period character. This method should only be used to determine if a word starts with a digit character.
        Parameters:
        character - The character to test if it's a digit
        Returns:
        true if the given character is a digit; false otherwise
      • isTail

        public boolean isTail()
        Determines whether the position of the cursor is at the end of the text.
        Returns:
        true if the position of the cursor is at the end of the text; false otherwise
      • isWordSeparator

        public boolean isWordSeparator​(char character)
        Determines whether the given character is not considered to be part of a word (which is usually comprise of alphanumeric characters).
        Parameters:
        character - The character used to determine if it should be part of a word or not
        Returns:
        true if the character can be part of a word; false if it is not an alphanumeric character, which usually means is a whitespace, a delimiter or an arithmetic symbol
        See Also:
        ExpressionTools#isWhiteSpace(character), isArithmeticSymbol(char), isDelimiter(char)
      • length

        public int length()
        Returns the length of the string value.
        Returns:
        The total count of characters
      • moveBackward

        public void moveBackward​(java.lang.CharSequence word)
        Moves the position of the cursor by the length of the given word.
        Parameters:
        word - The word used to determine how much to move the position forward
      • moveBackward

        public void moveBackward​(int position)
        Moves backward the position of the cursor by the given amount.
        Parameters:
        position - The amount to remove from the current position
      • moveForward

        public java.lang.String moveForward​(java.lang.CharSequence word)
        Moves the position of the cursor by the length of the given word.
        Parameters:
        word - The word used to determine how much to move the position forward
        Returns:
        The actual portion of the text that was skipped
      • moveForward

        public java.lang.String moveForward​(int position)
        Moves forward the position of the cursor by the given amount.
        Parameters:
        position - The amount to add to the current position
        Returns:
        The actual portion of the text that was skipped
      • moveForwardIgnoreWhitespace

        public java.lang.String moveForwardIgnoreWhitespace​(java.lang.CharSequence word)
        Moves the position of the cursor by the length of the given word and ignore any different in whitespace count. If the text has more than one whitespace and the given word usually has one, then only one will be part of the returned substring.
        Parameters:
        word - The word used to determine how much to move the position forward
        Returns:
        The actual portion of the text that was skipped
        Since:
        2.4.3
      • numericLiteral

        public java.lang.String numericLiteral()
        Retrieves the numeric literal that should be the current word to parse.
        Returns:
        The numeric literal value
      • partialWord

        public java.lang.String partialWord()
        Retrieves a word before the current position of the cursor, which determines when the parsing stop.

        For instance, "SELECT AVG(e.age) FROM Employee e":

        • Position 3, result is "SEL";
        • Position 6, result is "SELECT";
        • Position 7, result is an empty string.
        • Position 11, result is an empty string.
        • Position 13, result is "e.".
        Returns:
        The sub-string that is before the position
      • partialWord

        public java.lang.String partialWord​(int position)
        Retrieves a word before the specified position, which determines when the parsing stop.

        For instance, "SELECT AVG(e.age) FROM Employee e":

        • Position 3, result is "SEL";
        • Position 6, result is "SELECT";
        • Position 7, result is an empty string.
        • Position 11, result is an empty string.
        • Position 13, result is "e.".
        Parameters:
        position - The position of the cursor
        Returns:
        The sub-string that is before the position
      • partialWordStartPosition

        public int partialWordStartPosition​(int position)
        Finds the beginning of the word and the given position is within that word.

        For instance, "SELECT AVG(e.age) FROM Employee e":

        • Position 3, result is 0;
        • Position 8, result is 7;
        Parameters:
        position - The position from which the search ends
        Returns:
        The position, which is a smaller number or equal, than the given position
      • position

        public int position()
        Returns the current position of the cursor.
        Returns:
        The current position of the cursor
      • setPosition

        public void setPosition​(int position)
        Manually sets the position of the cursor within the string. If the position is a negative number, the position will be 0.
        Parameters:
        position - The new position of the cursor
      • skipLeadingWhitespace

        public int skipLeadingWhitespace()
        Removes the whitespace that starts the given text.
        Returns:
        The number of whitespace removed
      • startsWith

        public boolean startsWith​(char possibleCharacter)
        Determines whether the text starts with the given character. The case of the character is not ignored.
        Parameters:
        possibleCharacter - The possible character at the current position
        Returns:
        true if the text starts with the given character at the current position; false otherwise
      • startsWith

        public boolean startsWith​(java.lang.CharSequence prefix)
        Tests whether the query starts with the specified prefix from the current position.
        Parameters:
        prefix - The prefix
        Returns:
        true if the character sequence represented by the argument is a prefix of the text; false otherwise
      • startsWith

        public boolean startsWith​(java.lang.CharSequence prefix,
                                  int startIndex)
        Tests whether the substring of the query beginning at the specified index starts with the specified prefix.
        Parameters:
        prefix - The prefix
        startIndex - Where to begin looking in the query
        Returns:
        true if the character sequence represented by the argument is a prefix of the substring of this object starting at index startIndex; false otherwise
      • startsWithArithmeticOperator

        public boolean startsWithArithmeticOperator()
        Determines whether the character at the current position is one of the arithmetic operators: { '+', '-', '*', '/' },
        Returns:
        true if the character at the current position is an arithmetic operator; false otherwise
      • startsWithDigit

        public java.lang.Boolean startsWithDigit()
        Determines if the text starts with a digit (true), an arithmetic term (false) or anything else (null).
        Returns:
        true if the text starts with a digit (we'll assume it is a digit if the text starts with a digit or an arithmetic sign followed by a digit), false if it starts with an arithmetic term (we'll assume it is a digit followed by a non-digit character); otherwise returns null
      • startsWithIdentifier

        public boolean startsWithIdentifier​(java.lang.CharSequence identifier)
        Determines whether the text at the current position start with the following identifier.
        Parameters:
        identifier - The JPQL identifier to match with the text at the current position
        Returns:
        true if the text starts with the given text (case is ignored) and the cursor is at the end of the text or is following by a word separator character; false otherwise
      • startsWithIdentifier

        public boolean startsWithIdentifier​(java.lang.CharSequence identifier,
                                            int position)
        Determines whether the text at the current position start with the following identifier.
        Parameters:
        identifier - The JPQL identifier to match with the text at the current position
        position - The position to start matching the characters
        Returns:
        true if the text starts with the given text (case is ignored) and the cursor is at the end of the text or is following by a word separator character; false otherwise
      • startsWithIgnoreCase

        public boolean startsWithIgnoreCase​(char possibleCharacter)
        Determines whether the text starts with the given character. The case of the character is ignored.
        Parameters:
        possibleCharacter - The possible character at the current position
        Returns:
        true if the text starts with the given character at the current position; false otherwise
      • startsWithIgnoreCase

        public boolean startsWithIgnoreCase​(java.lang.CharSequence prefix)
        Tests if the string starts with the specified prefix. The case of the character is ignored.
        Parameters:
        prefix - The prefix to test against
        Returns:
        true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the Object.equals(Object) method
      • startsWithIgnoreCase

        public boolean startsWithIgnoreCase​(java.lang.CharSequence prefix,
                                            int offset)
        Tests if the string starts with the specified prefix. The case of the character is ignored.
        Parameters:
        prefix - The prefix to test against
        offset - Where to begin looking in this string
        Returns:
        true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise
      • substring

        public java.lang.String substring()
        Returns a substring that is within the current position of the cursor and the end of the text.
        Returns:
        The remain of the string starting at the current position
      • substring

        public java.lang.String substring​(int startIndex)
        Returns a substring that is within the given position and the end of the text.
        Parameters:
        startIndex - The beginning of the substring, inclusive
        Returns:
        The remain of the string starting at the given position
      • substring

        public java.lang.String substring​(int startIndex,
                                          int endIndex)
        Returns a substring that is within the given positions.
        Parameters:
        startIndex - The beginning of the substring, inclusive
        endIndex - The end of the substring, exclusive
        Returns:
        The remain of the string that is within the given positions
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • whitespaceCount

        public int whitespaceCount()
        Calculates the number of whitespace that are in the query. The check starts at the current position.
        Returns:
        The count of consecutive whitespace found from the current position
      • whitespaceCount

        public int whitespaceCount​(int position)
        Calculates the number of whitespace that are in the query. The check starts at the current position.
        Parameters:
        position - The position from where the scan starts
        Returns:
        The count of consecutive whitespace found from the given position
      • word

        public java.lang.String word()
        Retrieves the first word starting at the current position.
        Returns:
        The first word contained in the text, if none could be found, then an empty string is returned
      • wordEndPosition

        public int wordEndPosition()
        Returns the position a word would end based on the current cursor position. getWordType() can be used to determine the type of word that was scanned.
        Returns:
        The position where the current word ends
        See Also:
        WordParser.word(), WordParser.WordType
      • wordEndPosition

        public int wordEndPosition​(int position)
        Returns the position a word would end based on the given start position. getWordType() can be used to determine the type of word that was scanned.
        Parameters:
        position - The position to start scanning the text
        Returns:
        The position where the current word ends
        See Also:
        WordParser.word(), WordParser.WordType