org.apache.commons.configuration
public class HierarchicalConfiguration extends AbstractConfiguration implements Serializable, Cloneable
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. Similarily 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.
Version: $Id: HierarchicalConfiguration.java,v 1.14 2004/12/02 22:05:52 ebourg Exp $
Nested Class Summary | |
---|---|
protected abstract static class | HierarchicalConfiguration.BuilderVisitor
A specialized visitor base class that can be used for storing the tree of
configuration nodes. |
static class | HierarchicalConfiguration.CloneVisitor
A specialized visitor that is able to create a deep copy of a node
hierarchy. |
class | HierarchicalConfiguration.DefinedKeysVisitor
A specialized visitor that fills a list with keys that are defined in a
node hierarchy. |
static class | HierarchicalConfiguration.DefinedVisitor
A specialized visitor that checks if a node is defined.
|
static class | HierarchicalConfiguration.Node
A data class for storing (hierarchical) property information. |
static class | HierarchicalConfiguration.NodeVisitor Definition of a visitor class for traversing a node and all of its children. This class defines the interface of a visitor for
|
Field Summary | |
---|---|
static ExpressionEngine | defaultExpressionEngine Stores the default expression engine to be used for new objects. |
ExpressionEngine | expressionEngine Stores the expression engine for this instance. |
static int | EVENT_ADD_NODES Constant for the add nodes event. |
static int | EVENT_CLEAR_TREE Constant for the clear tree event. |
HierarchicalConfiguration.Node | root Stores the root node of this configuration. |
ConfigurationNode | rootNode Stores the root configuration node. |
static long | serialVersionUID
The serial version UID. |
Constructor Summary | |
---|---|
HierarchicalConfiguration()
Creates a new instance of HierarchicalConfiguration . | |
HierarchicalConfiguration(HierarchicalConfiguration c)
Creates a new instance of HierarchicalConfiguration and
copies all data contained in the specified configuration into the new
one.
|
Method Summary | |
---|---|
void | addNodes(String key, Collection nodes)
Adds a collection of nodes at the specified position of the configuration
tree. |
protected void | addPropertyDirect(String key, Object obj)
Adds the property with the specified key. |
protected void | clearNode(HierarchicalConfiguration.Node node)
Clears the value of the specified node. |
protected void | clearNode(ConfigurationNode node)
Clears the value of the specified node. |
void | clearProperty(String key)
Removes the property with the given key. |
protected static void | clearReferences(ConfigurationNode node)
Clears all reference fields in a node structure. |
void | clearTree(String key)
Removes all values of the property with the given name and of keys that
start with this name. |
Object | clone()
Creates a copy of this object. |
SubnodeConfiguration | configurationAt(String key) Returns a hierarchical subnode configuration object that wraps the configuration node specified by the given key. |
List | configurationsAt(String key)
Returns a list of sub configurations for all configuration nodes selected
by the given key. |
boolean | containsKey(String key)
Checks if the specified key is contained in this configuration. |
protected HierarchicalConfiguration.Node | createAddPath(ConfigurationKey.KeyIterator keyIt, HierarchicalConfiguration.Node root)
Creates the missing nodes for adding a new property. |
protected HierarchicalConfiguration.Node | createNode(String name)
Creates a new Node object with the specified name. |
protected SubnodeConfiguration | createSubnodeConfiguration(ConfigurationNode node)
Creates a subnode configuration for the specified node. |
protected HierarchicalConfiguration.Node | fetchAddNode(ConfigurationKey.KeyIterator keyIt, HierarchicalConfiguration.Node startNode)
Returns a reference to the parent node of an add operation. |
protected List | fetchNodeList(String key)
Helper method for fetching a list of all nodes that are addressed by the
specified key.
|
protected HierarchicalConfiguration.Node | findLastPathNode(ConfigurationKey.KeyIterator keyIt, HierarchicalConfiguration.Node node)
Finds the last existing node for an add operation. |
protected void | findPropertyNodes(ConfigurationKey.KeyIterator keyPart, HierarchicalConfiguration.Node node, Collection nodes)
Recursive helper method for fetching a property. |
static ExpressionEngine | getDefaultExpressionEngine()
Returns the default expression engine.
|
ExpressionEngine | getExpressionEngine()
Returns the expression engine used by this configuration. |
Iterator | getKeys()
Returns an iterator with all keys defined in this configuration.
|
Iterator | getKeys(String prefix)
Returns an iterator with all keys defined in this configuration that
start with the given prefix. |
int | getMaxIndex(String key)
Returns the maximum defined index for the given key. |
Object | getProperty(String key)
Fetches the specified property. |
HierarchicalConfiguration.Node | getRoot()
Returns the root node of this hierarchical configuration. |
ConfigurationNode | getRootNode()
Returns the root node of this hierarchical configuration.
|
boolean | isEmpty()
Checks if this configuration is empty. |
protected boolean | nodeDefined(HierarchicalConfiguration.Node node)
Checks if the specified node is defined.
|
protected boolean | nodeDefined(ConfigurationNode node)
Checks if the specified node is defined.
|
ConfigurationNode | processNodeAddData(NodeAddData data)
Helper method for processing a node add data object obtained from the
expression engine. |
protected void | removeNode(HierarchicalConfiguration.Node node)
Removes the specified node from this configuration. |
protected void | removeNode(ConfigurationNode node)
Removes the specified node from this configuration. |
static void | setDefaultExpressionEngine(ExpressionEngine engine)
Sets the default expression engine. |
void | setExpressionEngine(ExpressionEngine expressionEngine)
Sets the expression engine to be used by this configuration. |
void | setProperty(String key, Object value)
Sets the value of the specified property.
|
void | setRoot(HierarchicalConfiguration.Node node)
Sets the root node of this hierarchical configuration. |
void | setRootNode(ConfigurationNode rootNode)
Sets the root node of this hierarchical configuration.
|
Configuration | subset(String prefix)
Creates a new Configuration object containing all keys
that start with the specified prefix. |
HierarchicalConfiguration
.HierarchicalConfiguration
and
copies all data contained in the specified configuration into the new
one.
Parameters: c the configuration that is to be copied (if null, this constructor will behave like the standard constructor)
Since: 1.4
addProperty()
, but
instead of a single property a whole collection of nodes can be added -
and thus complete configuration sub trees. E.g. with this method it is
possible to add parts of another HierarchicalConfiguration
object to this object. If the passed in key refers to an existing and
unique node, the new nodes are added to this node. Otherwise a new node
will be created at the specified position in the hierarchy.
Parameters: key the key where the nodes are to be added; can be null ,
then they are added to the root node nodes a collection with the Node
objects to be
added
ExpressionEngine
, so the passed in key
must match the requirements of this implementation.
Parameters: key the key of the new property obj the value of the new property
Deprecated: Use the method clearNode
instead
Parameters: node the node to be cleard
Parameters: node the node to be cleard
Parameters: key the key of the property to be removed
Parameters: node the root node of the node hierarchy, in which the references are to be cleared
Since: 1.4
clearTree("foo")
would remove both properties.
Parameters: key the key of the property to be removed
Returns: the copy
Since: 1.2
Returns a hierarchical subnode configuration object that wraps the
configuration node specified by the given key. This method provides an
easy means of accessing sub trees of a hierarchical configuration. In the
returned configuration the sub tree can directly be accessed, it becomes
the root node of this configuration. Because of this the passed in key
must select exactly one configuration node; otherwise an
IllegalArgumentException
will be thrown.
The difference between this method and the
subset
method is that
subset()
supports arbitrary subsets of configuration nodes
while configurationAt()
only returns a single sub tree.
Please refer to the documentation of the
SubnodeConfiguration
class to obtain further information
about subnode configurations and when they should be used.
Parameters: key the key that selects the sub tree
Returns: a hierarchical configuration that contains this sub tree
Since: 1.3
See Also: SubnodeConfiguration
ExpressionEngine
) and then create a subnode
configuration for each returned node (like
configurationAt
}). This is especially
useful when dealing with list-like structures. As an example consider the
configuration that contains data about database tables and their fields.
If you need access to all fields of a certain table, you can simply do
List fields = config.configurationsAt("tables.table(0).fields.field"); for(Iterator it = fields.iterator(); it.hasNext();) { HierarchicalConfiguration sub = (HierarchicalConfiguration) it.next(); // now the children and attributes of the field node can be // directly accessed String fieldName = sub.getString("name"); String fieldType = sub.getString("type"); ...
Parameters: key the key for selecting the desired nodes
Returns: a list with hierarchical configuration objects; each configuration represents one of the nodes selected by the passed in key
Since: 1.3
Parameters: key the key to be chekced
Returns: a flag if this key is contained in this configuration
Deprecated: Adding new properties is now to a major part delegated to the
ExpressionEngine
associated with this configuration instance.
This method will no longer be called. Developers who want to modify the
process of adding new properties should consider implementing their own
expression engine.
Parameters: keyIt the key iterator root the base node of the path to be created
Returns: the last node of the path
Node
object with the specified name. This
method can be overloaded in derived classes if a specific node type is
needed. This base implementation always returns a new object of the
Node
class.
Parameters: name the name of the new node
Returns: the new node
configurationAt()
and
configurationsAt()
.
Parameters: node the node, for which a subnode configuration is to be created
Returns: the configuration for the given node
Since: 1.3
Deprecated: Adding new properties is now to a major part delegated to the
ExpressionEngine
associated with this configuration instance.
This method will no longer be called. Developers who want to modify the
process of adding new properties should consider implementing their own
expression engine.
Parameters: keyIt the iterator for the key of the new property startNode the node to start the search with
Returns: the parent node for the add operation
Parameters: key the key
Returns: a list with all affected nodes (never null )
Deprecated: Adding new properties is now to a major part delegated to the
ExpressionEngine
associated with this configuration instance.
This method will no longer be called. Developers who want to modify the
process of adding new properties should consider implementing their own
expression engine.
Parameters: keyIt the key iterator node the actual node
Returns: the last existing node on the given path
Deprecated: Property keys are now evaluated by the expression engine
associated with the configuration; this method will no longer be called.
If you want to modify the way properties are looked up, consider
implementing you own ExpressionEngine
implementation.
Parameters: keyPart the configuration key iterator node the actual node nodes here the found nodes are stored
Returns: the default expression engine
Since: 1.3
Returns: the current expression engine
Since: 1.3
Returns: an iterator with the defined keys in this configuration
Parameters: prefix the prefix of the keys to start with
Returns: an iterator with the found keys
Parameters: key the key to be checked
Returns: the maximum defined index for this key
Parameters: key the key to be looked up
Returns: the found value
getRootNode
method instead, which operates on
the preferred data type ConfigurationNode
.
Returns: the root node
Returns: the root node
Since: 1.3
Returns: a flag if this configuration is empty
Deprecated: Use the method nodeDefined
instead.
Parameters: node the node to be checked
Returns: a flag if this node is defined
Parameters: node the node to be checked
Returns: a flag if this node is defined
Parameters: data the data object
Returns: the new node
Since: 1.3
Deprecated: Use the method removeNode
instead.
Parameters: node the node to be removed
Parameters: node the node to be removed
Parameters: engine the new default expression engine
Since: 1.3
Parameters: expressionEngine the new expression engine; can be null, then the default expression engine will be used
Since: 1.3
Parameters: key the key of the property to set value the new value of this property
setRootNode
method instead,
which operates on the preferred data type ConfigurationNode
.
Parameters: node the root node
Parameters: rootNode the root node
Since: 1.3
Configuration
object containing all keys
that start with the specified prefix. This implementation will return a
HierarchicalConfiguration
object so that the structure of
the keys will be saved.
Parameters: prefix the prefix of the keys for the subset
Returns: a new configuration object representing the selected subset