Object value = null;
if( selectBbsDetail.get("NTTCN") instanceof java.sql.Clob ) {
value = StringUtil.clobToString(((java.sql.Clob)selectBbsDetail.get("NTTCN")));
} else {
value = selectBbsDetail.get("NTTCN");
}
package kr.co.eMobile.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.CollectionType;
import org.codehaus.jackson.map.type.MapType;
import org.codehaus.jackson.map.type.SimpleType;
import org.codehaus.jackson.type.JavaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StringUtil extends org.springframework.util.StringUtils {
static Logger log = LoggerFactory.getLogger(StringUtil.class);
static String logTitle = "########## StringUtil.";
private final static char b64EncodeTable[] = {
'A','B','C','D','E','F','G','H' // 0
, 'I','J','K','L','M','N','O','P' // 1
, 'Q','R','S','T','U','V','W','X' // 2
, 'Y','Z','a','b','c','d','e','f' // 3
, 'g','h','i','j','k','l','m','n' // 4
, 'o','p','q','r','s','t','u','v' // 5
, 'w','x','y','z','0','1','2','3' // 6
, '4','5','6','7','8','9','+','/' // 7
};
private final static byte b64DecodeTable[] = new byte[256];
static {
for (int i = 0; i < 255; i++) b64DecodeTable[i] = -1;
for(int i = 0; i < b64EncodeTable.length; i++) b64DecodeTable[b64EncodeTable[i]] = (byte) i;
}
public static String nvl(String src) {
return nvl(src, "");
}
public static String nvl(String src, String tgt) {
String res = tgt;
if (tgt == null) {
res = "";
}
if (src == null) {
return res;
} else if (src.equals("")) {
return res;
} else {
return src;
}
}
public static String nvl(Object obj) {
return nvl(obj, "");
}
public static String nvl(Object obj, String def) {
if (obj == null) {
return def;
}
return obj.toString();
}
public static String rpad(String str, int len, char pad) {
String result = str;
int templen = len - result.getBytes().length;
for (int i = 0; i < templen; i++) {
result = result + pad;
}
return result;
}
public static String lpad(String str, int len, char pad) {
String result = str;
int templen = len - result.getBytes().length;
for (int i = 0; i < templen; i++) {
result = pad + result;
}
return result;
}
public static String makePostType(String postno) {
if (postno == null || postno.length() == 0) {
return "";
}
if (postno.length() != 6) {
return postno;
}
String postno1 = postno.substring(0, 3);
String postno2 = postno.substring(3, 6);
return (postno1 + "-" + postno2);
}
public static String makeTelType(String tel) {
if (tel == null || tel.length() == 0) {
return "";
}
String DELEMETER = "-";
StringBuffer sb = new StringBuffer();
String tnum = "";
if (tel.startsWith("02")) {
sb.append("02").append(DELEMETER);
tnum = tel.substring(2);
} else {
sb.append(tel.substring(0, 3)).append(DELEMETER);
tnum = tel.substring(3);
}
if (tnum.length() == 7) {
sb.append(tnum.substring(0, 3)).append(DELEMETER).append(tnum.substring(3));
} else if (tnum.length() == 8) {
sb.append(tnum.substring(0, 4)).append(DELEMETER).append(tnum.substring(4));
} else {
return tel;
}
return sb.toString();
}
public static String makeJuminType(String jumin) {
if (jumin == null || jumin.length() == 0) {
return "";
}
if (jumin.length() != 13) {
return jumin;
}
String postno1 = jumin.substring(0, 6);
String postno2 = jumin.substring(6);
return (postno1 + "-" + postno2);
}
public static String getPostCode1(String postcode) {
if (postcode == null || postcode.length() == 0) {
return "";
}
if (postcode.length() == 6) {
return postcode.substring(0, 3);
} else {
return postcode;
}
}
public static String getPostCode2(String postcode) {
if (postcode == null || postcode.length() == 0) {
return "";
}
if (postcode.length() == 6) {
return postcode.substring(3);
} else {
return postcode;
}
}
public static String getTelNum1(String tel) {
if (tel == null || tel.length() == 0) {
return "";
}
if (tel.startsWith("02")) {
return "02";
} else {
return tel.substring(0, 3);
}
}
public static String getTelNum2(String tel) {
if (tel == null || tel.length() == 0) {
return "";
}
if (tel.startsWith("02")) {
if ((tel.length() - 2) == 7) {
return tel.substring(2, 5);
} else if ((tel.length() - 2) == 8) {
return tel.substring(2, 6);
} else {
return tel;
}
} else {
if ((tel.length() - 3) == 7) {
return tel.substring(3, 6);
} else if ((tel.length() - 3) == 8) {
return tel.substring(3, 7);
} else {
return tel;
}
}
}
public static String getTelNum3(String tel) {
if (tel == null || tel.length() == 0) {
return "";
}
if (tel.startsWith("02")) {
if ((tel.length() - 2) == 7) {
return tel.substring(5);
} else if ((tel.length() - 2) == 8) {
return tel.substring(6);
} else {
return tel;
}
} else {
if ((tel.length() - 3) == 7) {
return tel.substring(6);
} else if ((tel.length() - 3) == 8) {
return tel.substring(7);
} else {
return tel;
}
}
}
public static String getGender(String gender) {
if (gender == null || gender.length() == 0) {
return "";
}
String gen = gender.toUpperCase();
if (gen.equals("F") || gen.equals("FEMAIL") || gen.equals("W") || gender.equals("WOMAN")) {
return "여자";
} else if (gen.equals("M") || gen.equals("MAIL") || gender.equals("MAN")) {
return "남자";
} else {
return gender;
}
}
public String encodeSpace(String str) {
StringTokenizer st = new StringTokenizer(str);
String spString = "";
while (st.hasMoreTokens()) {
spString = spString + st.nextToken() + "_";
}
return spString;
}
public String decodeSpace(String str) {
StringTokenizer st = new StringTokenizer(str, "_");
String returnStr = "";
while (st.hasMoreTokens()) {
returnStr = returnStr + st.nextToken() + " ";
}
return returnStr;
}
public static String remove(String str, String tok) {
return replaceAll(str, tok, "");
}
public static String replace(String str1, String str2, String replace) {
return replace(str1, 0, str2, replace);
}
public static String replace(String str, int off, int len, String replace) {
StringBuffer buff = new StringBuffer(str);
buff.replace(off, off + len, replace);
return buff.toString();
}
public static String replace(String str1, int off, String str2, String replace) {
off = str1.indexOf(str2, off);
if (off == -1) {
return str1;
}
StringBuffer buff = new StringBuffer(str1);
buff.replace(off, off + str2.length(), replace);
return buff.toString();
}
public static String replaceAll(String str1, int off, String str2, String replace) {
if (str1 == null || str2 == null || replace == null) {
return str1;
}
off = str1.indexOf(str2, off);
StringBuffer buff = new StringBuffer(str1);
while (off != -1) {
buff.replace(off, off + str2.length(), replace);
str1 = buff.toString();
if (off + str2.length() < str1.length()) {
off = str1.indexOf(str2, off + replace.length());
} else {
off = -1;
}
}
return str1;
}
public static String replaceAll(String str1, String str2, String replace) {
return replaceAll(str1, 0, str2, replace);
}
public static String getShortString(String p_str, int p_len) {
boolean chkFlag = false;
String strName = p_str.trim();
byte[] arName = strName.getBytes();
if (arName.length > p_len) {
for (int idx = 0; idx < p_len; idx++) {
if (arName[idx] < 0) { // 0x80 이상 ( 1byte짜리. 키값들)
chkFlag = !chkFlag; // true로
} else {
chkFlag = false; // false로
}
}
if (chkFlag) {
strName = new String(arName, 0, p_len + 1); // 홀수이면
} else {
strName = new String(arName, 0, p_len); // 짝수일때
}
} else {
strName = new String(arName, 0, arName.length);
}
return strName;
}
public static String getShortStatement(String str, int maxNum) {
return str.length() > maxNum ? getShortString(str, maxNum) + "..." : str;
}
public static String getAuthStatement(String str, int maxNum) {
return str.length() > maxNum ? getShortString(str, maxNum) + "**" : str;
}
public static String getRawDigit(String str) {
char[] c = str.toCharArray();
StringBuffer buff = new StringBuffer();
try {
for (int i = 0; i < c.length; i++) {
if (Character.isDigit(c[i])) {
buff.append(c[i]);
}
}
} catch (Exception ex) {
log.error("{}", ex);
}
return buff.toString();
}
public static String changeEmptyStringToNULL(String inStr) {
if (inStr == null) {
return null;
}
if (inStr.equals("")) {
return null;
} else {
return inStr;
}
}
public static String changeEntityReference(String text) {
String result = text;
result = StringUtil.replaceAll(result, "&", "&");
result = StringUtil.replaceAll(result, "'", "'");
result = StringUtil.replaceAll(result, "\"", """);
result = StringUtil.replaceAll(result, "<", "<");
result = StringUtil.replaceAll(result, ">", ">");
return result;
}
public static String changeNormalText(String text) {
String result = text;
result = StringUtil.replaceAll(result, ">", ">");
result = StringUtil.replaceAll(result, "<", "<");
result = StringUtil.replaceAll(result, """, "\"");
result = StringUtil.replaceAll(result, "'", "'");
result = StringUtil.replaceAll(result, "&", "&");
return result;
}
public static String escapeHtmlSpecialChar(String str) {
if ("".equals(nvl(str))) {
return "";
}
str = str.replaceAll("'", "'");
str = str.replaceAll("\"", """);
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
return str;
}
public static String getRandomString(int minValue, int maxValue, int fixLength) {
double value = NumberUtil.getRandom(minValue, maxValue);
String str = Double.toString(value);
int index = str.indexOf(".");
str = str.substring(0, index);
if (fixLength == -1) {
return str;
}
index = str.length();
while (index < fixLength) {
str = "0" + str;
index++;
}
return str;
}
public static boolean checkEnumString(String[] list, String val) {
if (val == null) {
return false;
}
for (String s : list) {
if (s == null) {
continue;
}
if (s.equals(val)) {
return true;
}
}
return false;
}
public static byte[] base64Encode(byte[] inbuf) {
if (inbuf.length == 0) {
return inbuf;
}
byte[] outbuf = new byte[((inbuf.length + 2) / 3) * 4];
int inpos = 0, outpos = 0;
int size = inbuf.length;
while (size > 0) {
byte a, b, c;
if (size == 1) {
a = inbuf[inpos++];
b = 0;
c = 0;
outbuf[outpos++] = (byte)b64EncodeTable[(a >>> 2) & 0x3F];
outbuf[outpos++] = (byte)b64EncodeTable[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
outbuf[outpos++] = (byte)'='; // pad character
outbuf[outpos++] = (byte)'='; // pad character
} else if (size == 2) {
a = inbuf[inpos++];
b = inbuf[inpos++];
c = 0;
outbuf[outpos++] = (byte)b64EncodeTable[(a >>> 2) & 0x3F];
outbuf[outpos++] = (byte)b64EncodeTable[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
outbuf[outpos++] = (byte)b64EncodeTable[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
outbuf[outpos++] = (byte)'='; // pad character
} else {
a = inbuf[inpos++];
b = inbuf[inpos++];
c = inbuf[inpos++];
outbuf[outpos++] = (byte)b64EncodeTable[(a >>> 2) & 0x3F];
outbuf[outpos++] = (byte)b64EncodeTable[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
outbuf[outpos++] = (byte)b64EncodeTable[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
outbuf[outpos++] = (byte)b64EncodeTable[c & 0x3F];
}
size -= 3;
}
return outbuf;
}
public static byte[] base64Decode(byte[] inbuf) {
int size = (inbuf.length / 4) * 3;
if (size == 0) {
return inbuf;
}
if (inbuf[inbuf.length - 1] == '=') {
size--;
if (inbuf[inbuf.length - 2] == '=') {
size--;
}
}
byte[] outbuf = new byte[size];
int inpos = 0, outpos = 0;
size = inbuf.length;
while (size > 0) {
byte a, b;
a = b64DecodeTable[inbuf[inpos++] & 0xff];
b = b64DecodeTable[inbuf[inpos++] & 0xff];
// The first decoded byte
outbuf[outpos++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));
// End of this BASE64 encoding
if (inbuf[inpos] == '=') {
return outbuf;
}
a = b;
b = b64DecodeTable[inbuf[inpos++] & 0xff];
// The second decoded byte
outbuf[outpos++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));
// End of this BASE64 encoding
if (inbuf[inpos] == '=') {
return outbuf;
}
a = b;
b = b64DecodeTable[inbuf[inpos++] & 0xff];
// The third decoded byte
outbuf[outpos++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f));
size -= 4;
}
return outbuf;
}
public static boolean isEmpty(String str) {
if (str == null || str.trim().length() == 0) {
return true;
}
return false;
}
public static String splitTrim(String str, String splitType) {
String rtnVal = "";
if (!"".equals(nvl(str))) {
String[] temp = str.split(splitType);
for (int i=0; i<temp.length; i++) {
rtnVal += temp[i].toString().trim();
}
}
return rtnVal;
}
public static String toCamelCase(String strParam) {
String str = "";
String strCol = "";
StringTokenizer st = new StringTokenizer(strParam.toLowerCase(), "_");
int i = 0;
while (st.hasMoreElements()) {
str = st.nextToken();
if (i == 0) {
strCol = str.substring(0, 1).toLowerCase() + str.substring(1, str.length());
} else {
str = str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
strCol = strCol + str;
}
i++;
}
return strCol;
}
public static String toXMLString(String strParam) {
try {
return replace(replace(replace(replace(replace(strParam, """, "" ), "&", "&" ), "'", "'" ), "<", "<" ), ">", ">" );
} catch (Exception e) {
return "";
}
}
private static ObjectMapper mJsonMapper;
public static final synchronized ObjectMapper getInstance() {
if (mJsonMapper == null) {
mJsonMapper = new ObjectMapper();
}
return mJsonMapper;
}
public static final synchronized void recycleInstance() {
if (mJsonMapper != null) {
mJsonMapper = null;
}
}
public static final <T> T parseJsonObject(String source, Class<T> resultClass) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "parseJsonObject() :: {}";
T instance = null;
try {
instance = getInstance().readValue(source, resultClass);
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
throw e;
}
return instance;
}
public static final <T> List<T> parseJsonArray(String source, Class<T> itemClass) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "parseJsonArray() :: {}";
List<T> resultList = null;
try {
JavaType javaType = CollectionType.construct(List.class, SimpleType.construct(itemClass));
resultList = getInstance().readValue(source, javaType);
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
throw e;
}
return resultList;
}
public static final Map<String, String> parseJsonMap(String source) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "parseJsonMap() :: {}";
Map<String, String> resultMap = null;
try {
JavaType type = SimpleType.construct(String.class);
MapType typeRef = MapType.construct(Map.class, type, type);
resultMap = getInstance().readValue(source, typeRef);
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
throw e;
}
return resultMap;
}
public static final <T> Map<String, List<T>> parseJsonListMap(String source, Class<T> itemClass) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "parseJsonMap() :: {}";
Map<String, List<T>> resultMap = null;
try {
JavaType keyType = SimpleType.construct(String.class);
JavaType valueType = CollectionType.construct(List.class, SimpleType.construct(itemClass));
MapType typeRef = MapType.construct(Map.class, keyType, valueType);
resultMap = getInstance().readValue(source, typeRef);
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
throw e;
}
return resultMap;
}
public static final <T> T parseJsonObject(JsonNode node, Class<T> resultClass) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "parseJsonMap() :: {}";
T instance = null;
try {
instance = getInstance().readValue(node, resultClass);
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
throw e;
}
return instance;
}
public static final <T> List<T> parseJsonArray(JsonNode node, Class<T> itemClass) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "parseJsonMap() :: {}";
List<T> resultList = null;
try {
JavaType javaType = CollectionType.construct(List.class, SimpleType.construct(itemClass));
resultList = getInstance().readValue(node, javaType);
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
throw e;
}
return resultList;
}
public static String urlDataToJson(String param) throws Exception {
// 로그 타이틀 정의
String logTitleMethod = logTitle + "urlDataToJson() :: {}";
String[] strTmpArray1 = null;
String strTmp = "";
String strTmpMaster = "";
String[] strTmpArray2 = null;
log.error(logTitleMethod, "param : " + param);
strTmpArray1 = param.split("&");
for (int i = 0; i < strTmpArray1.length; i++) {
strTmp = strTmpArray1[i];
if (!strTmp.contains("=")) {
strTmp = strTmp.concat("=");
}
strTmpArray2 = strTmp.split("=");
if (strTmpArray2.length > 1) {
try {
strTmpArray2[1] = URLDecoder.decode(strTmpArray2[1], "EUC_KR");
} catch (Exception e) {
log.debug(logTitleMethod, "++++++++++++++++++++ ERROR ++++++++++++++++++++");
log.debug(logTitleMethod, e.getMessage());
log.debug(logTitleMethod, "if don't fix format, continue");
log.debug(logTitleMethod, " strTmpArray2[1] : " + strTmpArray2[1]);
continue;
}
strTmpArray2[1] = strTmpArray2[1].replace("\"", "\\\"");
if (i == 0) {
strTmpMaster = "{\"".concat(strTmpArray2[0]).concat("\":\"").concat(strTmpArray2[1]).concat("\"");
} else {
strTmpMaster = strTmpMaster.concat(",\"").concat(strTmpArray2[0]).concat("\":\"").concat(strTmpArray2[1]).concat("\"");
}
} else {
if (i == 0) {
strTmpMaster = "{\"".concat(strTmpArray2[0]).concat("\":\"\"");
} else {
strTmpMaster = strTmpMaster.concat(",\"").concat(strTmpArray2[0]).concat("\":\"\"");
}
}
}
strTmpMaster = strTmpMaster.concat("}");
return strTmpMaster;
}
/*
* Clob 를 String 으로 변경
*/
public static String clobToString(Clob clob) throws SQLException,
IOException {
if (clob == null) {
return "";
}
StringBuffer strOut = new StringBuffer();
String str = "";
BufferedReader br = new BufferedReader(clob.getCharacterStream());
while ((str = br.readLine()) != null) {
strOut.append(str);
}
return strOut.toString();
}
}
'Developers' 카테고리의 다른 글
JAVA PDF 생성 (0) | 2017.09.07 |
---|---|
하이브리드 웹뷰에서 PDF뷰어 오픈 (0) | 2017.08.18 |
JAVA 에서 HTML TAG 제거 (0) | 2017.08.17 |
js 정규식 (0) | 2017.06.17 |
high chart 차트 그리기 (0) | 2017.06.01 |
댓글