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 2014/03/23 21:32:53 UTC

svn commit: r1580597 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/INIConfiguration.java test/java/org/apache/commons/configuration/TestINIConfiguration.java

Author: oheger
Date: Sun Mar 23 20:32:52 2014
New Revision: 1580597

URL: http://svn.apache.org/r1580597
Log:
Started rework of INIConfiguration to be compatible with immutable nodes.

Loading the configuration now creates a hierarchy of immutable nodes.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java?rev=1580597&r1=1580596&r2=1580597&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/INIConfiguration.java Sun Mar 23 20:32:52 2014
@@ -23,14 +23,17 @@ import java.io.Reader;
 import java.io.Writer;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
-import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.commons.configuration.convert.ListDelimiterHandler;
 import org.apache.commons.configuration.ex.ConfigurationException;
-import org.apache.commons.configuration.tree.ConfigurationNode;
-import org.apache.commons.configuration.tree.ViewNode;
+import org.apache.commons.configuration.ex.ConfigurationRuntimeException;
+import org.apache.commons.configuration.tree.ImmutableNode;
+import org.apache.commons.configuration.tree.InMemoryNodeModel;
+import org.apache.commons.configuration.tree.NodeSelector;
 
 /**
  * <p>
@@ -255,7 +258,7 @@ public class INIConfiguration extends Ba
      * @param c the configuration to be copied
      * @since 2.0
      */
-    public INIConfiguration(HierarchicalConfiguration c)
+    public INIConfiguration(HierarchicalConfiguration<ImmutableNode> c)
     {
         super(c);
     }
@@ -271,23 +274,16 @@ public class INIConfiguration extends Ba
     public void write(Writer writer) throws ConfigurationException, IOException
     {
         PrintWriter out = new PrintWriter(writer);
-        Iterator<String> it = getSections().iterator();
-        while (it.hasNext())
+        for (String section : getSections())
         {
-            String section = it.next();
-            Configuration subset;
             if (section != null)
             {
                 out.print("[");
                 out.print(section);
                 out.print("]");
                 out.println();
-                subset = createSubnodeConfiguration(getSectionNode(section), null);
-            }
-            else
-            {
-                subset = getSection(null);
             }
+            Configuration subset = getSection(section);
 
             Iterator<String> keys = subset.getKeys();
             while (keys.hasNext())
@@ -326,7 +322,7 @@ public class INIConfiguration extends Ba
      * {@code clear()} method is not called so the configuration read in will
      * be merged with the current configuration.
      *
-     * @param reader The reader to read the configuration from.
+     * @param in the reader to read the configuration from.
      * @throws ConfigurationException If an error occurs while reading the
      *         configuration
      * @throws IOException if an I/O error occurs
@@ -334,9 +330,51 @@ public class INIConfiguration extends Ba
     public void read(Reader in) throws ConfigurationException, IOException
     {
         BufferedReader bufferedReader = new BufferedReader(in);
-        ConfigurationNode sectionNode = getRootNode();
+        Map<String, ImmutableNode.Builder> sectionBuilders = new LinkedHashMap<String, ImmutableNode.Builder>();
+        ImmutableNode.Builder rootBuilder = new ImmutableNode.Builder();
+
+        createNodeBuilders(bufferedReader, rootBuilder, sectionBuilders);
+        ImmutableNode rootNode = createNewRootNode(rootBuilder, sectionBuilders);
+        addNodes(null, rootNode.getChildren());
+    }
+
+    /**
+     * Creates a new root node from the builders constructed while reading the
+     * configuration file.
+     *
+     * @param rootBuilder the builder for the top-level section
+     * @param sectionBuilders a map storing the section builders
+     * @return the root node of the newly created hierarchy
+     */
+    private static ImmutableNode createNewRootNode(
+            ImmutableNode.Builder rootBuilder,
+            Map<String, ImmutableNode.Builder> sectionBuilders)
+    {
+        for (Map.Entry<String, ImmutableNode.Builder> e : sectionBuilders
+                .entrySet())
+        {
+            rootBuilder.addChild(e.getValue().name(e.getKey()).create());
+        }
+        return rootBuilder.create();
+    }
 
-        String line = bufferedReader.readLine();
+    /**
+     * Reads the content of an INI file from the passed in reader and creates a
+     * structure of builders for constructing the {@code ImmutableNode} objects
+     * representing the data.
+     *
+     * @param in the reader
+     * @param rootBuilder the builder for the top-level section
+     * @param sectionBuilders a map storing the section builders
+     * @throws IOException if an I/O error occurs
+     */
+    private void createNodeBuilders(BufferedReader in,
+            ImmutableNode.Builder rootBuilder,
+            Map<String, ImmutableNode.Builder> sectionBuilders)
+            throws IOException
+    {
+        ImmutableNode.Builder sectionBuilder = rootBuilder;
+        String line = in.readLine();
         while (line != null)
         {
             line = line.trim();
@@ -345,20 +383,23 @@ public class INIConfiguration extends Ba
                 if (isSectionLine(line))
                 {
                     String section = line.substring(1, line.length() - 1);
-                    sectionNode = getSectionNode(section);
+                    sectionBuilder = sectionBuilders.get(section);
+                    if (sectionBuilder == null)
+                    {
+                        sectionBuilder = new ImmutableNode.Builder();
+                        sectionBuilders.put(section, sectionBuilder);
+                    }
                 }
 
                 else
                 {
-                    String key = "";
+                    String key;
                     String value = "";
                     int index = findSeparator(line);
                     if (index >= 0)
                     {
                         key = line.substring(0, index);
-                        value =
-                                parseValue(line.substring(index + 1),
-                                        bufferedReader);
+                        value = parseValue(line.substring(index + 1), in);
                     }
                     else
                     {
@@ -370,11 +411,11 @@ public class INIConfiguration extends Ba
                         // use space for properties with no key
                         key = " ";
                     }
-                    createValueNodes(sectionNode, key, value);
+                    createValueNodes(sectionBuilder, key, value);
                 }
             }
 
-            line = bufferedReader.readLine();
+            line = in.readLine();
         }
     }
 
@@ -383,20 +424,20 @@ public class INIConfiguration extends Ba
      * 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 sectionBuilder the section builder for adding new nodes
      * @param key the key
      * @param value the value string
      */
-    private void createValueNodes(ConfigurationNode sectionNode, String key,
-            String value)
+    private void createValueNodes(ImmutableNode.Builder sectionBuilder,
+            String key, String value)
     {
-        Collection<String> values = getListDelimiterHandler().split(value, false);
+        Collection<String> values =
+                getListDelimiterHandler().split(value, false);
 
         for (String v : values)
         {
-            ConfigurationNode node = createNode(key);
-            node.setValue(v);
-            sectionNode.addChild(node);
+            sectionBuilder.addChild(new ImmutableNode.Builder().name(key)
+                    .value(v).create());
         }
     }
 
@@ -735,12 +776,12 @@ public class INIConfiguration extends Ba
         beginRead(false);
         try
         {
-            for (ConfigurationNode node : getRootNode().getChildren())
+            for (ImmutableNode node : getRootNode().getChildren())
             {
                 if (isSectionNode(node))
                 {
                     inSection = true;
-                    sections.add(node.getName());
+                    sections.add(node.getNodeName());
                 }
                 else
                 {
@@ -786,7 +827,7 @@ public class INIConfiguration extends Ba
      * @return a configuration containing only the properties of the specified
      *         section
      */
-    public SubnodeConfiguration getSection(String name)
+    public HierarchicalConfiguration<ImmutableNode> getSection(String name)
     {
         if (name == null)
         {
@@ -799,48 +840,18 @@ public class INIConfiguration extends Ba
             {
                 return configurationAt(name);
             }
-            catch (IllegalArgumentException iex)
+            catch (ConfigurationRuntimeException iex)
             {
                 // the passed in key does not map to exactly one node
                 // obtain the node for the section, create it on demand
-                // (creation of a SubnodeConfiguration has to be synchronized)
-                beginWrite(false);
-                try
-                {
-                    return createAndInitializeSubnodeConfiguration(
-                            getSectionNode(name), null, false);
-                }
-                finally
-                {
-                    endWrite();
-                }
+                InMemoryNodeModel parentModel = getSubConfigurationParentModel();
+                NodeSelector selector = parentModel.trackChildNodeWithCreation(null, name, this);
+                return createSubConfigurationForTrackedNode(selector, parentModel);
             }
         }
     }
 
     /**
-     * Obtains the node representing the specified section. This method is
-     * called while the configuration is loaded. If a node for this section
-     * already exists, it is returned. Otherwise a new node is created.
-     *
-     * @param sectionName the name of the section
-     * @return the node for this section
-     */
-    private ConfigurationNode getSectionNode(String sectionName)
-    {
-        List<ConfigurationNode> nodes = getRootNode().getChildren(sectionName);
-        if (!nodes.isEmpty())
-        {
-            return nodes.get(0);
-        }
-
-        ConfigurationNode node = createNode(sectionName);
-        markSectionNode(node);
-        getRootNode().addChild(node);
-        return node;
-    }
-
-    /**
      * Creates a sub configuration for the global section of the represented INI
      * configuration.
      *
@@ -848,37 +859,26 @@ public class INIConfiguration extends Ba
      */
     private SubnodeConfiguration getGlobalSection()
     {
-        ViewNode parent = new ViewNode();
-
-        beginWrite(false);
-        try
-        {
-            for (ConfigurationNode node : getRootNode().getChildren())
-            {
-                if (!isSectionNode(node))
-                {
-                    parent.addChild(node);
-                }
-            }
-
-            return createAndInitializeSubnodeConfiguration(parent, null, false);
-        }
-        finally
-        {
-            endWrite();
-        }
-    }
-
-    /**
-     * Marks a configuration node as a section node. This means that this node
-     * represents a section header. This implementation uses the node's
-     * reference property to store a flag.
-     *
-     * @param node the node to be marked
-     */
-    private static void markSectionNode(ConfigurationNode node)
-    {
-        node.setReference(Boolean.TRUE);
+//        ViewNode parent = new ViewNode();
+//
+//        beginWrite(false);
+//        try
+//        {
+//            for (ConfigurationNode node : getRootNode().getChildren())
+//            {
+//                if (!isSectionNode(node))
+//                {
+//                    parent.addChild(node);
+//                }
+//            }
+//
+//            return createAndInitializeSubnodeConfiguration(parent, null, false);
+//        }
+//        finally
+//        {
+//            endWrite();
+//        }
+        return null;
     }
 
     /**
@@ -887,8 +887,8 @@ public class INIConfiguration extends Ba
      * @param node the node in question
      * @return a flag whether this node represents a section
      */
-    private static boolean isSectionNode(ConfigurationNode node)
+    private static boolean isSectionNode(ImmutableNode node)
     {
-        return node.getReference() != null || node.getChildrenCount() > 0;
+        return !node.getChildren().isEmpty();
     }
 }

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java?rev=1580597&r1=1580596&r2=1580597&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java Sun Mar 23 20:32:52 2014
@@ -40,16 +40,14 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.convert.DefaultListDelimiterHandler;
 import org.apache.commons.configuration.ex.ConfigurationException;
 import org.apache.commons.configuration.sync.ReadWriteSynchronizer;
+import org.apache.commons.configuration.tree.ImmutableNode;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
 /**
- * Test class for INIConfiguration.
+ * Test class for {@code INIConfiguration}.
  *
- * @author <a
- *         href="http://commons.apache.org/configuration/team-list.html">Commons
- *         Configuration team</a>
  * @version $Id$
  */
 public class TestINIConfiguration
@@ -608,7 +606,7 @@ public class TestINIConfiguration
     public void testGetSectionExisting() throws ConfigurationException
     {
         INIConfiguration config = setUpConfig(INI_DATA);
-        SubnodeConfiguration section = config.getSection("section1");
+        HierarchicalConfiguration<ImmutableNode> section = config.getSection("section1");
         assertEquals("Wrong value of var1", "foo", section.getString("var1"));
         assertEquals("Wrong value of var2", "451", section.getString("var2"));
     }
@@ -623,7 +621,7 @@ public class TestINIConfiguration
         final String data = INI_DATA + "[section1]" + LINE_SEPARATOR
                 + "var3 = merged" + LINE_SEPARATOR;
         INIConfiguration config = setUpConfig(data);
-        SubnodeConfiguration section = config.getSection("section1");
+        HierarchicalConfiguration<ImmutableNode> section = config.getSection("section1");
         assertEquals("Wrong value of var1", "foo", section.getString("var1"));
         assertEquals("Wrong value of var2", "451", section.getString("var2"));
         assertEquals("Wrong value of var3", "merged", section.getString("var3"));
@@ -636,7 +634,7 @@ public class TestINIConfiguration
     public void testGetSectionGlobal() throws ConfigurationException
     {
         INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
-        SubnodeConfiguration section = config.getSection(null);
+        HierarchicalConfiguration<ImmutableNode> section = config.getSection(null);
         assertEquals("Wrong value of global variable", "testGlobal", section
                 .getString("globalVar"));
     }
@@ -671,7 +669,7 @@ public class TestINIConfiguration
     public void testGetSectionGlobalNonExisting() throws ConfigurationException
     {
         INIConfiguration config = setUpConfig(INI_DATA);
-        SubnodeConfiguration section = config.getSection(null);
+        HierarchicalConfiguration<ImmutableNode> section = config.getSection(null);
         assertTrue("Sub config not empty", section.isEmpty());
     }
 
@@ -682,7 +680,7 @@ public class TestINIConfiguration
     public void testGetSectionNonExisting() throws ConfigurationException
     {
         INIConfiguration config = setUpConfig(INI_DATA);
-        SubnodeConfiguration section = config
+        HierarchicalConfiguration<ImmutableNode> section = config
                 .getSection("Non existing section");
         assertTrue("Sub config not empty", section.isEmpty());
     }
@@ -861,7 +859,7 @@ public class TestINIConfiguration
     {
         final String data = "[section]\ntest = failed\n";
         INIConfiguration config = setUpConfig(data);
-        SubnodeConfiguration sub = config.getSection("section");
+        HierarchicalConfiguration<ImmutableNode> sub = config.getSection("section");
         assertFalse("No content", sub.isEmpty());
         sub.clear();
         sub.setProperty("test", "success");
@@ -883,7 +881,7 @@ public class TestINIConfiguration
         INIConfiguration config = setUpConfig(data);
         assertEquals("Wrong value 1", "sec1", config.getString("section.var1"));
         assertEquals("Wrong value 2", "sec2", config.getString("section.var2"));
-        SubnodeConfiguration sub = config.getSection("section");
+        HierarchicalConfiguration<ImmutableNode> sub = config.getSection("section");
         assertEquals("Wrong sub value 1", "sec1", sub.getString("var1"));
         assertEquals("Wrong sub value 2", "sec2", sub.getString("var2"));
         StringWriter writer = new StringWriter();
@@ -904,7 +902,7 @@ public class TestINIConfiguration
             throws ConfigurationException, IOException
     {
         INIConfiguration config = setUpConfig(INI_DATA);
-        SubnodeConfiguration section = config.getSection("newSection");
+        HierarchicalConfiguration<ImmutableNode> section = config.getSection("newSection");
         section.addProperty("test", "success");
         assertEquals("Main config not updated", "success",
                 config.getString("newSection.test"));
@@ -925,7 +923,7 @@ public class TestINIConfiguration
                 new INIConfiguration();
         config.addProperty("section.var1", "value1");
         config.addProperty("section(-1).var2", "value2");
-        SubnodeConfiguration section = config.getSection("section");
+        HierarchicalConfiguration<ImmutableNode> section = config.getSection("section");
         Iterator<String> keys = section.getKeys();
         assertEquals("Wrong key", "var1", keys.next());
         assertFalse("Too many keys", keys.hasNext());