Class MCRTextResolver
- Direct Known Subclasses:
MCRPropertiesResolver
This class parses and resolve strings which contains variables.
To add a variable call addVariable
.
The algorithm is optimized that each character is touched only once.
To resolve a string a valid syntax is required:
{}: Use curly brackets for variables or properties. For example "{var1}" or "{MCR.basedir}"
[]: Use squared brackets to define a condition. All data within squared brackets is only used if the internal variables are not null and not empty. For example "[hello {lastName}]" is only resolved if the value of "lastName" is not null and not empty. Otherwise the whole content in the squared brackets are ignored.
\: Use the escape character to use all predefined characters.
Sample:
"Lastname: {lastName}[, Firstname: {firstName}]"
- Author:
- Matthias Eichner
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
protected static class
protected static class
A condition is defined by squared brackets.protected static class
As escape character the backslashed is used.static enum
Defines how deep the text is resolved.protected static class
A term is a defined part in a text.protected static class
Simple class to hold terms and instantiate them.protected static class
A simple text, every character is added to the term (except its a special one).protected static class
A variable is surrounded by curly brackets. -
Field Summary
Modifier and TypeFieldDescriptionprotected MCRTextResolver.ResolveDepth
Defines how deep the text is resolved.protected boolean
Retains the text if a variable couldn't be resolved.protected MCRTextResolver.TermContainer
This map contains all variables that can be resolved. -
Constructor Summary
ConstructorDescriptionCreates a new text resolver with a map of variables.MCRTextResolver
(Map<String, String> variablesMap) Creates a new text resolver.MCRTextResolver
(Properties properties) -
Method Summary
Modifier and TypeMethodDescriptionaddVariable
(String name, String value) Adds a new variable to the resolver.boolean
containsVariable
(String name) Checks if a variable with the specified name exists.Returns the current resolve depth.protected MCRTextResolver.TermContainer
Returns the value of a variable.Returns aMap
of all variables.boolean
Checks if the text should be retained if a variable couldn't be resolved.void
void
mixin
(Properties properties) protected void
Creates the term list for the text resolver and adds the default terms.void
registerTerm
(Class<? extends MCRTextResolver.Term> termClass) Register a new term.removeVariable
(String name) Removes a variable from the resolver.This method resolves all variables in the text.void
setResolveDepth
(MCRTextResolver.ResolveDepth resolveDepth) Sets the resolve depth.void
setRetainText
(boolean retainText) Sets if the text should be retained if a variable couldn't be resolved.void
unregisterTerm
(Class<? extends MCRTextResolver.Term> termClass) Unregister a term.
-
Field Details
-
termContainer
-
variablesMap
This map contains all variables that can be resolved. -
retainText
protected boolean retainTextRetains the text if a variable couldn't be resolved. Example if {Variable} could not be resolved: true: "Hello {Variable}" -> "Hello {Variable}" false: "Hello "By default retainText is true
-
resolveDepth
Defines how deep the text is resolved.- Deep
- everything is resolved
- NoVariables
- the value of variables is not being resolved
-
tracker
-
-
Constructor Details
-
MCRTextResolver
public MCRTextResolver()Creates a new text resolver with a map of variables. -
MCRTextResolver
Creates a new text resolver. To add variables calladdVariable
, otherwise only MyCoRe property resolving is possible. -
MCRTextResolver
-
-
Method Details
-
registerDefaultTerms
protected void registerDefaultTerms() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationExceptionCreates the term list for the text resolver and adds the default terms. -
registerTerm
public void registerTerm(Class<? extends MCRTextResolver.Term> termClass) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException Register a new term. The resolver invokes the term via reflection.- Parameters:
termClass
- the term class to register.- Throws:
NoSuchMethodException
InvocationTargetException
IllegalAccessException
InstantiationException
-
unregisterTerm
public void unregisterTerm(Class<? extends MCRTextResolver.Term> termClass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException Unregister a term.- Parameters:
termClass
- this class is unregistered- Throws:
NoSuchMethodException
InvocationTargetException
InstantiationException
IllegalAccessException
-
getTermContainer
-
getTracker
-
mixin
-
mixin
-
setRetainText
public void setRetainText(boolean retainText) Sets if the text should be retained if a variable couldn't be resolved.Example:
true: "Hello {Variable}" -> "Hello {Variable}"
false: "Hello "By default retainText is true
-
isRetainText
public boolean isRetainText()Checks if the text should be retained if a variable couldn't be resolved.By default retainText is true
-
addVariable
Adds a new variable to the resolver. This overwrites a existing variable with the same name.- Parameters:
name
- name of the variablevalue
- value of the variable- Returns:
- the previous value of the specified name, or null if it did not have one
-
removeVariable
Removes a variable from the resolver. This method does nothing if no variable with the name exists.- Returns:
- the value of the removed variable, or null if no variable with the name exists
-
containsVariable
Checks if a variable with the specified name exists.- Returns:
- true if a variable exists, otherwise false
-
setResolveDepth
Sets the resolve depth.- Parameters:
resolveDepth
- defines how deep the text is resolved.
-
getResolveDepth
Returns the current resolve depth.- Returns:
- resolve depth enumeration
-
resolve
This method resolves all variables in the text. The syntax is described at the head of the class.- Parameters:
text
- the string where the variables have to be resolved- Returns:
- the resolved string
-
getValue
Returns the value of a variable.- Parameters:
varName
- the name of the variable- Returns:
- the value
-
getVariables
Returns aMap
of all variables.- Returns:
- a
Map
of all variables.
-