Author: oheger
Date: Sun Jun 1 12:30:02 2008
New Revision: 662269
URL: http://svn.apache.org/viewvc?rev=662269&view=rev
Log:
CONFIGURATION-307: Provide a variant of PropertiesConverter.split() which
allows disabling trimming
Modified:
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
Modified:
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java?rev=662269&r1=662268&r2=662269&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
(original)
+++
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
Sun Jun 1 12:30:02 2008
@@ -499,9 +499,10 @@
*
* @param s the string to split
* @param delimiter the delimiter
+ * @param trim a flag whether the single elements should be trimmed
* @return a list with the single tokens
*/
- public static List split(String s, char delimiter)
+ public static List split(String s, char delimiter, boolean trim)
{
if (s == null)
{
@@ -535,7 +536,12 @@
if (c == delimiter)
{
// found a list delimiter -> add token and reset buffer
- list.add(token.toString().trim());
+ String t = token.toString();
+ if (trim)
+ {
+ t = t.trim();
+ }
+ list.add(t);
token = new StringBuffer();
}
else if (c == LIST_ESC_CHAR)
@@ -558,12 +564,30 @@
token.append(LIST_ESC_CHAR);
}
// Add last token
- list.add(token.toString().trim());
+ String t = token.toString();
+ if (trim)
+ {
+ t = t.trim();
+ }
+ list.add(t);
return list;
}
/**
+ * Split a string on the specified delimiter always trimming the elements.
+ * This is a shortcut for <code>split(s, delimiter, true)</code>.
+ *
+ * @param s the string to split
+ * @param delimiter the delimiter
+ * @return a list with the single tokens
+ */
+ public static List split(String s, char delimiter)
+ {
+ return split(s, delimiter, true);
+ }
+
+ /**
* 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
Modified:
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java?rev=662269&r1=662268&r2=662269&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
(original)
+++
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
Sun Jun 1 12:30:02 2008
@@ -45,6 +45,17 @@
assertEquals("3rd token for '" + s + "'", "123", list.get(2));
}
+ public void testSplitNoTrim()
+ {
+ String s = "abc, xyz , 123";
+ List list = PropertyConverter.split(s, ',', false);
+
+ assertEquals("size", 3, list.size());
+ assertEquals("1st token for '" + s + "'", "abc", list.get(0));
+ assertEquals("2nd token for '" + s + "'", " xyz ", list.get(1));
+ assertEquals("3rd token for '" + s + "'", " 123", list.get(2));
+ }
+
public void testSplitWithEscapedSeparator()
{
String s = "abc\\,xyz, 123";