added json escape utility

This commit is contained in:
Adrian Cole 2010-08-05 19:03:10 -04:00
parent 41a27c6073
commit 55e7b82ba8
3 changed files with 70 additions and 28 deletions

View File

@ -0,0 +1,34 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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 com.google.gson;
/**
* The gson project use package to control access to their objects. However,
* this prevents us from doing valid work, like controling the json emitted on a
* per-object basis. This is here to afford us to do this.
*
* @author Adrian Cole
*/
public class ExposedEscaper extends Escaper {
public ExposedEscaper(boolean escapeHtmlCharacters) {
super(escapeHtmlCharacters);
}
}

View File

@ -70,6 +70,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.io.OutputSupplier;
import com.google.gson.ExposedEscaper;
import com.google.inject.Module;
import com.google.inject.ProvisionException;
import com.google.inject.spi.Message;
@ -80,6 +81,11 @@ import com.google.inject.spi.Message;
* @author Adrian Cole
*/
public class Utils {
private static final ExposedEscaper escaper = new ExposedEscaper(false);
public static String escapeJsonString(CharSequence plainText) {
return escaper.escapeJsonString(plainText);
}
public static <K, V> Supplier<Map<K, V>> composeMapSupplier(Iterable<Supplier<Map<K, V>>> suppliers) {
return new ListMapSupplier<K, V>(suppliers);
@ -245,14 +251,15 @@ public class Utils {
}
/**
* Encode the given string with the given encoding, if possible. If the encoding fails with
* {@link UnsupportedEncodingException}, log a warning and fall back to the system's default
* encoding.
* Encode the given string with the given encoding, if possible. If the
* encoding fails with {@link UnsupportedEncodingException}, log a warning
* and fall back to the system's default encoding.
*
* @param str
* what to encode
* @param charsetName
* the name of a supported {@link java.nio.charset.Charset </code>charset<code>}
* the name of a supported {@link java.nio.charset.Charset
* </code>charset<code>}
* @return properly encoded String.
*/
public static byte[] encodeString(String str, String charsetName) {
@ -266,9 +273,10 @@ public class Utils {
}
/**
* Encode the given string with the UTF-8 encoding, the sane default. In the very unlikely event
* the encoding fails with {@link UnsupportedEncodingException}, log a warning and fall back to
* the system's default encoding.
* Encode the given string with the UTF-8 encoding, the sane default. In the
* very unlikely event the encoding fails with
* {@link UnsupportedEncodingException}, log a warning and fall back to the
* system's default encoding.
*
* @param str
* what to encode
@ -319,7 +327,8 @@ public class Utils {
}
/**
* Will throw an exception if the argument is null or empty. Accepts a custom error message.
* Will throw an exception if the argument is null or empty. Accepts a custom
* error message.
*
* @param nullableString
* string to verify. Can be null or empty.
@ -331,8 +340,8 @@ public class Utils {
}
/**
* Gets a set of supported providers. Idea stolen from pallets (supported-clouds). Uses
* rest.properties to populate the set.
* Gets a set of supported providers. Idea stolen from pallets
* (supported-clouds). Uses rest.properties to populate the set.
*
*/
public static Iterable<String> getSupportedProviders() {
@ -340,8 +349,8 @@ public class Utils {
}
/**
* Gets a set of supported providers. Idea stolen from pallets (supported-clouds). Uses
* rest.properties to populate the set.
* Gets a set of supported providers. Idea stolen from pallets
* (supported-clouds). Uses rest.properties to populate the set.
*
*/
@SuppressWarnings("unchecked")

View File

@ -27,12 +27,12 @@ import java.util.concurrent.TimeoutException;
import org.jclouds.rest.AuthorizationException;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.inject.ProvisionException;
import com.google.inject.internal.ImmutableList;
import com.google.inject.spi.Message;
/**
@ -68,8 +68,7 @@ public class UtilsTest {
}
public void testReplaceTokens() throws UnsupportedEncodingException {
assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")),
"hello world");
assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world");
}
public void testMultiMax() {