You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/04/29 22:35:16 UTC

svn commit: r1591091 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/tree/xpath/ test/java/org/apache/commons/configuration/tree/xpath/

Author: oheger
Date: Tue Apr 29 20:35:15 2014
New Revision: 1591091

URL: http://svn.apache.org/r1591091
Log:
[CONFIGURATION-573] ConfigurationNodeIteratorAttribute now supports namespaces.

Namespace prefixes are now handled when iterating over the attributes of a
node. Some common functionality was refactored into the base class.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorBase.java
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorChildren.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java?rev=1591091&r1=1591090&r2=1591091&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorAttribute.java Tue Apr 29 20:35:15 2014
@@ -17,13 +17,13 @@
 package org.apache.commons.configuration.tree.xpath;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
 import org.apache.commons.jxpath.ri.QName;
 import org.apache.commons.jxpath.ri.model.NodePointer;
+import org.apache.commons.lang3.StringUtils;
 
 /**
  * A specialized node iterator implementation that deals with attribute nodes.
@@ -91,25 +91,25 @@ class ConfigurationNodeIteratorAttribute
     private List<String> createAttributeDataList(
             ConfigurationNodePointer<T> parent, QName name)
     {
-        if (name.getPrefix() != null)
-        {
-            // namespace prefixes are not supported
-            return Collections.emptyList();
-        }
-
         List<String> result = new ArrayList<String>();
         if (!WILDCARD.equals(name.getName()))
         {
-            addAttributeData(parent, result, name.getName());
+            addAttributeData(parent, result, qualifiedName(name));
         }
         else
         {
             Set<String> names =
                     new LinkedHashSet<String>(parent.getNodeHandler()
                             .getAttributes(parent.getConfigurationNode()));
+            String prefix =
+                    (name.getPrefix() != null) ? prefixName(name.getPrefix(),
+                            null) : null;
             for (String n : names)
             {
-                addAttributeData(parent, result, n);
+                if (prefix == null || StringUtils.startsWith(n, prefix))
+                {
+                    addAttributeData(parent, result, n);
+                }
             }
         }
 

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorBase.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorBase.java?rev=1591091&r1=1591090&r2=1591091&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorBase.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorBase.java Tue Apr 29 20:35:15 2014
@@ -17,8 +17,10 @@
 package org.apache.commons.configuration.tree.xpath;
 
 import org.apache.commons.configuration.tree.NodeHandler;
+import org.apache.commons.jxpath.ri.QName;
 import org.apache.commons.jxpath.ri.model.NodeIterator;
 import org.apache.commons.jxpath.ri.model.NodePointer;
+import org.apache.commons.lang3.StringUtils;
 
 /**
  * <p>
@@ -36,6 +38,12 @@ import org.apache.commons.jxpath.ri.mode
  */
 abstract class ConfigurationNodeIteratorBase<T> implements NodeIterator
 {
+    /** Constant for the prefix separator. */
+    private static final String PREFIX_SEPARATOR = ":";
+
+    /** A format for constructing a node name with a namespace prefix. */
+    private static final String FMT_NAMESPACE = "%s" + PREFIX_SEPARATOR + "%s";
+
     /** Stores the parent node pointer. */
     private final ConfigurationNodePointer<T> parent;
 
@@ -187,4 +195,31 @@ abstract class ConfigurationNodeIterator
      * @return the number of elements
      */
     protected abstract int size();
+
+    /**
+     * Generates a qualified name with a namespace prefix.
+     *
+     * @param prefix the prefix
+     * @param name the name (may be <b>null</b>)
+     * @return the qualified name
+     */
+    protected static String prefixName(String prefix, String name)
+    {
+        return String.format(FMT_NAMESPACE, prefix,
+                StringUtils.defaultString(name));
+    }
+
+    /**
+     * Returns the qualified name from the given {@code QName}. If the name has
+     * no namespace, result is the simple name. Otherwise, the namespace prefix
+     * is added.
+     *
+     * @param name the {@code QName}
+     * @return the qualified name
+     */
+    protected static String qualifiedName(QName name)
+    {
+        return (name.getPrefix() == null) ? name.getName() : prefixName(
+                name.getPrefix(), name.getName());
+    }
 }

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorChildren.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorChildren.java?rev=1591091&r1=1591090&r2=1591091&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorChildren.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/tree/xpath/ConfigurationNodeIteratorChildren.java Tue Apr 29 20:35:15 2014
@@ -38,11 +38,6 @@ import org.apache.commons.lang3.StringUt
 class ConfigurationNodeIteratorChildren<T> extends
         ConfigurationNodeIteratorBase<T>
 {
-    /** Constant for the prefix separator. */
-    private static final String PREFIX_SEPARATOR = ":";
-
-    /** A format for constructing a node name with a namespace prefix. */
-    private static final String FMT_NAMESPACE = "%s" + PREFIX_SEPARATOR + "%s";
 
     /** The list with the sub nodes to iterate over. */
     private final List<T> subNodes;
@@ -152,9 +147,7 @@ class ConfigurationNodeIteratorChildren<
      */
     private List<T> createSubNodeListForName(T node, QName name)
     {
-        String compareName =
-                (name.getPrefix() == null) ? name.getName() : prefixName(
-                        name.getPrefix(), name.getName());
+        String compareName = qualifiedName(name);
         List<T> result = new ArrayList<T>();
         for (T child : getNodeHandler().getChildren(node))
         {
@@ -221,16 +214,4 @@ class ConfigurationNodeIteratorChildren<
         return -1;
     }
 
-    /**
-     * Generates a qualified name with a namespace prefix.
-     *
-     * @param prefix the prefix
-     * @param name the name
-     * @return the qualified name
-     */
-    private static String prefixName(String prefix, String name)
-    {
-        return String.format(FMT_NAMESPACE, prefix,
-                StringUtils.defaultString(name));
-    }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java?rev=1591091&r1=1591090&r2=1591091&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/tree/xpath/TestConfigurationIteratorAttributes.java Tue Apr 29 20:35:15 2014
@@ -40,6 +40,12 @@ public class TestConfigurationIteratorAt
     /** Constant for the name of another test attribute.*/
     private static final String TEST_ATTR = "test";
 
+    /** Constant for a namespace prefix. */
+    private static final String NAMESPACE = "commons";
+
+    /** Constant for an attribute with a namespace prefix. */
+    private static final String NS_ATTR = NAMESPACE + ":attr";
+
     /** Stores the node pointer of the test node.*/
     private ConfigurationNodePointer<ImmutableNode> pointer;
 
@@ -51,7 +57,9 @@ public class TestConfigurationIteratorAt
 
         // Adds further attributes to the test node
         ImmutableNode orgNode = root.getChildren().get(1);
-        ImmutableNode testNode = orgNode.setAttribute(TEST_ATTR, "yes");
+        ImmutableNode testNode =
+                orgNode.setAttribute(TEST_ATTR, "yes").setAttribute(NS_ATTR,
+                        "configuration");
         pointer =
                 new ConfigurationNodePointer<ImmutableNode>(testNode,
                         Locale.getDefault(), handler);
@@ -66,7 +74,7 @@ public class TestConfigurationIteratorAt
         ConfigurationNodeIteratorAttribute<ImmutableNode> it =
                 new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
                         new QName(null, "*"));
-        assertEquals("Wrong number of attributes", 2, iteratorSize(it));
+        assertEquals("Wrong number of attributes", 3, iteratorSize(it));
         List<NodePointer> attrs = iterationElements(it);
         Set<String> attrNames = new HashSet<String>();
         for (NodePointer np : attrs)
@@ -75,6 +83,7 @@ public class TestConfigurationIteratorAt
         }
         assertTrue("First attribute not found", attrNames.contains(ATTR_NAME));
         assertTrue("Second attribute not found", attrNames.contains(TEST_ATTR));
+        assertTrue("Namespace attribute not found", attrNames.contains(NS_ATTR));
     }
 
     /**
@@ -104,15 +113,42 @@ public class TestConfigurationIteratorAt
     }
 
     /**
-     * Tests iteration if a namespace is specified. This is not supported, so
-     * the iteration should be empty.
+     * Tests iteration if an unknown namespace is specified.
      */
     @Test
-    public void testIterateNamespace()
+    public void testIterateNamespaceUnknown()
     {
         ConfigurationNodeIteratorAttribute<ImmutableNode> it =
                 new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
                         new QName("test", "*"));
         assertEquals("Found attributes", 0, iteratorSize(it));
     }
+
+    /**
+     * Tests whether a specific attribute with a namespace can be selected.
+     */
+    @Test
+    public void testIterateNamespaceAttribute()
+    {
+        ConfigurationNodeIteratorAttribute<ImmutableNode> it =
+                new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
+                        new QName(NAMESPACE, "attr"));
+        assertEquals("Wrong number of attributes", 1, iteratorSize(it));
+        assertEquals("Wrong attribute", NS_ATTR, iterationElements(it).get(0)
+                .getName().getName());
+    }
+
+    /**
+     * Tests whether a wildcard can be used together with a namespace.
+     */
+    @Test
+    public void testIterateNamespaceWildcard()
+    {
+        ConfigurationNodeIteratorAttribute<ImmutableNode> it =
+                new ConfigurationNodeIteratorAttribute<ImmutableNode>(pointer,
+                        new QName(NAMESPACE, "*"));
+        assertEquals("Wrong number of attributes", 1, iteratorSize(it));
+        assertEquals("Wrong attribute", NS_ATTR, iterationElements(it).get(0)
+                .getName().getName());
+    }
 }