Class ConfigurationInterpolator
- java.lang.Object
-
- org.apache.commons.configuration2.interpol.ConfigurationInterpolator
-
public class ConfigurationInterpolator extends java.lang.ObjectA class that handles interpolation (variable substitution) for configuration objects.
Each instance of
AbstractConfigurationis associated with an object of this class. All interpolation tasks are delegated to this object.ConfigurationInterpolatorinternally uses theStringSubstitutorclass from Commons Text. Thus it supports the same syntax of variable expressions.The basic idea of this class is that it can maintain a set of primitive
Lookupobjects, each of which is identified by a special prefix. The variables to be processed have the form${prefix:name}.ConfigurationInterpolatorwill extract the prefix and determine, which primitive lookup object is registered for it. Then the name of the variable is passed to this object to obtain the actual value. It is also possible to define an arbitrary number of default lookup objects, which are used for variables that do not have a prefix or that cannot be resolved by their associated lookup object. When adding default lookup objects their order matters; they are queried in this order, and the first non-null variable value is used.After an instance has been created it does not contain any
Lookupobjects. The current set of lookup objects can be modified using theregisterLookup()andderegisterLookup()methods. Default lookup objects (that are invoked for variables without a prefix) can be added or removed with theaddDefaultLookup()andremoveDefaultLookup()methods respectively. (When aConfigurationInterpolatorinstance is created by a configuration object, a default lookup object is added pointing to the configuration itself, so that variables are resolved using the configuration's properties.)The default usage scenario is that on a fully initialized instance the
interpolate()method is called. It is passed an object value which may contain variables. All these variables are substituted if they can be resolved. The result is the passed in value with variables replaced. Alternatively, theresolve()method can be called to obtain the values of specific variables without performing interpolation.Implementation node: This class is thread-safe. Lookup objects can be added or removed at any time concurrent to interpolation operations.
- Since:
- 1.4
-
-
Constructor Summary
Constructors Constructor Description ConfigurationInterpolator()Creates a new instance ofConfigurationInterpolator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddDefaultLookup(Lookup defaultLookup)Adds a defaultLookupobject.voidaddDefaultLookups(java.util.Collection<? extends Lookup> lookups)Adds allLookupobjects in the given collection as default lookups.booleanderegisterLookup(java.lang.String prefix)Deregisters theLookupobject for the specified prefix at this instance.protected LookupfetchLookupForPrefix(java.lang.String prefix)Obtains the lookup object for the specified prefix.static ConfigurationInterpolatorfromSpecification(InterpolatorSpecification spec)Creates a newConfigurationInterpolatorinstance based on the passed in specification object.java.util.List<Lookup>getDefaultLookups()Returns a collection with the defaultLookupobjects added to thisConfigurationInterpolator.static java.util.Map<java.lang.String,Lookup>getDefaultPrefixLookups()Returns a map containing the default prefix lookups.java.util.Map<java.lang.String,Lookup>getLookups()Returns a map with the currently registeredLookupobjects and their prefixes.ConfigurationInterpolatorgetParentInterpolator()Returns the parentConfigurationInterpolator.java.lang.Objectinterpolate(java.lang.Object value)Performs interpolation of the passed in value.booleanisEnableSubstitutionInVariables()Sets a flag that variable names can contain other variables.static LookupnullSafeLookup(Lookup lookup)Utility method for obtaining aLookupobject in a safe way.java.util.Set<java.lang.String>prefixSet()Returns an unmodifiable set with the prefixes, for whichLookupobjects are registered at this instance.voidregisterLookup(java.lang.String prefix, Lookup lookup)Registers the givenLookupobject for the specified prefix at this instance.voidregisterLookups(java.util.Map<java.lang.String,? extends Lookup> lookups)Registers allLookupobjects in the given map with their prefixes at thisConfigurationInterpolator.booleanremoveDefaultLookup(Lookup lookup)Removes the specifiedLookupobject from the list of defaultLookups.java.lang.Objectresolve(java.lang.String var)Resolves the specified variable.voidsetEnableSubstitutionInVariables(boolean f)Sets the flag whether variable names can contain other variables.voidsetParentInterpolator(ConfigurationInterpolator parentInterpolator)Sets the parentConfigurationInterpolator.
-
-
-
Method Detail
-
fromSpecification
public static ConfigurationInterpolator fromSpecification(InterpolatorSpecification spec)
Creates a newConfigurationInterpolatorinstance based on the passed in specification object. If theInterpolatorSpecificationalready contains aConfigurationInterpolatorobject, it is used directly. Otherwise, a new instance is created and initialized with the properties stored in the specification.- Parameters:
spec- theInterpolatorSpecification(must not be null)- Returns:
- the
ConfigurationInterpolatorobtained or created based on the given specification - Throws:
java.lang.IllegalArgumentException- if the specification is null- Since:
- 2.0
-
getDefaultPrefixLookups
public static java.util.Map<java.lang.String,Lookup> getDefaultPrefixLookups()
Returns a map containing the default prefix lookups. Every configuration object derived fromAbstractConfigurationis by default initialized with aConfigurationInterpolatorcontaining theseLookupobjects and their prefixes. The map cannot be modified- Returns:
- a map with the default prefix
Lookupobjects and their prefixes - Since:
- 2.0
-
nullSafeLookup
public static Lookup nullSafeLookup(Lookup lookup)
Utility method for obtaining aLookupobject in a safe way. This method always returns a non-nullLookupobject. If the passed inLookupis not null, it is directly returned. Otherwise, result is a dummyLookupwhich does not provide any values.- Parameters:
lookup- theLookupto check- Returns:
- a non-null
Lookupobject - Since:
- 2.0
-
addDefaultLookup
public void addDefaultLookup(Lookup defaultLookup)
Adds a defaultLookupobject. DefaultLookupobjects are queried (in the order they were added) for all variables without a special prefix. If no defaultLookupobjects are present, such variables won't be processed.- Parameters:
defaultLookup- the defaultLookupobject to be added (must not be null)- Throws:
java.lang.IllegalArgumentException- if theLookupobject is null
-
addDefaultLookups
public void addDefaultLookups(java.util.Collection<? extends Lookup> lookups)
Adds allLookupobjects in the given collection as default lookups. The collection can be null, then this method has no effect. It must not contain null entries.- Parameters:
lookups- theLookupobjects to be added as default lookups- Throws:
java.lang.IllegalArgumentException- if the collection contains a null entry
-
deregisterLookup
public boolean deregisterLookup(java.lang.String prefix)
Deregisters theLookupobject for the specified prefix at this instance. It will be removed from this instance.- Parameters:
prefix- the variable prefix- Returns:
- a flag whether for this prefix a lookup object had been registered
-
fetchLookupForPrefix
protected Lookup fetchLookupForPrefix(java.lang.String prefix)
Obtains the lookup object for the specified prefix. This method is called by thelookup()method. This implementation will check whether a lookup object is registered for the given prefix. If not, a null lookup object will be returned (never null).- Parameters:
prefix- the prefix- Returns:
- the lookup object to be used for this prefix
-
getDefaultLookups
public java.util.List<Lookup> getDefaultLookups()
Returns a collection with the defaultLookupobjects added to thisConfigurationInterpolator. These objects are not associated with a variable prefix. The returned list is a snapshot copy of the internal collection of default lookups; so manipulating it does not affect this instance.- Returns:
- the default lookup objects
-
getLookups
public java.util.Map<java.lang.String,Lookup> getLookups()
Returns a map with the currently registeredLookupobjects and their prefixes. This is a snapshot copy of the internally used map. So modifications of this map do not effect this instance.- Returns:
- a copy of the map with the currently registered
Lookupobjects
-
getParentInterpolator
public ConfigurationInterpolator getParentInterpolator()
Returns the parentConfigurationInterpolator.- Returns:
- the parent
ConfigurationInterpolator(can be null)
-
interpolate
public java.lang.Object interpolate(java.lang.Object value)
Performs interpolation of the passed in value. If the value is of type String, this method checks whether it contains variables. If so, all variables are replaced by their current values (if possible). For non string arguments, the value is returned without changes.- Parameters:
value- the value to be interpolated- Returns:
- the interpolated value
-
isEnableSubstitutionInVariables
public boolean isEnableSubstitutionInVariables()
Sets a flag that variable names can contain other variables. If enabled, variable substitution is also done in variable names.- Returns:
- the substitution in variables flag
-
prefixSet
public java.util.Set<java.lang.String> prefixSet()
Returns an unmodifiable set with the prefixes, for whichLookupobjects are registered at this instance. This means that variables with these prefixes can be processed.- Returns:
- a set with the registered variable prefixes
-
registerLookup
public void registerLookup(java.lang.String prefix, Lookup lookup)Registers the givenLookupobject for the specified prefix at this instance. From now on this lookup object will be used for variables that have the specified prefix.- Parameters:
prefix- the variable prefix (must not be null)lookup- theLookupobject to be used for this prefix (must not be null)- Throws:
java.lang.IllegalArgumentException- if either the prefix or theLookupobject is null
-
registerLookups
public void registerLookups(java.util.Map<java.lang.String,? extends Lookup> lookups)
Registers allLookupobjects in the given map with their prefixes at thisConfigurationInterpolator. Using this method multipleLookupobjects can be registered at once. If the passed in map is null, this method does not have any effect.- Parameters:
lookups- the map with lookups to register (may be null)- Throws:
java.lang.IllegalArgumentException- if the map contains entries
-
removeDefaultLookup
public boolean removeDefaultLookup(Lookup lookup)
Removes the specifiedLookupobject from the list of defaultLookups.- Parameters:
lookup- theLookupobject to be removed- Returns:
- a flag whether this
Lookupobject actually existed and was removed
-
resolve
public java.lang.Object resolve(java.lang.String var)
Resolves the specified variable. This implementation tries to extract a variable prefix from the given variable name (the first colon (':') is used as prefix separator). It then passes the name of the variable with the prefix stripped to the lookup object registered for this prefix. If no prefix can be found or if the associated lookup object cannot resolve this variable, the default lookup objects are used. If this is not successful either and a parentConfigurationInterpolatoris available, this object is asked to resolve the variable.- Parameters:
var- the name of the variable whose value is to be looked up which may contain a prefix.- Returns:
- the value of this variable or null if it cannot be resolved
-
setEnableSubstitutionInVariables
public void setEnableSubstitutionInVariables(boolean f)
Sets the flag whether variable names can contain other variables. This flag corresponds to theenableSubstitutionInVariablesproperty of the underlyingStringSubstitutorobject.- Parameters:
f- the new value of the flag
-
setParentInterpolator
public void setParentInterpolator(ConfigurationInterpolator parentInterpolator)
Sets the parentConfigurationInterpolator. This object is used if theLookupobjects registered at this object cannot resolve a variable.- Parameters:
parentInterpolator- the parentConfigurationInterpolatorobject (can be null)
-
-