/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.text; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.lang3.Validate; /** * Substitutes variables within a string by values. *
* This class takes a piece of text and substitutes all the variables within it. * The default definition of a variable is {@code ${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 {@code 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 {@link #setValueDelimiterMatcher(StrMatcher)}, * {@link #setValueDelimiter(char)} or {@link #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 {@code name} variable is {@code 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}} ** {@code StrSubstitutor} supports this recursive substitution in variable * names, but it has to be enabled explicitly by setting the * {@link #setEnableSubstitutionInVariables(boolean) enableSubstitutionInVariables} * property to true. *
This class is not thread safe.
* * @since 1.0 * @deprecated Deprecated as of 1.3, use {@link StringSubstitutor} instead. This class will be removed in 2.0. */ @Deprecated @SuppressWarnings("index") // class is deprecated public class StrSubstitutor { /** * Constant for the default escape character. */ public static final char DEFAULT_ESCAPE = '$'; /** * Constant for the default variable prefix. */ public static final StrMatcher DEFAULT_PREFIX = StrMatcher.stringMatcher("${"); /** * Constant for the default variable suffix. */ public static final StrMatcher DEFAULT_SUFFIX = StrMatcher.stringMatcher("}"); /** * Constant for the default value delimiter of a variable. */ public static final StrMatcher DEFAULT_VALUE_DELIMITER = StrMatcher.stringMatcher(":-"); /** * Replaces all the occurrences of variables in the given source object with * their matching values from the map. * * @param* The variable default value delimiter is the character or characters that delimit the * variable name and the variable default value. This delimiter is expressed in terms of a matcher * allowing advanced variable default value delimiter matches. *
** If it returns null, then the variable default value resolution is disabled. *
* * @return The variable default value delimiter matcher in use, may be null */ public StrMatcher getValueDelimiterMatcher() { return valueDelimiterMatcher; } /** * Gets the variable prefix matcher currently in use. ** The variable prefix is the character or characters that identify the * start of a variable. This prefix is expressed in terms of a matcher * allowing advanced prefix matches. *
* * @return The prefix matcher in use */ public StrMatcher getVariablePrefixMatcher() { return prefixMatcher; } /** * Gets the VariableResolver that is used to lookup variables. * * @return The VariableResolver */ public StrLookup> getVariableResolver() { return this.variableResolver; } /** * Gets the variable suffix matcher currently in use. ** The variable suffix is the character or characters that identify the * end of a variable. This suffix is expressed in terms of a matcher * allowing advanced suffix matches. *
* * @return The suffix matcher in use */ public StrMatcher getVariableSuffixMatcher() { return suffixMatcher; } /** * 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); ** yielding: *
* Hi Douglas ${surname} ** * @return The substitution in variable values flag * * @since 1.2 */ public boolean isDisableSubstitutionInValues() { return disableSubstitutionInValues; } /** * Returns a flag whether substitution is done in variable names. * * @return The substitution in variable names flag */ public boolean isEnableSubstitutionInVariables() { return enableSubstitutionInVariables; } /** * Returns the flag controlling whether escapes are preserved during * substitution. * * @return The preserve escape flag */ public boolean isPreserveEscapes() { return preserveEscapes; } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source array as a template. * The array is not altered by this method. * * @param source the character array to replace in, not altered, null returns null * @return The result of the replace operation */ public String replace(final char[] source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source.length).append(source); substitute(buf, 0, source.length); return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source array as a template. * The array is not altered by this method. *
* Only the specified portion of the array will be processed. * The rest of the array is not processed, and is not returned. *
* * @param source the character array to replace in, not altered, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return The result of the replace operation */ public String replace(final char[] source, final int offset, final int length) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); substitute(buf, 0, length); return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source as a template. * The source is not altered by this method. * * @param source the buffer to use as a template, not changed, null returns null * @return The result of the replace operation */ public String replace(final CharSequence source) { if (source == null) { return null; } return replace(source, 0, source.length()); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source as a template. * The source is not altered by this method. ** Only the specified portion of the buffer will be processed. * The rest of the buffer is not processed, and is not returned. *
* * @param source the buffer to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return The result of the replace operation */ public String replace(final CharSequence source, final int offset, final int length) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); substitute(buf, 0, length); return buf.toString(); } /** * Replaces all the occurrences of variables in the given source object with * their matching values from the resolver. The input source object is * converted to a string using {@code toString} and is not altered. * * @param source the source to replace in, null returns null * @return The result of the replace operation */ public String replace(final Object source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder().append(source); substitute(buf, 0, buf.length()); return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source builder as a template. * The builder is not altered by this method. * * @param source the builder to use as a template, not changed, null returns null * @return The result of the replace operation */ public String replace(final StrBuilder source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source.length()).append(source); substitute(buf, 0, buf.length()); return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source builder as a template. * The builder is not altered by this method. ** Only the specified portion of the builder will be processed. * The rest of the builder is not processed, and is not returned. *
* * @param source the builder to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return The result of the replace operation */ public String replace(final StrBuilder source, final int offset, final int length) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); substitute(buf, 0, length); return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source string as a template. * * @param source the string to replace in, null returns null * @return The result of the replace operation */ public String replace(final String source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source); if (!substitute(buf, 0, source.length())) { return source; } return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source string as a template. ** Only the specified portion of the string will be processed. * The rest of the string is not processed, and is not returned. *
* * @param source the string to replace in, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return The result of the replace operation */ public String replace(final String source, final int offset, final int length) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); if (!substitute(buf, 0, length)) { return source.substring(offset, offset + length); } return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source buffer as a template. * The buffer is not altered by this method. * * @param source the buffer to use as a template, not changed, null returns null * @return The result of the replace operation */ public String replace(final StringBuffer source) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(source.length()).append(source); substitute(buf, 0, buf.length()); return buf.toString(); } /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source buffer as a template. * The buffer is not altered by this method. ** Only the specified portion of the buffer will be processed. * The rest of the buffer is not processed, and is not returned. *
* * @param source the buffer to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return The result of the replace operation */ public String replace(final StringBuffer source, final int offset, final int length) { if (source == null) { return null; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); substitute(buf, 0, length); return buf.toString(); } /** * Replaces all the occurrences of variables within the given source * builder with their matching values from the resolver. * * @param source the builder to replace in, updated, null returns zero * @return true if altered */ public boolean replaceIn(final StrBuilder source) { if (source == null) { return false; } return substitute(source, 0, source.length()); } /** * Replaces all the occurrences of variables within the given source * builder with their matching values from the resolver. ** Only the specified portion of the builder will be processed. * The rest of the builder is not processed, but it is not deleted. *
* * @param source the builder to replace in, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ public boolean replaceIn(final StrBuilder source, final int offset, final int length) { if (source == null) { return false; } return substitute(source, offset, length); } /** * Replaces all the occurrences of variables within the given source buffer * with their matching values from the resolver. * The buffer is updated with the result. * * @param source the buffer to replace in, updated, null returns zero * @return true if altered */ public boolean replaceIn(final StringBuffer source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); } /** * Replaces all the occurrences of variables within the given source buffer * with their matching values from the resolver. * The buffer is updated with the result. ** Only the specified portion of the buffer will be processed. * The rest of the buffer is not processed, but it is not deleted. *
* * @param source the buffer to replace in, updated, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the buffer to be processed, must be valid * @return true if altered */ public boolean replaceIn(final StringBuffer source, final int offset, final int length) { if (source == null) { return false; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); if (!substitute(buf, 0, length)) { return false; } source.replace(offset, offset + length, buf.toString()); return true; } /** * Replaces all the occurrences of variables within the given source buffer * with their matching values from the resolver. * The buffer is updated with the result. * * @param source the buffer to replace in, updated, null returns zero * @return true if altered */ public boolean replaceIn(final StringBuilder source) { if (source == null) { return false; } return replaceIn(source, 0, source.length()); } /** * Replaces all the occurrences of variables within the given source builder * with their matching values from the resolver. * The builder is updated with the result. ** Only the specified portion of the buffer will be processed. * The rest of the buffer is not processed, but it is not deleted. *
* * @param source the buffer to replace in, updated, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the buffer to be processed, must be valid * @return true if altered */ public boolean replaceIn(final StringBuilder source, final int offset, final int length) { if (source == null) { return false; } final StrBuilder buf = new StrBuilder(length).append(source, offset, length); if (!substitute(buf, 0, length)) { return false; } source.replace(offset, offset + length, buf.toString()); return true; } /** * Internal method that resolves the value of a variable. ** Most users of this class do not need to call this method. This method is * called automatically by the substitution process. *
** Writers of subclasses can override this method if they need to alter * how each substitution occurs. The method is passed the variable's name * and must return the corresponding value. This implementation uses the * {@link #getVariableResolver()} with the variable's name as the key. *
* * @param variableName the name of the variable, not null * @param buf the buffer where the substitution is occurring, not null * @param startPos the start position of the variable including the prefix, valid * @param endPos the end position of the variable including the suffix, valid * @return The variable's value or null if the variable is unknown */ protected String resolveVariable(final String variableName, final StrBuilder buf, final int startPos, final int endPos) { final StrLookup> resolver = getVariableResolver(); if (resolver == null) { return null; } return resolver.lookup(variableName); } /** * Sets a flag whether substitution is done in variable values (recursive). * * @param disableSubstitutionInValues true if substitution in variable value are disabled * * @since 1.2 */ public void setDisableSubstitutionInValues(final boolean disableSubstitutionInValues) { this.disableSubstitutionInValues = disableSubstitutionInValues; } /** * Sets a flag whether substitution is done in variable names. If set to * true, the names of variables can contain other variables which are * processed first before the original variable is evaluated, e.g. * {@code ${jre-${java.version}}}. The default value is false. * * @param enableSubstitutionInVariables the new value of the flag */ public void setEnableSubstitutionInVariables( final boolean enableSubstitutionInVariables) { this.enableSubstitutionInVariables = enableSubstitutionInVariables; } /** * Sets the escape character. * If this character is placed before a variable reference in the source * text, this variable will be ignored. * * @param escapeCharacter the escape character (0 for disabling escaping) */ public void setEscapeChar(final char escapeCharacter) { this.escapeChar = escapeCharacter; } /** * Sets a flag controlling whether escapes are preserved during * substitution. If set to true, the escape character is retained * during substitution (e.g. {@code $${this-is-escaped}} remains * {@code $${this-is-escaped}}). If set to false, the escape * character is removed during substitution (e.g. * {@code $${this-is-escaped}} becomes * {@code ${this-is-escaped}}). The default value is false * * @param preserveEscapes true if escapes are to be preserved */ public void setPreserveEscapes(final boolean preserveEscapes) { this.preserveEscapes = preserveEscapes; } /** * Sets the variable default value delimiter to use. ** The variable default value delimiter is the character or characters that delimit the * variable name and the variable default value. This method allows a single character * variable default value delimiter to be easily set. *
* * @param valueDelimiter the variable default value delimiter character to use * @return this, to enable chaining */ public StrSubstitutor setValueDelimiter(final char valueDelimiter) { return setValueDelimiterMatcher(StrMatcher.charMatcher(valueDelimiter)); } /** * Sets the variable default value delimiter to use. ** The variable default value delimiter is the character or characters that delimit the * variable name and the variable default value. This method allows a string * variable default value delimiter to be easily set. *
** If the {@code valueDelimiter} is null or empty string, then the variable default * value resolution becomes disabled. *
* * @param valueDelimiter the variable default value delimiter string to use, may be null or empty * @return this, to enable chaining */ public StrSubstitutor setValueDelimiter(final String valueDelimiter) { if (valueDelimiter == null || valueDelimiter.isEmpty()) { setValueDelimiterMatcher(null); return this; } return setValueDelimiterMatcher(StrMatcher.stringMatcher(valueDelimiter)); } /** * Sets the variable default value delimiter matcher to use. ** The variable default value delimiter is the character or characters that delimit the * variable name and the variable default value. This delimiter is expressed in terms of a matcher * allowing advanced variable default value delimiter matches. *
** If the {@code valueDelimiterMatcher} is null, then the variable default value resolution * becomes disabled. *
* * @param valueDelimiterMatcher variable default value delimiter matcher to use, may be null * @return this, to enable chaining */ public StrSubstitutor setValueDelimiterMatcher(final StrMatcher valueDelimiterMatcher) { this.valueDelimiterMatcher = valueDelimiterMatcher; return this; } /** * Sets the variable prefix to use. ** The variable prefix is the character or characters that identify the * start of a variable. This method allows a single character prefix to * be easily set. *
* * @param prefix the prefix character to use * @return this, to enable chaining */ public StrSubstitutor setVariablePrefix(final char prefix) { return setVariablePrefixMatcher(StrMatcher.charMatcher(prefix)); } /** * Sets the variable prefix to use. ** The variable prefix is the character or characters that identify the * start of a variable. This method allows a string prefix to be easily set. *
* * @param prefix the prefix for variables, not null * @return this, to enable chaining * @throws IllegalArgumentException if the prefix is null */ public StrSubstitutor setVariablePrefix(final String prefix) { Validate.isTrue(prefix != null, "Variable prefix must not be null!"); return setVariablePrefixMatcher(StrMatcher.stringMatcher(prefix)); } /** * Sets the variable prefix matcher currently in use. ** The variable prefix is the character or characters that identify the * start of a variable. This prefix is expressed in terms of a matcher * allowing advanced prefix matches. *
* * @param prefixMatcher the prefix matcher to use, null ignored * @return this, to enable chaining * @throws IllegalArgumentException if the prefix matcher is null */ public StrSubstitutor setVariablePrefixMatcher(final StrMatcher prefixMatcher) { Validate.isTrue(prefixMatcher != null, "Variable prefix matcher must not be null!"); this.prefixMatcher = prefixMatcher; return this; } /** * Sets the VariableResolver that is used to lookup variables. * * @param variableResolver the VariableResolver */ public void setVariableResolver(final StrLookup> variableResolver) { this.variableResolver = variableResolver; } /** * Sets the variable suffix to use. ** The variable suffix is the character or characters that identify the * end of a variable. This method allows a single character suffix to * be easily set. *
* * @param suffix the suffix character to use * @return this, to enable chaining */ public StrSubstitutor setVariableSuffix(final char suffix) { return setVariableSuffixMatcher(StrMatcher.charMatcher(suffix)); } /** * Sets the variable suffix to use. ** The variable suffix is the character or characters that identify the * end of a variable. This method allows a string suffix to be easily set. *
* * @param suffix the suffix for variables, not null * @return this, to enable chaining * @throws IllegalArgumentException if the suffix is null */ public StrSubstitutor setVariableSuffix(final String suffix) { Validate.isTrue(suffix != null, "Variable suffix must not be null!"); return setVariableSuffixMatcher(StrMatcher.stringMatcher(suffix)); } /** * Sets the variable suffix matcher currently in use. ** The variable suffix is the character or characters that identify the * end of a variable. This suffix is expressed in terms of a matcher * allowing advanced suffix matches. *
* * @param suffixMatcher the suffix matcher to use, null ignored * @return this, to enable chaining * @throws IllegalArgumentException if the suffix matcher is null */ public StrSubstitutor setVariableSuffixMatcher(final StrMatcher suffixMatcher) { Validate.isTrue(suffixMatcher != null, "Variable suffix matcher must not be null!"); this.suffixMatcher = suffixMatcher; return this; } /** * Internal method that substitutes the variables. ** Most users of this class do not need to call this method. This method will * be called automatically by another (public) method. *
** Writers of subclasses can override this method if they need access to * the substitution process at the start or end. *
* * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ protected boolean substitute(final StrBuilder buf, final int offset, final int length) { return substitute(buf, offset, length, null) > 0; } /** * Recursive handler for multiple levels of interpolation. This is the main * interpolation method, which resolves the values of all variable references * contained in the passed in text. * * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid * @param length the length within the builder to be processed, must be valid * @param priorVariables the stack keeping track of the replaced variables, may be null * @return The length change that occurs, unless priorVariables is null when the int * represents a boolean flag as to whether any change occurred. */ private int substitute(final StrBuilder buf, final int offset, final int length, List