diff --git a/core/src/main/java/hudson/Util.java b/core/src/main/java/hudson/Util.java index 240e93425a..d599a1a410 100644 --- a/core/src/main/java/hudson/Util.java +++ b/core/src/main/java/hudson/Util.java @@ -92,7 +92,7 @@ public class Util { /** * Pattern for capturing variables. Either $xyz or ${xyz}, while ignoring "$$" */ - private static final Pattern VARIABLE = Pattern.compile("(?properties.get('key'). @@ -118,14 +118,21 @@ public class Util { if(!m.find(idx)) return s; String key = m.group().substring(1); - if(key.charAt(0)=='{') key = key.substring(1,key.length()-1); - String value = resolver.resolve(key); + // escape the dollar sign or get the key to resolve + String value; + if(key.charAt(0)=='$') { + value = "$"; + } else { + if(key.charAt(0)=='{') key = key.substring(1,key.length()-1); + value = resolver.resolve(key); + } + if(value==null) - idx = m.start()+1; // skip this + idx = m.end(); // skip this else { s = s.substring(0,m.start())+value+s.substring(m.end()); - idx = m.start(); + idx = m.start() + value.length(); } } } diff --git a/core/src/test/java/hudson/UtilTest.java b/core/src/test/java/hudson/UtilTest.java index 521c8fbfb2..7fafac1cbd 100644 --- a/core/src/test/java/hudson/UtilTest.java +++ b/core/src/test/java/hudson/UtilTest.java @@ -14,6 +14,8 @@ public class UtilTest extends TestCase { m.put("A","a"); m.put("AA","aa"); m.put("B","B"); + m.put("DOLLAR", "$"); + m.put("ENCLOSED", "a${A}"); // longest match assertEquals("aa",Util.replaceMacro("$AA",m)); @@ -24,10 +26,14 @@ public class UtilTest extends TestCase { assertEquals("aaB",Util.replaceMacro("${AA}B",m)); assertEquals("${AAB}",Util.replaceMacro("${AAB}",m)); + // $ escaping + assertEquals("asd$${AA}dd", Util.replaceMacro("asd$$$${AA}dd",m)); + // test that more complex scenarios work - assertEquals("/a/B/aa", Util.replaceMacro("/$A/$B/$AA",m)); + assertEquals("/a/B/aa", Util.replaceMacro("/$A/$B/$AA",m)); assertEquals("a-aa", Util.replaceMacro("$A-$AA",m)); assertEquals("/a/foo/can/B/you-believe_aa~it?", Util.replaceMacro("/$A/foo/can/$B/you-believe_$AA~it?",m)); + assertEquals("$$aa$Ba${A}$it", Util.replaceMacro("$$$DOLLAR${AA}$$B${ENCLOSED}$it",m)); }