Package de.aristaflow.adept2.util
Class VariableReplacer
java.lang.Object
de.aristaflow.adept2.util.VariableReplacer
Tool class to find and replace variables of the form
${variable}.
Example code:
VariableReplacer varrep = new VariableReplacer("a ${var} string");
while (varrep.findNext())
{
...
String var = varrep.getVariable();
...
varrep.replaceWith("short");
...
}
varrep.getResult()
- Author:
- Patrick Schmidt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Patternthe regular expression pattern used to find the variables -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanfindNext()Finds the next variable.Returns the string after replacing the variables.Returns the current variable name.static StringReplaces all variables with the values provided in the map.static StringReplaces all variables with the values provided in the map.voidreplaceWith(String value) Replace the current variable with the given value.
-
Field Details
-
VAR_PATTERN
the regular expression pattern used to find the variables
-
-
Constructor Details
-
VariableReplacer
Constructs a newVariableReplacer.- Parameters:
string- the string to be parsed
-
-
Method Details
-
findNext
public boolean findNext()Finds the next variable.- Returns:
- if a next variable was found
-
getVariable
Returns the current variable name. May be called afterfindNext()has returnedtrue.- Returns:
- the current variable name
-
replaceWith
Replace the current variable with the given value. Calling this method is optional, i.e. not every variable must be replaced.- Parameters:
value- the value to replace the current variable with
-
getResult
Returns the string after replacing the variables.- Returns:
- the string after replacing the variables
-
replace
Replaces all variables with the values provided in the map. Missing values will be ignored.- Parameters:
string- the string containing the variablesvalues- the values to replace the variables with- Returns:
- the result of the replacement
-
replace
Replaces all variables with the values provided in the map. Missing values can optionally be ignored or lead to an exception.- Parameters:
string- the string containing the variablesvalues- the values to replace the variables withexceptionOnMissing- whether a missing value for a variable should lead to an exception- Returns:
- the result of the replacement
- Throws:
MissingResourceException- ifexceptionOnMissingistrueand the value for an encountered variable is missing from the map
-