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/02 20:50:03 UTC

svn commit: r1573346 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java

Author: oheger
Date: Sun Mar  2 19:50:03 2014
New Revision: 1573346

URL: http://svn.apache.org/r1573346
Log:
Implemented subset() in BaseHierarchicalConfiguration.

The corresponding tests now pass.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java?rev=1573346&r1=1573345&r2=1573346&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/BaseHierarchicalConfiguration.java Sun Mar  2 19:50:03 2014
@@ -30,11 +30,13 @@ import java.util.WeakHashMap;
 
 import org.apache.commons.configuration.event.ConfigurationEvent;
 import org.apache.commons.configuration.event.ConfigurationListener;
+import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
 import org.apache.commons.configuration.tree.ConfigurationNode;
 import org.apache.commons.configuration.tree.ConfigurationNodeVisitorAdapter;
 import org.apache.commons.configuration.tree.ImmutableNode;
 import org.apache.commons.configuration.tree.InMemoryNodeModel;
 import org.apache.commons.configuration.tree.NodeModel;
+import org.apache.commons.configuration.tree.QueryResult;
 
 /**
  * <p>
@@ -128,70 +130,40 @@ public class BaseHierarchicalConfigurati
         beginRead(false);
         try
         {
-//            Collection<ConfigurationNode> nodes = fetchNodeList(prefix);
-//            if (nodes.isEmpty())
-//            {
-//                return new BaseHierarchicalConfiguration();
-//            }
-//
-//            final BaseHierarchicalConfiguration parent = this;
-//            BaseHierarchicalConfiguration result =
-//                    new BaseHierarchicalConfiguration()
-//                    {
-//                        // Override interpolate to always interpolate on the parent
-//                        @Override
-//                        protected Object interpolate(Object value)
-//                        {
-//                            return parent.interpolate(value);
-//                        }
-//
-//                        @Override
-//                        public ConfigurationInterpolator getInterpolator()
-//                        {
-//                            return parent.getInterpolator();
-//                        }
-//                    };
-//            CloneVisitor visitor = new CloneVisitor();
-//
-//            // Initialize the new root node
-//            Object value = null;
-//            int valueCount = 0;
-//            for (ConfigurationNode nd : nodes)
-//            {
-//                if (nd.getValue() != null)
-//                {
-//                    value = nd.getValue();
-//                    valueCount++;
-//                }
-//                nd.visit(visitor);
-//
-//                for (ConfigurationNode c : visitor.getClone().getChildren())
-//                {
-//                    result.getRootNode().addChild(c);
-//                }
-//                for (ConfigurationNode attr : visitor.getClone()
-//                        .getAttributes())
-//                {
-//                    result.getRootNode().addAttribute(attr);
-//                }
-//            }
-//
-//            // Determine the value of the new root
-//            if (valueCount == 1)
-//            {
-//                result.getRootNode().setValue(value);
-//            }
-//            if (result.isEmpty())
-//            {
-//                return new BaseHierarchicalConfiguration();
-//            }
-//            else
-//            {
-//                result.setSynchronizer(getSynchronizer());
-//                return result;
-//            }
-            //TODO implementation
-            return null;
+            List<QueryResult<ImmutableNode>> results = fetchNodeList(prefix);
+            if (results.isEmpty())
+            {
+                return new BaseHierarchicalConfiguration();
+            }
+
+            final BaseHierarchicalConfiguration parent = this;
+            BaseHierarchicalConfiguration result =
+                    new BaseHierarchicalConfiguration()
+                    {
+                        // Override interpolate to always interpolate on the parent
+                        @Override
+                        protected Object interpolate(Object value)
+                        {
+                            return parent.interpolate(value);
+                        }
+
+                        @Override
+                        public ConfigurationInterpolator getInterpolator()
+                        {
+                            return parent.getInterpolator();
+                        }
+                    };
+            result.setRootNode(createSubsetRootNode(results));
+
+            if (result.isEmpty())
+            {
+                return new BaseHierarchicalConfiguration();
+            }
+            else
+            {
+                result.setSynchronizer(getSynchronizer());
+                return result;
+            }
         }
         finally
         {
@@ -200,6 +172,48 @@ public class BaseHierarchicalConfigurati
     }
 
     /**
+     * Creates a root node for a subset configuration based on the passed in
+     * query results. This method creates a new root node and adds the children
+     * and attributes of all result nodes to it. If only a single node value is
+     * defined, it is assigned as value of the new root node.
+     *
+     * @param results the collection of query results
+     * @return the root node for the subset configuration
+     */
+    private ImmutableNode createSubsetRootNode(
+            Collection<QueryResult<ImmutableNode>> results)
+    {
+        ImmutableNode.Builder builder = new ImmutableNode.Builder();
+        Object value = null;
+        int valueCount = 0;
+
+        for (QueryResult<ImmutableNode> result : results)
+        {
+            if (result.isAttributeResult())
+            {
+                builder.addAttribute(result.getAttributeName(),
+                        result.getAttributeValue(getModel().getNodeHandler()));
+            }
+            else
+            {
+                if (result.getNode().getValue() != null)
+                {
+                    value = result.getNode().getValue();
+                    valueCount++;
+                }
+                builder.addChildren(result.getNode().getChildren());
+                builder.addAttributes(result.getNode().getAttributes());
+            }
+        }
+
+        if (valueCount == 1)
+        {
+            builder.value(value);
+        }
+        return builder.create();
+    }
+
+    /**
      * <p>
      * Returns a hierarchical subnode configuration object that wraps the
      * configuration node specified by the given key. This method provides an

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=1573346&r1=1573345&r2=1573346&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java Sun Mar  2 19:50:03 2014
@@ -100,7 +100,7 @@ public class TestHierarchicalConfigurati
     }
 
     /**
-     * Tests the subset() method when the specified node has a value. This value
+     * Tests the subset() method if the specified node has a value. This value
      * must be available in the subset, too. Related to CONFIGURATION-295.
      */
     @Test
@@ -114,7 +114,7 @@ public class TestHierarchicalConfigurati
     }
 
     /**
-     * Tests the subset() method when the specified key selects multiple keys.
+     * Tests the subset() method if the specified key selects multiple keys.
      * The resulting root node should have a value only if exactly one of the
      * selected nodes has a value. Related to CONFIGURATION-295.
      */
@@ -131,6 +131,22 @@ public class TestHierarchicalConfigurati
     }
 
     /**
+     * Tests subset() if the passed in key selects an attribute.
+     */
+    @Test
+    public void testSubsetAttributeResult()
+    {
+        String key = "tables.table(0)[@type]";
+        config.addProperty(key, "system");
+        BaseHierarchicalConfiguration subset =
+                (BaseHierarchicalConfiguration) config.subset(key);
+        assertTrue("Got children of root node", subset.getRootNode()
+                .getChildren().isEmpty());
+        assertEquals("Attribute not found", "system",
+                subset.getString("[@type]"));
+    }
+
+    /**
      * Tests the configurationAt() method to obtain a configuration for a sub
      * tree.
      */