brooklyn.util.text.StringEscapes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-utils-common Show documentation
Show all versions of brooklyn-utils-common Show documentation
Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else
package brooklyn.util.text;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import brooklyn.util.net.URLParamEncoder;
import com.google.common.base.Throwables;
public class StringEscapes {
/** if s is wrapped in double quotes containing no unescaped double quotes */
public static boolean isWrappedInDoubleQuotes(String s) {
if (Strings.isEmpty(s)) return false;
if (!s.startsWith("\"") || !s.endsWith("\"")) return false;
return (s.substring(1, s.length()-1).replace("\\\\", "").replace("\\\"", "").indexOf("\"")==-1);
}
/** if s is wrapped in single quotes containing no unescaped single quotes */
public static boolean isWrappedInSingleQuotes(String s) {
if (Strings.isEmpty(s)) return false;
if (!s.startsWith("\'") || !s.endsWith("\'")) return false;
return (s.substring(1, s.length()-1).replace("\\\\", "").replace("\\\'", "").indexOf("\'")==-1);
}
/** if s is wrapped in single or double quotes containing no unescaped quotes of that type */
public static boolean isWrappedInMatchingQuotes(String s) {
return isWrappedInDoubleQuotes(s) || isWrappedInSingleQuotes(s);
}
/**
* Encodes a string suitable for use as a parameter in a URL.
*/
public static String escapeUrlParam(String input) {
return URLParamEncoder.encode(input);
}
/**
* Encodes a string suitable for use as a URL in an HTML form: space to +, and high-numbered chars assuming UTF-8.
* However, it will also convert the first "http://" to "http%3A%2F%2F" so is not suitable for converting an
* entire URL.
*
* Also note that parameter-conversion doesn't work in way you'd expect when trying to create a "normal" url.
* See http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java
*
* @see escapeUrlParam(String), and consider using that instead.
*/
public static String escapeHtmlFormUrl(String url) {
try {
return URLEncoder.encode(url, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw Throwables.propagate(e);
}
}
/** encodes a string to SQL, that is ' becomes '' */
public static String escapeSql(String x) {
//identical to apache commons StringEscapeUtils.escapeSql
if (x==null) return null;
return x.replaceAll("'", "''");
}
public static class BashStringEscapes {
// single quotes don't permit escapes! e.g. echo 'hello \' world' doesn't work
/** wraps plain text in double quotes escaped for use in bash double-quoting */
public static String wrapBash(String value) {
StringBuilder out = new StringBuilder();
try {
wrapBash(value, out);
} catch (IOException e) {
//shouldn't happen for string buffer
throw Throwables.propagate(e);
}
return out.toString();
}
/** @see #wrapBash(String) */
public static void wrapBash(String value, Appendable out) throws IOException {
out.append('"');
escapeLiteralForDoubleQuotedBash(value, out);
out.append('"');
}
private static void escapeLiteralForDoubleQuotedBash(String value, Appendable out) throws IOException {
for (int i=0; i unwrapQuotedJavaStringList(String s, String separator) {
List result = new ArrayList();
String remaining = s.trim();
while (remaining.length() > 0) {
int endIndex = findNextClosingQuoteOf(remaining);
result.add(unwrapJavaString(remaining.substring(0, endIndex+1)));
remaining = remaining.substring(endIndex+1).trim();
if (remaining.startsWith(separator)) {
remaining = remaining.substring(separator.length()).trim();
} else if (remaining.length() > 0) {
throw new IllegalArgumentException("String '"+s+"' has invalid separators, should be '"+separator+"'");
}
}
return result;
}
private static int findNextClosingQuoteOf(String s) {
boolean escaped = false;
boolean quoted = false;
for (int i=0; i