Deprecated
Deprecated as of 1.3, use StringSubstitutor instead. This class will be removed in 2.0.
This class takes a piece of text and substitutes all the variables within it.
The default definition of a variable is ${variableName}.
The prefix and suffix can be changed via constructors and set methods.
Variable values are typically resolved from a map, but could also be resolved from system properties, or by supplying a custom variable resolver.
The simplest example is to use this class to replace Java System properties. For example:
StrSubstitutor.replaceSystemProperties(
"You are running with java.version = ${java.version} and os.name = ${os.name}.");
Typical usage of this class follows the following pattern: First an instance is created
and initialized with the map that contains the values for the available variables.
If a prefix and/or suffix for variables should be used other than the default ones,
the appropriate settings can be performed. After that the replace()
method can be called passing in the source text for interpolation. In the returned
text all variable references (as long as their values are known) will be resolved.
The following example demonstrates this:
Map<String, String> valuesMap = new HashMap<>();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}.";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
yielding:
The quick brown fox jumped over the lazy dog.
Also, this class allows to set a default value for unresolved variables.
The default value for a variable can be appended to the variable name after the variable
default value delimiter. The default value of the variable default value delimiter is ':-',
as in bash and other *nix shells, as those are arguably where the default ${} delimiter set originated.
The variable default value delimiter can be manually set by calling setValueDelimiterMatcher(StrMatcher),
setValueDelimiter(char) or setValueDelimiter(String).
The following shows an example with variable default value settings:
Map<String, String> valuesMap = new HashMap<>();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}. ${undefined.number:-1234567890}.";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
yielding:
The quick brown fox jumped over the lazy dog. 1234567890.
In addition to this usage pattern there are some static convenience methods that cover the most common use cases. These methods can be used without the need of manually creating an instance. However if multiple replace operations are to be performed, creating and reusing an instance of this class will be more efficient.
Variable replacement works in a recursive way. Thus, if a variable value contains a variable then that variable will also be replaced. Cyclic replacements are detected and will cause an exception to be thrown.
Sometimes the interpolation's result must contain a variable prefix. As an example take the following source text:
The variable ${${name}} must be used.
Here only the variable's name referred to in the text should be replaced resulting
in the text (assuming that the value of the name variable is x):
The variable ${x} must be used.
To achieve this effect there are two possibilities: Either set a different prefix
and suffix for variables which do not conflict with the result text you want to
produce. The other possibility is to use the escape character, by default '$'.
If this character is placed before a variable reference, this reference is ignored
and won't be replaced. For example:
The variable $${${name}} must be used.
In some complex scenarios you might even want to perform substitution in the names of variables, for instance
${jre-${java.specification.version}}
StrSubstitutor supports this recursive substitution in variable
names, but it has to be enabled explicitly by setting the
enableSubstitutionInVariables
property to true.
This class is not thread safe.
| Modifier and Type | Field and Description |
|---|---|
| public static final char | DEFAULT_ESCAPE
Constant for the default escape character. |
| public static final StrMatcher | DEFAULT_PREFIX
References Deprecated
Constant for the default variable prefix.
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
|
| public static final StrMatcher | DEFAULT_SUFFIX
References Deprecated
Constant for the default variable suffix.
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
|
| public static final StrMatcher | DEFAULT_VALUE_DELIMITER
References Deprecated
Constant for the default value delimiter of a variable.
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
|
| private boolean | disableSubstitutionInValues
The flag whether substitution in variable values is disabled. |
| private boolean | enableSubstitutionInVariables
The flag whether substitution in variable names is enabled. |
| private char | escapeChar
Stores the escape character. |
| private StrMatcher | prefixMatcher
References Deprecated
Stores the variable prefix.
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
|
| private boolean | preserveEscapes
Whether escapes should be preserved. |
| private StrMatcher | suffixMatcher
References Deprecated
Stores the variable suffix.
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
|
| private StrMatcher | valueDelimiterMatcher
References Deprecated
Stores the default variable value delimiter.
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
|
| private StrLookup | variableResolver
References Deprecated
Variable resolution is delegated to an implementor of VariableResolver.
StrLookup is deprecated or references (maybe indirectly) at least one deprecated element.
|
| Access | Constructor and Description |
|---|---|
| public | StrSubstitutor()
Constructs a new instance with defaults for variable prefix and suffix and the escaping character. |
| public < the type of the values in the map. V> | StrSubstitutor(final Map<String, V>
the map with the variables' values, may be null. valueMap)Constructs a new instance and initializes it. |
| public < the type of the values in the map. V> | StrSubstitutor(final Map<String, V>
the map with the variables' values, may be null. valueMap, final String the prefix for variables, not null. prefix, final String the suffix for variables, not null. suffix)Constructs a new instance and initializes it. |
| public < the type of the values in the map. V> | StrSubstitutor(final Map<String, V>
the map with the variables' values, may be null. valueMap, final String the prefix for variables, not null. prefix, final String the suffix for variables, not null. suffix, final char the escape character. escape)Constructs a new instance and initializes it. |
| public < the type of the values in the map. V> | StrSubstitutor(final Map<String, V>
the map with the variables' values, may be null. valueMap, final String the prefix for variables, not null. prefix, final String the suffix for variables, not null. suffix, final char the escape character. escape, final String the variable default value delimiter, may be null. valueDelimiter)Constructs a new instance and initializes it. |
| public | StrSubstitutor(final StrLookup<?>
the variable resolver, may be null. variableResolver)
References Deprecated
StrLookup is deprecated or references (maybe indirectly) at least one deprecated element.
Constructs a new instance and initializes it. |
| public | StrSubstitutor(final StrLookup<?>
the variable resolver, may be null. variableResolver, final String the prefix for variables, not null. prefix, final String the suffix for variables, not null. suffix, final char the escape character. escape)
References Deprecated
StrLookup is deprecated or references (maybe indirectly) at least one deprecated element.
Constructs a new instance and initializes it. |
| public | StrSubstitutor(final StrLookup<?>
the variable resolver, may be null. variableResolver, final String the prefix for variables, not null. prefix, final String the suffix for variables, not null. suffix, final char the escape character. escape, final String the variable default value delimiter string, may be null. valueDelimiter)
References Deprecated
StrLookup is deprecated or references (maybe indirectly) at least one deprecated element.
Constructs a new instance and initializes it. |
| public | StrSubstitutor(final StrLookup<?>
the variable resolver, may be null. variableResolver, final StrMatcher the prefix for variables, not null. prefixMatcher, final StrMatcher the suffix for variables, not null. suffixMatcher, final char the escape character. escape)
References Deprecated
StrLookup and StrMatcher are deprecated or reference (maybe indirectly) at least one deprecated element.
Constructs a new instance and initializes it. |
| public | StrSubstitutor(final StrLookup<?>
the variable resolver, may be null. variableResolver, final StrMatcher the prefix for variables, not null. prefixMatcher, final StrMatcher the suffix for variables, not null. suffixMatcher, final char the escape character. escape, final StrMatcher the variable default value delimiter matcher, may be null. valueDelimiterMatcher)
References Deprecated
StrLookup and StrMatcher are deprecated or reference (maybe indirectly) at least one deprecated element.
Constructs a new instance and initializes it. |
| Modifier and Type | Method and Description |
|---|---|
| private void | checkCyclicSubstitution(final String
the variable name to check. varName, final List<String> the list of prior variables. priorVariables)Checks if the specified variable is already in the stack (list) of variables. |
| public char | Returns: The character used for escaping variable references.Returns the escape character. |
| public StrMatcher | Returns: The variable default value delimiter matcher in use, may be null.
References Deprecated
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
Gets the variable default value delimiter matcher currently in use. |
| public StrMatcher | Returns: The prefix matcher in use.
References Deprecated
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
Gets the variable prefix matcher currently in use. |
| public StrLookup | Returns: The VariableResolver.
References Deprecated
StrLookup is deprecated or references (maybe indirectly) at least one deprecated element.
Gets the VariableResolver that is used to lookup variables. |
| public StrMatcher | Returns: The suffix matcher in use.
References Deprecated
StrMatcher is deprecated or references (maybe indirectly) at least one deprecated element.
Gets the variable suffix matcher currently in use. |
| public boolean | Returns: The substitution in variable values flag.Returns a flag whether substitution is disabled in variable values.If set to true, the values of variables can contain other variables will not be processed and substituted original variable is evaluated, e.g.
Map<String, String> valuesMap = new HashMap<>();
valuesMap.put("name", "Douglas ${surname}");
valuesMap.put("surname", "Crockford");
String templateString = "Hi ${name}";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
|
| public boolean | Returns: The substitution in variable names flag.Returns a flag whether substitution is done in variable names. |
| public boolean | Returns: The preserve escape flag.Returns the flag controlling whether escapes are preserved during substitution. |
| public static < the type of the values in the map. V> String | Returns: The result of the replace operation.the source text containing the variables to substitute, null returns null. source, final Map<String, V> the map with the values, may be null. valueMap)Replaces all the occurrences of variables in the given source object with their matching values from the map. |
| public static < the type of the values in the map. V> String | Returns: The result of the replace operation.the source text containing the variables to substitute, null returns null. source, final Map<String, V> the map with the values, may be null. valueMap, final String the prefix of variables, not null. prefix, final String the suffix of variables, not null. suffix)Replaces all the occurrences of variables in the given source object with their matching values from the map. |
| public static String | Returns: The result of the replace operation.the source text containing the variables to substitute, null returns null. source, final Properties the properties with values, may be null. valueProperties)Replaces all the occurrences of variables in the given source object with their matching values from the properties. |
| public String | Returns: The result of the replace operation.the character array to replace in, not altered, null returns null. source)Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template. |
| public String | Returns: The result of the replace operation.the character array to replace in, not altered, null returns null. source, final int the start offset within the array, must be valid. offset, final int the length within the array to be processed, must be valid. length)Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template. |
| public String | Returns: The result of the replace operation.the buffer to use as a template, not changed, null returns null. source)Replaces all the occurrences of variables with their matching values from the resolver using the given source as a template. |
| public String | Returns: The result of the replace operation.the buffer to use as a template, not changed, null returns null. source, final int the start offset within the array, must be valid. offset, final int the length within the array to be processed, must be valid. length)Replaces all the occurrences of variables with their matching values from the resolver using the given source as a template. |
| public String | |
| public String | Returns: The result of the replace operation.the builder to use as a template, not changed, null returns null. source)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template. |
| public String | Returns: The result of the replace operation.the builder to use as a template, not changed, null returns null. source, final int the start offset within the array, must be valid. offset, final int the length within the array to be processed, must be valid. length)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template. |
| public String | |
| public String | Returns: The result of the replace operation.the string to replace in, null returns null. source, final int the start offset within the array, must be valid. offset, final int the length within the array to be processed, must be valid. length)Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template. |
| public String | Returns: The result of the replace operation.the buffer to use as a template, not changed, null returns null. source)Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template. |
| public String | Returns: The result of the replace operation.the buffer to use as a template, not changed, null returns null. source, final int the start offset within the array, must be valid. offset, final int the length within the array to be processed, must be valid. length)Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template. |
| public boolean | Returns: true if altered.the builder to replace in, updated, null returns zero. source)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. |
| public boolean | Returns: true if altered.the builder to replace in, null returns zero. source, final int the start offset within the array, must be valid. offset, final int the length within the builder to be processed, must be valid. length)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. |
| public boolean | Returns: true if altered.the buffer to replace in, updated, null returns zero. source)Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver. |
| public boolean | Returns: true if altered.the buffer to replace in, updated, null returns zero. source, final int the start offset within the array, must be valid. offset, final int the length within the buffer to be processed, must be valid. length)Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver. |
| public boolean | Returns: true if altered.the buffer to replace in, updated, null returns zero. source)Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver. |
| public boolean | Returns: true if altered.the buffer to replace in, updated, null returns zero. source, final int the start offset within the array, must be valid. offset, final int the length within the buffer to be processed, must be valid. length)Replaces all the occurrences of variables within the given source builder with their matching values from the resolver. |
| public static String | Returns: The result of the replace operation.the source text containing the variables to substitute, null returns null. source)Replaces all the occurrences of variables in the given source object with their matching values from the system properties. |
| protected String | Returns: The variable's value or null if the variable is unknown.the name of the variable, not null. variableName, final StrBuilder the buffer where the substitution is occurring, not null. buf, final int the start position of the variable including the prefix, valid. startPos, final int the end position of the variable including the suffix, valid. endPos)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Internal method that resolves the value of a variable. |
| public void | setDisableSubstitutionInValues(final boolean
true if substitution in variable value are disabled. disableSubstitutionInValues)Sets a flag whether substitution is done in variable values (recursive). |
| public void | setEnableSubstitutionInVariables(final boolean
the new value of the flag. enableSubstitutionInVariables)Sets a flag whether substitution is done in variable names. |
| public void | setEscapeChar(final char
the escape character (0 for disabling escaping) escapeCharacter)Sets the escape character. |
| public void | setPreserveEscapes(final boolean
true if escapes are to be preserved preserveEscapes)Sets a flag controlling whether escapes are preserved during substitution. |
| public StrSubstitutor | Returns: this instance.the variable default value delimiter character to use. valueDelimiter)
References Deprecated
StrSubstitutor is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the variable default value delimiter to use. |
| public StrSubstitutor | Returns: this instance.the variable default value delimiter string to use, may be null or empty valueDelimiter)
References Deprecated
StrSubstitutor is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the variable default value delimiter to use. |
| public StrSubstitutor | Returns: this instance.variable default value delimiter matcher to use, may be null valueDelimiterMatcher)
References Deprecated
StrMatcher and StrSubstitutor are deprecated or reference (maybe indirectly) at least one deprecated element.
Sets the variable default value delimiter matcher to use. |
| public StrSubstitutor | Returns: this instance.the prefix character to use. prefix)
References Deprecated
StrSubstitutor is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the variable prefix to use. |
| public StrSubstitutor | Returns: this instance.the prefix for variables, not null. prefix)
References Deprecated
StrSubstitutor is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the variable prefix to use. |
| public StrSubstitutor | Returns: this instance.the prefix matcher to use, null ignored. prefixMatcher)
References Deprecated
StrMatcher and StrSubstitutor are deprecated or reference (maybe indirectly) at least one deprecated element.
Sets the variable prefix matcher currently in use. |
| public void | setVariableResolver(final StrLookup<?>
the VariableResolver. variableResolver)
References Deprecated
StrLookup is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the VariableResolver that is used to lookup variables. |
| public StrSubstitutor | Returns: this instance.the suffix character to use. suffix)
References Deprecated
StrSubstitutor is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the variable suffix to use. |
| public StrSubstitutor | Returns: this instance.the suffix for variables, not null. suffix)
References Deprecated
StrSubstitutor is deprecated or references (maybe indirectly) at least one deprecated element.
Sets the variable suffix to use. |
| public StrSubstitutor | Returns: this instance.the suffix matcher to use, null ignored. suffixMatcher)
References Deprecated
StrMatcher and StrSubstitutor are deprecated or reference (maybe indirectly) at least one deprecated element.
Sets the variable suffix matcher currently in use. |
| protected boolean | Returns: true if altered.the string builder to substitute into, not null. buf, final int the start offset within the builder, must be valid. offset, final int the length within the builder to be processed, must be valid. length)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Internal method that substitutes the variables. |
| private int | Returns: The length change that occurs, unless priorVariables is null when the int represents a boolean flag as to whether any change occurred.the string builder to substitute into, not null. buf, final int the start offset within the builder, must be valid. offset, final int the length within the builder to be processed, must be valid. length, List<String> the stack keeping track of the replaced variables, may be null. priorVariables)
References Deprecated
StrBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Recursive handler for multiple levels of interpolation. |