1/*2 * Copyright 2001-2010 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17package org.apache.juddi.v3.client.config;
1819import java.util.Properties;
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
2223import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
2526publicclassTokenResolver {
2728privatestatic Log log = LogFactory.getLog(TokenResolver.class);
2930publicsynchronizedstatic String replaceTokens(String string, Properties properties) {
3132if (properties==null || string==null) return string;
33 string = string.replaceAll("\\n"," ").replaceAll("\\r", "").replaceAll(" ","");
34/* pattern that is multi-line (?m), and looks for a pattern of35 * ${token}, in a 'reluctant' manner, by using the ? to 36 * make sure we find ALL the tokens.37 */38 Pattern pattern = Pattern.compile("(?m)\\$\\{.*?\\}");
39 Matcher matcher = pattern.matcher(string);
40while (matcher.find()) {
41 String token = matcher.group();
42 token = token.substring(2,token.length()-1);
43 String replacement = properties.getProperty(token);
44if (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);
52return string;
53 }
54 }