You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2006/04/15 18:53:29 UTC
svn commit: r394331 - in /jakarta/commons/proper/configuration/trunk:
src/java/org/apache/commons/configuration/
src/java/org/apache/commons/configuration/tree/
src/test/org/apache/commons/configuration/ xdocs/
Author: oheger
Date: Sat Apr 15 09:53:28 2006
New Revision: 394331
URL: http://svn.apache.org/viewcvs?rev=394331&view=rev
Log:
Deeper integration of ConfigurationNode interface into HierarchicalConfiguration; the old Node type was replaced by ConfigurationNode whereever possible.
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/tree/ConfigurationNode.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?rev=394331&r1=394330&r2=394331&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java Sat Apr 15 09:53:28 2006
@@ -103,14 +103,30 @@
/** Stores the default expression engine to be used for new objects.*/
private static ExpressionEngine defaultExpressionEngine = new DefaultExpressionEngine();
- /** Stores the root node of this configuration. */
- private Node root = new Node();
+ /** Stores the root node of this configuration. This field is required for
+ * backwards compatibility only.
+ */
+ private Node root;
+
+ /** Stores the root configuration node.*/
+ private ConfigurationNode rootNode;
/** Stores the expression engine for this instance.*/
private ExpressionEngine expressionEngine;
/**
- * Returns the root node of this hierarchical configuration.
+ * Creates a new instance of <code>HierarchicalConfiguration</code>.
+ */
+ public HierarchicalConfiguration()
+ {
+ setRootNode(new Node());
+ }
+
+ /**
+ * Returns the root node of this hierarchical configuration. This method
+ * exists for backwards compatibility only. New code should use the
+ * <code>{@link #getRootNode()}</code> method instead, which operates on
+ * the preferred data type <code>ConfigurationNode</code>.
*
* @return the root node
*/
@@ -120,7 +136,10 @@
}
/**
- * Sets the root node of this hierarchical configuration.
+ * Sets the root node of this hierarchical configuration. This method
+ * exists for backwards compatibility only. New code should use the
+ * <code>{@link #setRootNode(ConfigurationNode)}</code> method instead,
+ * which operates on the preferred data type <code>ConfigurationNode</code>.
*
* @param node the root node
*/
@@ -131,6 +150,36 @@
throw new IllegalArgumentException("Root node must not be null!");
}
root = node;
+ rootNode = null;
+ }
+
+ /**
+ * Returns the root node of this hierarchical configuration.
+ *
+ * @return the root node
+ * @since 1.3
+ */
+ public ConfigurationNode getRootNode()
+ {
+ return (rootNode != null) ? rootNode : root;
+ }
+
+ /**
+ * Sets the root node of this hierarchical configuration.
+ *
+ * @param node the root node
+ * @since 1.3
+ */
+ public void setRootNode(ConfigurationNode rootNode)
+ {
+ if (rootNode == null)
+ {
+ throw new IllegalArgumentException("Root node must not be null!");
+ }
+ this.rootNode = rootNode;
+
+ // For backward compatibility also set the old root field.
+ root = (rootNode instanceof Node) ? (Node) rootNode : new Node(rootNode);
}
/**
@@ -211,7 +260,7 @@
List list = new ArrayList();
for (Iterator it = nodes.iterator(); it.hasNext();)
{
- Node node = (Node) it.next();
+ ConfigurationNode node = (ConfigurationNode) it.next();
if (node.getValue() != null)
{
list.add(node.getValue());
@@ -239,7 +288,7 @@
*/
protected void addPropertyDirect(String key, Object obj)
{
- NodeAddData data = getExpressionEngine().prepareAdd(getRoot(), key);
+ NodeAddData data = getExpressionEngine().prepareAdd(getRootNode(), key);
ConfigurationNode node = processNodeAddData(data);
node.setValue(obj);
}
@@ -277,7 +326,7 @@
{
// otherwise perform an add operation
parent = processNodeAddData(getExpressionEngine().prepareAdd(
- getRoot(), key));
+ getRootNode(), key));
}
if (parent.isAttribute())
@@ -307,7 +356,7 @@
*/
public boolean isEmpty()
{
- return !nodeDefined(getRoot());
+ return !nodeDefined(getRootNode());
}
/**
@@ -332,16 +381,16 @@
for (Iterator it = nodes.iterator(); it.hasNext();)
{
- Node nd = (Node) it.next();
- nd.visit(visitor, null);
+ ConfigurationNode nd = (ConfigurationNode) it.next();
+ nd.visit(visitor);
for (Iterator it2 = visitor.getClone().getChildren().iterator(); it2.hasNext();)
{
- result.getRoot().addChild((Node) it2.next());
+ result.getRootNode().addChild((ConfigurationNode) it2.next());
}
for (Iterator it2 = visitor.getClone().getAttributes().iterator(); it2.hasNext();)
{
- result.getRoot().addAttribute((Node) it2.next());
+ result.getRootNode().addAttribute((ConfigurationNode) it2.next());
}
}
@@ -382,7 +431,7 @@
throw new IllegalArgumentException(
"Passed in key must select exactly one node: " + key);
}
- return createSubnodeConfiguration((Node) nodes.get(0));
+ return createSubnodeConfiguration((ConfigurationNode) nodes.get(0));
}
/**
@@ -418,7 +467,7 @@
List configs = new ArrayList(nodes.size());
for (Iterator it = nodes.iterator(); it.hasNext();)
{
- configs.add(createSubnodeConfiguration((Node) it.next()));
+ configs.add(createSubnodeConfiguration((ConfigurationNode) it.next()));
}
return configs;
}
@@ -432,7 +481,7 @@
* @return the configuration for the given node
* @since 1.3
*/
- protected HierarchicalConfiguration createSubnodeConfiguration(Node node)
+ protected HierarchicalConfiguration createSubnodeConfiguration(ConfigurationNode node)
{
return new SubnodeConfiguration(this, node);
}
@@ -472,7 +521,7 @@
}
while (itNodes.hasNext() && itValues.hasNext())
{
- ((Node) itNodes.next()).setValue(itValues.next());
+ ((ConfigurationNode) itNodes.next()).setValue(itValues.next());
}
// Add additional nodes if necessary
@@ -484,7 +533,7 @@
// Remove remaining nodes
while (itNodes.hasNext())
{
- clearNode((Node) itNodes.next());
+ clearNode((ConfigurationNode) itNodes.next());
}
}
@@ -502,7 +551,7 @@
for (Iterator it = nodes.iterator(); it.hasNext();)
{
- removeNode((Node) it.next());
+ removeNode((ConfigurationNode) it.next());
}
}
@@ -519,7 +568,7 @@
for (Iterator it = nodes.iterator(); it.hasNext();)
{
- clearNode((Node) it.next());
+ clearNode((ConfigurationNode) it.next());
}
}
@@ -533,7 +582,7 @@
public Iterator getKeys()
{
DefinedKeysVisitor visitor = new DefinedKeysVisitor();
- getRoot().visit(visitor);
+ getRootNode().visit(visitor);
return visitor.getKeyList().iterator();
}
@@ -553,14 +602,14 @@
for (Iterator itNodes = nodes.iterator(); itNodes.hasNext();)
{
- Node node = (Node) itNodes.next();
+ ConfigurationNode node = (ConfigurationNode) itNodes.next();
for (Iterator it = node.getChildren().iterator(); it.hasNext();)
{
- ((Node) it.next()).visit(visitor);
+ ((ConfigurationNode) it.next()).visit(visitor);
}
for (Iterator it = node.getAttributes().iterator(); it.hasNext();)
{
- ((Node) it.next()).visit(visitor);
+ ((ConfigurationNode) it.next()).visit(visitor);
}
}
@@ -597,8 +646,8 @@
// clone the nodes, too
CloneVisitor v = new CloneVisitor();
- getRoot().visit(v, null);
- copy.setRoot(v.getClone());
+ getRootNode().visit(v);
+ copy.setRootNode(v.getClone());
return copy;
}
@@ -618,7 +667,7 @@
*/
protected List fetchNodeList(String key)
{
- return getExpressionEngine().query(getRoot(), key);
+ return getExpressionEngine().query(getRootNode(), key);
}
/**
@@ -644,11 +693,24 @@
*
* @param node the node to be checked
* @return a flag if this node is defined
+ * @deprecated Use the method <code>{@link #nodeDefined(ConfigurationNode)}</code>
+ * instead.
*/
protected boolean nodeDefined(Node node)
{
+ return nodeDefined((ConfigurationNode) node);
+ }
+
+ /**
+ * Checks if the specified node is defined.
+ *
+ * @param node the node to be checked
+ * @return a flag if this node is defined
+ */
+ protected boolean nodeDefined(ConfigurationNode node)
+ {
DefinedVisitor visitor = new DefinedVisitor();
- node.visit(visitor, null);
+ node.visit(visitor);
return visitor.isDefined();
}
@@ -658,13 +720,27 @@
* removed.
*
* @param node the node to be removed
+ * @deprecated Use the method <code>{@link #removeNode(ConfigurationNode)}</code>
+ * instead.
*/
protected void removeNode(Node node)
{
- Node parent = node.getParent();
+ removeNode((ConfigurationNode) node);
+ }
+
+ /**
+ * Removes the specified node from this configuration. This method ensures
+ * that parent nodes that become undefined by this operation are also
+ * removed.
+ *
+ * @param node the node to be removed
+ */
+ protected void removeNode(ConfigurationNode node)
+ {
+ ConfigurationNode parent = node.getParentNode();
if (parent != null)
{
- parent.remove(node);
+ parent.removeChild(node);
if (!nodeDefined(parent))
{
removeNode(parent);
@@ -677,9 +753,22 @@
* this operation, it is removed from the hierarchy.
*
* @param node the node to be cleard
+ * @deprecated Use the method <code>{@link #clearNode(ConfigurationNode)}</code>
+ * instead
*/
protected void clearNode(Node node)
{
+ clearNode((ConfigurationNode) node);
+ }
+
+ /**
+ * Clears the value of the specified node. If the node becomes undefined by
+ * this operation, it is removed from the hierarchy.
+ *
+ * @param node the node to be cleard
+ */
+ protected void clearNode(ConfigurationNode node)
+ {
node.setValue(null);
if (!nodeDefined(node))
{
@@ -829,6 +918,27 @@
}
/**
+ * Creates a new instance of <code>Node</code> based on the given
+ * source node. All properties of the source node, including its
+ * children and attributes, will be copied.
+ *
+ * @param src the node to be copied
+ */
+ public Node(ConfigurationNode src)
+ {
+ this(src.getName(), src.getValue());
+ setReference(src.getReference());
+ for (Iterator it = src.getChildren().iterator(); it.hasNext();)
+ {
+ addChild((ConfigurationNode) it.next());
+ }
+ for (Iterator it = src.getAttributes().iterator(); it.hasNext();)
+ {
+ addAttribute((ConfigurationNode) it.next());
+ }
+ }
+
+ /**
* Returns the parent of this node.
*
* @return this node's parent (can be <b>null</b>)
@@ -990,7 +1100,7 @@
* its sub nodes is associated with a value.
*
*/
- static class DefinedVisitor extends NodeVisitor
+ static class DefinedVisitor extends ConfigurationNodeVisitorAdapter
{
/** Stores the defined flag. */
private boolean defined;
@@ -1010,9 +1120,8 @@
* Visits the node. Checks if a value is defined.
*
* @param node the actual node
- * @param key the key of this node
*/
- public void visitBeforeChildren(Node node, ConfigurationKey key)
+ public void visitBeforeChildren(ConfigurationNode node)
{
defined = node.getValue() != null;
}
@@ -1106,7 +1215,7 @@
* hierarchy.
*
*/
- static class CloneVisitor extends NodeVisitor
+ static class CloneVisitor extends ConfigurationNodeVisitorAdapter
{
/** A stack with the actual object to be copied. */
private Stack copyStack;
@@ -1126,9 +1235,8 @@
* Visits the specified node after its children have been processed.
*
* @param node the node
- * @param key the key of this node
*/
- public void visitAfterChildren(Node node, ConfigurationKey key)
+ public void visitAfterChildren(ConfigurationNode node)
{
Node copy = (Node) copyStack.pop();
if (copyStack.isEmpty())
@@ -1141,11 +1249,10 @@
* Visits and copies the specified node.
*
* @param node the node
- * @param key the key of this node
*/
- public void visitBeforeChildren(Node node, ConfigurationKey key)
+ public void visitBeforeChildren(ConfigurationNode node)
{
- Node copy = (Node) node.clone();
+ ConfigurationNode copy = (ConfigurationNode) node.clone();
copy.setParentNode(null);
if (!copyStack.isEmpty())
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java?rev=394331&r1=394330&r2=394331&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/SubnodeConfiguration.java Sat Apr 15 09:53:28 2006
@@ -15,6 +15,7 @@
*/
package org.apache.commons.configuration;
+import org.apache.commons.configuration.tree.ConfigurationNode;
import org.apache.commons.configuration.tree.ExpressionEngine;
/**
@@ -76,7 +77,7 @@
* @param parent the parent configuration
* @param root the root node of this subnode configuration
*/
- public SubnodeConfiguration(HierarchicalConfiguration parent, Node root)
+ public SubnodeConfiguration(HierarchicalConfiguration parent, ConfigurationNode root)
{
if (parent == null)
{
@@ -88,7 +89,7 @@
throw new IllegalArgumentException("Root node must not be null!");
}
- setRoot(root);
+ setRootNode(root);
this.parent = parent;
}
@@ -197,7 +198,7 @@
* @param node the sub node, for which the configuration is to be created
* @return a hierarchical configuration for this sub node
*/
- protected HierarchicalConfiguration createSubnodeConfiguration(Node node)
+ protected HierarchicalConfiguration createSubnodeConfiguration(ConfigurationNode node)
{
return new SubnodeConfiguration(getParent(), node);
}
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/tree/ConfigurationNode.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/tree/ConfigurationNode.java?rev=394331&r1=394330&r2=394331&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/tree/ConfigurationNode.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/tree/ConfigurationNode.java Sat Apr 15 09:53:28 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 2001-2005 The Apache Software Foundation.
+ * Copyright 2001-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
*
* @since 1.3
* @author Oliver Heger
+ * @version $Id$
*/
public interface ConfigurationNode
{
@@ -254,4 +255,10 @@
* @param visitor the visitor
*/
void visit(ConfigurationNodeVisitor visitor);
+
+ /**
+ * Returns a copy of this node.
+ * @return the copy
+ */
+ Object clone();
}
Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=394331&r1=394330&r2=394331&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java Sat Apr 15 09:53:28 2006
@@ -25,6 +25,7 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.configuration.HierarchicalConfiguration.Node;
+import org.apache.commons.configuration.tree.DefaultConfigurationNode;
import org.apache.commons.configuration.tree.DefaultExpressionEngine;
import org.apache.commons.configuration.tree.ExpressionEngine;
@@ -95,6 +96,29 @@
config.setRoot(new HierarchicalConfiguration.Node("test"));
assertTrue(config.isEmpty());
+ }
+
+ public void testSetRootNode()
+ {
+ config.setRootNode(new DefaultConfigurationNode("testNode"));
+ assertNotSame("Same root node", config.getRootNode(), config.getRoot());
+ assertEquals("Wrong name of root node", "testNode", config.getRoot().getName());
+
+ config.setRootNode(new HierarchicalConfiguration.Node("test"));
+ assertSame("Wrong root node", config.getRootNode(), config.getRoot());
+ }
+
+ public void testSetRootNodeNull()
+ {
+ try
+ {
+ config.setRootNode(null);
+ fail("Could set null root node!");
+ }
+ catch(IllegalArgumentException iex)
+ {
+ //ok
+ }
}
public void testIsEmpty()
Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java?rev=394331&r1=394330&r2=394331&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestSubnodeConfiguration.java Sat Apr 15 09:53:28 2006
@@ -307,11 +307,10 @@
* @param conf the parent config
* @return the root node for the subnode config
*/
- protected HierarchicalConfiguration.Node getSubnodeRoot(
- HierarchicalConfiguration conf)
+ protected ConfigurationNode getSubnodeRoot(HierarchicalConfiguration conf)
{
ConfigurationNode root = conf.getRoot();
- return (HierarchicalConfiguration.Node) root.getChild(0).getChild(0);
+ return root.getChild(0).getChild(0);
}
/**
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=394331&r1=394330&r2=394331&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Apr 15 09:53:28 2006
@@ -92,7 +92,10 @@
provided. HierarchicalConfiguration.Node now extends this default
implementation. The new ConfigurationNode interface defines some more
methods than the Node class did originally for conveniently dealing with
- sub nodes and attributes.
+ sub nodes and attributes. HierarchicalConfiguration now uses the new
+ type ConfigurationNode whereever possible. Some methods dealing with
+ Node objects have been deprecated and replaced by versions operating on
+ ConfigurationNode objects instead.
</action>
<action dev="oheger" type="update" issue="35828" due-to="Jorge Ferrer">
All configuration classes derived from AbstractConfiguration now allow
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org