commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r536324 - in /jakarta/commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/PropertyConverter.java test/org/apache/commons/configuration/TestPropertyConverter.java
Date Tue, 08 May 2007 20:54:34 GMT
Author: oheger
Date: Tue May  8 13:54:33 2007
New Revision: 536324

URL: http://svn.apache.org/viewvc?view=rev&rev=536324
Log:
CONFIGURATION-268: Updated splitting of list values so that the escape character can be itself
escaped

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertyConverter.java?view=diff&rev=536324&r1=536323&r2=536324
==============================================================================
--- 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
Tue May  8 13:54:33 2007
@@ -54,8 +54,11 @@
  */
 public final class PropertyConverter
 {
-    /** Constant for the list delimiter escaping character.*/
-    static final String LIST_ESCAPE = "\\";
+    /** Constant for the list delimiter as char.*/
+    static final char LIST_ESC_CHAR = '\\';
+
+    /** Constant for the list delimiter escaping character as string.*/
+    static final String LIST_ESCAPE = String.valueOf(LIST_ESC_CHAR);
 
     /** Constant for the prefix of hex numbers.*/
     private static final String HEX_PREFIX = "0x";
@@ -506,39 +509,53 @@
 
         StringBuffer token = new StringBuffer();
         int begin = 0;
-        int end = 0;
-        while (begin <= s.length())
-        {
-            // find the next delimiter
-            int index = s.indexOf(delimiter, end);
-
-            // move the end index at the end of the string if the delimiter is not found
-            end = (index != -1) ? index : s.length();
+        boolean inEscape = false;
 
-            // extract the chunk
-            String chunk = s.substring(begin , end);
-
-            if (chunk.endsWith(LIST_ESCAPE) && end != s.length())
+        while (begin < s.length())
+        {
+            char c = s.charAt(begin);
+            if (inEscape)
             {
-                token.append(chunk.substring(0, chunk.length() - 1));
-                token.append(delimiter);
+                // last character was the escape marker
+                // can current character be escaped?
+                if (c != delimiter && c != LIST_ESC_CHAR)
+                {
+                    // no, also add escape character
+                    token.append(LIST_ESC_CHAR);
+                }
+                token.append(c);
+                inEscape = false;
             }
+
             else
             {
-                // append the chunk to the token
-                token.append(chunk);
-
-                // add the token to the list
-                list.add(token.toString().trim());
-
-                // reset the token
-                token = new StringBuffer();
+                if (c == delimiter)
+                {
+                    // found a list delimiter -> add token and reset buffer
+                    list.add(token.toString().trim());
+                    token = new StringBuffer();
+                }
+                else if (c == LIST_ESC_CHAR)
+                {
+                    // eventually escape next character
+                    inEscape = true;
+                }
+                else
+                {
+                    token.append(c);
+                }
             }
 
-            // move to the next chunk
-            end = end + 1;
-            begin = end;
+            begin++;
+        }
+
+        // Trailing delimiter?
+        if (inEscape)
+        {
+            token.append(LIST_ESC_CHAR);
         }
+        // Add last token
+        list.add(token.toString().trim());
 
         return list;
     }
@@ -548,7 +565,7 @@
      * 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.
+     * property. A single backslash will also be escaped.
      *
      * @param s the string with the value
      * @param delimiter the list delimiter to use
@@ -556,7 +573,8 @@
      */
     public static String escapeDelimiters(String s, char delimiter)
     {
-        return StringUtils.replace(s, String.valueOf(delimiter), LIST_ESCAPE + delimiter);
+        String s1 = StringUtils.replace(s, LIST_ESCAPE, LIST_ESCAPE + LIST_ESCAPE);
+        return StringUtils.replace(s1, String.valueOf(delimiter), LIST_ESCAPE + delimiter);
     }
 
     /**

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java?view=diff&rev=536324&r1=536323&r2=536324
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertyConverter.java
Tue May  8 13:54:33 2007
@@ -83,6 +83,27 @@
         assertTrue(list.isEmpty());
     }
 
+    /**
+     * Tests whether an escape character can be itself escaped.
+     */
+    public void testSplitEscapeEscapeChar()
+    {
+        List list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
+        assertEquals("Wrong list size", 2, list.size());
+        assertEquals("Wrong element 1", "C:\\Temp\\", list.get(0));
+        assertEquals("Wrong element 2", "xyz", list.get(1));
+    }
+
+    /**
+     * Tests whether delimiters are correctly escaped.
+     */
+    public void testEscapeDelimiters()
+    {
+        assertEquals("Wrong escaped delimiters",
+                "C:\\\\Temp\\\\\\,D:\\\\Data\\\\", PropertyConverter
+                        .escapeDelimiters("C:\\Temp\\,D:\\Data\\", ','));
+    }
+
     public void testToIterator()
     {
         int[] array = new int[]{1, 2, 3};



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message