Class DefaultConfigurationNode
- java.lang.Object
-
- org.apache.commons.configuration.tree.DefaultConfigurationNode
-
- All Implemented Interfaces:
java.lang.Cloneable,ConfigurationNode
- Direct Known Subclasses:
HierarchicalConfiguration.Node,ViewNode
public class DefaultConfigurationNode extends java.lang.Object implements ConfigurationNode, java.lang.Cloneable
A default implementation of the
ConfigurationNodeinterface.- Since:
- 1.3
- Version:
- $Id: DefaultConfigurationNode.java 1301991 2012-03-17 20:18:02Z sebb $
- Author:
- Commons Configuration team
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classDefaultConfigurationNode.SubNodesAn internally used helper class for managing a collection of sub nodes.
-
Constructor Summary
Constructors Constructor Description DefaultConfigurationNode()Creates a new uninitialized instance ofDefaultConfigurationNode.DefaultConfigurationNode(java.lang.String name)Creates a new instance ofDefaultConfigurationNodeand initializes it with the node name.DefaultConfigurationNode(java.lang.String name, java.lang.Object value)Creates a new instance ofDefaultConfigurationNodeand initializes it with the name and a value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAttribute(ConfigurationNode attr)Adds the specified attribute to this node.voidaddChild(ConfigurationNode child)Adds a new child to this node.protected voidcheckState()Checks if a modification of this node is allowed.java.lang.Objectclone()Creates a copy of this object.protected DefaultConfigurationNode.SubNodescreateSubNodes(boolean attributes)Creates aSubNodesinstance that is used for storing either this node's children or attributes.ConfigurationNodegetAttribute(int index)Returns the attribute with the given index.intgetAttributeCount()Returns the number of attributes contained in this node.intgetAttributeCount(java.lang.String name)Returns the number of attributes of this node with the given name.java.util.List<ConfigurationNode>getAttributes()Returns a list with the attributes of this node.java.util.List<ConfigurationNode>getAttributes(java.lang.String name)Returns a list with all attributes of this node with the given name.ConfigurationNodegetChild(int index)Returns the child node with the given index.java.util.List<ConfigurationNode>getChildren()Returns a list with all children of this node.java.util.List<ConfigurationNode>getChildren(java.lang.String name)Returns a list of all children with the given name.intgetChildrenCount()Returns the number of all children of this node.intgetChildrenCount(java.lang.String name)Returns the number of children with the given name.java.lang.StringgetName()Returns the name of this node.ConfigurationNodegetParentNode()Returns a reference to this node's parent.java.lang.ObjectgetReference()Returns the reference.java.lang.ObjectgetValue()Returns the value of this node.booleanisAttribute()Checks if this node is an attribute node.booleanisDefined()Returns a flag if this node is defined.booleanremoveAttribute(java.lang.String name)Removes all attributes with the specified name.booleanremoveAttribute(ConfigurationNode node)Removes the specified attribute.voidremoveAttributes()Removes all attributes of this node.booleanremoveChild(java.lang.String childName)Removes all children with the given name.booleanremoveChild(ConfigurationNode child)Removes the specified child node from this node.voidremoveChildren()Removes all child nodes of this node.protected voidremoveReference()Deals with the reference when a node is removed.voidsetAttribute(boolean f)Sets the attribute flag.voidsetName(java.lang.String name)Sets the name of this node.voidsetParentNode(ConfigurationNode parent)Sets the parent of this node.voidsetReference(java.lang.Object reference)Sets the reference.voidsetValue(java.lang.Object val)Sets the value of this node.voidvisit(ConfigurationNodeVisitor visitor)Visits this node and all its sub nodes.
-
-
-
Constructor Detail
-
DefaultConfigurationNode
public DefaultConfigurationNode()
Creates a new uninitialized instance ofDefaultConfigurationNode.
-
DefaultConfigurationNode
public DefaultConfigurationNode(java.lang.String name)
Creates a new instance ofDefaultConfigurationNodeand initializes it with the node name.- Parameters:
name- the name of this node
-
DefaultConfigurationNode
public DefaultConfigurationNode(java.lang.String name, java.lang.Object value)
Creates a new instance ofDefaultConfigurationNodeand initializes it with the name and a value.- Parameters:
name- the node's namevalue- the node's value
-
-
Method Detail
-
getName
public java.lang.String getName()
Returns the name of this node.- Specified by:
getNamein interfaceConfigurationNode- Returns:
- the name of this node
-
setName
public void setName(java.lang.String name)
Sets the name of this node.- Specified by:
setNamein interfaceConfigurationNode- Parameters:
name- the new name
-
getValue
public java.lang.Object getValue()
Returns the value of this node.- Specified by:
getValuein interfaceConfigurationNode- Returns:
- the value of this node
-
setValue
public void setValue(java.lang.Object val)
Sets the value of this node.- Specified by:
setValuein interfaceConfigurationNode- Parameters:
val- the value of this node
-
getReference
public java.lang.Object getReference()
Returns the reference.- Specified by:
getReferencein interfaceConfigurationNode- Returns:
- the reference
-
setReference
public void setReference(java.lang.Object reference)
Sets the reference.- Specified by:
setReferencein interfaceConfigurationNode- Parameters:
reference- the reference object
-
getParentNode
public ConfigurationNode getParentNode()
Returns a reference to this node's parent.- Specified by:
getParentNodein interfaceConfigurationNode- Returns:
- the parent node or null if this is the root
-
setParentNode
public void setParentNode(ConfigurationNode parent)
Sets the parent of this node.- Specified by:
setParentNodein interfaceConfigurationNode- Parameters:
parent- the parent of this node
-
addChild
public void addChild(ConfigurationNode child)
Adds a new child to this node.- Specified by:
addChildin interfaceConfigurationNode- Parameters:
child- the new child
-
getChildren
public java.util.List<ConfigurationNode> getChildren()
Returns a list with all children of this node.- Specified by:
getChildrenin interfaceConfigurationNode- Returns:
- a list with all child nodes
-
getChildrenCount
public int getChildrenCount()
Returns the number of all children of this node.- Specified by:
getChildrenCountin interfaceConfigurationNode- Returns:
- the number of all children
-
getChildren
public java.util.List<ConfigurationNode> getChildren(java.lang.String name)
Returns a list of all children with the given name.- Specified by:
getChildrenin interfaceConfigurationNode- Parameters:
name- the name; can be null , then all children are returned- Returns:
- a list of all children with the given name
-
getChildrenCount
public int getChildrenCount(java.lang.String name)
Returns the number of children with the given name.- Specified by:
getChildrenCountin interfaceConfigurationNode- Parameters:
name- the name; can be null , then the number of all children is returned- Returns:
- the number of child nodes with this name
-
getChild
public ConfigurationNode getChild(int index)
Returns the child node with the given index.- Specified by:
getChildin interfaceConfigurationNode- Parameters:
index- the index (0-based)- Returns:
- the child with this index
-
removeChild
public boolean removeChild(ConfigurationNode child)
Removes the specified child node from this node.- Specified by:
removeChildin interfaceConfigurationNode- Parameters:
child- the node to be removed- Returns:
- a flag if a node was removed
-
removeChild
public boolean removeChild(java.lang.String childName)
Removes all children with the given name.- Specified by:
removeChildin interfaceConfigurationNode- Parameters:
childName- the name of the children to be removed- Returns:
- a flag if at least one child node was removed
-
removeChildren
public void removeChildren()
Removes all child nodes of this node.- Specified by:
removeChildrenin interfaceConfigurationNode
-
isAttribute
public boolean isAttribute()
Checks if this node is an attribute node.- Specified by:
isAttributein interfaceConfigurationNode- Returns:
- a flag if this is an attribute node
-
setAttribute
public void setAttribute(boolean f)
Sets the attribute flag. Note: this method can only be called if the node is not already part of a node hierarchy.- Specified by:
setAttributein interfaceConfigurationNode- Parameters:
f- the attribute flag
-
addAttribute
public void addAttribute(ConfigurationNode attr)
Adds the specified attribute to this node.- Specified by:
addAttributein interfaceConfigurationNode- Parameters:
attr- the attribute to be added
-
getAttributes
public java.util.List<ConfigurationNode> getAttributes()
Returns a list with the attributes of this node. This list containsDefaultConfigurationNodeobjects, too.- Specified by:
getAttributesin interfaceConfigurationNode- Returns:
- the attribute list, never null
-
getAttributeCount
public int getAttributeCount()
Returns the number of attributes contained in this node.- Specified by:
getAttributeCountin interfaceConfigurationNode- Returns:
- the number of attributes
-
getAttributes
public java.util.List<ConfigurationNode> getAttributes(java.lang.String name)
Returns a list with all attributes of this node with the given name.- Specified by:
getAttributesin interfaceConfigurationNode- Parameters:
name- the attribute's name- Returns:
- all attributes with this name
-
getAttributeCount
public int getAttributeCount(java.lang.String name)
Returns the number of attributes of this node with the given name.- Specified by:
getAttributeCountin interfaceConfigurationNode- Parameters:
name- the name- Returns:
- the number of attributes with this name
-
removeAttribute
public boolean removeAttribute(ConfigurationNode node)
Removes the specified attribute.- Specified by:
removeAttributein interfaceConfigurationNode- Parameters:
node- the attribute node to be removed- Returns:
- a flag if the attribute could be removed
-
removeAttribute
public boolean removeAttribute(java.lang.String name)
Removes all attributes with the specified name.- Specified by:
removeAttributein interfaceConfigurationNode- Parameters:
name- the name- Returns:
- a flag if at least one attribute was removed
-
getAttribute
public ConfigurationNode getAttribute(int index)
Returns the attribute with the given index.- Specified by:
getAttributein interfaceConfigurationNode- Parameters:
index- the index (0-based)- Returns:
- the attribute with this index
-
removeAttributes
public void removeAttributes()
Removes all attributes of this node.- Specified by:
removeAttributesin interfaceConfigurationNode
-
isDefined
public boolean isDefined()
Returns a flag if this node is defined. This means that the node contains some data.- Specified by:
isDefinedin interfaceConfigurationNode- Returns:
- a flag whether this node is defined
-
visit
public void visit(ConfigurationNodeVisitor visitor)
Visits this node and all its sub nodes.- Specified by:
visitin interfaceConfigurationNode- Parameters:
visitor- the visitor- See Also:
ConfigurationNodeVisitor
-
clone
public java.lang.Object clone()
Creates a copy of this object. This is not a deep copy, the children are not cloned.- Specified by:
clonein interfaceConfigurationNode- Overrides:
clonein classjava.lang.Object- Returns:
- a copy of this object
-
checkState
protected void checkState()
Checks if a modification of this node is allowed. Some properties of a node must not be changed when the node has a parent. This method checks this and throws a runtime exception if necessary.
-
createSubNodes
protected DefaultConfigurationNode.SubNodes createSubNodes(boolean attributes)
Creates aSubNodesinstance that is used for storing either this node's children or attributes.- Parameters:
attributes- true if the returned instance is used for storing attributes, false for storing child nodes- Returns:
- the
SubNodesobject to use
-
removeReference
protected void removeReference()
Deals with the reference when a node is removed. This method is called for each removed child node or attribute. It can be overloaded in sub classes, for which the reference has a concrete meaning and remove operations need some update actions. This default implementation is empty.
-
-