/**
* $Revision: 10205 $
* $Date: 2008-04-11 15:48:27 -0700 (Fri, 11 Apr 2008) $
*
* Copyright (C) 2004-2008 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.BreakIterator;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* Utility class to peform common String manipulation algorithms.
*/
public class StringUtils {
// Constants used by escapeHTMLTags
private static final char[] QUOTE_ENCODE = """.toCharArray();
private static final char[] AMP_ENCODE = "&".toCharArray();
private static final char[] LT_ENCODE = "<".toCharArray();
private static final char[] GT_ENCODE = ">".toCharArray();
private StringUtils() {
// Not instantiable.
}
/**
* Escapes all necessary characters in the String so that it can be used in SQL
*
* @param string the string to escape.
* @return the string with appropriate characters escaped.
*/
public static String escapeForSQL(String string) {
if (string == null) {
return null;
}
else if (string.length() == 0) {
return string;
}
char ch;
char[] input = string.toCharArray();
int i = 0;
int last = 0;
int len = input.length;
StringBuilder out = null;
for (; i < len; i++) {
ch = input[i];
if (ch == '\'') {
if (out == null) {
out = new StringBuilder(len + 2);
}
if (i > last) {
out.append(input, last, i - last);
}
last = i + 1;
out.append('\'').append('\'');
}
}
if (out == null) {
return string;
}
else if (i > last) {
out.append(input, last, i - last);
}
return out.toString();
}
}
|