提交 d9184ad8 编写于 作者: K kohsuke

added code to handle macro expansion.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@1166 71c3de6d-444a-0410-be80-ed276b4c234a
上级 8dd1446d
......@@ -17,6 +17,7 @@ import java.util.Map;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.SimpleTimeZone;
import java.util.Map.Entry;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.regex.Matcher;
......@@ -31,6 +32,36 @@ import org.apache.tools.ant.BuildException;
* @author Kohsuke Kawaguchi
*/
public class Util {
/**
* Replaces the occurence of '$key' by <tt>properties.get('key')</tt>.
*
* <p>
* This is a rather naive implementation that causes somewhat unexpected
* behavior when the expansion contains other macros.
*/
public static String replaceMacro(String s, Map<String,String> properties) {
int idx=0;
while((idx=s.indexOf('$',idx))>=0) {
// identify the key
int end=idx+1;
while(end<s.length()) {
char ch = s.charAt(end++);
if(!Character.isJavaIdentifierPart(ch))
break;
}
String key = s.substring(idx+1,end);
String value = properties.get(key);
if(value==null) {
idx++; // skip this '$' mark
} else {
s = s.substring(0,idx)+value+s.substring(end);
}
}
return s;
}
/**
* Loads the contents of a file into a string.
*/
......
package hudson;
import junit.framework.TestCase;
import java.util.Map;
import java.util.HashMap;
/**
* @author Kohsuke Kawaguchi
*/
public class UtilTest extends TestCase {
public void testReplaceMacro() {
Map<String,String> m = new HashMap<String,String>();
m.put("A","a");
m.put("AA","aa");
m.put("B","B");
// longest match
assertEquals("aa",Util.replaceMacro("$AA",m));
// invalid keys are ignored
assertEquals("$AAB",Util.replaceMacro("$AAB",m));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册