Class StrSubstitutor

java.lang.Object
org.apache.commons.text.StrSubstitutor

@Deprecated public class StrSubstitutor extends Object
Deprecated.
Deprecated as of 1.3, use StringSubstitutor instead. This class will be removed in 2.0.
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 ${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.

Since:
1.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
    Deprecated.
    Constant for the default escape character.
    static final StrMatcher
    Deprecated.
    Constant for the default variable prefix.
    static final StrMatcher
    Deprecated.
    Constant for the default variable suffix.
    static final StrMatcher
    Deprecated.
    Constant for the default value delimiter of a variable.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Deprecated.
    Constructs a new instance with defaults for variable prefix and suffix and the escaping character.
    StrSubstitutor(Map<String,V> valueMap)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(Map<String,V> valueMap, String prefix, String suffix)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(Map<String,V> valueMap, String prefix, String suffix, char escape)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(Map<String,V> valueMap, String prefix, String suffix, char escape, String valueDelimiter)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(StrLookup<?> variableResolver)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(StrLookup<?> variableResolver, String prefix, String suffix, char escape)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(StrLookup<?> variableResolver, String prefix, String suffix, char escape, String valueDelimiter)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(StrLookup<?> variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape)
    Deprecated.
    Constructs a new instance and initializes it.
    StrSubstitutor(StrLookup<?> variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape, StrMatcher valueDelimiterMatcher)
    Deprecated.
    Constructs a new instance and initializes it.
  • Method Summary

    Modifier and Type
    Method
    Description
    char
    Deprecated.
    Returns the escape character.
    Deprecated.
    Gets the variable default value delimiter matcher currently in use.
    Deprecated.
    Gets the variable prefix matcher currently in use.
    Deprecated.
    Gets the VariableResolver that is used to lookup variables.
    Deprecated.
    Gets the variable suffix matcher currently in use.
    boolean
    Deprecated.
    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.
    boolean
    Deprecated.
    Returns a flag whether substitution is done in variable names.
    boolean
    Deprecated.
    Returns the flag controlling whether escapes are preserved during substitution.
    replace(char[] source)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template.
    replace(char[] source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source array as a template.
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source as a template.
    replace(CharSequence source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source as a template.
    replace(Object source)
    Deprecated.
    Replaces all the occurrences of variables in the given source object with their matching values from the resolver.
    static <V> String
    replace(Object source, Map<String,V> valueMap)
    Deprecated.
    Replaces all the occurrences of variables in the given source object with their matching values from the map.
    static <V> String
    replace(Object source, Map<String,V> valueMap, String prefix, String suffix)
    Deprecated.
    Replaces all the occurrences of variables in the given source object with their matching values from the map.
    static String
    replace(Object source, Properties valueProperties)
    Deprecated.
    Replaces all the occurrences of variables in the given source object with their matching values from the properties.
    replace(String source)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template.
    replace(StringBuffer source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source buffer as a template.
    replace(String source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template.
    replace(StrBuilder source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables with their matching values from the resolver using the given source builder as a template.
    boolean
    Deprecated.
    Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver.
    boolean
    replaceIn(StringBuffer source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver.
    boolean
    Deprecated.
    Replaces all the occurrences of variables within the given source buffer with their matching values from the resolver.
    boolean
    replaceIn(StringBuilder source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
    boolean
    Deprecated.
    Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
    boolean
    replaceIn(StrBuilder source, int offset, int length)
    Deprecated.
    Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
    static String
    Deprecated.
    Replaces all the occurrences of variables in the given source object with their matching values from the system properties.
    protected String
    resolveVariable(String variableName, StrBuilder buf, int startPos, int endPos)
    Deprecated.
    Internal method that resolves the value of a variable.
    void
    setDisableSubstitutionInValues(boolean disableSubstitutionInValues)
    Deprecated.
    Sets a flag whether substitution is done in variable values (recursive).
    void
    setEnableSubstitutionInVariables(boolean enableSubstitutionInVariables)
    Deprecated.
    Sets a flag whether substitution is done in variable names.
    void
    setEscapeChar(char escapeCharacter)
    Deprecated.
    Sets the escape character.
    void
    setPreserveEscapes(boolean preserveEscapes)
    Deprecated.
    Sets a flag controlling whether escapes are preserved during substitution.
    setValueDelimiter(char valueDelimiter)
    Deprecated.
    Sets the variable default value delimiter to use.
    setValueDelimiter(String valueDelimiter)
    Deprecated.
    Sets the variable default value delimiter to use.
    setValueDelimiterMatcher(StrMatcher valueDelimiterMatcher)
    Deprecated.
    Sets the variable default value delimiter matcher to use.
    setVariablePrefix(char prefix)
    Deprecated.
    Sets the variable prefix to use.
    Deprecated.
    Sets the variable prefix to use.
    Deprecated.
    Sets the variable prefix matcher currently in use.
    void
    setVariableResolver(StrLookup<?> variableResolver)
    Deprecated.
    Sets the VariableResolver that is used to lookup variables.
    setVariableSuffix(char suffix)
    Deprecated.
    Sets the variable suffix to use.
    Deprecated.
    Sets the variable suffix to use.
    Deprecated.
    Sets the variable suffix matcher currently in use.
    protected boolean
    substitute(StrBuilder buf, int offset, int length)
    Deprecated.
    Internal method that substitutes the variables.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • StrSubstitutor

      public StrSubstitutor()
      Deprecated.
      Constructs a new instance with defaults for variable prefix and suffix and the escaping character.
    • StrSubstitutor

      public StrSubstitutor(Map<String,V> valueMap)
      Deprecated.
      Constructs a new instance and initializes it. Uses defaults for variable prefix and suffix and the escaping character.
      Type Parameters:
      V - the type of the values in the map
      Parameters:
      valueMap - the map with the variables' values, may be null
    • StrSubstitutor

      public StrSubstitutor(Map<String,V> valueMap, String prefix, String suffix)
      Deprecated.
      Constructs a new instance and initializes it. Uses a default escaping character.
      Type Parameters:
      V - the type of the values in the map
      Parameters:
      valueMap - the map with the variables' values, may be null
      prefix - the prefix for variables, not null
      suffix - the suffix for variables, not null
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • StrSubstitutor

      public StrSubstitutor(Map<String,V> valueMap, String prefix, String suffix, char escape)
      Deprecated.
      Constructs a new instance and initializes it.
      Type Parameters:
      V - the type of the values in the map
      Parameters:
      valueMap - the map with the variables' values, may be null
      prefix - the prefix for variables, not null
      suffix - the suffix for variables, not null
      escape - the escape character
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • StrSubstitutor

      public StrSubstitutor(Map<String,V> valueMap, String prefix, String suffix, char escape, String valueDelimiter)
      Deprecated.
      Constructs a new instance and initializes it.
      Type Parameters:
      V - the type of the values in the map
      Parameters:
      valueMap - the map with the variables' values, may be null
      prefix - the prefix for variables, not null
      suffix - the suffix for variables, not null
      escape - the escape character
      valueDelimiter - the variable default value delimiter, may be null
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • StrSubstitutor

      public StrSubstitutor(StrLookup<?> variableResolver)
      Deprecated.
      Constructs a new instance and initializes it.
      Parameters:
      variableResolver - the variable resolver, may be null
    • StrSubstitutor

      public StrSubstitutor(StrLookup<?> variableResolver, String prefix, String suffix, char escape)
      Deprecated.
      Constructs a new instance and initializes it.
      Parameters:
      variableResolver - the variable resolver, may be null
      prefix - the prefix for variables, not null
      suffix - the suffix for variables, not null
      escape - the escape character
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • StrSubstitutor

      public StrSubstitutor(StrLookup<?> variableResolver, String prefix, String suffix, char escape, String valueDelimiter)
      Deprecated.
      Constructs a new instance and initializes it.
      Parameters:
      variableResolver - the variable resolver, may be null
      prefix - the prefix for variables, not null
      suffix - the suffix for variables, not null
      escape - the escape character
      valueDelimiter - the variable default value delimiter string, may be null
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • StrSubstitutor

      public StrSubstitutor(StrLookup<?> variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape)
      Deprecated.
      Constructs a new instance and initializes it.
      Parameters:
      variableResolver - the variable resolver, may be null
      prefixMatcher - the prefix for variables, not null
      suffixMatcher - the suffix for variables, not null
      escape - the escape character
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • StrSubstitutor

      public StrSubstitutor(StrLookup<?> variableResolver, StrMatcher prefixMatcher, StrMatcher suffixMatcher, char escape, StrMatcher valueDelimiterMatcher)
      Deprecated.
      Constructs a new instance and initializes it.
      Parameters:
      variableResolver - the variable resolver, may be null
      prefixMatcher - the prefix for variables, not null
      suffixMatcher - the suffix for variables, not null
      escape - the escape character
      valueDelimiterMatcher - the variable default value delimiter matcher, may be null
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
  • Method Details

    • replace

      public static <V> String replace(Object source, Map<String,V> valueMap)
      Deprecated.
      Replaces all the occurrences of variables in the given source object with their matching values from the map.
      Type Parameters:
      V - the type of the values in the map
      Parameters:
      source - the source text containing the variables to substitute, null returns null
      valueMap - the map with the values, may be null
      Returns:
      The result of the replace operation
    • replace

      public static <V> String replace(Object source, Map<String,V> valueMap, String prefix, String suffix)
      Deprecated.
      Replaces all the occurrences of variables in the given source object with their matching values from the map. This method allows to specify a custom variable prefix and suffix
      Type Parameters:
      V - the type of the values in the map
      Parameters:
      source - the source text containing the variables to substitute, null returns null
      valueMap - the map with the values, may be null
      prefix - the prefix of variables, not null
      suffix - the suffix of variables, not null
      Returns:
      The result of the replace operation
      Throws:
      IllegalArgumentException - if the prefix or suffix is null
    • replace

      public static String replace(Object source, Properties valueProperties)
      Deprecated.
      Replaces all the occurrences of variables in the given source object with their matching values from the properties.
      Parameters:
      source - the source text containing the variables to substitute, null returns null
      valueProperties - the properties with values, may be null
      Returns:
      The result of the replace operation
    • replaceSystemProperties

      public static String replaceSystemProperties(Object source)
      Deprecated.
      Replaces all the occurrences of variables in the given source object with their matching values from the system properties.
      Parameters:
      source - the source text containing the variables to substitute, null returns null
      Returns:
      The result of the replace operation
    • getEscapeChar

      public char getEscapeChar()
      Deprecated.
      Returns the escape character.
      Returns:
      The character used for escaping variable references
    • getValueDelimiterMatcher

      Deprecated.
      Gets the variable default value delimiter matcher currently in 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 it returns null, then the variable default value resolution is disabled.

      Returns:
      The variable default value delimiter matcher in use, may be null
    • getVariablePrefixMatcher

      Deprecated.
      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.

      Returns:
      The prefix matcher in use
    • getVariableResolver

      Deprecated.
      Gets the VariableResolver that is used to lookup variables.
      Returns:
      The VariableResolver
    • getVariableSuffixMatcher

      Deprecated.
      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.

      Returns:
      The suffix matcher in use
    • isDisableSubstitutionInValues

      Deprecated.
      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}
       
      Returns:
      The substitution in variable values flag
      Since:
      1.2
    • isEnableSubstitutionInVariables

      Deprecated.
      Returns a flag whether substitution is done in variable names.
      Returns:
      The substitution in variable names flag
    • isPreserveEscapes

      public boolean isPreserveEscapes()
      Deprecated.
      Returns the flag controlling whether escapes are preserved during substitution.
      Returns:
      The preserve escape flag
    • replace

      public String replace(char[] source)
      Deprecated.
      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.
      Parameters:
      source - the character array to replace in, not altered, null returns null
      Returns:
      The result of the replace operation
    • replace

      public String replace(char[] source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the character array to replace in, not altered, null returns null
      offset - the start offset within the array, must be valid
      length - the length within the array to be processed, must be valid
      Returns:
      The result of the replace operation
    • replace

      public String replace(CharSequence source)
      Deprecated.
      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.
      Parameters:
      source - the buffer to use as a template, not changed, null returns null
      Returns:
      The result of the replace operation
    • replace

      public String replace(CharSequence source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the buffer to use as a template, not changed, null returns null
      offset - the start offset within the array, must be valid
      length - the length within the array to be processed, must be valid
      Returns:
      The result of the replace operation
    • replace

      public String replace(Object source)
      Deprecated.
      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 toString and is not altered.
      Parameters:
      source - the source to replace in, null returns null
      Returns:
      The result of the replace operation
    • replace

      public String replace(StrBuilder source)
      Deprecated.
      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.
      Parameters:
      source - the builder to use as a template, not changed, null returns null
      Returns:
      The result of the replace operation
    • replace

      public String replace(StrBuilder source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the builder to use as a template, not changed, null returns null
      offset - the start offset within the array, must be valid
      length - the length within the array to be processed, must be valid
      Returns:
      The result of the replace operation
    • replace

      public String replace(String source)
      Deprecated.
      Replaces all the occurrences of variables with their matching values from the resolver using the given source string as a template.
      Parameters:
      source - the string to replace in, null returns null
      Returns:
      The result of the replace operation
    • replace

      public String replace(String source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the string to replace in, null returns null
      offset - the start offset within the array, must be valid
      length - the length within the array to be processed, must be valid
      Returns:
      The result of the replace operation
    • replace

      public String replace(StringBuffer source)
      Deprecated.
      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.
      Parameters:
      source - the buffer to use as a template, not changed, null returns null
      Returns:
      The result of the replace operation
    • replace

      public String replace(StringBuffer source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the buffer to use as a template, not changed, null returns null
      offset - the start offset within the array, must be valid
      length - the length within the array to be processed, must be valid
      Returns:
      The result of the replace operation
    • replaceIn

      public boolean replaceIn(StrBuilder source)
      Deprecated.
      Replaces all the occurrences of variables within the given source builder with their matching values from the resolver.
      Parameters:
      source - the builder to replace in, updated, null returns zero
      Returns:
      true if altered
    • replaceIn

      public boolean replaceIn(StrBuilder source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the builder to replace in, null returns zero
      offset - the start offset within the array, must be valid
      length - the length within the builder to be processed, must be valid
      Returns:
      true if altered
    • replaceIn

      public boolean replaceIn(StringBuffer source)
      Deprecated.
      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.
      Parameters:
      source - the buffer to replace in, updated, null returns zero
      Returns:
      true if altered
    • replaceIn

      public boolean replaceIn(StringBuffer source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the buffer to replace in, updated, null returns zero
      offset - the start offset within the array, must be valid
      length - the length within the buffer to be processed, must be valid
      Returns:
      true if altered
    • replaceIn

      public boolean replaceIn(StringBuilder source)
      Deprecated.
      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.
      Parameters:
      source - the buffer to replace in, updated, null returns zero
      Returns:
      true if altered
    • replaceIn

      public boolean replaceIn(StringBuilder source, int offset, int length)
      Deprecated.
      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.

      Parameters:
      source - the buffer to replace in, updated, null returns zero
      offset - the start offset within the array, must be valid
      length - the length within the buffer to be processed, must be valid
      Returns:
      true if altered
    • resolveVariable

      protected String resolveVariable(String variableName, StrBuilder buf, int startPos, int endPos)
      Deprecated.
      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 getVariableResolver() with the variable's name as the key.

      Parameters:
      variableName - the name of the variable, not null
      buf - the buffer where the substitution is occurring, not null
      startPos - the start position of the variable including the prefix, valid
      endPos - the end position of the variable including the suffix, valid
      Returns:
      The variable's value or null if the variable is unknown
    • setDisableSubstitutionInValues

      public void setDisableSubstitutionInValues(boolean disableSubstitutionInValues)
      Deprecated.
      Sets a flag whether substitution is done in variable values (recursive).
      Parameters:
      disableSubstitutionInValues - true if substitution in variable value are disabled
      Since:
      1.2
    • setEnableSubstitutionInVariables

      public void setEnableSubstitutionInVariables(boolean enableSubstitutionInVariables)
      Deprecated.
      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. ${jre-${java.version}}. The default value is false.
      Parameters:
      enableSubstitutionInVariables - the new value of the flag
    • setEscapeChar

      public void setEscapeChar(char escapeCharacter)
      Deprecated.
      Sets the escape character. If this character is placed before a variable reference in the source text, this variable will be ignored.
      Parameters:
      escapeCharacter - the escape character (0 for disabling escaping)
    • setPreserveEscapes

      public void setPreserveEscapes(boolean preserveEscapes)
      Deprecated.
      Sets a flag controlling whether escapes are preserved during substitution. If set to true, the escape character is retained during substitution (e.g. $${this-is-escaped} remains $${this-is-escaped}). If set to false, the escape character is removed during substitution (e.g. $${this-is-escaped} becomes ${this-is-escaped}). The default value is false
      Parameters:
      preserveEscapes - true if escapes are to be preserved
    • setValueDelimiter

      public StrSubstitutor setValueDelimiter(char valueDelimiter)
      Deprecated.
      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.

      Parameters:
      valueDelimiter - the variable default value delimiter character to use
      Returns:
      this, to enable chaining
    • setValueDelimiter

      public StrSubstitutor setValueDelimiter(String valueDelimiter)
      Deprecated.
      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 valueDelimiter is null or empty string, then the variable default value resolution becomes disabled.

      Parameters:
      valueDelimiter - the variable default value delimiter string to use, may be null or empty
      Returns:
      this, to enable chaining
    • setValueDelimiterMatcher

      public StrSubstitutor setValueDelimiterMatcher(StrMatcher valueDelimiterMatcher)
      Deprecated.
      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 valueDelimiterMatcher is null, then the variable default value resolution becomes disabled.

      Parameters:
      valueDelimiterMatcher - variable default value delimiter matcher to use, may be null
      Returns:
      this, to enable chaining
    • setVariablePrefix

      public StrSubstitutor setVariablePrefix(char prefix)
      Deprecated.
      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.

      Parameters:
      prefix - the prefix character to use
      Returns:
      this, to enable chaining
    • setVariablePrefix

      Deprecated.
      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.

      Parameters:
      prefix - the prefix for variables, not null
      Returns:
      this, to enable chaining
      Throws:
      IllegalArgumentException - if the prefix is null
    • setVariablePrefixMatcher

      Deprecated.
      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.

      Parameters:
      prefixMatcher - the prefix matcher to use, null ignored
      Returns:
      this, to enable chaining
      Throws:
      IllegalArgumentException - if the prefix matcher is null
    • setVariableResolver

      public void setVariableResolver(StrLookup<?> variableResolver)
      Deprecated.
      Sets the VariableResolver that is used to lookup variables.
      Parameters:
      variableResolver - the VariableResolver
    • setVariableSuffix

      public StrSubstitutor setVariableSuffix(char suffix)
      Deprecated.
      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.

      Parameters:
      suffix - the suffix character to use
      Returns:
      this, to enable chaining
    • setVariableSuffix

      Deprecated.
      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.

      Parameters:
      suffix - the suffix for variables, not null
      Returns:
      this, to enable chaining
      Throws:
      IllegalArgumentException - if the suffix is null
    • setVariableSuffixMatcher

      Deprecated.
      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.

      Parameters:
      suffixMatcher - the suffix matcher to use, null ignored
      Returns:
      this, to enable chaining
      Throws:
      IllegalArgumentException - if the suffix matcher is null
    • substitute

      protected boolean substitute(StrBuilder buf, int offset, int length)
      Deprecated.
      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.

      Parameters:
      buf - the string builder to substitute into, not null
      offset - the start offset within the builder, must be valid
      length - the length within the builder to be processed, must be valid
      Returns:
      true if altered