commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r640020 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/expr/NodeList.java test/java/org/apache/commons/configuration2/expr/TestNodeList.java
Date Sat, 22 Mar 2008 17:44:44 GMT
Author: oheger
Date: Sat Mar 22 10:44:43 2008
New Revision: 640020

URL: http://svn.apache.org/viewvc?rev=640020&view=rev
Log:
Added support for attributes with multiple values 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=640020&r1=640019&r2=640020&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 22 10:44:43 2008
@@ -42,6 +42,9 @@
  */
 public class NodeList<T>
 {
+    /** Constant for an undefined index.*/
+    private static final int IDX_UNDEF = -1;
+
     /** Stores the entries of this list. */
     private List<ListElement<T>> elements;
 
@@ -192,7 +195,21 @@
      */
     public void addAttribute(T parent, String name)
     {
-        elements.add(new AttributeListElement<T>(parent, name));
+        addAttribute(parent, name, IDX_UNDEF);
+    }
+
+    /**
+     * Adds a new attribute to this list and selects a specific value. This
+     * method is used for attributes with multiple values if a specific value is
+     * selected.
+     *
+     * @param parent the parent node of the attribute
+     * @param name the name of the attribute
+     * @param index the index of the attribute's value
+     */
+    public void addAttribute(T parent, String name, int index)
+    {
+        elements.add(new AttributeListElement<T>(parent, name, index));
     }
 
     /**
@@ -340,21 +357,28 @@
         /** Stores the name of the attribute. */
         private String name;
 
+        /** Stores the index of the value.*/
+        private int index;
+
         /**
          * Creates a new instance of <code>AttributeListElement</code> and
          * initializes it.
          *
          * @param nd the parent node
          * @param attrName the name of the attribute
+         * @param idx the index of the value
          */
-        public AttributeListElement(T nd, String attrName)
+        public AttributeListElement(T nd, String attrName, int idx)
         {
             super(nd);
             name = attrName;
+            index = idx;
         }
 
         /**
-         * Returns the value of the represented attribute.
+         * Returns the value of the represented attribute. If an index is
+         * defined, this method checks whether there are multiple values. In
+         * this case the value with the given index is returned.
          *
          * @param handler the node handler
          * @return the value of this attribute
@@ -362,7 +386,18 @@
         @Override
         public Object getValue(NodeHandler<T> handler)
         {
-            return handler.getAttributeValue(getAssociatedNode(), name);
+            Object value = handler.getAttributeValue(getAssociatedNode(), name);
+
+            if(index != IDX_UNDEF && value instanceof List)
+            {
+                List<?> valList = (List<?>) value;
+                if(index < valList.size())
+                {
+                    value = valList.get(index);
+                }
+            }
+
+            return value;
         }
 
         /**

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=640020&r1=640019&r2=640020&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 22 10:44:43 2008
@@ -223,4 +223,71 @@
             // ok
         }
     }
+
+    /**
+     * Tests selecting specific values of a multi-valued attribute.
+     */
+    public void testAddAttributeIndex()
+    {
+        ConfigurationNode parent = new DefaultConfigurationNode("parent");
+        for (int i = 0; i < COUNT; i++)
+        {
+            ConfigurationNode attr = new DefaultConfigurationNode(NAME, VALUE
+                    + i);
+            parent.addAttribute(attr);
+            list.addAttribute(parent, attr.getName(), i);
+        }
+        NodeList<ConfigurationNode> list2 = new NodeList<ConfigurationNode>();
+        list2.addAttribute(parent, NAME);
+
+        assertEquals("Wrong number of nodes", COUNT, list.size());
+        ConfigurationNodeHandler handler = new ConfigurationNodeHandler();
+        List<?> values = (List<?>) list2.getValue(0, handler);
+        assertEquals("Wrong number of list values", COUNT, values.size());
+        for (int i = 0; i < COUNT; i++)
+        {
+            assertFalse("A node", list.isNode(i));
+            assertTrue("Not an attribute", list.isAttribute(i));
+            assertEquals("Wrong node value", VALUE + i, list.getValue(i,
+                    handler));
+            assertEquals("Wrong list value", VALUE + i, values.get(i));
+        }
+    }
+
+    /**
+     * Tests accessing an attribute with an index that does not have multiple
+     * values. In this case the normal value of the attribute should be
+     * returned.
+     */
+    public void testGetAttributeIndexNoCollection()
+    {
+        ConfigurationNode parent = new DefaultConfigurationNode("parent");
+        parent.addAttribute(new DefaultConfigurationNode(NAME, VALUE));
+        list.addAttribute(parent, NAME, 1);
+        assertEquals("Wrong attribute value", VALUE, list.getValue(0,
+                new ConfigurationNodeHandler()));
+    }
+
+    /**
+     * Tests accessing an attribute with an invalid index. In this case the
+     * whole value collection should be returned.
+     */
+    public void testGetAttributeIndexInvalid()
+    {
+        ConfigurationNode parent = new DefaultConfigurationNode("parent");
+        for (int i = 0; i < COUNT; i++)
+        {
+            ConfigurationNode attr = new DefaultConfigurationNode(NAME, VALUE
+                    + i);
+            parent.addAttribute(attr);
+        }
+        list.addAttribute(parent, NAME, COUNT + 10);
+        List<?> val = (List<?>) list
+                .getValue(0, new ConfigurationNodeHandler());
+        assertEquals("Wrong number of list elements", COUNT, val.size());
+        for (int i = 0; i < COUNT; i++)
+        {
+            assertEquals("Wrong list value " + i, VALUE + i, val.get(i));
+        }
+    }
 }



Mime
View raw message