Author: oheger
Date: Fri Jul 11 12:55:00 2008
New Revision: 676061
URL: http://svn.apache.org/viewvc?rev=676061&view=rev
Log:
CONFIGURATION-333: Added new constructPath() method for determining the unique
key for a given node.
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java?rev=676061&r1=676060&r2=676061&view=diff
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
(original)
+++
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
Fri Jul 11 12:55:00 2008
@@ -34,6 +34,7 @@
import org.apache.commons.configuration2.expr.NodeVisitor;
import org.apache.commons.configuration2.expr.NodeVisitorAdapter;
import org.apache.commons.configuration2.expr.def.DefaultExpressionEngine;
+import org.apache.commons.lang.StringUtils;
/**
* <p>A base class for hierarchical configurations.</p>
@@ -398,6 +399,30 @@
}
/**
+ * Determines the path from the given node to the root node. The return
+ * value is the key that uniquely identifies the given node. The associated
+ * expression engine is used for constructing and combining the parts the
+ * key is composed of.
+ *
+ * @param node the node in question (must not be <b>null</b>)
+ * @return a unique key for this node
+ */
+ protected String constructPath(T node)
+ {
+ if (node == null)
+ {
+ return StringUtils.EMPTY;
+ }
+ else
+ {
+ // recursively navigate to the root and construct all paths
+ return getExpressionEngine().uniqueNodeKey(node,
+ constructPath(getNodeHandler().getParent(node)),
+ getNodeHandler());
+ }
+ }
+
+ /**
* Checks if the specified key is contained in this configuration. Note
that
* for this configuration the term "contained" means that the key
* has an associated value. If there is a node for this key that has no
@@ -714,7 +739,7 @@
* Creates a new node object with the specified name and value. This base
* implementation delegates to the <code>NodeHandler</code> for creating a
* new node.
- *
+ *
* @param parent the parent of the new node
* @param name the name of the new node
* @param value the value of the new node
@@ -724,7 +749,7 @@
{
return getNodeHandler().addChild(parent, name, value);
}
-
+
/**
* Helper method for processing a <code>NodeAddData</code> object obtained
from the
* expression engine. This method will create all new nodes and set the
value
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java?rev=676061&r1=676060&r2=676061&view=diff
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
(original)
+++
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
Fri Jul 11 12:55:00 2008
@@ -970,6 +970,41 @@
}
/**
+ * Tests the constructPath() method for child nodes.
+ */
+ public void testConstructPathChildNodes()
+ {
+ ConfigurationNode nodTables = config.getRootNode().getChild(0);
+ int tabIdx = 0;
+ for (ConfigurationNode nodTab : nodTables.getChildren())
+ {
+ String path = config.constructPath(nodTab);
+ String tabPath = "tables(0).table(" + tabIdx + ")";
+ assertEquals("Wrong path for table", tabPath, path);
+ ConfigurationNode nodFields = nodTab.getChild(1);
+ int fieldIdx = 0;
+ for (ConfigurationNode nodField : nodFields.getChildren())
+ {
+ ConfigurationNode nodName = nodField.getChild(0);
+ String fldPath = config.constructPath(nodName);
+ assertEquals("Wrong path for field", tabPath
+ + ".fields(0).field(" + fieldIdx + ").name(0)",
fldPath);
+ fieldIdx++;
+ }
+ tabIdx++;
+ }
+ }
+
+ /**
+ * Tests the constructPath() method for the root node.
+ */
+ public void testConstructPathRoot()
+ {
+ assertEquals("Wrong path for root node", "", config
+ .constructPath(config.getRootNode()));
+ }
+
+ /**
* Helper method for testing the getKeys(String) method.
*
* @param prefix the key to pass into getKeys()