Class StringReplacer<I extends VariableDeclaration,R extends Resolver<I>>
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.expressions.StringParser<java.lang.String,I,R>
-
- net.ssehub.easy.instantiation.core.model.expressions.StringReplacer<I,R>
-
- Type Parameters:
I- the variable declaration typeR- 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classStringReplacer.PositionsStores in-place command positions for replacing.-
Nested classes/interfaces inherited from class net.ssehub.easy.instantiation.core.model.expressions.StringParser
StringParser.State
-
-
Field Summary
Fields Modifier and Type Field Description private IRuntimeEnvironmentenvironmentprivate IExpressionVisitorexpressionEvaluatorprivate IExpressionParser<R>expressionParserprivate java.util.Map<java.lang.Object,StringReplacer.Positions>inPlacePositionsprivate IExpressionVisitorrecursiveReplacer
-
Constructor Summary
Constructors Modifier Constructor Description privateStringReplacer(java.lang.String text, R resolver, IExpressionParser<R> expressionParser, IExpressionVisitor expressionEvaluator, IStringParserFactory<I> factory)Creates a replacer instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private intcheckForEmptyLine(java.lang.String value, Expression expr, int pos)Checks and removes empty lines depending onvalueandExpression.replaceEmptyLine().protected java.lang.StringcreateParseResult()Creates the result of parsing a string.private voidhandleConstant(int curStart, int position, int posAdvance)Handle constant placeholder fromcurStartuntilposition.protected inthandleEndOfText(int curStart, int pos, StringParser.State state)Handles the very end of text at so far unhandled state.protected inthandleExpression(int curStart, int pos)Handle an expression placeholder.protected voidhandleQuote(int pos)Called to handle a quote character atpos(beginning) of the internal parse buffer.protected voidhandleTextEnd(int curStart, int pos)Handles the end of the text if there is no variable/expression.protected inthandleVariable(int curStart, int pos)Handle a variable placeholder.protected inthandleVariableStartExpression(int curStart, int pos)Handles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.EXPRESSION.protected inthandleVariableStartVariable(int curStart, int pos)Handles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.VARIABLE.protected voidnotifyBracketsClosed(InPlaceCommand<I> cmd, int pos)Called to notify that all bracket levels for an expression within an in-place command are closed atpos.protected voidnotifyEndInPlaceCommand(InPlaceCommand<I> cmd, Expression expr, int curStart, int pos)Notifies that parsing an in-place command ended.protected voidnotifyStartInPlaceCommand(InPlaceCommand<I> cmd, int curStart, int pos)Notifies about start parsing an in-place command.protected ExpressionparseExpressionImpl(java.lang.String expressionString)Parses an expression fromexpressionString.private Expressionresolve(VarModelIdentifierExpression ex)Resolves aVarModelIdentifierExpressionbased on the actual runtime environment and, if resolved, replaces it by a constant.static <I extends VariableDeclaration,R extends Resolver<I>>
java.lang.Stringsubstitute(java.lang.String text, R resolver, IExpressionParser<R> expressionParser, IExpressionVisitor expressionEvaluator, IStringParserFactory<I> factory)Substitutes the variable and expression placeholders intext.-
Methods inherited from class net.ssehub.easy.instantiation.core.model.expressions.StringParser
addExpression, charAt, clearStatementStack, consume, consumeJavaIdentifierPart, consumeWhitespaces, createConstantStringExpression, delete, deleteCharAt, getCurrentInPlaceCommand, getFactory, getLogger, getResolver, handleInPlaceCommands, isNonEmptyCommandStack, join, length, parse, parseExpression, popFromCommandStack, removePrefix, replace, setPos, substring, toText, warnParsingIgnoring
-
-
-
-
Field Detail
-
environment
private IRuntimeEnvironment environment
-
expressionEvaluator
private IExpressionVisitor expressionEvaluator
-
expressionParser
private IExpressionParser<R extends Resolver<I>> expressionParser
-
inPlacePositions
private java.util.Map<java.lang.Object,StringReplacer.Positions> inPlacePositions
-
recursiveReplacer
private IExpressionVisitor recursiveReplacer
-
-
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 analyzedresolver- the resolver instanceexpressionParser- 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 intext.- Type Parameters:
I- the variable declaration typeR- the resolver type- Parameters:
text- the text to be analyzedresolver- the resolver instance containing the variablesexpressionParser- 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
-
createParseResult
protected java.lang.String createParseResult() throws VilExceptionDescription copied from class:StringParserCreates the result of parsing a string.- Specified by:
createParseResultin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Returns:
- the result
- Throws:
VilException- in case of evaluation problems- See Also:
StringParser.parse()
-
handleQuote
protected void handleQuote(int pos) throws VilExceptionDescription copied from class:StringParserCalled to handle a quote character atpos(beginning) of the internal parse buffer.- Specified by:
handleQuotein classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
pos- the position within the parse buffer- Throws:
VilException- in case of evaluation problems
-
handleTextEnd
protected void handleTextEnd(int curStart, int pos) throws VilExceptionDescription copied from class:StringParserHandles the end of the text if there is no variable/expression.- Specified by:
handleTextEndin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
curStart- the start positionpos- the actual position (end of text)- Throws:
VilException- in case of evaluation problems
-
handleVariableStartExpression
protected int handleVariableStartExpression(int curStart, int pos) throws VilExceptionDescription copied from class:StringParserHandles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.EXPRESSION.- Specified by:
handleVariableStartExpressionin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
curStart- the start positionpos- the actual position- Returns:
- the next start position (typically
pos, ignored if negative) - Throws:
VilException- in case of evaluation problems
-
handleVariableStartVariable
protected int handleVariableStartVariable(int curStart, int pos) throws VilExceptionDescription copied from class:StringParserHandles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.VARIABLE.- Specified by:
handleVariableStartVariablein classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
curStart- the start positionpos- the actual position- Returns:
- the next start position (typically
pos, ignored if negative) - Throws:
VilException- in case of evaluation problems
-
handleEndOfText
protected int handleEndOfText(int curStart, int pos, StringParser.State state) throws VilExceptionDescription copied from class:StringParserHandles the very end of text at so far unhandled state.- Specified by:
handleEndOfTextin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
curStart- the start positionpos- the current positionstate- 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 VilExceptionHandle constant placeholder fromcurStartuntilposition.- Parameters:
curStart- the current start positionposition- the actual position in the text.posAdvance- where the next parsing position shall be with respect toposition- Throws:
VilException- in case of evaluation problems
-
handleVariable
protected int handleVariable(int curStart, int pos) throws VilExceptionDescription copied from class:StringParserHandle a variable placeholder.startpoints to "$",posto the end position of the variable name.- Specified by:
handleVariablein classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
curStart- the start positionpos- 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 VilExceptionDescription copied from class:StringParserHandle an expression placeholder.startpoints to "${",posto "}". name.- Specified by:
handleExpressionin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
curStart- the start positionpos- 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 onvalueandExpression.replaceEmptyLine().- Parameters:
value- the actual value (must not be null)expr- the actual expression (must not be nullpos- the actual parsing position- Returns:
- the new parsing position
-
notifyStartInPlaceCommand
protected void notifyStartInPlaceCommand(InPlaceCommand<I> cmd, int curStart, int pos)
Description copied from class:StringParserNotifies 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:
notifyStartInPlaceCommandin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
cmd- the current in-place commandcurStart- the start position of the current conceptpos- the end position of parsing
-
notifyBracketsClosed
protected void notifyBracketsClosed(InPlaceCommand<I> cmd, int pos)
Description copied from class:StringParserCalled to notify that all bracket levels for an expression within an in-place command are closed atpos.- Overrides:
notifyBracketsClosedin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
cmd- the current in-place commandpos- the end position of parsing
-
notifyEndInPlaceCommand
protected void notifyEndInPlaceCommand(InPlaceCommand<I> cmd, Expression expr, int curStart, int pos)
Description copied from class:StringParserNotifies that parsing an in-place command ended.- Overrides:
notifyEndInPlaceCommandin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
cmd- the current in-place commandexpr- the resulting expressioncurStart- the start position of the current conceptpos- the end position of parsing
-
parseExpressionImpl
protected Expression parseExpressionImpl(java.lang.String expressionString) throws VilException
Description copied from class:StringParserParses an expression fromexpressionString.- Specified by:
parseExpressionImplin classStringParser<java.lang.String,I extends VariableDeclaration,R extends Resolver<I>>- Parameters:
expressionString- the string representation of the expression- Returns:
- the parsed expression
- Throws:
VilException- in case that parsing/creating the expression fails
-
resolve
private Expression resolve(VarModelIdentifierExpression ex)
Resolves aVarModelIdentifierExpressionbased on the actual runtime environment and, if resolved, replaces it by a constant.- Parameters:
ex- the expression to resolve- Returns:
- the resolved expression, null if not possible to resolve
- Throws:
VilException- in case of resolution problems
-
-