1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juddi.v3.client.config;
18
19 import java.util.Properties;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 public class TokenResolver {
27
28 private static Log log = LogFactory.getLog(TokenResolver.class);
29
30 public synchronized static String replaceTokens(String string, Properties properties) {
31
32 if (properties==null || string==null) return string;
33 string = string.replaceAll("\\n"," ").replaceAll("\\r", "").replaceAll(" ","");
34
35
36
37
38 Pattern pattern = Pattern.compile("(?m)\\$\\{.*?\\}");
39 Matcher matcher = pattern.matcher(string);
40 while (matcher.find()) {
41 String token = matcher.group();
42 token = token.substring(2,token.length()-1);
43 String replacement = properties.getProperty(token);
44 if (replacement!=null) {
45 log.debug("Found token " + token + " and replacement value " + replacement);
46 string = string.replaceAll("\\$\\{" + token + "\\}", replacement);
47 } else {
48 log.error("Found token " + token + " but could not obtain its value. Data: " + string);
49 }
50 }
51 log.debug("Data after token replacement: " + string);
52 return string;
53 }
54 }