diff --git a/core/src/main/java/org/jclouds/util/Utils.java b/core/src/main/java/org/jclouds/util/Utils.java index 1d1aa17c6d..cec4747a49 100644 --- a/core/src/main/java/org/jclouds/util/Utils.java +++ b/core/src/main/java/org/jclouds/util/Utils.java @@ -34,6 +34,8 @@ import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Map; import java.util.concurrent.ExecutionException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.annotation.Resource; @@ -79,7 +81,7 @@ public class Utils { String returnVal = URLEncoder.encode(in, "UTF-8").replaceAll("\\+", "%20").replaceAll( "\\*", "%2A").replaceAll("%7E", "~"); for (char c : skipEncode) { - returnVal = returnVal.replaceAll(plainToEncodedChars.get(c+""), c + ""); + returnVal = returnVal.replaceAll(plainToEncodedChars.get(c + ""), c + ""); } return returnVal; } catch (UnsupportedEncodingException e) { @@ -266,4 +268,36 @@ public class Utils { return decodeString(bytes, UTF8_ENCODING); } + public static final Pattern pattern = Pattern.compile("\\{(.+?)\\}"); + + /** + * replaces tokens that are expressed as {token} + * + *

+ * ex. if input is "hello {where}"
+ * and replacements is "where" -> "world"
+ * then replaceTokens returns "hello world" + * + * @param input + * source to replace + * @param replacements + * token/value pairs + */ + public static String replaceTokens(String input, Map replacements) { + Matcher matcher = pattern.matcher(input); + StringBuilder builder = new StringBuilder(); + int i = 0; + while (matcher.find()) { + String replacement = replacements.get(matcher.group(1)); + builder.append(input.substring(i, matcher.start())); + if (replacement == null) + builder.append(matcher.group(0)); + else + builder.append(replacement); + i = matcher.end(); + } + builder.append(input.substring(i, input.length())); + return builder.toString(); + } + } diff --git a/core/src/test/java/org/jclouds/util/HttpUtilsTest.java b/core/src/test/java/org/jclouds/util/HttpUtilsTest.java index 5c4315a3ae..ccd0aadbc3 100644 --- a/core/src/test/java/org/jclouds/util/HttpUtilsTest.java +++ b/core/src/test/java/org/jclouds/util/HttpUtilsTest.java @@ -45,7 +45,7 @@ import org.testng.annotations.Test; * * @author Adrian Cole */ -@Test(groups = "performance", sequential = true, testName = "s3.S3UtilsTest") +@Test(groups = "performance", sequential = true, testName = "jclouds.HttpUtils") public class HttpUtilsTest extends PerformanceTest { @Test(dataProvider = "hmacsha1") diff --git a/core/src/test/java/org/jclouds/util/UtilsTest.java b/core/src/test/java/org/jclouds/util/UtilsTest.java new file mode 100644 index 0000000000..b21be5a58b --- /dev/null +++ b/core/src/test/java/org/jclouds/util/UtilsTest.java @@ -0,0 +1,45 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.util; + +import static org.testng.Assert.assertEquals; + +import java.io.UnsupportedEncodingException; + +import org.jclouds.PerformanceTest; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; + +/** + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "jclouds.UtilsTest") +public class UtilsTest extends PerformanceTest { + + public void testReplaceTokens() throws UnsupportedEncodingException { + assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), + "hello world"); + } +}