org.apache.commons.configuration.tree

Class DefaultConfigurationNode

public class DefaultConfigurationNode extends Object implements ConfigurationNode, Cloneable

A default implementation of the ConfigurationNode interface.

Since: 1.3

Author: Oliver Heger

Nested Class Summary
protected static classDefaultConfigurationNode.SubNodes
An internally used helper class for managing a collection of sub nodes.
Field Summary
booleanattribute
Stores a flag if this is an attribute.
DefaultConfigurationNode.SubNodesattributes
Stores the attributes of this node.
DefaultConfigurationNode.SubNodeschildren
Stores the children of this node.
Stringname
Stores the name of this node.
ConfigurationNodeparent
Stores a reference to this node's parent.
Objectreference
Stores the reference.
Objectvalue
Stores the value of this node.
Constructor Summary
DefaultConfigurationNode()
Creates a new uninitialized instance of DefaultConfigurationNode.
DefaultConfigurationNode(String name)
Creates a new instance of DefaultConfigurationNode and initializes it with the node name.
DefaultConfigurationNode(String name, Object value)
Creates a new instance of DefaultConfigurationNode and initializes it with the name and a value.
Method Summary
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.
Objectclone()
Creates a copy of this object.
protected DefaultConfigurationNode.SubNodescreateSubNodes(boolean attributes)
Creates a SubNodes instance 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(String name)
Returns the number of attributes of this node with the given name.
ListgetAttributes()
Returns a list with the attributes of this node.
ListgetAttributes(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.
ListgetChildren()
Returns a list with all children of this node.
ListgetChildren(String name)
Returns a list of all children with the given name.
intgetChildrenCount()
Returns the number of all children of this node.
intgetChildrenCount(String name)
Returns the number of children with the given name.
StringgetName()
Returns the name of this node.
ConfigurationNodegetParentNode()
Returns a reference to this node's parent.
ObjectgetReference()
Returns the reference.
ObjectgetValue()
Returns the value of this node.
voidinitSubNodes()
Helper method for initializing the sub nodes objects.
booleanisAttribute()
Checks if this node is an attribute node.
booleanisDefined()
Returns a flag if this node is defined.
booleanremoveAttribute(ConfigurationNode node)
Removes the specified attribute.
booleanremoveAttribute(String name)
Removes all attributes with the specified name.
voidremoveAttributes()
Removes all attributes of this node.
booleanremoveChild(ConfigurationNode child)
Removes the specified child node from this node.
booleanremoveChild(String childName)
Removes all children with the given name.
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(String name)
Sets the name of this node.
voidsetParentNode(ConfigurationNode parent)
Sets the parent of this node.
voidsetReference(Object reference)
Sets the reference.
voidsetValue(Object val)
Sets the value of this node.
voidvisit(ConfigurationNodeVisitor visitor)
Visits this node and all its sub nodes.

Field Detail

attribute

private boolean attribute
Stores a flag if this is an attribute.

attributes

private DefaultConfigurationNode.SubNodes attributes
Stores the attributes of this node.

children

private DefaultConfigurationNode.SubNodes children
Stores the children of this node.

name

private String name
Stores the name of this node.

parent

private ConfigurationNode parent
Stores a reference to this node's parent.

reference

private Object reference
Stores the reference.

value

private Object value
Stores the value of this node.

Constructor Detail

DefaultConfigurationNode

public DefaultConfigurationNode()
Creates a new uninitialized instance of DefaultConfigurationNode.

DefaultConfigurationNode

public DefaultConfigurationNode(String name)
Creates a new instance of DefaultConfigurationNode and initializes it with the node name.

Parameters: name the name of this node

DefaultConfigurationNode

public DefaultConfigurationNode(String name, Object value)
Creates a new instance of DefaultConfigurationNode and initializes it with the name and a value.

Parameters: name the node's name value the node's value

Method Detail

addAttribute

public void addAttribute(ConfigurationNode attr)
Adds the specified attribute to this node.

Parameters: attr the attribute to be added

addChild

public void addChild(ConfigurationNode child)
Adds a new child to this node.

Parameters: child the new child

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.

clone

public Object clone()
Creates a copy of this object. This is not a deep copy, the children are not cloned.

Returns: a copy of this object

createSubNodes

protected DefaultConfigurationNode.SubNodes createSubNodes(boolean attributes)
Creates a SubNodes instance 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 SubNodes object to use

getAttribute

public ConfigurationNode getAttribute(int index)
Returns the attribute with the given index.

Parameters: index the index (0-based)

Returns: the attribute with this index

getAttributeCount

public int getAttributeCount()
Returns the number of attributes contained in this node.

Returns: the number of attributes

getAttributeCount

public int getAttributeCount(String name)
Returns the number of attributes of this node with the given name.

Parameters: name the name

Returns: the number of attributes with this name

getAttributes

public List getAttributes()
Returns a list with the attributes of this node. This list contains ConfigurationNode objects, too.

Returns: the attribute list, never null

getAttributes

public List getAttributes(String name)
Returns a list with all attributes of this node with the given name.

Parameters: name the attribute's name

Returns: all attributes with this name

getChild

public ConfigurationNode getChild(int index)
Returns the child node with the given index.

Parameters: index the index (0-based)

Returns: the child with this index

getChildren

public List getChildren()
Returns a list with all children of this node.

Returns: a list with all child nodes

getChildren

public List getChildren(String name)
Returns a list of all children with the given name.

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()
Returns the number of all children of this node.

Returns: the number of all children

getChildrenCount

public int getChildrenCount(String name)
Returns the number of children with the given name.

Parameters: name the name; can be null , then the number of all children is returned

Returns: the number of child nodes with this name

getName

public String getName()
Returns the name of this node.

Returns: the name of this node

getParentNode

public ConfigurationNode getParentNode()
Returns a reference to this node's parent.

Returns: the parent node or null if this is the root

getReference

public Object getReference()
Returns the reference.

Returns: the reference

getValue

public Object getValue()
Returns the value of this node.

Returns: the value of this node

initSubNodes

private void initSubNodes()
Helper method for initializing the sub nodes objects.

isAttribute

public boolean isAttribute()
Checks if this node is an attribute node.

Returns: a flag if this is an attribute node

isDefined

public boolean isDefined()
Returns a flag if this node is defined. This means that the node contains some data.

Returns: a flag whether this node is defined

removeAttribute

public boolean removeAttribute(ConfigurationNode node)
Removes the specified attribute.

Parameters: node the attribute node to be removed

Returns: a flag if the attribute could be removed

removeAttribute

public boolean removeAttribute(String name)
Removes all attributes with the specified name.

Parameters: name the name

Returns: a flag if at least one attribute was removed

removeAttributes

public void removeAttributes()
Removes all attributes of this node.

removeChild

public boolean removeChild(ConfigurationNode child)
Removes the specified child node from this node.

Parameters: child the node to be removed

Returns: a flag if a node was removed

removeChild

public boolean removeChild(String childName)
Removes all children with the given name.

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.

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.

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.

Parameters: f the attribute flag

setName

public void setName(String name)
Sets the name of this node.

Parameters: name the new name

setParentNode

public void setParentNode(ConfigurationNode parent)
Sets the parent of this node.

Parameters: parent the parent of this node

setReference

public void setReference(Object reference)
Sets the reference.

Parameters: reference the reference object

setValue

public void setValue(Object val)
Sets the value of this node.

Parameters: val the value of this node

visit

public void visit(ConfigurationNodeVisitor visitor)
Visits this node and all its sub nodes.

Parameters: visitor the visitor