Package org.apache.commons.configuration
Class SubsetConfiguration
java.lang.Object
org.apache.commons.configuration.event.EventSource
org.apache.commons.configuration.AbstractConfiguration
org.apache.commons.configuration.SubsetConfiguration
- All Implemented Interfaces:
Configuration
A subset of another configuration. The new Configuration object contains every key from the parent Configuration that starts with prefix. The prefix is removed from the keys in the subset.
It is usually not necessary to use this class directly. Instead the
Configuration.subset(String)
method should be used,
which will return a correctly initialized instance.
- Version:
- $Id: SubsetConfiguration.java 1210202 2011-12-04 20:30:46Z oheger $
- Author:
- Emmanuel Bourg
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String
The prefix delimiterprotected Configuration
The parent configuration.protected String
The prefix used to select the properties.Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
-
Constructor Summary
ConstructorsConstructorDescriptionSubsetConfiguration
(Configuration parent, String prefix) Create a subset of the specified configurationSubsetConfiguration
(Configuration parent, String prefix, String delimiter) Create a subset of the specified configuration -
Method Summary
Modifier and TypeMethodDescriptionvoid
addPropertyDirect
(String key, Object value) Adds a key/value pair to the Configuration.protected void
Removes the specified property from this configuration.boolean
containsKey
(String key) Check if the configuration contains the specified key.protected String
getChildKey
(String key) Return the key in the subset configuration associated to the specified key in the parent configuration.getKeys()
Get the list of the keys contained in the configuration.Get the list of the keys contained in the configuration that match the specified prefix.char
Returns the list delimiter.Return the parent configuration for this subset.protected String
getParentKey
(String key) Return the key in the parent configuration associated to the specified key in this subset.Return the prefix used to select the properties in the parent configuration.getProperty
(String key) Gets a property from the configuration.protected Object
interpolate
(Object base) Returns the interpolated value.protected String
interpolate
(String base) interpolate key names to handle ${key} stuffboolean
Returns a flag whether string properties should be checked for list delimiter characters.boolean
isEmpty()
Check if the configuration is empty.boolean
Returns true if missing values throw Exceptions.void
setDelimiterParsingDisabled
(boolean delimiterParsingDisabled) Sets a flag whether list parsing is disabled.void
setListDelimiter
(char delim) Sets the list delimiter.void
Set the prefix used to select the properties in the parent configuration.void
setThrowExceptionOnMissing
(boolean throwExceptionOnMissing) Allows to set thethrowExceptionOnMissing
flag.Return a decorator Configuration containing every key from the current Configuration that starts with the specified prefix.Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
addErrorLogListener, addProperty, append, clear, clearProperty, copy, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getList, getList, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, getSubstitutor, interpolatedConfiguration, interpolateHelper, isScalarValue, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setLogger, setProperty
Methods inherited from class org.apache.commons.configuration.event.EventSource
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, clone, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
-
Field Details
-
parent
The parent configuration. -
prefix
The prefix used to select the properties. -
delimiter
The prefix delimiter
-
-
Constructor Details
-
SubsetConfiguration
Create a subset of the specified configuration- Parameters:
parent
- The parent configurationprefix
- The prefix used to select the properties
-
SubsetConfiguration
Create a subset of the specified configuration- Parameters:
parent
- The parent configurationprefix
- The prefix used to select the propertiesdelimiter
- The prefix delimiter
-
-
Method Details
-
getParentKey
Return the key in the parent configuration associated to the specified key in this subset.- Parameters:
key
- The key in the subset.- Returns:
- the key as to be used by the parent
-
getChildKey
Return the key in the subset configuration associated to the specified key in the parent configuration.- Parameters:
key
- The key in the parent configuration.- Returns:
- the key in the context of this subset configuration
-
getParent
Return the parent configuration for this subset.- Returns:
- the parent configuration
-
getPrefix
Return the prefix used to select the properties in the parent configuration.- Returns:
- the prefix used by this subset
-
setPrefix
Set the prefix used to select the properties in the parent configuration.- Parameters:
prefix
- the prefix
-
subset
Description copied from interface:Configuration
Return a decorator Configuration containing every key from the current Configuration that starts with the specified prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following properties:prefix.number = 1 prefix.string = Apache prefixed.foo = bar prefix = Jakarta
the Configuration returned bysubset("prefix")
will contain the properties:number = 1 string = Apache = Jakarta
(The key for the value "Jakarta" is an empty string)Since the subset is a decorator and not a modified copy of the initial Configuration, any change made to the subset is available to the Configuration, and reciprocally.
- Specified by:
subset
in interfaceConfiguration
- Overrides:
subset
in classAbstractConfiguration
- Parameters:
prefix
- The prefix used to select the properties.- Returns:
- a subset configuration
- See Also:
-
isEmpty
Description copied from interface:Configuration
Check if the configuration is empty.- Returns:
true
if the configuration contains no property,false
otherwise.
-
containsKey
Description copied from interface:Configuration
Check if the configuration contains the specified key.- Parameters:
key
- the key whose presence in this configuration is to be tested- Returns:
true
if the configuration contains a value for this key,false
otherwise
-
addPropertyDirect
Description copied from class:AbstractConfiguration
Adds a key/value pair to the Configuration. Override this method to provide write access to underlying Configuration store.- Specified by:
addPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- key to use for mappingvalue
- object to store
-
clearPropertyDirect
Description copied from class:AbstractConfiguration
Removes the specified property from this configuration. This method is called byclearProperty()
after it has done some preparations. It should be overridden in sub classes. This base implementation is just left empty.- Overrides:
clearPropertyDirect
in classAbstractConfiguration
- Parameters:
key
- the key to be removed
-
getProperty
Description copied from interface:Configuration
Gets a property from the configuration. This is the most basic get method for retrieving values of properties. In a typical implementation of theConfiguration
interface the other get methods (that return specific data types) will internally make use of this method. On this level variable substitution is not yet performed. The returned object is an internal representation of the property value for the passed in key. It is owned by theConfiguration
object. So a caller should not modify this object. It cannot be guaranteed that this object will stay constant over time (i.e. further update operations on the configuration may change its internal state).- Parameters:
key
- property to retrieve- Returns:
- the value to which this configuration maps the specified key, or null if the configuration contains no mapping for this key.
-
getKeys
Description copied from class:AbstractConfiguration
Get the list of the keys contained in the configuration that match the specified prefix. For instance, if the configuration contains the following keys:
db.user, db.pwd, db.url, window.xpos, window.ypos
,
an invocation ofgetKeys("db");
will return the keys below:
db.user, db.pwd, db.url
.
Note that the prefix itself is included in the result set if there is a matching key. The exact behavior - how the prefix is actually interpreted - depends on a concrete implementation. This implementation returns keys that either match the prefix or start with the prefix followed by a dot ('.'). So the callgetKeys("db");
will find the keysdb
,db.user
, ordb.password
, but not the keydbdriver
.- Specified by:
getKeys
in interfaceConfiguration
- Overrides:
getKeys
in classAbstractConfiguration
- Parameters:
prefix
- The prefix to test against.- Returns:
- An Iterator of keys that match the prefix.
- See Also:
-
getKeys
Description copied from interface:Configuration
Get the list of the keys contained in the configuration. The returned iterator can be used to obtain all defined keys. Note that the exact behavior of the iterator'sremove()
method is specific to a concrete implementation. It may remove the corresponding property from the configuration, but this is not guaranteed. In any case it is no replacement for callingConfiguration.clearProperty(String)
for this property. So it is highly recommended to avoid using the iterator'sremove()
method.- Returns:
- An Iterator.
-
interpolate
Description copied from class:AbstractConfiguration
Returns the interpolated value. Non String values are returned without change.- Overrides:
interpolate
in classAbstractConfiguration
- Parameters:
base
- the value to interpolate- Returns:
- returns the value with variables substituted
-
interpolate
Description copied from class:AbstractConfiguration
interpolate key names to handle ${key} stuff- Overrides:
interpolate
in classAbstractConfiguration
- Parameters:
base
- string to interpolate- Returns:
- returns the key name with the ${key} substituted
-
setThrowExceptionOnMissing
Allows to set thethrowExceptionOnMissing
flag. This flag controls the behavior of property getter methods that return objects if the requested property is missing. If the flag is set to false (which is the default value), these methods will return null. If set to true, they will throw aNoSuchElementException
exception. Note that getter methods for primitive data types are not affected by this flag. Change the behavior of the parent configuration if it supports this feature.- Overrides:
setThrowExceptionOnMissing
in classAbstractConfiguration
- Parameters:
throwExceptionOnMissing
- The new value for the property
-
isThrowExceptionOnMissing
Returns true if missing values throw Exceptions. The subset inherits this feature from its parent if it supports this feature.- Overrides:
isThrowExceptionOnMissing
in classAbstractConfiguration
- Returns:
- true if missing values throw Exceptions
-
getListDelimiter
Returns the list delimiter. This property will be fetched from the parent configuration if supported.- Overrides:
getListDelimiter
in classAbstractConfiguration
- Returns:
- the list delimiter
- Since:
- 1.4
-
setListDelimiter
Sets the list delimiter. If the parent configuration supports this feature, the delimiter will be set at the parent.- Overrides:
setListDelimiter
in classAbstractConfiguration
- Parameters:
delim
- the new list delimiter- Since:
- 1.4
-
isDelimiterParsingDisabled
Returns a flag whether string properties should be checked for list delimiter characters. This implementation ensures that this flag is kept in sync with the parent configuration if this object supports this feature.- Overrides:
isDelimiterParsingDisabled
in classAbstractConfiguration
- Returns:
- the delimiter parsing disabled flag
- Since:
- 1.4
-
setDelimiterParsingDisabled
Sets a flag whether list parsing is disabled. This implementation will also set the flag at the parent configuration if this object supports this feature.- Overrides:
setDelimiterParsingDisabled
in classAbstractConfiguration
- Parameters:
delimiterParsingDisabled
- the delimiter parsing disabled flag- Since:
- 1.4
-