What I'm trying to do is a simple system property substitution for the properties file. The idea being that once the properties file has been loaded,
all the strings having form of ${system.property.key} will be substituted with the equivalent system property value for that given key.
For example if we have:
log.file = ${user.dir}\\logs\\system.log
at runtime this will become for example
log.file = c:\whatever\foobar\logs\system.log
I'm using regular expressions to perform the substitution. Everything is working
fine except the windows style paths. Somehow the regex strips all backslashes on windows paths.
Naturally on unix systems this is not a problem.
So with the above example the result would be
c:whateverfoobar\logs\system.log
The dummy test code is pretty simple
// substitutes ${whatever} style patterns with system properties
final Pattern p = Pattern.compile("\\$\\{([\\.\\w]+)\\}");
Properties props = new Properties();
try {
props.load(new FileInputStream("config.properties"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
Iterator it = props.entrySet().iterator();
while (it.hasNext()) {
StringBuffer sb = new StringBuffer();
Map.Entry entry = (Map.Entry)it.next();
Matcher m = p.matcher((String)entry.getValue());
while (m.find()) {
m.appendReplacement(sb, System.getProperty(m.group(1)));
}
m.appendTail(sb);
entry.setValue(sb.toString());
}
Am I missing something here or what's the cause of the problem?