Author: oheger Date: Sat Jul 12 08:22:35 2008 New Revision: 676184 URL: http://svn.apache.org/viewvc?rev=676184&view=rev Log: CONFIGURATION-333: Added an overloaded version of the configurationsAt() method that supports a "supportsUpdate" parameter.
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubConfiguration.java commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java?rev=676184&r1=676183&r2=676184&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java Sat Jul 12 08:22:35 2008 @@ -301,42 +301,76 @@ /** * Returns a list of sub configurations for all configuration nodes selected * by the given key. This method will evaluate the passed in key (using the - * current <code>ExpressionEngine</code>) and then create a subnode - * configuration for each returned node (like + * current <code>ExpressionEngine</code>) and then create a + * <code>[EMAIL PROTECTED] SubConfiguration}</code> for each returned node (like * <code>[EMAIL PROTECTED] #configurationAt(String)}</code>}). This is especially * useful when dealing with list-like structures. As an example consider the * configuration that contains data about database tables and their fields. * If you need access to all fields of a certain table, you can simply do * * <pre> - * List fields = config.configurationsAt("tables.table(0).fields.field"); - * for(Iterator it = fields.iterator(); it.hasNext();) + * List<SubConfiguration<T>> fields = config.configurationsAt("tables.table(0).fields.field"); + * for(SubConfiguration sub : fields) * { - * HierarchicalConfiguration sub = (HierarchicalConfiguration) it.next(); * // now the children and attributes of the field node can be * // directly accessed * String fieldName = sub.getString("name"); * String fieldType = sub.getString("type"); * ... * </pre> + * This method also supports a <code>supportUpdates</code> parameter for + * making the sub configurations returned aware of structural changes in + * the parent configuration. Refer to the documentation of + * <code>[EMAIL PROTECTED] #configurationAt(String, boolean)}</code> for more details + * about the effect of this flag. * * @param key the key for selecting the desired nodes + * @param supportUpdates a flag whether the returned sub configurations + * should be able to handle updates of its parent * @return a list with hierarchical configuration objects; each * configuration represents one of the nodes selected by the passed in key */ - public List<SubConfiguration<T>> configurationsAt(String key) + public List<SubConfiguration<T>> configurationsAt(String key, boolean supportUpdates) { NodeList<T> nodes = fetchNodeList(key); List<SubConfiguration<T>> configs = new ArrayList<SubConfiguration<T>>( nodes.size()); + for (int index = 0; index < nodes.size(); index++) { - configs.add(createSubnodeConfiguration(nodes.getNode(index))); + SubConfiguration<T> subConfig; + if(supportUpdates) + { + String subnodeKey = constructPath(nodes.getNode(index)); + subConfig = createSubnodeConfiguration(nodes.getNode(index), subnodeKey); + } + else + { + subConfig = createSubnodeConfiguration(nodes.getNode(index)); + } + configs.add(subConfig); } + return configs; } /** + * Returns a list of sub configurations for all configuration nodes selected + * by the given key that are not aware of structural updates of their + * parent. This is a short form for + * <code>configurationsAt(key, <b>false</b>)</code>. + * + * @param key the key for selecting the desired nodes + * @return a list with hierarchical configuration objects; each + * configuration represents one of the nodes selected by the passed + * in key + */ + public List<SubConfiguration<T>> configurationsAt(String key) + { + return configurationsAt(key, false); + } + + /** * Creates a sub configuration for the specified node. This method is * called by <code>configurationAt()</code> and * <code>configurationsAt()</code>. Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java?rev=676184&r1=676183&r2=676184&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java Sat Jul 12 08:22:35 2008 @@ -30,6 +30,7 @@ import org.apache.commons.configuration2.event.ConfigurationListener; import org.apache.commons.configuration2.expr.ExpressionEngine; import org.apache.commons.configuration2.expr.NodeHandler; +import org.apache.commons.configuration2.expr.NodeList; import org.apache.commons.configuration2.expr.NodeVisitorAdapter; import org.apache.commons.configuration2.expr.def.DefaultExpressionEngine; import org.apache.commons.configuration2.tree.ConfigurationNode; @@ -572,9 +573,10 @@ assertEquals("Wrong size of FIELDS", FIELDS[1].length, lstFlds.size()); for (int i = 0; i < FIELDS[1].length; i++) { - AbstractHierarchicalConfiguration<ConfigurationNode> sub = lstFlds.get(i); + SubConfiguration<ConfigurationNode> sub = lstFlds.get(i); assertEquals("Wrong field at position " + i, FIELDS[1][i], sub .getString("name")); + assertNull("Sub configuration detects changes", sub.getSubnodeKey()); } } @@ -588,6 +590,24 @@ .isEmpty()); } + /** + * Tests the configurationsAt() method when the sub configurations should be + * aware of updates. + */ + public void testConfigurationsAtSupportUpdates() + { + List<SubConfiguration<ConfigurationNode>> lstFlds = config + .configurationsAt("tables.table(1).fields.field", true); + for (SubConfiguration<ConfigurationNode> sub : lstFlds) + { + assertNotNull("No subnode key set", sub.getSubnodeKey()); + NodeList<ConfigurationNode> nodes = config.fetchNodeList(sub + .getSubnodeKey()); + assertEquals("Wrong elements in node list", 1, nodes.size()); + assertEquals("Wrong root node", nodes.getNode(0), sub.getRootNode()); + } + } + public void testClone() { Configuration copy = (Configuration) config.clone(); Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubConfiguration.java?rev=676184&r1=676183&r2=676184&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubConfiguration.java Sat Jul 12 08:22:35 2008 @@ -476,7 +476,6 @@ */ protected InMemoryConfiguration setUpParentConfig() { - @SuppressWarnings("serial") InMemoryConfiguration conf = new InMemoryConfiguration() { // Provide a special implementation of createNode() to check Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=676184&r1=676183&r2=676184&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original) +++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Sat Jul 12 08:22:35 2008 @@ -76,6 +76,12 @@ A new PreferencesConfiguration class was added that wraps the java.util.prefs API. </action> + <action dev="oheger" type="add" issue="CONFIGURATION-333"> + The configurationsAt() method of hierarchical configurations, like + configurationAt(), now supports an "supportsUpdate" parameter that + makes the sub configurations returned aware of structural updates of + their parent. + </action> </release> <release version="1.6" date="in SVN" description="">