Permalink
Browse files

additional method to add a child to a MutableConfiguration. Patch fro…

…m Jakub.
  • Loading branch information...
florin.patrascu
florin.patrascu committed Sep 24, 2007
1 parent 72b287f commit 48013639da4d8f5d5c6357ba6f9ac7afd6c009e3
@@ -1,4 +1,7 @@
EdenLib change log:
PostEdenLib changelog:
[2007.09.24]
- additional method to add a child to a MutableConfiguration. Patch from Jakub.
[2007.09.21]
- ensure element is never added with a null value to mutable configuration. Another
@@ -57,6 +57,7 @@ Anthony Eden (http://www.anthonyeden.com/)."
* interface.
*
* @author Anthony Eden
* @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
*/
public class ConfigurationBase implements MutableConfiguration {
@@ -287,6 +288,24 @@ public MutableConfiguration addChild(String name, Object value) {
return child;
}
/**
* Add a child node to the configuration. The value's toString() method
* will be used to convert the value to a String. If the value is null use the
* default value.
*
* @param name The name of the new configuration node
* @param value The value of the new configuration node
* @param defaultValue value to use if the value was null
* @return The configuration node
*/
public MutableConfiguration addChild(String name, Object value, Object defaultValue) {
if (value == null) {
return addChild(name, defaultValue);
} else {
return addChild(name, value);
}
}
/**
* Add the configuration object as a child of this configuration object.
*
@@ -347,7 +366,7 @@ public List getAttributeNames() {
List attributeNames = new ArrayList();
Iterator attributeKeys = attributes.keySet().iterator();
while (attributeKeys.hasNext()) {
attributeNames.add((String) attributeKeys.next());
attributeNames.add(attributeKeys.next());
}
return attributeNames;
}
@@ -505,11 +524,12 @@ public Configuration copy() {
}
/**
* This would be a deep copy if you used immutable objects such as Strings for values and attributes,
* and shallow otherwise.
* It clones the Location object, however it sets the parent to null.
* This would be a deep copy if you used immutable objects such as Strings for values and
* attributes, and shallow otherwise. It clones the Location object, however it sets the
* parent to null.
*
* @return copy of this Configuration
* @param parentConfig
*/
public Configuration copy(Configuration parentConfig) {
ArrayList childrenCopy = new ArrayList();
@@ -47,106 +47,119 @@ Anthony Eden (http://www.anthonyeden.com/)."
package com.anthonyeden.lib.config;
import java.io.Writer;
import java.io.OutputStream;
import java.io.Writer;
/**
/**
* Interface implemented by configuration objects which are mutable.
*
*
* @author Anthony Eden
* @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
*/
public interface MutableConfiguration extends Configuration {
/**
/**
* Set the node name.
*
*
* @param name The new node name
*/
public void setName(String name);
/**
/**
* Add a child node with no child value to the configuration. This
* method should be the same as calling
* method should be the same as calling
* <code>addChild(name, null)</code>
*
*
* @param name The name of the new configuration node
* @return The configuration node
*/
public MutableConfiguration addChild(String name);
/**
/**
* Add a child node to the configuration. The value's toString() method
* will be used to convert the value to a String.
*
* @param name The name of the new configuration node
*
* @param name The name of the new configuration node
* @param value The value of the new configuration node
* @return The configuration node
*/
public MutableConfiguration addChild(String name, Object value);
/**
/**
* Add a child node to the configuration. The value's toString() method
* will be used to convert the value to a String. If the value is null use the
* default value. [patch from Jakub]
*
* @param name The name of the new configuration node
* @param value The value of the new configuration node
* @param defaultValue value to use if the value was null
* @return The configuration node
*/
public MutableConfiguration addChild(String name, Object value, Object defaultValue);
/**
* Add the configuration object as a child of this configuration object.
*
*
* @param configuration The child configuration object
*/
public void addChild(Configuration configuration);
/**
/**
* Remove the specified configuration object.
*
*
* @param configuration The child configuration object
*/
public void removeChild(Configuration configuration);
/**
/**
* Add an attribute with the given name. The value's toString() method
* will be used to convert the value to a String.
*
* @param name The attribute name
*
* @param name The attribute name
* @param value The attribute value
*/
public void addAttribute(String name, Object value);
/**
/**
* Set the configuration object's value.
*
*
* @param value The new value
*/
public void setValue(String value);
/**
* Remove all of the children of this configuration node.
/**
* Remove all of the children of this configuration node.
*/
public void clearChildren();
/**
* Save the configuration data to the specified output stream. Not all
* implementations will support writing configuration data.
*
* @param out The OutputStream
* @throws ConfigurationException
*/
public void save(OutputStream out) throws ConfigurationException;
/**
* Save the configuration data to the specified output stream. Not all
* Save the configuration data to the specified output stream. Not all
* implementations will support writing configuration data.
*
* @param out The Writer
* @throws ConfigurationException
*/
public void save(Writer out) throws ConfigurationException;
}
@@ -397,6 +397,23 @@ public synchronized MutableConfiguration addChild(String name, Object value) {
}
}
/**
* Add a child node to the configuration.
*
* @param name The name of the new configuration node
* @param value The value of the new configuration node
* @param defaultValue The value to use in case the value is null
* @return The configuration node
*/
public synchronized MutableConfiguration addChild(String name, Object value, Object defaultValue) {
if (configuration instanceof MutableConfiguration) {
return ((MutableConfiguration) configuration).addChild(name, value, defaultValue);
} else {
throw new UnsupportedOperationException("This configuration is not mutable.");
}
}
/**
* Add the configuration object as a child of this configuration object.
*
@@ -18,15 +18,15 @@
import com.anthonyeden.lib.config.Configuration;
import com.anthonyeden.lib.config.ConfigurationException;
import com.anthonyeden.lib.config.XMLConfiguration;
import com.anthonyeden.lib.config.MutableConfiguration;
import com.anthonyeden.lib.config.XMLConfiguration;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
/**
* unit tests for the {@link com.anthonyeden.lib.config.XMLConfiguration} class
@@ -38,7 +38,7 @@
private static final Log log = LogFactory.getLog(XMLConfigurationTests.class);
String hitsTagString = "<hits>Καλημέρα κόσμε</hits>";
byte[] hitsTagByteArray = {'<', 'h', 'i', 't', 's', '>', 'a', 't', 'e', 'n',
(byte) 0xE5, (byte) 0xBF, (byte) 0xAE,'<', '/', 'h', 'i', 't', 's', '>', '\n'};
(byte) 0xE5, (byte) 0xBF, (byte) 0xAE, '<', '/', 'h', 'i', 't', 's', '>', '\n'};
public void testParseString() throws ConfigurationException, UnsupportedEncodingException {
log.info("parsing a String containing an xml structure....");
@@ -95,16 +95,16 @@ public void testToXMLString() throws ConfigurationException {
public void testCopy() throws ConfigurationException {
log.info("trying to compare outputs of two different toXMLString methods");
XMLConfiguration c = new XMLConfiguration("testXML1", hitsTagString);
c.addChild("boo","foo");
c.addChild("boo", "foo");
Configuration d = c.copy();
assertEquals(c.getChildValue("boo"), d.getChildValue("boo"));
((MutableConfiguration) c.getChild("boo")).addChild("zchor","smor");
assertEquals(1,c.getChild("boo").getChildren().size());
assertEquals(0,d.getChild("boo").getChildren().size());
((MutableConfiguration) d.getChild("boo")).addChild("aaa","1a");
((MutableConfiguration) d.getChild("boo")).addChild("bbb","2b");
assertEquals(2,d.getChild("boo").getChildren().size());
assertEquals(1,c.getChild("boo").getChildren().size());
((MutableConfiguration) c.getChild("boo")).addChild("zchor", "smor");
assertEquals(1, c.getChild("boo").getChildren().size());
assertEquals(0, d.getChild("boo").getChildren().size());
((MutableConfiguration) d.getChild("boo")).addChild("aaa", "1a");
((MutableConfiguration) d.getChild("boo")).addChild("bbb", "2b");
assertEquals(2, d.getChild("boo").getChildren().size());
assertEquals(1, c.getChild("boo").getChildren().size());
}
public void testAddMutableChild() throws ConfigurationException {
@@ -115,4 +115,17 @@ public void testAddMutableChild() throws ConfigurationException {
assertNotNull(boo.getChildValue("foo"));
assertNotNull(c.toXMLString());
}
public void testAddChildWithDefaultValue() throws ConfigurationException {
log.info("trying to ensure that if child is added with null value that it indeed uses the provided default");
XMLConfiguration c = new XMLConfiguration("testXML", hitsTagString);
MutableConfiguration boo = c.addChild("boo", null, "Test");
String s = null;
try {
s = c.toXMLString();
} catch (Exception ignore) {
}
assertNotNull(s);
assertEquals("Test", c.getChildValue("boo"));
}
}

0 comments on commit 4801363

Please sign in to comment.