Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5DD321017F for ; Sun, 2 Mar 2014 19:50:31 +0000 (UTC) Received: (qmail 11304 invoked by uid 500); 2 Mar 2014 19:50:29 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 11232 invoked by uid 500); 2 Mar 2014 19:50:28 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 11225 invoked by uid 99); 2 Mar 2014 19:50:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 02 Mar 2014 19:50:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 02 Mar 2014 19:50:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id A424A238897A; Sun, 2 Mar 2014 19:50:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Sun, 02 Mar 2014 19:50:03 -0000 To: commits@commons.apache.org From: oheger@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140302195003.A424A238897A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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; /** *

@@ -128,70 +130,40 @@ public class BaseHierarchicalConfigurati beginRead(false); try { -// Collection 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> 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> results) + { + ImmutableNode.Builder builder = new ImmutableNode.Builder(); + Object value = null; + int valueCount = 0; + + for (QueryResult 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(); + } + + /** *

* 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. */