Class StringReplacer<I extends VariableDeclaration,​R extends Resolver<I>>

  • Type Parameters:
    I - the variable declaration type
    R - the resolver type

    public class StringReplacer<I extends VariableDeclaration,​R extends Resolver<I>>
    extends StringParser<java.lang.String,​I,​R>
    A parser and replacer for values ($name) and expressions (${expression}) in string values.
    Author:
    Holger Eichelberger
    • Constructor Detail

      • StringReplacer

        private StringReplacer​(java.lang.String text,
                               R resolver,
                               IExpressionParser<R> expressionParser,
                               IExpressionVisitor expressionEvaluator,
                               IStringParserFactory<I> factory)
        Creates a replacer instance.
        Parameters:
        text - the text to be analyzed
        resolver - the resolver instance
        expressionParser - an instance for parsing strings into expression (may be null but then expression replacement will not work)
        expressionEvaluator - the expression visitor for evaluating expressions (may be null but then expression replacement will not work)
        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

      • substitute

        public static <I extends VariableDeclaration,​R extends Resolver<I>> java.lang.String substitute​(java.lang.String text,
                                                                                                              R resolver,
                                                                                                              IExpressionParser<R> expressionParser,
                                                                                                              IExpressionVisitor expressionEvaluator,
                                                                                                              IStringParserFactory<I> factory)
                                                                                                       throws VilException
        Substitutes the variable and expression placeholders in text.
        Type Parameters:
        I - the variable declaration type
        R - the resolver type
        Parameters:
        text - the text to be analyzed
        resolver - the resolver instance containing the variables
        expressionParser - an instance for parsing strings into expression (may be null but then expression replacement will not work)
        expressionEvaluator - the expression visitor for evaluating expressions (may be null but then expression replacement will not work)
        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)
        Returns:
        the modified text
        Throws:
        VilException - in case that something goes wrong while resolving variables
      • handleTextEnd

        protected void handleTextEnd​(int curStart,
                                     int pos)
                              throws VilException
        Description copied from class: StringParser
        Handles the end of the text if there is no variable/expression.
        Specified by:
        handleTextEnd in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        Parameters:
        curStart - the start position
        pos - the actual position (end of text)
        Throws:
        VilException - in case of evaluation problems
      • handleEndOfText

        protected int handleEndOfText​(int curStart,
                                      int pos,
                                      StringParser.State state)
                               throws VilException
        Description copied from class: StringParser
        Handles the very end of text at so far unhandled state.
        Specified by:
        handleEndOfText in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        Parameters:
        curStart - 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
      • handleConstant

        private void handleConstant​(int curStart,
                                    int position,
                                    int posAdvance)
                             throws VilException
        Handle constant placeholder from curStart until position.
        Parameters:
        curStart - the current start position
        position - the actual position in the text.
        posAdvance - where the next parsing position shall be with respect to position
        Throws:
        VilException - in case of evaluation problems
      • handleVariable

        protected int handleVariable​(int curStart,
                                     int pos)
                              throws VilException
        Description copied from class: StringParser
        Handle a variable placeholder. start points to "$", pos to the end position of the variable name.
        Specified by:
        handleVariable in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        Parameters:
        curStart - 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:
        StringParser.parse()
      • handleExpression

        protected int handleExpression​(int curStart,
                                       int pos)
                                throws VilException
        Description copied from class: StringParser
        Handle an expression placeholder. start points to "${", pos to "}". name.
        Specified by:
        handleExpression in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        Parameters:
        curStart - 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:
        StringParser.parse()
      • checkForEmptyLine

        private int checkForEmptyLine​(java.lang.String value,
                                      Expression expr,
                                      int pos)
        Checks and removes empty lines depending on value and Expression.replaceEmptyLine().
        Parameters:
        value - the actual value (must not be null)
        expr - the actual expression (must not be null
        pos - the actual parsing position
        Returns:
        the new parsing position
      • notifyStartInPlaceCommand

        protected void notifyStartInPlaceCommand​(InPlaceCommand<I> cmd,
                                                 int curStart,
                                                 int pos)
        Description copied from class: StringParser
        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.
        Overrides:
        notifyStartInPlaceCommand in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        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)
        Description copied from class: StringParser
        Called to notify that all bracket levels for an expression within an in-place command are closed at pos.
        Overrides:
        notifyBracketsClosed in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        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)
        Description copied from class: StringParser
        Notifies that parsing an in-place command ended.
        Overrides:
        notifyEndInPlaceCommand in class StringParser<java.lang.String,​I extends VariableDeclaration,​R extends Resolver<I>>
        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