Class StringParser<P,I extends VariableDeclaration,R extends Resolver<I>>
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.expressions.StringParser<P,I,R>
-
- Type Parameters:
P- the parse result typeI- the actual variable declaration typeR- the actual resolver type
- Direct Known Subclasses:
StringReplacer,StringResolver
public abstract class StringParser<P,I extends VariableDeclaration,R extends Resolver<I>> extends java.lang.ObjectA 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 classStringParser.StateThe parser states.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Stack<InPlaceCommand<I>>commandStackprivate intcurStartprivate IStringParserFactory<I>factoryprivate intinnerBracketLevelprivate intposprivate Rresolverprivate java.lang.StringBuildertext
-
Constructor Summary
Constructors Modifier Constructor Description protectedStringParser(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 voidaddExpression(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 voidadvanceState(int curStart, int pos)Advances the state of the current in-place command.protected charcharAt(int pos)Returns the character of the internal text buffer at positionpos.protected voidclearStatementStack(java.util.List<Expression> expressions)Clears the statement stack and adds all expressions toexprs.private Expressionclose(int curStart, int pos)Closes the creation of the current in-place command.private PcompleteParsing(StringParser.State state)Completes the parsing at the end of the input string.protected java.lang.Stringconsume(java.lang.String text, char ch)Consumes all leadingchintext.protected intconsumeJavaIdentifierPart(java.lang.String string)Returns the last Java identifier part character instring.protected java.lang.StringconsumeWhitespaces(java.lang.String text)Consumes all leading whitespaces intext.protected ExpressioncreateConstantStringExpression(java.lang.String string)Creates a constant String expression.protected abstract PcreateParseResult()Creates the result of parsing a string.protected voiddelete(int start, int end)Deletes the given characters from the internal parse buffer.protected voiddeleteCharAt(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.EASyLoggergetLogger()Returns the logger.protected RgetResolver()Returns the resolver.protected abstract inthandleEndOfText(int start, int pos, StringParser.State state)Handles the very end of text at so far unhandled state.protected abstract inthandleExpression(int start, int pos)Handle an expression placeholder.protected ExpressionhandleInPlaceCommands(java.lang.String expressionString, int curStart, int pos)Handles in-place commands and pushes them onto the command stack.private java.lang.StringhandleInPlaceForStart(java.lang.String expressionString, int curStart, int pos)Handles/parses an in-place for-loop start.private java.lang.StringhandleInPlaceImport(java.lang.String expressionString, int curStart, int pos)Handles/parses an in-place import.private java.lang.StringhandleInPlaceVarDecl(java.lang.String expressionString, int curStart, int pos)Handles/parses an in-place variable declaration start.protected abstract voidhandleQuote(int pos)Called to handle a quote character atpos(beginning) of the internal parse buffer.protected abstract voidhandleTextEnd(int start, int pos)Handles the end of the text if there is no variable/expression.protected abstract inthandleVariable(int start, int pos)Handle a variable placeholder.protected abstract inthandleVariableStartExpression(int start, int pos)Handles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.EXPRESSION.protected abstract inthandleVariableStartVariable(int start, int pos)Handles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.VARIABLE.protected booleanisNonEmptyCommandStack()Returns whether the command stack exists and is not empty.protected Expressionjoin(Expression expr)Joins the results from#handleInPlaceCommands(String)with the current command stack.protected intlength()Returns the actual length of the internal parse buffer.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.Pparse()Parses for the variables intext.protected ExpressionparseExpression(java.lang.String expressionString)Parses an expression fromexpressionString.protected abstract ExpressionparseExpressionImpl(java.lang.String expressionString)Parses an expression fromexpressionString.protected voidpopFromCommandStack(java.util.List<Expression> expressions)Removes the top-level element from the command stack and appends its contents toexpressions.private voidpush(InPlaceCommand<I> cmd, int curStart, int pos)Pushes an in-place command into its parent on the command stack.protected java.lang.StringremovePrefix(java.lang.String text, java.lang.String prefix, boolean consumeFollowingWhitespaces)Removes the givenprefixfromtextif present.protected intreplace(int start, int end, java.lang.String value)Replaces text in the parse buffer and adjusts the position.private voidsetCurStart(int pos)Changes the current/next start position.protected intsetPos(int pos)Changes the current position.protected java.lang.Stringsubstring(int start, int end)Returns the substring from the internal text buffer at the given positions.protected java.lang.StringtoText()Turns the internal parse buffer into a string.protected voidwarnParsingIgnoring(java.lang.String detail, java.lang.Throwable th)Logs a parse warning message that thedetailis being ignored.
-
-
-
Field Detail
-
text
private java.lang.StringBuilder text
-
pos
private int pos
-
curStart
private int curStart
-
innerBracketLevel
private int innerBracketLevel
-
factory
private IStringParserFactory<I extends VariableDeclaration> factory
-
commandStack
private java.util.Stack<InPlaceCommand<I extends VariableDeclaration>> commandStack
-
-
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 usedfactory- 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 intext.- 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 analyzedcurStart- the start position of the current conceptpos- 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 stringcurStart- the start position of the current conceptpos- 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 stringcurStart- the start position of the current conceptpos- 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 stringcurStart- the start position of the current conceptpos- 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 aCompositeExpression, which leads to specific code formattings.- Parameters:
expr- the expression returned by#handleInPlaceCommands(String)- Returns:
expror the result from the command stack
-
parseExpression
protected Expression parseExpression(java.lang.String expressionString) throws VilException
Parses an expression fromexpressionString.- 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 fromexpressionString.- 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 conceptpos- 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 conceptpos- 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 toexprs.- 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 toexpressions.- 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 listexpressions- expressions collection to be modified
-
isNonEmptyCommandStack
protected boolean isNonEmptyCommandStack()
Returns whether the command stack exists and is not empty.- Returns:
truefor not empty,falseelse
-
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 pushedcurStart- the start position of the current conceptpos- 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 commandcurStart- the start position of the current conceptpos- 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 atpos.- 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)
Notifies that parsing an in-place command ended.- Parameters:
cmd- the current in-place commandexpr- the resulting expressioncurStart- the start position of the current conceptpos- the end position of parsing
-
consumeJavaIdentifierPart
protected int consumeJavaIdentifierPart(java.lang.String string)
Returns the last Java identifier part character instring.- Parameters:
string- the string to search for- Returns:
- the position,
0for none
-
removePrefix
protected java.lang.String removePrefix(java.lang.String text, java.lang.String prefix, boolean consumeFollowingWhitespaces)Removes the givenprefixfromtextif present. IfcutFollowingWhitespacesand whitespaces followprefixintextremove also the whitespaces.- Parameters:
text- the text to be manipulatedprefix- the prefix to be removedconsumeFollowingWhitespaces- iftrueremove also whitespaces occurring afterprefix, iffalseremove only theprefixif present- Returns:
- the modified string or
text
-
consumeWhitespaces
protected java.lang.String consumeWhitespaces(java.lang.String text)
Consumes all leading whitespaces intext.- 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 leadingchintext.- Parameters:
text- the text to consume the charactersch- 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
-
getLogger
protected EASyLoggerFactory.EASyLogger getLogger()
Returns the logger.- Returns:
- the logger
-
warnParsingIgnoring
protected void warnParsingIgnoring(java.lang.String detail, java.lang.Throwable th)Logs a parse warning message that thedetailis being ignored.- Parameters:
detail- the detailth- 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- ifstart<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- ifpos<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, inclusiveend- the end position to delete, exclusive- Throws:
java.lang.IndexOutOfBoundsException- ifstart<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 positionpos.- Parameters:
pos- the position- Returns:
- the character
- Throws:
java.lang.IndexOutOfBoundsException- ifpos<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 intextend- the end position for the replacement intext(inclusive)value- the replacement for the specified fragment- Returns:
- end, but adjusted to the replacement
-
handleQuote
protected abstract void handleQuote(int pos) throws VilExceptionCalled to handle a quote character atpos(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 VilExceptionHandles the end of the text if there is no variable/expression.- Parameters:
start- the start positionpos- the actual position (end of text)- Throws:
VilException- in case of evaluation problems
-
handleVariableStartExpression
protected abstract int handleVariableStartExpression(int start, int pos) throws VilExceptionHandles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.EXPRESSION.- Parameters:
start- 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 abstract int handleVariableStartVariable(int start, int pos) throws VilExceptionHandles the transition from stateStringParser.State.VARIABLE_STARTtoStringParser.State.VARIABLE.- Parameters:
start- 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 abstract int handleEndOfText(int start, int pos, StringParser.State state) throws VilExceptionHandles the very end of text at so far unhandled state.- Parameters:
start- 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
-
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 VilExceptionHandle a variable placeholder.startpoints to "$",posto the end position of the variable name.- Parameters:
start- 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:
parse()
-
handleExpression
protected abstract int handleExpression(int start, int pos) throws VilExceptionHandle an expression placeholder.startpoints to "${",posto "}". name.- Parameters:
start- 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:
parse()
-
-