Author: oheger Date: Tue Mar 18 14:52:03 2008 New Revision: 638601 URL: http://svn.apache.org/viewvc?rev=638601&view=rev Log: Add support for attributes with multiple values to node handlers
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java?rev=638601&r1=638600&r2=638601&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/ConfigurationNodeHandler.java Tue Mar 18 14:52:03 2008 @@ -49,9 +49,9 @@ } /** - * Returns the value of the attribute with the given name. This - * implementation supports only a single attribute value. If the attribute - * is present multiple times, only the value of the first occurrence is + * Returns the value of the attribute with the given name. If the attribute + * has exactly one value, this value is returned. If the attribute + * is present multiple times, a collection with all values is * returned. If the attribute cannot be found, result is <b>null</b>. * * @param node the node @@ -61,7 +61,25 @@ public Object getAttributeValue(ConfigurationNode node, String name) { List<ConfigurationNode> attrs = node.getAttributes(name); - return (attrs.isEmpty()) ? null : attrs.get(0).getValue(); + + if (attrs.isEmpty()) + { + return null; + } + else if (attrs.size() == 1) + { + return attrs.get(0).getValue(); + } + + else + { + List<Object> result = new ArrayList<Object>(attrs.size()); + for (ConfigurationNode attr : attrs) + { + result.add(attr.getValue()); + } + return result; + } } /** @@ -153,9 +171,8 @@ } /** - * Sets the value of the specified attribute. This implementation only - * supports a single value per attribute. So any existing attributes with - * the given name are removed first. + * Sets the value of the specified attribute. This implementation removes + * any existing attribute values before calling <code>addAttributeValue()</code>. * * @param node the node * @param name the name of the attribute to set @@ -165,6 +182,19 @@ Object value) { node.removeAttribute(name); + addAttributeValue(node, name, value); + } + + /** + * Adds another value to an attribute. Using this method it is possible to + * create attributes with multiple values. + * @param node the parent node + * @param name the name of the affected attribute + * @param value the value to add + */ + public void addAttributeValue(ConfigurationNode node, String name, + Object value) + { ConfigurationNode attr = createNode(node, name); attr.setValue(value); node.addAttribute(attr); Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java?rev=638601&r1=638600&r2=638601&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/expr/NodeHandler.java Tue Mar 18 14:52:03 2008 @@ -122,7 +122,9 @@ List<String> getAttributes(T node); /** - * Returns the value of the specified attribute from the given node. + * Returns the value of the specified attribute from the given node. If a + * concrete <code>NodeHandler</code> supports attributes with multiple + * values, result might be a collection. * * @param node the node * @param name the name of the attribute @@ -133,12 +135,25 @@ /** * Sets the value of an attribute for the specified node. * - * @param node the node + * @param node the parent node * @param name the name of the attribute * @param value the value of the attribute */ void setAttributeValue(T node, String name, Object value); + /** + * Adds a value to an attribute. This method can be used to create + * attributes with multiple values (as far as the specific + * <code>NodeHandler</code> implementation supports this). In contrast to + * <code>setAttributeValue()</code>, an existing attribute value is not + * removed, but the new value is added to the existing values. + * + * @param node the parent node + * @param name the name of the attribute + * @param value the value to be added + */ + void addAttributeValue(T node, String name, Object value); + /** * Removes the attribute with the specified name from the given node. * Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java?rev=638601&r1=638600&r2=638601&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/expr/TestConfigurationNodeHandler.java Tue Mar 18 14:52:03 2008 @@ -17,6 +17,7 @@ package org.apache.commons.configuration2.expr; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.apache.commons.configuration2.tree.ConfigurationNode; @@ -222,6 +223,37 @@ } /** + * Tests querying an attribute with multiple values. + */ + public void testGetAttributeValueMulti() + { + final int attrCount = 10; + ConfigurationNode node = mockNode(); + List<ConfigurationNode> attrs = new ArrayList<ConfigurationNode>( + attrCount); + for (int i = 0; i < attrCount; i++) + { + ConfigurationNode attr = mockNode(); + EasyMock.expect(attr.getValue()).andReturn( + String.valueOf(VALUE) + i); + EasyMock.replay(attr); + attrs.add(attr); + } + EasyMock.expect(node.getAttributes(NAME)).andReturn(attrs); + EasyMock.replay(node); + Collection<?> values = (Collection<?>) handler.getAttributeValue(node, + NAME); + assertEquals("Wrong number of values", attrCount, values.size()); + int idx = 0; + for (Object val : values) + { + String expected = String.valueOf(VALUE) + idx; + assertEquals("Wrong value at " + idx, expected, val); + idx++; + } + } + + /** * Tests querying the value of a non-existing attribute. Result should be * null. */ @@ -248,6 +280,21 @@ List<ConfigurationNode> attrs = node.getAttributes(NAME); assertEquals("Wrong size of attribute list", 1, attrs.size()); assertEquals("Wrong attribute value", VALUE, attrs.get(0).getValue()); + } + + /** + * Tests adding multiple values to an attribute. + */ + public void testAddAttributeValue() + { + ConfigurationNode node = new DefaultConfigurationNode(); + handler.addAttributeValue(node, NAME, "oldValue"); + handler.addAttributeValue(node, NAME, VALUE); + List<ConfigurationNode> attrs = node.getAttributes(NAME); + assertEquals("Wrong size of attribute list", 2, attrs.size()); + assertEquals("Wrong attribute value 1", "oldValue", attrs.get(0) + .getValue()); + assertEquals("Wrong attribute value 2", VALUE, attrs.get(1).getValue()); } /**