Author: oheger
Date: Sat Jul 30 08:28:01 2005
New Revision: 226535
URL: http://svn.apache.org/viewcvs?rev=226535&view=rev
Log:
Fixed some issues with XMLConfiguration and comma delimited lists; see BZ 35938
Modified:
jakarta/commons/proper/configuration/trunk/conf/test.xml
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Modified: jakarta/commons/proper/configuration/trunk/conf/test.xml
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/conf/test.xml?rev=226535&r1=226534&r2=226535&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/test.xml (original)
+++ jakarta/commons/proper/configuration/trunk/conf/test.xml Sat Jul 30
08:28:01 2005
@@ -38,6 +38,14 @@
<item>six</item>
</sublist>
</list>
+
+ <!-- Comma delimited lists -->
+ <split>
+ <list1>a,b,c</list1>
+ <list2>a\,b\,c</list2>
+ <list3 values="a,b,c"/>
+ <list4 values="a\,b\,c"/>
+ </split>
<!-- clear tests -->
<clear>
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?rev=226535&r1=226534&r2=226535&view=diff
==============================================================================
---
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
Sat Jul 30 08:28:01 2005
@@ -1367,6 +1367,7 @@
{
insertNode.setReference(ref);
}
+ sibling1 = insertNode;
}
}
}
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java?rev=226535&r1=226534&r2=226535&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
Sat Jul 30 08:28:01 2005
@@ -46,6 +46,9 @@
*/
public final class PropertyConverter
{
+ /** Constant for the list delimiter escaping character.*/
+ static final String LIST_ESCAPE = "\\";
+
/**
* Convert the specified object into a Boolean.
*
@@ -443,7 +446,7 @@
// extract the chunk
String chunk = s.substring(begin , end);
- if (chunk.endsWith("\\") && end != s.length())
+ if (chunk.endsWith(LIST_ESCAPE) && end != s.length())
{
token.append(chunk.substring(0, chunk.length() - 1));
token.append(delimiter);
@@ -466,6 +469,23 @@
}
return list;
+ }
+
+ /**
+ * Escapes the delimiters that might be contained in the given string. This
+ * method ensures that list delimiter characters that are part of a
+ * property's value are correctly escaped when a configuration is saved to
a
+ * file. Otherwise when loaded again the property will be treated as a list
+ * property.
+ *
+ * @param s the string with the value
+ * @param delimiter the list delimiter to use
+ * @return the correctly esaped string
+ */
+ public static String escapeDelimiters(String s, char delimiter)
+ {
+ return StringUtils.replace(s, String.valueOf(delimiter), LIST_ESCAPE
+ + delimiter);
}
/**
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=226535&r1=226534&r2=226535&view=diff
==============================================================================
---
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
Sat Jul 30 08:28:01 2005
@@ -296,6 +296,7 @@
(elemRefs) ? child : null);
constructHierarchy(childNode, child, elemRefs);
node.addChild(childNode);
+ handleDelimiters(node, childNode);
}
else if (w3cNode instanceof Text)
{
@@ -339,6 +340,40 @@
}
/**
+ * Deals with elements whose value is a list. In this case multiple child
+ * elements must be added.
+ *
+ * @param parent the parent element
+ * @param child the child element
+ */
+ private void handleDelimiters(Node parent, Node child)
+ {
+ if (child.getValue() != null)
+ {
+ List values = PropertyConverter.split(child.getValue().toString(),
+ getDelimiter());
+ if (values.size() > 1)
+ {
+ // remove the original child
+ parent.remove(child);
+ // add multiple new children
+ for (Iterator it = values.iterator(); it.hasNext();)
+ {
+ Node c = new XMLNode(child.getName(), null);
+ c.setValue(it.next());
+ parent.addChild(c);
+ }
+ }
+ else if (values.size() == 1)
+ {
+ // we will have to replace the value because it might
+ // contain escaped delimiters
+ child.setValue(values.get(0));
+ }
+ }
+ }
+
+ /**
* Creates the <code>DocumentBuilder</code> to be used for loading files.
* This implementation checks whether a specific
* <code>DocumentBuilder</code> has been set. If this is the case, this
@@ -752,10 +787,13 @@
{
if (txtNode == null)
{
- txtNode = document.createTextNode(value.toString());
+ txtNode = document
+ .createTextNode(PropertyConverter.escapeDelimiters(
+ value.toString(), getDelimiter()));
if (((Element) getReference()).getFirstChild() != null)
{
- ((Element) getReference()).insertBefore(txtNode,
((Element) getReference()).getFirstChild());
+ ((Element) getReference()).insertBefore(txtNode,
+ ((Element) getReference()).getFirstChild());
}
else
{
@@ -764,7 +802,8 @@
}
else
{
- txtNode.setNodeValue(value.toString());
+ txtNode.setNodeValue(PropertyConverter.escapeDelimiters(
+ value.toString(), getDelimiter()));
}
}
}
@@ -870,7 +909,7 @@
Element elem = document.createElement(newNode.getName());
if (newNode.getValue() != null)
{
-
elem.appendChild(document.createTextNode(newNode.getValue().toString()));
+
elem.appendChild(document.createTextNode(PropertyConverter.escapeDelimiters(newNode.getValue().toString(),
getDelimiter())));
}
if (sibling2 == null)
{
@@ -911,18 +950,21 @@
{
buf.append(getDelimiter());
}
- buf.append(attr.getValue());
+ buf.append(PropertyConverter.escapeDelimiters(attr
+ .getValue().toString(), getDelimiter()));
}
attr.setReference(elem);
}
if (buf.length() < 1)
{
-
elem.removeAttribute(ConfigurationKey.removeAttributeMarkers(name));
+ elem.removeAttribute(ConfigurationKey
+ .removeAttributeMarkers(name));
}
else
{
-
elem.setAttribute(ConfigurationKey.removeAttributeMarkers(name),
buf.toString());
+ elem.setAttribute(ConfigurationKey
+ .removeAttributeMarkers(name), buf.toString());
}
}
}
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?rev=226535&r1=226534&r2=226535&view=diff
==============================================================================
---
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
Sat Jul 30 08:28:01 2005
@@ -208,8 +208,6 @@
// test single attribute
conf.load();
key = "[EMAIL PROTECTED]";
- Object p = conf.getProperty(key);
- p = conf.getProperty("clear.element2");
conf.clearProperty(key);
assertNull(key, conf.getProperty(key));
assertNull(key, conf.getProperty(key));
@@ -367,6 +365,11 @@
{
conf.addProperty("[EMAIL PROTECTED]", "value" + i);
}
+
+ // add comma delimited lists with escaped delimiters
+ conf.addProperty("split.list5", "a\\,b\\,c");
+ conf.setProperty("element3", "value\\,value1\\,value2");
+ conf.setProperty("[EMAIL PROTECTED]", "foo\\,bar");
// save the configuration
conf.save(testSaveConf.getAbsolutePath());
@@ -554,6 +557,10 @@
assertEquals("foo", copy.getString("[EMAIL PROTECTED]"));
}
+ /**
+ * Tests the subset() method. There was a bug that calling subset() had
+ * undesired side effects.
+ */
public void testSubset() throws ConfigurationException
{
conf = new XMLConfiguration();
@@ -567,5 +574,18 @@
conf = new XMLConfiguration(testSaveConf);
assertEquals("users", conf.getString("tables.table(0).name"));
+ }
+
+ /**
+ * Tests string properties with list delimiters and escaped delimiters.
+ */
+ public void testSplitLists()
+ {
+ assertEquals("a", conf.getString("[EMAIL PROTECTED]"));
+ assertEquals(2, conf.getMaxIndex("[EMAIL PROTECTED]"));
+ assertEquals("a,b,c", conf.getString("[EMAIL PROTECTED]"));
+ assertEquals("a", conf.getString("split.list1"));
+ assertEquals(2, conf.getMaxIndex("split.list1"));
+ assertEquals("a,b,c", conf.getString("split.list2"));
}
}
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=226535&r1=226534&r2=226535&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Jul 30
08:28:01 2005
@@ -23,6 +23,11 @@
<body>
<release version="1.2-dev" date="in SVN">
+ <action dev="oheger" type="update" issue="35938">
+ Resolved some issues with XMLConfiguration and properties containing
+ the delimiter character. These properties are now correctly treated,
+ escaping the delimiter will work, too.
+ </action>
<action dev="ebourg" type="add">
Added support for XMLPropertiesConfiguration in ConfigurationFactory.
A <properties> element will generate a XMLPropertiesConfiguration
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]