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 2008/03/22 19:37:35 UTC

svn commit: r640042 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/expr/xpath/ test/java/org/apache/commons/configuration2/expr/xpath/

Author: oheger
Date: Sat Mar 22 11:37:34 2008
New Revision: 640042

URL: http://svn.apache.org/viewvc?rev=640042&view=rev
Log:
XPathExpressionEngine now supports enhanced queries for attributes with multiple values

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationAttributePointer.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationNodeIteratorAttribute.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/XPathExpressionEngine.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationAttributePointer.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationAttributePointer.java?rev=640042&r1=640041&r2=640042&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationAttributePointer.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationAttributePointer.java Sat Mar 22 11:37:34 2008
@@ -38,6 +38,9 @@
  */
 class ConfigurationAttributePointer<T> extends NodePointer
 {
+    /** Constant for an undefined attribute index.*/
+    public static final int IDX_UNDEF = -1;
+
     /**
      * The serial version UID.
      */
@@ -49,18 +52,23 @@
     /** Stores the name of the managed attribute. */
     private String attributeName;
 
+    /** Stores the index of the attribute value.*/
+    private int valueIndex;
+
     /**
      * Creates a new instance of <code>ConfigurationAttributePointer</code>.
      *
      * @param parent the parent node pointer
      * @param attrName the name of the managed attribute
+     * @param idx the index of the desired attribute value
      */
     public ConfigurationAttributePointer(ConfigurationNodePointer<T> parent,
-            String attrName)
+            String attrName, int idx)
     {
         super(parent);
         parentNode = parent.getConfigurationNode();
         attributeName = attrName;
+        valueIndex = idx;
     }
 
     /**
@@ -246,6 +254,16 @@
         public String getAttributeName()
         {
             return attributeName;
+        }
+
+        /**
+         * Returns the index of the selected value if there are multiple values.
+         *
+         * @return the index of the selected value
+         */
+        public int getValueIndex()
+        {
+            return valueIndex;
         }
     }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationNodeIteratorAttribute.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationNodeIteratorAttribute.java?rev=640042&r1=640041&r2=640042&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationNodeIteratorAttribute.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/ConfigurationNodeIteratorAttribute.java Sat Mar 22 11:37:34 2008
@@ -17,8 +17,12 @@
 package org.apache.commons.configuration2.expr.xpath;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
+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;
@@ -38,8 +42,8 @@
     /** Stores the parent node pointer. */
     private ConfigurationNodePointer<T> parentPointer;
 
-    /** A list with the names of the managed attributes. */
-    private List<String> attributeNames;
+    /** A list with the data of the managed attributes. */
+    private List<AttributeData> attributeData;
 
     /**
      * Creates a new instance of <code>ConfigurationNodeIteratorAttribute</code>.
@@ -52,7 +56,7 @@
     {
         super(parent, false);
         parentPointer = parent;
-        attributeNames = createAttributeNameList(parent, name);
+        attributeData = createAttributeDataList(parent, name);
     }
 
     /**
@@ -63,7 +67,7 @@
      * @param name the name of the selected attribute
      * @return a list with the selected attributes
      */
-    protected List<String> createAttributeNameList(
+    protected List<AttributeData> createAttributeDataList(
             ConfigurationNodePointer<T> parent, QName name)
     {
         if (name.getPrefix() != null)
@@ -72,19 +76,18 @@
             return Collections.emptyList();
         }
 
-        List<String> result = new ArrayList<String>();
+        List<AttributeData> result = new ArrayList<AttributeData>();
         if (!WILDCARD.equals(name.getName()))
         {
-            if (parent.getNodeHandler().getAttributeValue(
-                    parent.getConfigurationNode(), name.getName()) != null)
-            {
-                result.add(name.getName());
-            }
+            addAttributeData(parent, result, name.getName());
         }
         else
         {
-            result.addAll(parent.getNodeHandler().getAttributes(
-                    parent.getConfigurationNode()));
+            Set<String> names = new LinkedHashSet<String>(parent.getNodeHandler().getAttributes(parent.getConfigurationNode()));
+            for(String n : names)
+            {
+                addAttributeData(parent, result, n);
+            }
         }
 
         return result;
@@ -99,8 +102,9 @@
     @Override
     protected NodePointer createNodePointer(int position)
     {
+        AttributeData ad = attributeData.get(position);
         return new ConfigurationAttributePointer<T>(parentPointer,
-                attributeNames.get(position));
+                ad.name, ad.valueIndex);
     }
 
     /**
@@ -111,6 +115,66 @@
     @Override
     protected int size()
     {
-        return attributeNames.size();
+        return attributeData.size();
+    }
+
+    /**
+     * Helper method for adding data about an attribute to the data list. If the
+     * attribute has multiple values, correct indices will be set.
+     *
+     * @param parent the parent pointer
+     * @param lst the result list
+     * @param name the name of the attribute
+     */
+    private void addAttributeData(ConfigurationNodePointer<T> parent,
+            List<AttributeData> lst, String name)
+    {
+        Object value = parent.getNodeHandler().getAttributeValue(
+                parent.getConfigurationNode(), name);
+        if (value != null)
+        {
+            if (value instanceof Collection)
+            {
+                // add entries for all values
+                int idx = 0;
+                for (Iterator<?> it = ((Collection<?>) value).iterator(); it
+                        .hasNext(); idx++)
+                {
+                    lst.add(new AttributeData(name, idx));
+                    it.next();
+                }
+            }
+
+            else
+            {
+                lst.add(new AttributeData(name,
+                        ConfigurationAttributePointer.IDX_UNDEF));
+            }
+        }
+    }
+
+    /**
+     * A simple data class for storing the information required to select an
+     * attribute.
+     */
+    private static class AttributeData
+    {
+        /** The name of the attribute. */
+        String name;
+
+        /** The index of the value. */
+        int valueIndex;
+
+        /**
+         * Creates a new instance of <code>AttributeData</code>
+         *
+         * @param n the name
+         * @param idx the value index
+         */
+        public AttributeData(String n, int idx)
+        {
+            name = n;
+            valueIndex = idx;
+        }
     }
 }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/XPathExpressionEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/XPathExpressionEngine.java?rev=640042&r1=640041&r2=640042&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/XPathExpressionEngine.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/xpath/XPathExpressionEngine.java Sat Mar 22 11:37:34 2008
@@ -144,7 +144,7 @@
                         ConfigurationAttributePointer<T>.AttributeNodeProxy anp =
                             (ConfigurationAttributePointer.AttributeNodeProxy) o;
                         result.addAttribute(anp.getParentNode(), anp
-                                .getAttributeName());
+                                .getAttributeName(), anp.getValueIndex());
                     }
                     else
                     {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java?rev=640042&r1=640041&r2=640042&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/xpath/TestConfigurationAttributePointer.java Sat Mar 22 11:37:34 2008
@@ -60,7 +60,7 @@
         parent = new ConfigurationNodePointer<ConfigurationNode>(nd,
                 new ConfigurationNodeHandler(), Locale.ENGLISH);
         pointer = new ConfigurationAttributePointer<ConfigurationNode>(parent,
-                ATTR_NAME);
+                ATTR_NAME, -1);
     }
 
     /**
@@ -96,6 +96,7 @@
                 .getParentNode());
         assertEquals("Wrong attribute name", ATTR_NAME, proxy
                 .getAttributeName());
+        assertEquals("Wrong value index", -1, proxy.getValueIndex());
     }
 
     /**