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/01/21 17:59:48 UTC

svn commit: r1234362 - in /commons/proper/configuration/trunk/src: changes/changes.xml main/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java test/java/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java

Author: oheger
Date: Sat Jan 21 16:59:48 2012
New Revision: 1234362

URL: http://svn.apache.org/viewvc?rev=1234362&view=rev
Log:
[CONFIGURATION-474] Implemented list delimiter parsing in HierarchicalINIConfiguration.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1234362&r1=1234361&r2=1234362&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sat Jan 21 16:59:48 2012
@@ -34,6 +34,12 @@
       <action dev="oheger" type="update" issue="CONFIGURATION-475">
         Class ConfigurationKey was deprecated in favour of DefaultConfigurationKey.
       </action>
+      <action dev="oheger" type="update" issue="CONFIGURATION-474">
+        Implemented delimiter parsing in HierarchicalINIConfiguration to be
+        consistent with other Configuration implementations. Note that this can
+        impact existing code. To switch back to the old behavior, call
+        setDelimiterParsingDisabled(true) before loading the configuration.
+      </action>
       <action dev="oheger" type="add" issue="CONFIGURATION-471">
         CompositeConfiguration now provides better support for child
         configurations that are used as in-memory configuration.

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java?rev=1234362&r1=1234361&r2=1234362&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java Sat Jan 21 16:59:48 2012
@@ -24,6 +24,7 @@ import java.io.Reader;
 import java.io.Writer;
 import java.net.URL;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -314,7 +315,7 @@ public class HierarchicalINIConfiguratio
                     Iterator<?> values = ((Collection<?>) value).iterator();
                     while (values.hasNext())
                     {
-                        value = (Object) values.next();
+                        value = values.next();
                         out.print(key);
                         out.print(" = ");
                         out.print(formatValue(value.toString()));
@@ -384,9 +385,7 @@ public class HierarchicalINIConfiguratio
                             // use space for properties with no key
                             key = " ";
                         }
-                        ConfigurationNode node = createNode(key);
-                        node.setValue(value);
-                        sectionNode.addChild(node);
+                        createValueNodes(sectionNode, key, value);
                     }
                 }
 
@@ -401,6 +400,36 @@ public class HierarchicalINIConfiguratio
     }
 
     /**
+     * Creates the node(s) for the given key value-pair. If delimiter parsing is
+     * enabled, the value string is split if possible, and for each single value
+     * a node is created. Otherwise only a single node is added to the section.
+     *
+     * @param sectionNode the section node new nodes have to be added
+     * @param key the key
+     * @param value the value string
+     */
+    private void createValueNodes(ConfigurationNode sectionNode, String key,
+            String value)
+    {
+        Collection<String> values;
+        if (isDelimiterParsingDisabled())
+        {
+            values = Collections.singleton(value);
+        }
+        else
+        {
+            values = PropertyConverter.split(value, getListDelimiter(), false);
+        }
+
+        for (String v : values)
+        {
+            ConfigurationNode node = createNode(key);
+            node.setValue(v);
+            sectionNode.addChild(node);
+        }
+    }
+
+    /**
      * Parse the value to remove the quotes and ignoring the comment. Example:
      *
      * <pre>

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java?rev=1234362&r1=1234361&r2=1234362&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java Sat Jan 21 16:59:48 2012
@@ -30,6 +30,7 @@ import java.io.StringWriter;
 import java.io.Writer;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import org.junit.After;
@@ -123,11 +124,24 @@ public class TestHierarchicalINIConfigur
     private static HierarchicalINIConfiguration setUpConfig(String data)
             throws ConfigurationException
     {
-        StringReader reader = new StringReader(data);
         HierarchicalINIConfiguration instance = new HierarchicalINIConfiguration();
+        load(instance, data);
+        return instance;
+    }
+
+    /**
+     * Loads the specified content into the given configuration instance.
+     *
+     * @param instance the configuration
+     * @param data the data to be loaded
+     * @throws ConfigurationException if an error occurs
+     */
+    private static void load(HierarchicalINIConfiguration instance, String data)
+            throws ConfigurationException
+    {
+        StringReader reader = new StringReader(data);
         instance.load(reader);
         reader.close();
-        return instance;
     }
 
     /**
@@ -882,6 +896,35 @@ public class TestHierarchicalINIConfigur
     }
 
     /**
+     * Tests whether the list delimiter character is recognized.
+     */
+    @Test
+    public void testValueWithDelimiters() throws ConfigurationException
+    {
+        HierarchicalINIConfiguration config =
+                setUpConfig("[test]" + LINE_SEPARATOR + "list=1,2,3"
+                        + LINE_SEPARATOR);
+        List<Object> list = config.getList("test.list");
+        assertEquals("Wrong number of elements", 3, list.size());
+        assertEquals("Wrong element at 1", "1", list.get(0));
+        assertEquals("Wrong element at 2", "2", list.get(1));
+        assertEquals("Wrong element at 3", "3", list.get(2));
+    }
+
+    /**
+     * Tests whether parsing of lists can be disabled.
+     */
+    @Test
+    public void testListParsingDisabled() throws ConfigurationException
+    {
+        HierarchicalINIConfiguration config =
+                new HierarchicalINIConfiguration();
+        config.setDelimiterParsingDisabled(true);
+        load(config, "[test]" + LINE_SEPARATOR + "nolist=1,2,3");
+        assertEquals("Wrong value", "1,2,3", config.getString("test.nolist"));
+    }
+
+    /**
      * A thread class for testing concurrent access to the global section.
      */
     private static class GlobalSectionTestThread extends Thread