You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2011/11/25 20:43:56 UTC

svn commit: r1206292 - /commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java

Author: oheger
Date: Fri Nov 25 19:43:55 2011
New Revision: 1206292

URL: http://svn.apache.org/viewvc?rev=1206292&view=rev
Log:
Replaced method for parsing properties by a regular expression. Ported from configuration2 branch to trunk.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=1206292&r1=1206291&r2=1206292&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java Fri Nov 25 19:43:55 2011
@@ -27,6 +27,8 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringEscapeUtils;
@@ -619,6 +621,12 @@ public class PropertiesConfiguration ext
      */
     public static class PropertiesReader extends LineNumberReader
     {
+        /** The regular expression to parse the key and the value of a property. */
+        private static final Pattern PROPERTY_PATTERN = Pattern
+                .compile("(([\\S&&[^\\\\" + new String(SEPARATORS)
+                        + "]]|\\\\.)*)(\\s*(\\s+|[" + new String(SEPARATORS)
+                        + "])\\s*)(.*)");
+
         /** Stores the comment lines for the currently processed property.*/
         private List<String> commentLines;
 
@@ -862,96 +870,16 @@ public class PropertiesConfiguration ext
          */
         private static String[] doParseProperty(String line)
         {
-            // sorry for this spaghetti code, please replace it as soon as
-            // possible with a regexp when the Java 1.3 requirement is dropped
-
-            String[] result = new String[3];
-            StringBuffer key = new StringBuffer();
-            StringBuffer value = new StringBuffer();
-            StringBuffer separator = new StringBuffer();
-
-            // state of the automaton:
-            // 0: key parsing
-            // 1: antislash found while parsing the key
-            // 2: separator crossing
-            // 3: value parsing
-            int state = 0;
+            Matcher matcher = PROPERTY_PATTERN.matcher(line);
 
-            for (int pos = 0; pos < line.length(); pos++)
-            {
-                char c = line.charAt(pos);
+            String[] result = {"", "", ""};
 
-                switch (state)
-                {
-                    case 0:
-                        if (c == '\\')
-                        {
-                            state = 1;
-                        }
-                        else if (ArrayUtils.contains(WHITE_SPACE, c))
-                        {
-                            // switch to the separator crossing state
-                            separator.append(c);
-                            state = 2;
-                        }
-                        else if (ArrayUtils.contains(SEPARATORS, c))
-                        {
-                            // switch to the value parsing state
-                            separator.append(c);
-                            state = 3;
-                        }
-                        else
-                        {
-                            key.append(c);
-                        }
-
-                        break;
-
-                    case 1:
-                        if (ArrayUtils.contains(SEPARATORS, c) || ArrayUtils.contains(WHITE_SPACE, c))
-                        {
-                            // this is an escaped separator or white space
-                            key.append(c);
-                        }
-                        else
-                        {
-                            // another escaped character, the '\' is preserved
-                            key.append('\\');
-                            key.append(c);
-                        }
-
-                        // return to the key parsing state
-                        state = 0;
-
-                        break;
-
-                    case 2:
-                        if (ArrayUtils.contains(WHITE_SPACE, c) || ArrayUtils.contains(SEPARATORS, c))
-                        {
-                            // record the separator
-                            separator.append(c);
-                        }
-                        else
-                        {
-                            // any other character indicates we encountered the beginning of the value
-                            value.append(c);
-
-                            // switch to the value parsing state
-                            state = 3;
-                        }
-
-                        break;
-
-                    case 3:
-                        value.append(c);
-                        break;
-                }
+            if (matcher.matches()) {
+                result[0] = matcher.group(1).trim();
+                result[1] = matcher.group(5).trim();
+                result[2] = matcher.group(3);
             }
 
-            result[0] = key.toString().trim();
-            result[1] = value.toString().trim();
-            result[2] = separator.toString();
-
             return result;
         }
     } // class PropertiesReader