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 2012/09/08 16:55:30 UTC

svn commit: r1382310 - in /commons/proper/configuration/trunk/src: changes/changes.xml main/java/org/apache/commons/configuration/XMLConfiguration.java test/java/org/apache/commons/configuration/TestXMLConfiguration.java test/resources/test.xml

Author: oheger
Date: Sat Sep  8 14:55:29 2012
New Revision: 1382310

URL: http://svn.apache.org/viewvc?rev=1382310&view=rev
Log:
[CONFIGURATION-500] XMLConfiguration now adds attributes defined for list elements to all nodes in the list instead of to the first element only.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java
    commons/proper/configuration/trunk/src/test/resources/test.xml

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1382310&r1=1382309&r2=1382310&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sat Sep  8 14:55:29 2012
@@ -27,6 +27,10 @@
   <body>
     <release version="2.0" date="in SVN"
       description="TBD">
+      <action dev="oheger" type="update" issue="CONFIGURATION-500">
+        XMLConfiguration now adds attributes of elements defining a list to
+        all list nodes.
+      </action>
     </release>
 
     <release version="1.9" date="2012-08-22"

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java?rev=1382310&r1=1382309&r2=1382310&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/XMLConfiguration.java Sat Sep  8 14:55:29 2012
@@ -608,11 +608,14 @@ public class XMLConfiguration extends Ab
      * @param elemRefs a flag whether references to the XML elements should be set
      * @param trim a flag whether the text content of elements should be trimmed;
      * this controls the whitespace handling
+     * @return a map with all attribute values extracted for the current node
      */
-    private void constructHierarchy(Node node, Element element, boolean elemRefs, boolean trim)
+    private Map<String, Collection<String>> constructHierarchy(Node node,
+            Element element, boolean elemRefs, boolean trim)
     {
         boolean trimFlag = shouldTrim(element, trim);
-        processAttributes(node, element, elemRefs);
+        Map<String, Collection<String>> attributes =
+                processAttributes(node, element, elemRefs);
         StringBuilder buffer = new StringBuilder();
         NodeList list = element.getChildNodes();
         for (int i = 0; i < list.getLength(); i++)
@@ -623,9 +626,10 @@ public class XMLConfiguration extends Ab
                 Element child = (Element) w3cNode;
                 Node childNode = new XMLNode(child.getTagName(),
                         elemRefs ? child : null);
-                constructHierarchy(childNode, child, elemRefs, trimFlag);
+                Map<String, Collection<String>> attrmap =
+                        constructHierarchy(childNode, child, elemRefs, trimFlag);
                 node.addChild(childNode);
-                handleDelimiters(node, childNode, trimFlag);
+                handleDelimiters(node, childNode, trimFlag, attrmap);
             }
             else if (w3cNode instanceof Text)
             {
@@ -643,19 +647,32 @@ public class XMLConfiguration extends Ab
         {
             node.setValue(text);
         }
+        return attributes;
     }
 
     /**
      * Helper method for constructing node objects for the attributes of the
      * given XML element.
      *
-     * @param node the actual node
+     * @param node the current node
      * @param element the actual XML element
      * @param elemRefs a flag whether references to the XML elements should be set
+     * @return a map with all attribute values extracted for the current node
      */
-    private void processAttributes(Node node, Element element, boolean elemRefs)
+    private Map<String, Collection<String>> processAttributes(Node node,
+            Element element, boolean elemRefs)
     {
         NamedNodeMap attributes = element.getAttributes();
+        Map<String, Collection<String>> attrmap;
+        if (attributes.getLength() > 0)
+        {
+            attrmap = new HashMap<String, Collection<String>>();
+        }
+        else
+        {
+            attrmap = Collections.emptyMap();
+        }
+
         for (int i = 0; i < attributes.getLength(); ++i)
         {
             org.w3c.dom.Node w3cNode = attributes.item(i);
@@ -674,15 +691,32 @@ public class XMLConfiguration extends Ab
                                     : getListDelimiter());
                 }
 
-                for (String value : values)
-                {
-                    Node child = new XMLNode(attr.getName(), elemRefs ? element
-                            : null);
-                    child.setValue(value);
-                    node.addAttribute(child);
-                }
+                appendAttributes(node, element, elemRefs, attr.getName(), values);
+                attrmap.put(attr.getName(), values);
             }
         }
+
+        return attrmap;
+    }
+
+    /**
+     * Adds attribute nodes to the given node. For each attribute value, a new
+     * attribute node is created and added as child to the current node.
+     *
+     * @param node the current node
+     * @param element the corresponding XML element
+     * @param attr the name of the attribute
+     * @param values the attribute values
+     */
+    private void appendAttributes(Node node, Element element, boolean elemRefs,
+            String attr, Collection<String> values)
+    {
+        for (String value : values)
+        {
+            Node child = new XMLNode(attr, elemRefs ? element : null);
+            child.setValue(value);
+            node.addAttribute(child);
+        }
     }
 
     /**
@@ -692,8 +726,10 @@ public class XMLConfiguration extends Ab
      * @param parent the parent element
      * @param child the child element
      * @param trim flag whether texts of elements should be trimmed
+     * @param attrmap a map with the attributes of the current node
      */
-    private void handleDelimiters(Node parent, Node child, boolean trim)
+    private void handleDelimiters(Node parent, Node child, boolean trim,
+            Map<String, Collection<String>> attrmap)
     {
         if (child.getValue() != null)
         {
@@ -729,6 +765,12 @@ public class XMLConfiguration extends Ab
                 {
                     c = new XMLNode(child.getName(), null);
                     c.setValue(it.next());
+                    for (Map.Entry<String, Collection<String>> e : attrmap
+                            .entrySet())
+                    {
+                        appendAttributes(c, null, false, e.getKey(),
+                                e.getValue());
+                    }
                     parent.addChild(c);
                 }
             }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java?rev=1382310&r1=1382309&r2=1382310&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java Sat Sep  8 14:55:29 2012
@@ -1288,58 +1288,49 @@ public class TestXMLConfiguration
                 .getString("attrList.a(0)"));
         assertEquals("Wrong value of first name attribute", "x", conf
                 .getString("attrList.a(0)[@name]"));
-        assertEquals("Wrong number of name attributes", 5, conf.getList(
+        assertEquals("Wrong number of name attributes", 6, conf.getList(
                 "attrList.a[@name]").size());
     }
 
     /**
      * Tests a list node with attributes that has multiple values separated by
-     * the list delimiter. In this scenario the attribute should be added to the
-     * node with the first value.
+     * the list delimiter. In this scenario the attribute should be added to all
+     * list nodes.
      */
     @Test
     public void testListWithAttributesMultiValue()
     {
-        assertEquals("Wrong value of 2nd element", "1", conf
-                .getString("attrList.a(1)"));
-        assertEquals("Wrong value of 2nd name attribute", "y", conf
-                .getString("attrList.a(1)[@name]"));
-        for (int i = 2; i <= 3; i++)
+        assertEquals("Wrong value of 2nd element", "1",
+                conf.getString("attrList.a(1)"));
+        assertEquals("Wrong value of 2nd name attribute", "y",
+                conf.getString("attrList.a(1)[@name]"));
+        for (int i = 1; i <= 3; i++)
         {
-            assertEquals("Wrong value of element " + (i + 1), i, conf
-                    .getInt("attrList.a(" + i + ")"));
-            assertFalse("element " + (i + 1) + " has attribute", conf
-                    .containsKey("attrList.a(2)[@name]"));
+            assertEquals("Wrong value of element " + (i + 1), i,
+                    conf.getInt("attrList.a(" + i + ")"));
+            assertEquals("Wrong name attribute for element " + (i), "y",
+                    conf.getString("attrList.a(" + i + ")[@name]"));
         }
     }
 
     /**
-     * Tests a list node with a multi-value attribute and multiple values. All
-     * attribute values should be assigned to the node with the first value.
+     * Tests a list node with multiple values and multiple attributes. All
+     * attribute values should be assigned to all list nodes.
      */
     @Test
-    public void testListWithMultiAttributesMultiValue()
+    public void testListWithMultipleAttributesMultiValue()
     {
         for (int i = 1; i <= 2; i++)
         {
-            assertEquals("Wrong value of multi-valued node", "value" + i, conf
-                    .getString("attrList.a(" + (i + 3) + ")"));
+            String idxStr = String.format("(%d)", Integer.valueOf(i + 3));
+            String nodeKey = "attrList.a" + idxStr;
+            assertEquals("Wrong value of multi-valued node", "value" + i,
+                    conf.getString(nodeKey));
+            assertEquals("Wrong name attribute at " + i, "u",
+                    conf.getString(nodeKey + "[@name]"));
+            assertEquals("Wrong test attribute at " + i, "yes",
+                    conf.getString(nodeKey + "[@test]"));
         }
-        List<Object> attrs = conf.getList("attrList.a(4)[@name]");
-        final String attrVal = "uvw";
-        assertEquals("Wrong number of name attributes", attrVal.length(), attrs
-                .size());
-        for (int i = 0; i < attrVal.length(); i++)
-        {
-            assertEquals("Wrong value for attribute " + i, String
-                    .valueOf(attrVal.charAt(i)), attrs.get(i));
-        }
-        assertEquals("Wrong value of test attribute", "yes", conf
-                .getString("attrList.a(4)[@test]"));
-        assertFalse("Name attribute for 2nd value", conf
-                .containsKey("attrList.a(5)[@name]"));
-        assertFalse("Test attribute for 2nd value", conf
-                .containsKey("attrList.a(5)[@test]"));
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/test.xml?rev=1382310&r1=1382309&r2=1382310&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/test.xml (original)
+++ commons/proper/configuration/trunk/src/test/resources/test.xml Sat Sep  8 14:55:29 2012
@@ -99,7 +99,7 @@ And even longer.
     <attrList>
       <a name="x">ABC</a>
       <a name="y">1,2,3</a>
-      <a name="u,v,w" test="yes">value1,value2</a>
+      <a name="u" test="yes">value1,value2</a>
     </attrList>
 
     <!-- An attribute with multiple values and escape characters for testing