Class StringParser<P,​I extends VariableDeclaration,​R extends Resolver<I>>

  • Type Parameters:
    P - the parse result type
    I - the actual variable declaration type
    R - the actual resolver type
    Direct Known Subclasses:
    StringReplacer, StringResolver

    public abstract class StringParser<P,​I extends VariableDeclaration,​R extends Resolver<I>>
    extends java.lang.Object
    A generic parser and replacer for values ($name) and expressions (${expression}) in strings. Provides only the parsing mechanism to be re-used by the VTL parser and VTL runtime file processor.
    Author:
    Holger Eichelberger
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  StringParser.State
      The parser states.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected StringParser​(java.lang.String text, R resolver, IStringParserFactory<I> factory)
      Creates a string parser instance for the given text.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addExpression​(Expression expr, java.util.List<Expression> expressions)
      Adds the expression to the expression list or to the active in-place command on the command stack (if present).
      private void advanceState​(int curStart, int pos)
      Advances the state of the current in-place command.
      protected char charAt​(int pos)
      Returns the character of the internal text buffer at position pos.
      protected void clearStatementStack​(java.util.List<Expression> expressions)
      Clears the statement stack and adds all expressions to exprs.
      private Expression close​(int curStart, int pos)
      Closes the creation of the current in-place command.
      private P completeParsing​(StringParser.State state)
      Completes the parsing at the end of the input string.
      protected java.lang.String consume​(java.lang.String text, char ch)
      Consumes all leading ch in text.
      protected int consumeJavaIdentifierPart​(java.lang.String string)
      Returns the last Java identifier part character in string.
      protected java.lang.String consumeWhitespaces​(java.lang.String text)
      Consumes all leading whitespaces in text.
      protected Expression createConstantStringExpression​(java.lang.String string)
      Creates a constant String expression.
      protected abstract P createParseResult()
      Creates the result of parsing a string.
      protected void delete​(int start, int end)
      Deletes the given characters from the internal parse buffer.
      protected void deleteCharAt​(int pos)
      Deletes the given character from the internal parse buffer.
      protected InPlaceCommand<I> getCurrentInPlaceCommand()
      Returns the current in-place command on the command stack.
      protected IStringParserFactory<I> getFactory()
      Returns the resolver factory.
      protected EASyLoggerFactory.EASyLogger getLogger()
      Returns the logger.
      protected R getResolver()
      Returns the resolver.
      protected abstract int handleEndOfText​(int start, int pos, StringParser.State state)
      Handles the very end of text at so far unhandled state.
      protected abstract int handleExpression​(int start, int pos)
      Handle an expression placeholder.
      protected Expression handleInPlaceCommands​(java.lang.String expressionString, int curStart, int pos)
      Handles in-place commands and pushes them onto the command stack.
      private java.lang.String handleInPlaceForStart​(java.lang.String expressionString, int curStart, int pos)
      Handles/parses an in-place for-loop start.
      private java.lang.String handleInPlaceImport​(java.lang.String expressionString, int curStart, int pos)
      Handles/parses an in-place import.
      private java.lang.String handleInPlaceVarDecl​(java.lang.String expressionString, int curStart, int pos)
      Handles/parses an in-place variable declaration start.
      protected abstract void handleQuote​(int pos)
      Called to handle a quote character at pos (beginning) of the internal parse buffer.
      protected abstract void handleTextEnd​(int start, int pos)
      Handles the end of the text if there is no variable/expression.
      protected abstract int handleVariable​(int start, int pos)
      Handle a variable placeholder.
      protected abstract int handleVariableStartExpression​(int start, int pos)
      protected abstract int handleVariableStartVariable​(int start, int pos)
      protected boolean isNonEmptyCommandStack()
      Returns whether the command stack exists and is not empty.
      protected Expression join​(Expression expr)
      Joins the results from #handleInPlaceCommands(String) with the current command stack.
      protected int length()
      Returns the actual length of the internal parse buffer.
      protected void notifyBracketsClosed​(InPlaceCommand<I> cmd, int pos)
      Called to notify that all bracket levels for an expression within an in-place command are closed at pos.
      protected void notifyEndInPlaceCommand​(InPlaceCommand<I> cmd, Expression expr, int curStart, int pos)
      Notifies that parsing an in-place command ended.
      protected void notifyStartInPlaceCommand​(InPlaceCommand<I> cmd, int curStart, int pos)
      Notifies about start parsing an in-place command.
      P parse()
      Parses for the variables in text.
      protected Expression parseExpression​(java.lang.String expressionString)
      Parses an expression from expressionString.
      protected abstract Expression parseExpressionImpl​(java.lang.String expressionString)
      Parses an expression from expressionString.
      protected void popFromCommandStack​(java.util.List<Expression> expressions)
      Removes the top-level element from the command stack and appends its contents to expressions.
      private void push​(InPlaceCommand<I> cmd, int curStart, int pos)
      Pushes an in-place command into its parent on the command stack.
      protected java.lang.String removePrefix​(java.lang.String text, java.lang.String prefix, boolean consumeFollowingWhitespaces)
      Removes the given prefix from text if present.
      protected int replace​(int start, int end, java.lang.String value)
      Replaces text in the parse buffer and adjusts the position.
      private void setCurStart​(int pos)
      Changes the current/next start position.
      protected int setPos​(int pos)
      Changes the current position.
      protected java.lang.String substring​(int start, int end)
      Returns the substring from the internal text buffer at the given positions.
      protected java.lang.String toText()
      Turns the internal parse buffer into a string.
      protected void warnParsingIgnoring​(java.lang.String detail, java.lang.Throwable th)
      Logs a parse warning message that the detail is being ignored.
      • Methods inherited from class java.lang.Object

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

      • StringParser

        protected StringParser​(java.lang.String text,
                               R resolver,
                               IStringParserFactory<I> factory)
        Creates a string parser instance for the given text.
        Parameters:
        text - the text to parse (may be null)
        resolver - the resolver to be used
        factory - a factory turning in-place commands into language-specific expressions (may be null, then in-place commands are not resolved but remain as string expressions)
    • Method Detail

      • getFactory

        protected IStringParserFactory<I> getFactory()
        Returns the resolver factory.
        Returns:
        the factory
      • getResolver

        protected R getResolver()
        Returns the resolver.
        Returns:
        the resolver
      • parse

        public P parse()
                throws VilException
        Parses for the variables in text.
        Returns:
        the modified text
        Throws:
        VilException - in case that something goes wrong while resolving variables
        See Also:
        #handleVariableStartExpression(int), #handleVariableStartVariable(int), #handleEndOfText(int, State), #handleExpression(int), #handleVariable(int), #createParseResult(State)
      • handleInPlaceCommands

        protected Expression handleInPlaceCommands​(java.lang.String expressionString,
                                                   int curStart,
                                                   int pos)
                                            throws VilException
        Handles in-place commands and pushes them onto the command stack.
        Parameters:
        expressionString - the expression string to be analyzed
        curStart - the start position of the current concept
        pos - the end position of parsing
        Returns:
        the corresponding expression
        Throws:
        VilException - in case of parsing problems
      • handleInPlaceForStart

        private java.lang.String handleInPlaceForStart​(java.lang.String expressionString,
                                                       int curStart,
                                                       int pos)
        Handles/parses an in-place for-loop start.
        Parameters:
        expressionString - the expression string
        curStart - the start position of the current concept
        pos - the end position of parsing
        Returns:
        the remaining expression string
        Throws:
        VilException - in case of parsing problems
      • handleInPlaceVarDecl

        private java.lang.String handleInPlaceVarDecl​(java.lang.String expressionString,
                                                      int curStart,
                                                      int pos)
        Handles/parses an in-place variable declaration start.
        Parameters:
        expressionString - the expression string
        curStart - the start position of the current concept
        pos - the end position of parsing
        Returns:
        the remaining expression string
        Throws:
        VilException - in case of parsing problems
      • handleInPlaceImport

        private java.lang.String handleInPlaceImport​(java.lang.String expressionString,
                                                     int curStart,
                                                     int pos)
        Handles/parses an in-place import.
        Parameters:
        expressionString - the expression string
        curStart - the start position of the current concept
        pos - the end position of parsing
        Returns:
        the remaining expression string
        Throws:
        VilException - in case of parsing problems
      • join

        protected Expression join​(Expression expr)
        Joins the results from #handleInPlaceCommands(String) with the current command stack. For temporary use only, as the result may be a CompositeExpression, which leads to specific code formattings.
        Parameters:
        expr - the expression returned by #handleInPlaceCommands(String)
        Returns:
        expr or the result from the command stack
      • parseExpression

        protected Expression parseExpression​(java.lang.String expressionString)
                                      throws VilException
        Parses an expression from expressionString.
        Parameters:
        expressionString - the string representation of the expression
        Returns:
        the parsed expression
        Throws:
        VilException - in case that parsing/creating the expression fails
        See Also:
        parseExpressionImpl(String)
      • parseExpressionImpl

        protected abstract Expression parseExpressionImpl​(java.lang.String expressionString)
                                                   throws VilException
        Parses an expression from expressionString.
        Parameters:
        expressionString - the string representation of the expression
        Returns:
        the parsed expression
        Throws:
        VilException - in case that parsing/creating the expression fails
      • createConstantStringExpression

        protected Expression createConstantStringExpression​(java.lang.String string)
                                                     throws VilException
        Creates a constant String expression.
        Parameters:
        string - the string to create the expression for
        Returns:
        the constant expression representing string
        Throws:
        VilException - in case of evaluation/creation problems
      • advanceState

        private void advanceState​(int curStart,
                                  int pos)
        Advances the state of the current in-place command.
        Parameters:
        curStart - the start position of the current concept
        pos - the end position of parsing
      • close

        private Expression close​(int curStart,
                                 int pos)
                          throws VilException
        Closes the creation of the current in-place command.
        Parameters:
        curStart - the start position of the current concept
        pos - the end position of parsing
        Returns:
        the related expression, null for optional/none
        Throws:
        VilException - in case that creation failed
      • clearStatementStack

        protected void clearStatementStack​(java.util.List<Expression> expressions)
        Clears the statement stack and adds all expressions to exprs.
        Parameters:
        expressions - expressions collection to be modified
      • popFromCommandStack

        protected void popFromCommandStack​(java.util.List<Expression> expressions)
        Removes the top-level element from the command stack and appends its contents to expressions.
        Parameters:
        expressions - expressions collection to be modified
      • addExpression

        protected void addExpression​(Expression expr,
                                     java.util.List<Expression> expressions)
        Adds the expression to the expression list or to the active in-place command on the command stack (if present).
        Parameters:
        expr - the expression that should be added to the list
        expressions - expressions collection to be modified
      • isNonEmptyCommandStack

        protected boolean isNonEmptyCommandStack()
        Returns whether the command stack exists and is not empty.
        Returns:
        true for not empty, false else
      • push

        private void push​(InPlaceCommand<I> cmd,
                          int curStart,
                          int pos)
        Pushes an in-place command into its parent on the command stack.
        Parameters:
        cmd - the command to be pushed
        curStart - the start position of the current concept
        pos - the end position of parsing
      • getCurrentInPlaceCommand

        protected InPlaceCommand<I> getCurrentInPlaceCommand()
        Returns the current in-place command on the command stack.
        Returns:
        the current in-place command or null
      • notifyStartInPlaceCommand

        protected void notifyStartInPlaceCommand​(InPlaceCommand<I> cmd,
                                                 int curStart,
                                                 int pos)
        Notifies about start parsing an in-place command. If a command consists of a middle part (if-else), then this function is called twice/multiple times for the same command.
        Parameters:
        cmd - the current in-place command
        curStart - the start position of the current concept
        pos - the end position of parsing
      • notifyBracketsClosed

        protected void notifyBracketsClosed​(InPlaceCommand<I> cmd,
                                            int pos)
        Called to notify that all bracket levels for an expression within an in-place command are closed at pos.
        Parameters:
        cmd - the current in-place command
        pos - the end position of parsing
      • notifyEndInPlaceCommand

        protected void notifyEndInPlaceCommand​(InPlaceCommand<I> cmd,
                                               Expression expr,
                                               int curStart,
                                               int pos)
        Notifies that parsing an in-place command ended.
        Parameters:
        cmd - the current in-place command
        expr - the resulting expression
        curStart - the start position of the current concept
        pos - the end position of parsing
      • consumeJavaIdentifierPart

        protected int consumeJavaIdentifierPart​(java.lang.String string)
        Returns the last Java identifier part character in string.
        Parameters:
        string - the string to search for
        Returns:
        the position, 0 for none
      • removePrefix

        protected java.lang.String removePrefix​(java.lang.String text,
                                                java.lang.String prefix,
                                                boolean consumeFollowingWhitespaces)
        Removes the given prefix from text if present. If cutFollowingWhitespaces and whitespaces follow prefix in text remove also the whitespaces.
        Parameters:
        text - the text to be manipulated
        prefix - the prefix to be removed
        consumeFollowingWhitespaces - if true remove also whitespaces occurring after prefix, if false remove only the prefix if present
        Returns:
        the modified string or text
      • consumeWhitespaces

        protected java.lang.String consumeWhitespaces​(java.lang.String text)
        Consumes all leading whitespaces in text.
        Parameters:
        text - the text to consume the whitespaces
        Returns:
        the text without leading whitespaces
      • consume

        protected java.lang.String consume​(java.lang.String text,
                                           char ch)
        Consumes all leading ch in text.
        Parameters:
        text - the text to consume the characters
        ch - the character to consume
        Returns:
        the text without leading ch
      • completeParsing

        private P completeParsing​(StringParser.State state)
                           throws VilException
        Completes the parsing at the end of the input string.
        Parameters:
        state - the actual state
        Returns:
        the parsing result
        Throws:
        VilException - in case of evaluation problems
      • warnParsingIgnoring

        protected void warnParsingIgnoring​(java.lang.String detail,
                                           java.lang.Throwable th)
        Logs a parse warning message that the detail is being ignored.
        Parameters:
        detail - the detail
        th - the reason for the warning
      • setCurStart

        private void setCurStart​(int pos)
        Changes the current/next start position.
        Parameters:
        pos - the position, ignored if negative
      • setPos

        protected int setPos​(int pos)
        Changes the current position. Handle with care.
        Parameters:
        pos - the new current position
        Returns:
        pos
      • toText

        protected java.lang.String toText()
        Turns the internal parse buffer into a string.
        Returns:
        the string contents of the parse buffer
      • substring

        protected java.lang.String substring​(int start,
                                             int end)
        Returns the substring from the internal text buffer at the given positions.
        Parameters:
        start - the start position (inclusive)
        end - the end position (exclusive)
        Returns:
        the character
        Throws:
        java.lang.IndexOutOfBoundsException - if start<0 || start>=#getTextLength() || end<0 || end>=#getTextLength()
      • deleteCharAt

        protected void deleteCharAt​(int pos)
        Deletes the given character from the internal parse buffer.
        Parameters:
        pos - the position to delete the character at
        Throws:
        java.lang.IndexOutOfBoundsException - if pos<0 || pos>=#getTextLength()
      • delete

        protected void delete​(int start,
                              int end)
        Deletes the given characters from the internal parse buffer.
        Parameters:
        start - the start position to delete, inclusive
        end - the end position to delete, exclusive
        Throws:
        java.lang.IndexOutOfBoundsException - if start<0 || start>=#getTextLength() || end<0 || end>=#getTextLength()
      • length

        protected int length()
        Returns the actual length of the internal parse buffer.
        Returns:
        the length of the parse buffer in characters.
      • charAt

        protected char charAt​(int pos)
        Returns the character of the internal text buffer at position pos.
        Parameters:
        pos - the position
        Returns:
        the character
        Throws:
        java.lang.IndexOutOfBoundsException - if pos<0 || pos>=#getTextLength()
      • replace

        protected int replace​(int start,
                              int end,
                              java.lang.String value)
        Replaces text in the parse buffer and adjusts the position.
        Parameters:
        start - the start position for the replacement in text
        end - the end position for the replacement in text (inclusive)
        value - the replacement for the specified fragment
        Returns:
        end, but adjusted to the replacement
      • handleQuote

        protected abstract void handleQuote​(int pos)
                                     throws VilException
        Called to handle a quote character at pos (beginning) of the internal parse buffer.
        Parameters:
        pos - the position within the parse buffer
        Throws:
        VilException - in case of evaluation problems
      • handleTextEnd

        protected abstract void handleTextEnd​(int start,
                                              int pos)
                                       throws VilException
        Handles the end of the text if there is no variable/expression.
        Parameters:
        start - the start position
        pos - the actual position (end of text)
        Throws:
        VilException - in case of evaluation problems
      • handleEndOfText

        protected abstract int handleEndOfText​(int start,
                                               int pos,
                                               StringParser.State state)
                                        throws VilException
        Handles the very end of text at so far unhandled state.
        Parameters:
        start - the start position
        pos - the current position
        state - the current state
        Returns:
        the next start position (typically pos, ignored if negative)
        Throws:
        VilException - in case of evaluation problems
      • createParseResult

        protected abstract P createParseResult()
                                        throws VilException
        Creates the result of parsing a string.
        Returns:
        the result
        Throws:
        VilException - in case of evaluation problems
        See Also:
        parse()
      • handleVariable

        protected abstract int handleVariable​(int start,
                                              int pos)
                                       throws VilException
        Handle a variable placeholder. start points to "$", pos to the end position of the variable name.
        Parameters:
        start - the start position
        pos - the actual/end position
        Returns:
        the next start position (typically pos, ignored if negative)
        Throws:
        VilException - in case of evaluation problems
        See Also:
        parse()
      • handleExpression

        protected abstract int handleExpression​(int start,
                                                int pos)
                                         throws VilException
        Handle an expression placeholder. start points to "${", pos to "}". name.
        Parameters:
        start - the start position
        pos - the actual/end position
        Returns:
        the next start position (typically pos, ignored if negative)
        Throws:
        VilException - in case of evaluation problems
        See Also:
        parse()