Author: oheger Date: Fri Feb 7 20:36:22 2014 New Revision: 1565797 URL: http://svn.apache.org/r1565797 Log: Reworked ConfigurationNodePointer to use a NodeHandler.
The pointer can now handle arbitrary nodes for which a NodeHandler implementation exists. Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodePointer.java commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointer.java Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodePointer.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodePointer.java?rev=1565797&r1=1565796&r2=1565797&view=diff ============================================================================== --- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodePointer.java (original) +++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodePointer.java Fri Feb 7 20:36:22 2014 @@ -16,10 +16,8 @@ */ package org.apache.commons.configuration.tree.xpath; -import java.util.List; import java.util.Locale; -import org.apache.commons.configuration.tree.ConfigurationNode; import org.apache.commons.configuration.tree.NodeHandler; import org.apache.commons.jxpath.ri.Compiler; import org.apache.commons.jxpath.ri.QName; @@ -37,10 +35,8 @@ import org.apache.commons.jxpath.ri.mode * </p> * * @since 1.3 - * @author <a - * href="http://commons.apache.org/configuration/team-list.html">Commons - * Configuration team</a> * @version $Id$ + * @param <T> the type of the nodes this pointer deals with */ class ConfigurationNodePointer<T> extends NodePointer { @@ -49,26 +45,26 @@ class ConfigurationNodePointer<T> extend */ private static final long serialVersionUID = -1087475639680007713L; - /** Stores the associated configuration node. */ - private ConfigurationNode node; + /** The node handler. */ + private final NodeHandler<T> handler; + + /** Stores the associated node. */ + private final T node; /** - * Creates a new instance of {@code ConfigurationNodePointer}. + * Creates a new instance of {@code ConfigurationNodePointer} pointing to + * the specified node. * - * @param node the node + * @param node the wrapped node * @param locale the locale + * @param handler the {@code NodeHandler} */ - public ConfigurationNodePointer(ConfigurationNode node, Locale locale) + public ConfigurationNodePointer(T node, Locale locale, + NodeHandler<T> handler) { super(null, locale); this.node = node; - } - - public ConfigurationNodePointer(T node, Locale locale, NodeHandler<T> handler) - { - super(null, locale); - //TODO implementation - throw new UnsupportedOperationException("Not yet implemented!"); + this.handler = handler; } /** @@ -77,19 +73,14 @@ class ConfigurationNodePointer<T> extend * * @param parent the parent pointer * @param node the associated node + * @param handler the {@code NodeHandler} */ - public ConfigurationNodePointer(NodePointer parent, ConfigurationNode node) - { - super(parent); - this.node = node; - } - public ConfigurationNodePointer(ConfigurationNodePointer<T> parent, T node, NodeHandler<T> handler) { super(parent); - // TODO implementation - throw new UnsupportedOperationException("Not yet implemented!"); + this.node = node; + this.handler = handler; } /** @@ -101,7 +92,7 @@ class ConfigurationNodePointer<T> extend @Override public boolean isLeaf() { - return node.getChildrenCount() < 1; + return getNodeHandler().getChildrenCount(node, null) < 1; } /** @@ -127,15 +118,15 @@ class ConfigurationNodePointer<T> extend } /** - * Checks whether this node pointer refers to an attribute node. This method - * checks the attribute flag of the associated configuration node. + * Checks whether this node pointer refers to an attribute node. This is + * not the case. * * @return the attribute flag */ @Override public boolean isAttribute() { - return node.isAttribute(); + return false; } /** @@ -146,7 +137,7 @@ class ConfigurationNodePointer<T> extend @Override public QName getName() { - return new QName(null, node.getName()); + return new QName(null, getNodeHandler().nodeName(node)); } /** @@ -180,18 +171,19 @@ class ConfigurationNodePointer<T> extend @Override public Object getValue() { - return node.getValue(); + return getNodeHandler().getValue(node); } /** - * Sets the value of this node. + * Sets the value of this node. This is not supported, so always an + * exception is thrown. * * @param value the new value */ @Override public void setValue(Object value) { - node.setValue(value); + throw new UnsupportedOperationException("Node value cannot be set!"); } /** @@ -205,37 +197,22 @@ class ConfigurationNodePointer<T> extend public int compareChildNodePointers(NodePointer pointer1, NodePointer pointer2) { - ConfigurationNode node1 = (ConfigurationNode) pointer1.getBaseValue(); - ConfigurationNode node2 = (ConfigurationNode) pointer2.getBaseValue(); + Object node1 = pointer1.getBaseValue(); + Object node2 = pointer2.getBaseValue(); - // attributes will be sorted before child nodes - if (node1.isAttribute() && !node2.isAttribute()) - { - return -1; - } - else if (node2.isAttribute() && !node1.isAttribute()) + // sort based on the occurrence in the sub node list + for (T child : getNodeHandler().getChildren(node)) { - return 1; - } - - else - { - // sort based on the occurrence in the sub node list - List<ConfigurationNode> subNodes = node1.isAttribute() ? node.getAttributes() : node - .getChildren(); - for (ConfigurationNode child : subNodes) + if (child == node1) { - if (child == node1) - { - return -1; - } - else if (child == node2) - { - return 1; - } + return -1; + } + else if (child == node2) + { + return 1; } - return 0; // should not happen } + return 0; // should not happen } /** @@ -247,7 +224,7 @@ class ConfigurationNodePointer<T> extend @Override public NodeIterator attributeIterator(QName name) { - return new ConfigurationNodeIteratorAttribute(this, name); + return new ConfigurationNodeIteratorAttribute<T>(this, name); } /** @@ -262,8 +239,8 @@ class ConfigurationNodePointer<T> extend public NodeIterator childIterator(NodeTest test, boolean reverse, NodePointer startWith) { - return new ConfigurationNodeIteratorChildren(this, test, reverse, - startWith); + return new ConfigurationNodeIteratorChildren<T>(this, test, reverse, + castPointer(startWith)); } /** @@ -284,18 +261,39 @@ class ConfigurationNodePointer<T> extend return super.testNode(test); } - public NodeHandler<T> getNodeHandler() { - //TODO implementation - throw new UnsupportedOperationException("Not yet implemented!"); + /** + * Returns the {@code NodeHandler} used by this instance. + * + * @return the {@code NodeHandler} + */ + public NodeHandler<T> getNodeHandler() + { + return handler; } /** * Returns the wrapped configuration node. - * @return the wrapped configuration node + * + * @return the wrapped node */ public T getConfigurationNode() { - //TODO implementation - throw new UnsupportedOperationException("Not yet implemented!"); + return node; + } + + /** + * Casts the given child pointer to a node pointer of this type. This is a + * bit dangerous. However, in a typical setup, child node pointers can only + * be created by this instance which ensures that they are of the correct + * type. Therefore, this cast is safe. + * + * @param p the {@code NodePointer} to cast + * @return the resulting {@code ConfigurationNodePointer} + */ + private ConfigurationNodePointer<T> castPointer(NodePointer p) + { + @SuppressWarnings("unchecked") // see method comment + ConfigurationNodePointer<T> result = (ConfigurationNodePointer<T>) p; + return result; } } Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointer.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointer.java?rev=1565797&r1=1565796&r2=1565797&view=diff ============================================================================== --- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointer.java (original) +++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationNodePointer.java Fri Feb 7 20:36:22 2014 @@ -22,8 +22,7 @@ import static org.junit.Assert.assertTru import java.util.Locale; -import org.apache.commons.configuration.tree.ConfigurationNode; -import org.apache.commons.configuration.tree.DefaultConfigurationNode; +import org.apache.commons.configuration.tree.ImmutableNode; import org.apache.commons.jxpath.ri.QName; import org.apache.commons.jxpath.ri.model.NodeIterator; import org.apache.commons.jxpath.ri.model.NodePointer; @@ -31,24 +30,23 @@ import org.junit.Before; import org.junit.Test; /** - * Test class for ConfigurationNodePointer. + * Test class for {@code ConfigurationNodePointer}. * - * @author <a - * href="http://commons.apache.org/configuration/team-list.html">Commons - * Configuration team</a> * @version $Id$ */ public class TestConfigurationNodePointer extends AbstractXPathTest { /** Stores the node pointer to be tested. */ - NodePointer pointer; + private ConfigurationNodePointer<ImmutableNode> pointer; @Override @Before public void setUp() throws Exception { super.setUp(); - pointer = new ConfigurationNodePointer(root, Locale.getDefault()); + pointer = + new ConfigurationNodePointer<ImmutableNode>(root, + Locale.getDefault(), handler); } /** @@ -57,8 +55,10 @@ public class TestConfigurationNodePointe @Test public void testCompareChildNodePointersChildren() { - NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(1)); - NodePointer p2 = new ConfigurationNodePointer(pointer, root.getChild(3)); + NodePointer p1 = new ConfigurationNodePointer<ImmutableNode>( + pointer, root.getChildren().get(1), handler); + NodePointer p2 = new ConfigurationNodePointer<ImmutableNode>( + pointer, root.getChildren().get(3), handler); assertEquals("Incorrect order", -1, pointer.compareChildNodePointers( p1, p2)); assertEquals("Incorrect symmetric order", 1, pointer @@ -66,52 +66,23 @@ public class TestConfigurationNodePointe } /** - * Tests comparing child node pointers for attribute nodes. + * Tests whether a comparison of child node pointers handle the case that + * the child nodes are unknown. (This should not happen in practice.) */ @Test public void testCompareChildNodePointersAttributes() { - root.addAttribute(new DefaultConfigurationNode("attr1", "test1")); - root.addAttribute(new DefaultConfigurationNode("attr2", "test2")); - NodePointer p1 = new ConfigurationNodePointer(pointer, root - .getAttribute(0)); - NodePointer p2 = new ConfigurationNodePointer(pointer, root - .getAttribute(1)); - assertEquals("Incorrect order", -1, pointer.compareChildNodePointers( - p1, p2)); - assertEquals("Incorrect symmetric order", 1, pointer - .compareChildNodePointers(p2, p1)); - } - - /** - * tests comparing child node pointers for both child and attribute nodes. - */ - @Test - public void testCompareChildNodePointersChildAndAttribute() - { - root.addAttribute(new DefaultConfigurationNode("attr1", "test1")); - NodePointer p1 = new ConfigurationNodePointer(pointer, root.getChild(2)); - NodePointer p2 = new ConfigurationNodePointer(pointer, root - .getAttribute(0)); - assertEquals("Incorrect order for attributes", 1, pointer - .compareChildNodePointers(p1, p2)); - assertEquals("Incorrect symmetric order for attributes", -1, pointer - .compareChildNodePointers(p2, p1)); - } - - /** - * Tests comparing child node pointers for child nodes that do not belong to - * the parent node. - */ - @Test - public void testCompareChildNodePointersInvalidChildren() - { - ConfigurationNode node = root.getChild(1); - NodePointer p1 = new ConfigurationNodePointer(pointer, node.getChild(1)); - NodePointer p2 = new ConfigurationNodePointer(pointer, node.getChild(3)); - assertEquals("Non child nodes could be sorted", 0, pointer - .compareChildNodePointers(p1, p2)); - assertEquals("Non child nodes could be sorted symmetrically", 0, + ImmutableNode n1 = new ImmutableNode.Builder().name("n1").create(); + ImmutableNode n2 = new ImmutableNode.Builder().name("n2").create(); + NodePointer p1 = + new ConfigurationNodePointer<ImmutableNode>(pointer, n1, + handler); + NodePointer p2 = + new ConfigurationNodePointer<ImmutableNode>(pointer, n2, + handler); + assertEquals("Incorrect order", 0, + pointer.compareChildNodePointers(p1, p2)); + assertEquals("Incorrect symmetric order", 0, pointer.compareChildNodePointers(p2, p1)); } @@ -121,11 +92,7 @@ public class TestConfigurationNodePointe @Test public void testIsAttribute() { - ConfigurationNode node = new DefaultConfigurationNode("test", "testval"); - NodePointer p = new ConfigurationNodePointer(pointer, node); - assertFalse("Node is an attribute", p.isAttribute()); - node.setAttribute(true); - assertTrue("Node is no attribute", p.isAttribute()); + assertFalse("Node is an attribute", pointer.isAttribute()); } /** @@ -135,16 +102,20 @@ public class TestConfigurationNodePointe public void testIsLeave() { assertFalse("Root node is leaf", pointer.isLeaf()); + } - NodePointer p = pointer; - while (!p.isLeaf()) - { - ConfigurationNode node = (ConfigurationNode) p.getNode(); - assertTrue("Node has no children", node.getChildrenCount() > 0); - p = new ConfigurationNodePointer(p, node.getChild(0)); - } - assertTrue("Node has children", ((ConfigurationNode) p.getNode()) - .getChildrenCount() == 0); + /** + * Tests the leaf flag for a real leaf node. + */ + @Test + public void testIsLeafTrue() + { + ImmutableNode leafNode = + new ImmutableNode.Builder().name("leafNode").create(); + pointer = + new ConfigurationNodePointer<ImmutableNode>(pointer, leafNode, + handler); + assertTrue("Not a leaf node", pointer.isLeaf()); } /** @@ -163,29 +134,37 @@ public class TestConfigurationNodePointe */ private void checkIterators(NodePointer p) { - ConfigurationNode node = (ConfigurationNode) p.getNode(); + ImmutableNode node = (ImmutableNode) p.getNode(); NodeIterator it = p.childIterator(null, false, null); assertEquals("Iterator count differs from children count", node - .getChildrenCount(), iteratorSize(it)); + .getChildren().size(), iteratorSize(it)); for (int index = 1; it.setPosition(index); index++) { NodePointer pchild = it.getNodePointer(); - assertEquals("Wrong child", node.getChild(index - 1), pchild - .getNode()); + assertEquals("Wrong child", node.getChildren().get(index - 1), + pchild.getNode()); checkIterators(pchild); } it = p.attributeIterator(new QName(null, "*")); assertEquals("Iterator count differs from attribute count", node - .getAttributeCount(), iteratorSize(it)); + .getAttributes().size(), iteratorSize(it)); for (int index = 1; it.setPosition(index); index++) { NodePointer pattr = it.getNodePointer(); assertTrue("Node pointer is no attribute", pattr.isAttribute()); - assertEquals("Wrong attribute", node.getAttribute(index - 1), pattr - .getNode()); - checkIterators(pattr); + assertTrue("Wrong attribute name", node.getAttributes() + .containsKey(pattr.getName().getName())); } } + + /** + * Tests that no new value can be set. + */ + @Test(expected = UnsupportedOperationException.class) + public void testSetValue() + { + pointer.setValue("newValue"); + } }