Author: oheger Date: Sat Mar 8 12:37:07 2008 New Revision: 635075 URL: http://svn.apache.org/viewvc?rev=635075&view=rev Log: Added getAttributeParent() method to NodeList
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeList.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestNodeList.java Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeList.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeList.java?rev=635075&r1=635074&r2=635075&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeList.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeList.java Sat Mar 8 12:37:07 2008 @@ -111,6 +111,27 @@ } /** + * Returns the parent of the attribute at the specified index. If the + * element at this index is not an attribute, an exception will be thrown. + * + * @param index the index + * @return the parent node, to which the attribute at this index belongs + * @throws IndexOutOfBoundsException if the index is invalid + * @throws IllegalArgumentException if the element at this index is not an + * attribute + */ + public T getAttributeParent(int index) + { + if (!isAttribute(index)) + { + throw new IllegalArgumentException("Element at " + index + + " is not an attribute!"); + } + + return element(index).getAssociatedNode(); + } + + /** * Returns the name of the element at the specified index. If the element is * a node, the node name is returned. For an attribute the attribute name is * returned. Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestNodeList.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestNodeList.java?rev=635075&r1=635074&r2=635075&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestNodeList.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestNodeList.java Sat Mar 8 12:37:07 2008 @@ -195,4 +195,32 @@ assertEquals("Wrong attribute name", NAME, list.getName(0, new ConfigurationNodeHandler())); } + + /** + * Tests querying the parent of an attribute. + */ + public void testGetAttributeParent() + { + ConfigurationNode parent = new DefaultConfigurationNode(); + list.addAttribute(parent, NAME); + assertEquals("Wrong parent node", parent, list.getAttributeParent(0)); + } + + /** + * Tests querying the parent of an attribute when the specified element is + * not an attribute. This should cause an exception. + */ + public void testGetAttributeParentNoAttribute() + { + list.addNode(new DefaultConfigurationNode(NAME)); + try + { + list.getAttributeParent(0); + fail("Invalid element type not detected!"); + } + catch (IllegalArgumentException iex) + { + // ok + } + } }