Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Refactor escaping in config file read/write code. Now it uses Apache Commons StringUtils instead of RegEx.

JeremyRand58824dfa6cdf
diff --git a/source/net/yacy/kelondro/util/FileUtils.java b/source/net/yacy/kelondro/util/FileUtils.java
index 7911c97dc..b513ba5b0 100644
--- a/source/net/yacy/kelondro/util/FileUtils.java
+++ b/source/net/yacy/kelondro/util/FileUtils.java
@@ -55,13 +55,14 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.storage.Files;
import net.yacy.cora.util.ConcurrentLog;
+import org.apache.commons.lang.StringUtils;
+
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsPSMDetector;
@@ -420,11 +421,10 @@ public final class FileUtils {
return mb;
}
- private final static Pattern backslashbackslash = Pattern.compile("\\\\");
- private final static Pattern unescaped_equal = Pattern.compile("=");
- private final static Pattern escaped_equal = Pattern.compile("\\=", Pattern.LITERAL);
- private final static Pattern escaped_newline = Pattern.compile("\\n", Pattern.LITERAL);
- private final static Pattern escaped_backslash = Pattern.compile(Pattern.quote("\\"), Pattern.LITERAL);
+ private final static String[] unescaped_strings_in = {"\r\n", "\r", "\n", "=", "\\"};
+ private final static String[] escaped_strings_out = {"\\n", "\\n", "\\n", "\\=", "\\\\"};
+ private final static String[] escaped_strings_in = {"\\\\", "\\n", "\\="};
+ private final static String[] unescaped_strings_out = {"\\", "\n", "="};
public static void saveMap(final File file, final Map<String, String> props, final String comment) {
boolean err = false;
@@ -437,16 +437,13 @@ public final class FileUtils {
for ( final Map.Entry<String, String> entry : props.entrySet() ) {
key = entry.getKey();
if ( key != null ) {
- key = backslashbackslash.matcher(key).replaceAll("\\\\");
- key = escaped_newline.matcher(key).replaceAll("\\n");
- key = unescaped_equal.matcher(key).replaceAll("\\=");
+ key = StringUtils.replaceEach(key, unescaped_strings_in, escaped_strings_out);
}
if ( entry.getValue() == null ) {
value = "";
} else {
value = entry.getValue();
- value = backslashbackslash.matcher(value).replaceAll("\\\\");
- value = escaped_newline.matcher(value).replaceAll("\\n");
+ value = StringUtils.replaceEach(value, unescaped_strings_in, escaped_strings_out);
}
pw.println(key + "=" + value);
}
@@ -495,11 +492,8 @@ public final class FileUtils {
pos = line.indexOf('=', pos + 1);
} while ( pos > 0 && line.charAt(pos - 1) == '\\' );
if ( pos > 0 ) try {
- String key = escaped_equal.matcher(line.substring(0, pos).trim()).replaceAll("=");
- key = escaped_newline.matcher(key).replaceAll("\n");
- key = escaped_backslash.matcher(key).replaceAll("\\");
- String value = escaped_newline.matcher(line.substring(pos + 1).trim()).replaceAll("\n");
- value = value.replace("\\\\", "\\"); // does not work: escaped_backslashbackslash.matcher(value).replaceAll("\\");
+ String key = StringUtils.replaceEach(line.substring(0, pos).trim(), escaped_strings_in, unescaped_strings_out);
+ String value = StringUtils.replaceEach(line.substring(pos + 1).trim(), escaped_strings_in, unescaped_strings_out);
//System.out.println("key = " + key + ", value = " + value);
props.put(key, value);
} catch (final IndexOutOfBoundsException e) {