From d9184ad8716cd35e5dd67bef1ebf21a0db45b8e0 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sat, 18 Nov 2006 17:52:04 +0000 Subject: [PATCH] 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 --- core/src/main/java/hudson/Util.java | 31 +++++++++++++++++++++++++ core/src/test/java/hudson/UtilTest.java | 24 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 core/src/test/java/hudson/UtilTest.java 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)); + } +}