Author: henning Date: Mon Oct 21 20:35:11 2013 New Revision: 1534366 URL: http://svn.apache.org/r1534366 Log: Backport CONFIGURATION-500 from r1382310.
Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/pom.xml commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt?rev=1534366&r1=1534365&r2=1534366&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt Mon Oct 21 20:35:11 2013 @@ -31,6 +31,11 @@ Following is a complete list of all chan BUG FIXES IN 1.10 ================= +* [CONFIGURATION-500] Attributes in xml config should apply to all entries of a list + + XMLConfiguration now adds attributes of elements defining a list to + all list nodes. + * [CONFIGURATION-556] Regression with SystemProperties in 1.8 and 1.9 In 1.7 and before, any change to the system properties was immediately reflected in a Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/pom.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/pom.xml?rev=1534366&r1=1534365&r2=1534366&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/pom.xml (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/pom.xml Mon Oct 21 20:35:11 2013 @@ -25,7 +25,7 @@ <parent> <groupId>org.apache.commons</groupId> <artifactId>commons-parent</artifactId> - <version>25</version> + <version>26</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>commons-configuration</groupId> Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java?rev=1534366&r1=1534365&r2=1534366&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java Mon Oct 21 20:35:11 2013 @@ -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/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java?rev=1534366&r1=1534365&r2=1534366&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java Mon Oct 21 20:35:11 2013 @@ -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/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml?rev=1534366&r1=1534365&r2=1534366&view=diff ============================================================================== --- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml (original) +++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml Mon Oct 21 20:35:11 2013 @@ -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