added Utils.replaceTokens

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2336 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-28 01:50:26 +00:00
parent 17c976ce01
commit a6e43612fd
3 changed files with 81 additions and 2 deletions

View File

@ -34,6 +34,8 @@ import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -79,7 +81,7 @@ public class Utils {
String returnVal = URLEncoder.encode(in, "UTF-8").replaceAll("\\+", "%20").replaceAll( String returnVal = URLEncoder.encode(in, "UTF-8").replaceAll("\\+", "%20").replaceAll(
"\\*", "%2A").replaceAll("%7E", "~"); "\\*", "%2A").replaceAll("%7E", "~");
for (char c : skipEncode) { for (char c : skipEncode) {
returnVal = returnVal.replaceAll(plainToEncodedChars.get(c+""), c + ""); returnVal = returnVal.replaceAll(plainToEncodedChars.get(c + ""), c + "");
} }
return returnVal; return returnVal;
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
@ -266,4 +268,36 @@ public class Utils {
return decodeString(bytes, UTF8_ENCODING); return decodeString(bytes, UTF8_ENCODING);
} }
public static final Pattern pattern = Pattern.compile("\\{(.+?)\\}");
/**
* replaces tokens that are expressed as <code>{token}</code>
*
* <p/>
* ex. if input is "hello {where}"<br/>
* and replacements is "where" -> "world" <br/>
* then replaceTokens returns "hello world"
*
* @param input
* source to replace
* @param replacements
* token/value pairs
*/
public static String replaceTokens(String input, Map<String, String> 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();
}
} }

View File

@ -45,7 +45,7 @@ import org.testng.annotations.Test;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "performance", sequential = true, testName = "s3.S3UtilsTest") @Test(groups = "performance", sequential = true, testName = "jclouds.HttpUtils")
public class HttpUtilsTest extends PerformanceTest { public class HttpUtilsTest extends PerformanceTest {
@Test(dataProvider = "hmacsha1") @Test(dataProvider = "hmacsha1")

View File

@ -0,0 +1,45 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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");
}
}