Author: oheger
Date: Fri Feb 7 20:35:05 2014
New Revision: 1565795
URL: http://svn.apache.org/r1565795
Log:
Reworked ConfigurationNodeIteratorAttribute to use a NodeHandler.
Attributes can now also be processed with the help of a NodeHandler.
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java?rev=1565795&r1=1565794&r2=1565795&view=diff
==============================================================================
---
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java
(original)
+++
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java
Fri Feb 7 20:35:05 2014
@@ -18,46 +18,78 @@ package org.apache.commons.configuration
import java.util.ArrayList;
import java.util.Collections;
+import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Set;
-import org.apache.commons.configuration.tree.ConfigurationNode;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* A specialized node iterator implementation that deals with attribute nodes.
*
- * @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 iterator deals with
*/
-class ConfigurationNodeIteratorAttribute extends
- ConfigurationNodeIteratorBase
+class ConfigurationNodeIteratorAttribute<T> extends
+ ConfigurationNodeIteratorBase<T>
{
/** Constant for the wildcard node name.*/
private static final String WILDCARD = "*";
+ /** Stores the parent node pointer. */
+ private ConfigurationNodePointer<T> parentPointer;
+
+ /** A list with the names of the managed attributes. */
+ private List<String> attributeNames;
+
/**
* Creates a new instance of {@code ConfigurationNodeIteratorAttribute}.
* @param parent the parent node pointer
* @param name the name of the selected attribute
*/
- public ConfigurationNodeIteratorAttribute(NodePointer parent, QName name)
+ public ConfigurationNodeIteratorAttribute(
+ ConfigurationNodePointer<T> parent, QName name)
{
super(parent, false);
- initSubNodeList(createSubNodeList((ConfigurationNode) parent.getNode(),
- name));
+ parentPointer = parent;
+ attributeNames = createAttributeDataList(parent, name);
+ }
+
+ /**
+ * Creates a pointer for the node at the specified position.
+ *
+ * @param position the desired position
+ * @return a pointer for the attribute at this position
+ */
+ @Override
+ protected NodePointer createNodePointer(int position)
+ {
+ return new ConfigurationAttributePointer<T>(parentPointer,
+ attributeNames.get(position));
+ }
+
+ /**
+ * Returns the size of the managed iteration.
+ *
+ * @return the iteration size
+ */
+ @Override
+ protected int size()
+ {
+ return attributeNames.size();
}
/**
* Determines which attributes are selected based on the passed in node
* name.
- * @param node the current node
+ *
+ * @param parent the parent node pointer
* @param name the name of the selected attribute
* @return a list with the selected attributes
*/
- protected List<ConfigurationNode> createSubNodeList(ConfigurationNode
node, QName name)
+ private List<String> createAttributeDataList(
+ ConfigurationNodePointer<T> parent, QName name)
{
if (name.getPrefix() != null)
{
@@ -65,16 +97,40 @@ class ConfigurationNodeIteratorAttribute
return Collections.emptyList();
}
- List<ConfigurationNode> result = new ArrayList<ConfigurationNode>();
+ List<String> result = new ArrayList<String>();
if (!WILDCARD.equals(name.getName()))
{
- result.addAll(node.getAttributes(name.getName()));
+ addAttributeData(parent, result, name.getName());
}
else
{
- result.addAll(node.getAttributes());
+ Set<String> names =
+ new LinkedHashSet<String>(parent.getNodeHandler()
+ .getAttributes(parent.getConfigurationNode()));
+ for (String n : names)
+ {
+ addAttributeData(parent, result, n);
+ }
}
return result;
}
+
+ /**
+ * Helper method for checking whether an attribute is defined and adding it
+ * to the list of attributes to iterate over.
+ *
+ * @param parent the parent node pointer
+ * @param result the result list
+ * @param name the name of the current attribute
+ */
+ private void addAttributeData(ConfigurationNodePointer<T> parent,
+ List<String> result, String name)
+ {
+ if (parent.getNodeHandler().getAttributeValue(
+ parent.getConfigurationNode(), name) != null)
+ {
+ result.add(name);
+ }
+ }
}
Modified:
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java?rev=1565795&r1=1565794&r2=1565795&view=diff
==============================================================================
---
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java
(original)
+++
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java
Fri Feb 7 20:35:05 2014
@@ -17,23 +17,22 @@
package org.apache.commons.configuration.tree.xpath;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import java.util.HashSet;
import java.util.List;
import java.util.Locale;
+import java.util.Set;
-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.NodePointer;
import org.junit.Before;
import org.junit.Test;
/**
- * Test class for ConfigurationIteratorAttributes.
+ * Test class for {@code ConfigurationNodeIteratorAttributes}.
*
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
* @version $Id$
*/
public class TestConfigurationIteratorAttributes extends AbstractXPathTest
@@ -42,7 +41,7 @@ public class TestConfigurationIteratorAt
private static final String TEST_ATTR = "test";
/** Stores the node pointer of the test node.*/
- NodePointer pointer;
+ private ConfigurationNodePointer<ImmutableNode> pointer;
@Override
@Before
@@ -51,9 +50,11 @@ public class TestConfigurationIteratorAt
super.setUp();
// Adds further attributes to the test node
- ConfigurationNode testNode = root.getChild(1);
- testNode.addAttribute(new DefaultConfigurationNode(TEST_ATTR, "yes"));
- pointer = new ConfigurationNodePointer(testNode, Locale.getDefault());
+ ImmutableNode orgNode = root.getChildren().get(1);
+ ImmutableNode testNode = orgNode.setAttribute(TEST_ATTR, "yes");
+ pointer =
+ new ConfigurationNodePointer<ImmutableNode>(testNode,
+ Locale.getDefault(), handler);
}
/**
@@ -62,11 +63,18 @@ public class TestConfigurationIteratorAt
@Test
public void testIterateAllAttributes()
{
- ConfigurationNodeIteratorAttribute it = new
ConfigurationNodeIteratorAttribute(pointer, new QName(null, "*"));
+ ConfigurationNodeIteratorAttribute<ImmutableNode> it =
+ new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
+ new QName(null, "*"));
assertEquals("Wrong number of attributes", 2, iteratorSize(it));
- List<ConfigurationNode> attrs = iterationElements(it);
- assertEquals("Wrong first attribute", ATTR_NAME,
attrs.get(0).getName());
- assertEquals("Wrong first attribute", TEST_ATTR,
attrs.get(1).getName());
+ List<NodePointer> attrs = iterationElements(it);
+ Set<String> attrNames = new HashSet<String>();
+ for (NodePointer np : attrs)
+ {
+ attrNames.add(np.getName().getName());
+ }
+ assertTrue("First attribute not found", attrNames.contains(ATTR_NAME));
+ assertTrue("Second attribute not found",
attrNames.contains(TEST_ATTR));
}
/**
@@ -75,9 +83,12 @@ public class TestConfigurationIteratorAt
@Test
public void testIterateSpecificAttribute()
{
- ConfigurationNodeIteratorAttribute it = new
ConfigurationNodeIteratorAttribute(pointer, new QName(null, TEST_ATTR));
+ ConfigurationNodeIteratorAttribute<ImmutableNode> it =
+ new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
+ new QName(null, TEST_ATTR));
assertEquals("Wrong number of attributes", 1, iteratorSize(it));
- assertEquals("Wrong attribute", TEST_ATTR,
iterationElements(it).get(0).getName());
+ assertEquals("Wrong attribute", TEST_ATTR, iterationElements(it).get(0)
+ .getName().getName());
}
/**
@@ -86,18 +97,22 @@ public class TestConfigurationIteratorAt
@Test
public void testIterateUnknownAttribute()
{
- ConfigurationNodeIteratorAttribute it = new
ConfigurationNodeIteratorAttribute(pointer, new QName(null, "unknown"));
+ ConfigurationNodeIteratorAttribute<ImmutableNode> it =
+ new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
+ new QName(null, "unknown"));
assertEquals("Found attributes", 0, iteratorSize(it));
}
/**
- * Tests iteration when a namespace is specified. This is not supported, so
+ * Tests iteration if a namespace is specified. This is not supported, so
* the iteration should be empty.
*/
@Test
public void testIterateNamespace()
{
- ConfigurationNodeIteratorAttribute it = new
ConfigurationNodeIteratorAttribute(pointer, new QName("test", "*"));
+ ConfigurationNodeIteratorAttribute<ImmutableNode> it =
+ new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
+ new QName("test", "*"));
assertEquals("Found attributes", 0, iteratorSize(it));
}
}