diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 20d271891b..e57d2c2032 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -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 properties.get('key'). + * + *

+ * This is a rather naive implementation that causes somewhat unexpected + * behavior when the expansion contains other macros. + */ + public static String replaceMacro(String s, Map properties) { + int idx=0; + while((idx=s.indexOf('$',idx))>=0) { + // identify the key + int end=idx+1; + while(end m = new HashMap(); + 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)); + } +}