A specialized configuration class that extends its base class by the ability of keeping more structure in the stored properties.
There are some sources of configuration data that cannot be stored very well in a BaseConfiguration
object because then their structure is lost. This is especially true for XML documents. This class can deal with such structured configuration sources by storing the properties in a tree-like organization.
The internal used storage form allows for a more sophisticated access to single properties. As an example consider the following XML document:
<database> <tables> <table> <name>users</name> <fields> <field> <name>lid</name> <type>long</name> </field> <field> <name>usrName</name> <type>java.lang.String</type> </field> ... </fields> </table> <table> <name>documents</name> <fields> <field> <name>docid</name> <type>long</type> </field> ... </fields> </table> ... </tables> </database>
If this document is parsed and stored in a HierarchicalConfiguration
object (which can be done by one of the sub classes), there are enhanced possibilities of accessing properties. The keys for querying information can contain indices that select a certain element if there are multiple hits.
For instance the key tables.table(0).name
can be used to find out the name of the first table. In opposite tables.table.name
would return a collection with the names of all available tables. Similarly the key tables.table(1).fields.field.name
returns a collection with the names of all fields of the second table. If another index is added after the field
element, a single field can be accessed: tables.table(1).fields.field(0).name
.
There is a getMaxIndex()
method that returns the maximum allowed index that can be added to a given property key. This method can be used to iterate over all values defined for a certain property.
Since the 1.3 release of Commons Configuration hierarchical configurations support an expression engine. This expression engine is responsible for evaluating the passed in configuration keys and map them to the stored properties. The examples above are valid for the default expression engine, which is used when a new HierarchicalConfiguration
instance is created. With the setExpressionEngine()
method a different expression engine can be set. For instance with {@link org.apache.commons.configuration.tree.xpath.XPathExpressionEngine}
there is an expression engine available that supports configuration keys in XPATH syntax.
In addition to the events common for all configuration classes hierarchical configurations support some more events that correspond to some specific methods and features:
addNodes()
method was called; the event object contains the key, to which the nodes were added, and a collection with the new nodes as value.clearTree()
method was called; the event object stores the key of the removed sub tree.SubnodeConfiguration
that was created from this configuration has been changed. The value property of the event object contains the original event object as it was sent by the subnode configuration.Note:Configuration objects of this type can be read concurrently by multiple threads. However if one of these threads modifies the object, synchronization has to be performed manually.
@author Oliver Heger @version $Id: HierarchicalConfiguration.java 722238 2008-12-01 21:28:31Z oheger $
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|