Author: oheger
Date: Tue Nov 21 09:58:06 2006
New Revision: 477789
URL: http://svn.apache.org/viewvc?view=rev&rev=477789
Log:
Added a copy constructor to HierarchicalConfiguration and its sub classes
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractHierarchicalFileConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -51,13 +51,30 @@
*/
private FileConfigurationDelegate delegate;
- protected AbstractHierarchicalFileConfiguration()
- {
- delegate = createDelegate();
- initDelegate(delegate);
- }
+ /**
+ * Creates a new instance of
+ * <code>AbstractHierarchicalFileConfiguration</code>.
+ */
+ protected AbstractHierarchicalFileConfiguration()
+ {
+ initialize();
+ }
+
+ /**
+ * Creates a new instance of
+ * <code>AbstractHierarchicalFileConfiguration</code> and copies the
+ * content of the specified configuration into this object.
+ *
+ * @param c the configuration to copy
+ * @since 1.4
+ */
+ protected
AbstractHierarchicalFileConfiguration(HierarchicalConfiguration c)
+ {
+ super(c);
+ initialize();
+ }
- /**
+ /**
* Creates and loads the configuration from the specified file.
*
* @param fileName The name of the plist file to load.
@@ -108,7 +125,16 @@
load();
}
- protected void addPropertyDirect(String key, Object obj)
+ /**
+ * Initializes this instance, mainly the internally used delegate object.
+ */
+ private void initialize()
+ {
+ delegate = createDelegate();
+ initDelegate(delegate);
+ }
+
+ protected void addPropertyDirect(String key, Object obj)
{
super.addPropertyDirect(key, obj);
delegate.possiblySave();
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -134,7 +134,27 @@
setRootNode(new Node());
}
- /**
+ /**
+ * Creates a new instance of <code>HierarchicalConfiguration</code> and
+ * copies all data contained in the specified configuration into the new
+ * one.
+ *
+ * @param c the configuration that is to be copied (if <b>null</b>, this
+ * constructor will behave like the standard constructor)
+ * @since 1.4
+ */
+ public HierarchicalConfiguration(HierarchicalConfiguration c)
+ {
+ this();
+ if (c != null)
+ {
+ CloneVisitor visitor = new CloneVisitor();
+ c.getRootNode().visit(visitor);
+ setRootNode(visitor.getClone());
+ }
+ }
+
+ /**
* Returns the root node of this hierarchical configuration. This method
* exists for backwards compatibility only. New code should use the
* <code>[EMAIL PROTECTED] #getRootNode()}</code> method instead, which
operates on
@@ -904,10 +924,32 @@
return child;
}
- /**
+ /**
+ * Clears all reference fields in a node structure. A configuration node
can
+ * store a so-called "reference". The meaning of this data is
+ * determined by a concrete sub class. Typically such references are
+ * specific for a configuration instance. If this instance is cloned or
+ * copied, they must be cleared. This can be done using this method.
+ *
+ * @param node the root node of the node hierarchy, in which the references
+ * are to be cleared
+ * @since 1.4
+ */
+ protected static void clearReferences(ConfigurationNode node)
+ {
+ node.visit(new ConfigurationNodeVisitorAdapter()
+ {
+ public void visitBeforeChildren(ConfigurationNode node)
+ {
+ node.setReference(null);
+ }
+ });
+ }
+
+ /**
* A data class for storing (hierarchical) property information. A property
- * can have a value and an arbitrary number of child properties. From
version 1.3 on this class
- * is only a thin wrapper over the
+ * can have a value and an arbitrary number of child properties. From
+ * version 1.3 on this class is only a thin wrapper over the
* <code>[EMAIL PROTECTED]
org.apache.commons.configuration.tree.DefaultConfigurationNode
DefaultconfigurationNode}</code>
* class that exists mainly for the purpose of backwards compatibility.
*/
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -141,10 +141,26 @@
super();
}
- /**
- * Creates a new instance of <code>XMLConfiguration</code>.
- * The configuration is loaded from the specified file
- *
+ /**
+ * Creates a new instance of <code>XMLConfiguration</code> and copies the
+ * content of the passed in configuration into this object. Note that only
+ * the data of the passed in configuration will be copied. If, for
instance,
+ * the other configuration is a <code>XMLConfiguration</code>, too,
+ * things like comments or processing instructions will be lost.
+ *
+ * @param c the configuration to copy
+ * @since 1.4
+ */
+ public XMLConfiguration(HierarchicalConfiguration c)
+ {
+ super(c);
+ clearReferences(getRootNode());
+ }
+
+ /**
+ * Creates a new instance of <code>XMLConfiguration</code>. The
+ * configuration is loaded from the specified file
+ *
* @param fileName the name of the file to load
* @throws ConfigurationException if the file cannot be loaded
*/
@@ -696,13 +712,7 @@
copy.document = null;
copy.setDelegate(createDelegate());
// clear all references in the nodes, too
- copy.getRoot().visit(new NodeVisitor()
- {
- public void visitBeforeChildren(Node node, ConfigurationKey key)
- {
- node.setReference(null);
- }
- }, null);
+ clearReferences(copy.getRootNode());
return copy;
}
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -84,9 +84,21 @@
{
}
- /**
- * Creates and loads the property list from the specified file.
+ /**
+ * Creates a new instance of <code>PropertyListConfiguration</code> and
+ * copies the content of the specified configuration into this object.
*
+ * @param c the configuration to copy
+ * @since 1.4
+ */
+ public PropertyListConfiguration(HierarchicalConfiguration c)
+ {
+ super(c);
+ }
+
+ /**
+ * Creates and loads the property list from the specified file.
+ *
* @param fileName The name of the plist file to load.
* @throws ConfigurationException Error while loading the plist file
*/
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -133,11 +133,24 @@
{
}
- /**
- * Creates and loads the property list from the specified file.
+ /**
+ * Creates a new instance of <code>XMLPropertyListConfiguration</code> and
+ * copies the content of the specified configuration into this object.
*
+ * @param c the configuration to copy
+ * @since 1.4
+ */
+ public XMLPropertyListConfiguration(HierarchicalConfiguration c)
+ {
+ super(c);
+ }
+
+ /**
+ * Creates and loads the property list from the specified file.
+ *
* @param fileName The name of the plist file to load.
- * @throws org.apache.commons.configuration.ConfigurationException Error
while loading the plist file
+ * @throws org.apache.commons.configuration.ConfigurationException Error
+ * while loading the plist file
*/
public XMLPropertyListConfiguration(String fileName) throws
ConfigurationException
{
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -480,14 +480,7 @@
{
Configuration copy = (Configuration) config.clone();
assertTrue(copy instanceof HierarchicalConfiguration);
- for (int i = 0; i < tables.length; i++)
- {
- assertEquals(tables[i], copy.getString("tables.table(" + i +
").name"));
- for (int j = 0; j < fields[i].length; j++)
- {
- assertEquals(fields[i][j], copy.getString("tables.table(" + i
+ ").fields.field(" + j + ").name"));
- }
- }
+ checkContent(copy);
}
/**
@@ -655,8 +648,38 @@
}
}
- /**
+ /**
+ * Tests the copy constructor.
+ */
+ public void testInitCopy()
+ {
+ HierarchicalConfiguration copy = new
HierarchicalConfiguration(config);
+ checkContent(copy);
+ }
+
+ /**
+ * Tests whether the nodes of a copied configuration are independent from
+ * the source configuration.
+ */
+ public void testInitCopyUpdate()
+ {
+ HierarchicalConfiguration copy = new
HierarchicalConfiguration(config);
+ config.setProperty("tables.table(0).name", "NewTable");
+ checkContent(copy);
+ }
+
+ /**
+ * Tests the copy constructor when a null reference is passed.
+ */
+ public void testInitCopyNull()
+ {
+ HierarchicalConfiguration copy = new
HierarchicalConfiguration(null);
+ assertTrue("Configuration not empty", copy.isEmpty());
+ }
+
+ /**
* Helper method for testing the getKeys(String) method.
+ *
* @param prefix the key to pass into getKeys()
* @param expected the expected result
*/
@@ -720,8 +743,27 @@
.contains("tables/table/fields/field/name"));
}
- private ExpressionEngine createAlternativeExpressionEngine()
- {
+ /**
+ * Checks the content of the passed in configuration object. Used by some
+ * tests that copy a configuration.
+ *
+ * @param c the configuration to check
+ */
+ private void checkContent(Configuration c)
+ {
+ for (int i = 0; i < tables.length; i++)
+ {
+ assertEquals(tables[i], c.getString("tables.table(" + i
+ ").name"));
+ for (int j = 0; j < fields[i].length; j++)
+ {
+ assertEquals(fields[i][j],
c.getString("tables.table(" + i
+ + ").fields.field(" + j +
").name"));
+ }
+ }
+ }
+
+ private ExpressionEngine createAlternativeExpressionEngine()
+ {
DefaultExpressionEngine engine = new DefaultExpressionEngine();
engine.setPropertyDelimiter("/");
engine.setIndexStart("[");
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -34,6 +34,7 @@
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.configuration.reloading.InvariantReloadingStrategy;
+import org.apache.commons.configuration.tree.ConfigurationNode;
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
@@ -924,6 +925,28 @@
.getString("test[1]/entity/@name"));
conf.clear();
assertNull(conf.getString("test[1]/entity/@name"));
+ }
+
+ /**
+ * Tests the copy constructor.
+ */
+ public void testInitCopy() throws ConfigurationException
+ {
+ XMLConfiguration copy = new XMLConfiguration(conf);
+ assertEquals("value", copy.getProperty("element"));
+ assertNull("Document was copied, too", copy.getDocument());
+ ConfigurationNode root = copy.getRootNode();
+ for(Iterator it = root.getChildren().iterator(); it.hasNext();)
+ {
+ ConfigurationNode node = (ConfigurationNode) it.next();
+ assertNull("Reference was not cleared", node.getReference());
+ }
+
+ removeTestFile();
+ copy.setFile(testSaveConf);
+ copy.save();
+ copy.clear();
+ checkSavedConfig(copy);
}
/**
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -247,4 +247,10 @@
assertEquals("string with a quote", "\"foo\\\"bar\"",
config.quoteString("foo\"bar"));
assertEquals("string with a special char", "\"foo;bar\"",
config.quoteString("foo;bar"));
}
+
+ public void testInitCopy()
+ {
+ PropertyListConfiguration copy = new PropertyListConfiguration(config);
+ assertFalse("Nothing was copied", copy.isEmpty());
+ }
}
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/plist/TestXMLPropertyListConfiguration.java
Tue Nov 21 09:58:06 2006
@@ -26,6 +26,7 @@
import junitx.framework.ListAssert;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.StrictConfigurationComparator;
import org.apache.commons.configuration.ConfigurationComparator;
@@ -253,4 +254,12 @@
}
}
+
+ public void testInitCopy()
+ {
+ XMLPropertyListConfiguration copy = new
XMLPropertyListConfiguration(
+ (HierarchicalConfiguration) config);
+ StrictConfigurationComparator comp = new
StrictConfigurationComparator();
+ assertTrue("Configurations are not equal", comp.compare(config,
copy));
+ }
}
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=477789&r1=477788&r2=477789
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Tue Nov 21
09:58:06 2006
@@ -23,6 +23,10 @@
<body>
<release version="1.4-dev" date="in SVN">
+ <action dev="oheger" type="add" issue="CONFIGURATION-236">
+ HierarchicalConfiguration and some of its sub classes now define a
+ copy constructor.
+ </action>
<action dev="oheger" type="add" issue="CONFIGURATION-197" due-to="Trevor
Charles Miller">
A new configuration class for windows ini files was added.
</action>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]