Author: ebourg Date: Fri Apr 25 02:30:07 2008 New Revision: 651550 URL: http://svn.apache.org/viewvc?rev=651550&view=rev Log: Changed the property parsing in PropertiesConfiguration to use a regular expression
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java?rev=651550&r1=651549&r2=651550&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java Fri Apr 25 02:30:07 2008 @@ -27,6 +27,8 @@ 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; @@ -542,6 +544,10 @@ */ 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) +"])(.*)"); + /** Stores the comment lines for the currently processed property.*/ private List<String> commentLines; @@ -718,96 +724,14 @@ */ private static String[] parseProperty(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[2]; - StringBuilder key = new StringBuilder(); - StringBuilder value = new StringBuilder(); - - // 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 - state = 2; - } - else if (ArrayUtils.contains(SEPARATORS, c)) - { - // switch to the value parsing state - 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)) - { - // do nothing, eat all white spaces - state = 2; - } - else if (ArrayUtils.contains(SEPARATORS, c)) - { - // switch to the value parsing state - state = 3; - } - else - { - // any other character indicates we encoutered 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(4).trim(); } - - result[0] = key.toString().trim(); - result[1] = value.toString().trim(); return result; }