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:36:01 UTC

svn commit: r1580604 - 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:36:00 2014
New Revision: 1580604

URL: http://svn.apache.org/r1580604
Log:
Initial implementation of the method for getting the global section.

A specialized node handler is used to ensure that only nodes which are not
section nodes are visible.

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=1580604&r1=1580603&r2=1580604&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:36:00 2014
@@ -21,10 +21,12 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.Writer;
+import java.util.ArrayList;
 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;
 
@@ -33,7 +35,10 @@ import org.apache.commons.configuration.
 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.NodeHandler;
+import org.apache.commons.configuration.tree.NodeHandlerDecorator;
 import org.apache.commons.configuration.tree.NodeSelector;
+import org.apache.commons.configuration.tree.TrackedNodeModel;
 
 /**
  * <p>
@@ -859,26 +864,13 @@ 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();
-//        }
-        return null;
+        InMemoryNodeModel parentModel = getSubConfigurationParentModel();
+        NodeSelector selector = new NodeSelector(null); // selects parent
+        parentModel.trackNode(selector, this);
+        GlobalSectionNodeModel model = new GlobalSectionNodeModel(parentModel, selector);
+        SubnodeConfiguration sub = new SubnodeConfiguration(this, model);
+        initSubConfigurationForThisParent(sub);
+        return sub;
     }
 
     /**
@@ -891,4 +883,81 @@ public class INIConfiguration extends Ba
     {
         return !node.getChildren().isEmpty();
     }
+
+    /**
+     * A specialized node model implementation for the sub configuration
+     * representing the global section of the INI file. This is a regular
+     * {@code TrackedNodeModel} with one exception: The {@code NodeHandler} used
+     * by this model applies a filter on the children of the root node so that
+     * only nodes are visible that are no sub sections.
+     */
+    private static class GlobalSectionNodeModel extends TrackedNodeModel
+    {
+        /**
+         * Creates a new instance of {@code GlobalSectionNodeModel} and
+         * initializes it with the given underlying model.
+         *
+         * @param model the underlying {@code InMemoryNodeModel}
+         * @param selector the {@code NodeSelector}
+         */
+        public GlobalSectionNodeModel(InMemoryNodeModel model,
+                NodeSelector selector)
+        {
+            super(model, selector, true);
+        }
+
+        @Override
+        public NodeHandler<ImmutableNode> getNodeHandler()
+        {
+            return new NodeHandlerDecorator<ImmutableNode>()
+            {
+                @Override
+                public List<ImmutableNode> getChildren(ImmutableNode node)
+                {
+                    List<ImmutableNode> children = super.getChildren(node);
+                    return filterChildrenOfGlobalSection(node, children);
+                }
+
+                @Override
+                protected NodeHandler<ImmutableNode> getDecoratedNodeHandler()
+                {
+                    return GlobalSectionNodeModel.super.getNodeHandler();
+                }
+
+                /**
+                 * Filters the child nodes of the global section. This method
+                 * checks whether the passed in node is the root node of the
+                 * configuration. If so, from the list of children all nodes are
+                 * filtered which are section nodes.
+                 *
+                 * @param node the node in question
+                 * @param children the children of this node
+                 * @return a list with the filtered children
+                 */
+                private List<ImmutableNode> filterChildrenOfGlobalSection(
+                        ImmutableNode node, List<ImmutableNode> children)
+                {
+                    List<ImmutableNode> filteredList;
+                    if (node == getRootNode())
+                    {
+                        filteredList =
+                                new ArrayList<ImmutableNode>(children.size());
+                        for (ImmutableNode child : children)
+                        {
+                            if (!isSectionNode(child))
+                            {
+                                filteredList.add(child);
+                            }
+                        }
+                    }
+                    else
+                    {
+                        filteredList = children;
+                    }
+
+                    return filteredList;
+                }
+            };
+        }
+    }
 }

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=1580604&r1=1580603&r2=1580604&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:36:00 2014
@@ -19,7 +19,6 @@ package org.apache.commons.configuration
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
@@ -972,72 +971,6 @@ public class TestINIConfiguration
     }
 
     /**
-     * Helper method for testing whether the access to a section is
-     * synchronized. This method delegates to the method with the same name
-     * passing in default methods.
-     *
-     * @param sectionName the name of the section to be queried
-     * @throws ConfigurationException if an error occurs
-     */
-    private void checkGetSectionSynchronized(String sectionName)
-            throws ConfigurationException
-    {
-        checkGetSectionSynchronized(sectionName, Methods.BEGIN_WRITE,
-                Methods.END_WRITE);
-    }
-
-    /**
-     * Helper method for testing whether the access to a section causes the
-     * specified methods to be called on the synchronizer.
-     *
-     * @param sectionName the name of the section to be queried
-     * @param expMethods the expected methods
-     * @throws ConfigurationException if an error occurs
-     */
-    private void checkGetSectionSynchronized(String sectionName,
-            Methods... expMethods) throws ConfigurationException
-    {
-        INIConfiguration config = setUpConfig(INI_DATA);
-        SynchronizerTestImpl sync = new SynchronizerTestImpl();
-        config.setSynchronizer(sync);
-        assertNotNull("No global section", config.getSection(sectionName));
-        sync.verify(expMethods);
-    }
-
-    /**
-     * Tests whether access to the global section is synchronized.
-     */
-    @Test
-    public void testGetSectionGlobalSynchronized()
-            throws ConfigurationException
-    {
-        checkGetSectionSynchronized(null);
-    }
-
-    /**
-     * Tests whether access to an existing section is synchronized.
-     */
-    @Test
-    public void testGetSectionExistingSynchronized()
-            throws ConfigurationException
-    {
-        // 1 x configurationAt(), then direct access to root node
-        checkGetSectionSynchronized("section1");
-    }
-
-    /**
-     * Tests whether access to a non-existing section is synchronized.
-     */
-    @Test
-    public void testGetSectionNonExistingSynchronized()
-            throws ConfigurationException
-    {
-        checkGetSectionSynchronized("Non-existing-section?",
-                Methods.BEGIN_WRITE, Methods.END_WRITE, Methods.BEGIN_WRITE,
-                Methods.END_WRITE);
-    }
-
-    /**
      * Tests whether the configuration deals correctly with list delimiters.
      */
     @Test