/* * Copyright 2008-2009 the original author or authors. * * 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 kcc.com.cmm.util; import java.io.IOException; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.security.SecureRandom; import java.sql.Clob; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.regex.Pattern; import kcc.let.utl.sim.service.EgovFileScrty; public class StringUtil { private static final int HIGHEST_SPECIAL = '>'; // for escaping html code. by hessie; since 2007/10/01. private static final int LOWEST_SPECIAL = '\"'; // for escaping html code. by hessie; since 2007/10/01. private static final int HIGHEST_BR = '\r'; // for escaping html code. by hessie; since 2007/10/01. private static final int LOWEST_BR = '\n'; // for escaping html code. by hessie; since 2007/10/01. private static final int HIGHEST_JS_SPECIAL = '\\'; // for escaping js literal code. by hessie; since 2007/10/01 private static char[][] specialCharactersRepresentation = new char[HIGHEST_SPECIAL + 1][]; private static char[][] specialCharactersRepresentationWithNbsp = new char[HIGHEST_SPECIAL + 1][]; public final static char[] byteCodes; static { byteCodes = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; } public static String bytesToOrgString(byte[] bytes) { int len = bytes.length; char[] chars = new char[len * 2]; for (int i = 0; i < len; ++i) { chars[i * 2] = byteCodes[(bytes[i] & 0XF0) >> 4]; chars[i * 2 + 1] = byteCodes[(bytes[i] & 0X0F)]; } return new String(chars); } /** * 글자 특정 사이즈로 잘라내기 * * @param str * @param size * @return String */ public String cutString(String str, int size) { String returnStr = null; if (str.length() > size) { returnStr = str.substring(0, size) + "..."; } else { returnStr = str; } return returnStr; } /** * 글자 특정 사이즈로 잘라내기 * * @param str * @param size * @param tail * @return String */ public String cutString(String str, int size, boolean tail) { String returnStr = null; String tails = ""; if (tail == true) { tails = "..."; } if (str.length() > size) { returnStr = str.substring(0, size) + tails; } else { returnStr = str; } return returnStr; } /** * 글자 특정 사이즈로
분이기 * * @param str * @param size * @return String */ public String brString(String str, int size) { String returnStr = ""; int j = 0; for(int i = 0; i < str.length(); i++) { if(j >= size) { returnStr += "
"; j = 0; } returnStr += str.charAt(i); j++; } return returnStr; } /** * \r\n을
태그로 변환처리 * * @param str * @return String */ public String nl2br(String str) { String returnStr = null; returnStr = str.replaceAll("\r\n", "
"); return returnStr; } /** * \r\n을 삭제 * * @param str * @return String */ public String nl2Null(String str) { String returnStr = null; returnStr = str.replaceAll("\r\n", ""); return returnStr; } /** * \n을
태그로 변환처리 * * @param str * @return String */ public String nl2br2(String str) { String returnStr = null; returnStr = str.replaceAll("\n", "
"); return returnStr; } /** * 특수문자를 변환합니다 * * @param str * @return String */ public String middot(String str) { str = str.replaceAll("·", "·"); str = str.replaceAll("“", "“"); str = str.replaceAll("”", "”"); str = str.replaceAll("→", "→"); return str; } /** * html 태그를 제거합니다 * * @param str * @return String */ public static String stripTag(String str) { str = str.replaceAll("\\<.*?\\>", ""); str = str.replaceAll(" ", ""); str = str.replaceAll("",""); return str; } /** * clob 스트링 출력 * * @param str * @param size * @return String * @throws IOException */ public String getClob(Clob str) throws IOException { try { Reader reader = str.getCharacterStream(); StringBuffer out = new StringBuffer(); char[] buff = new char[1024]; int nchars = 0; // 스트링 버퍼에 append 시킨후 while ((nchars = reader.read(buff)) > 0) { out.append(buff, 0, nchars); } return out.toString(); } catch (SQLException e) { System.out.println("clob에러"); return "clob에러"; } } /** * 글자 널값이면 대체하기 * * @param str * @param size * @return String */ public String nvl(String str, String str_r) { String returnStr = str; if (str == null) { if (str_r == null) { str_r = ""; } returnStr = str_r; } else if (str.length() == 0) { if (str_r == null) { str_r = ""; } returnStr = str_r; } return returnStr; } /** * https 검색 * * @param str * @param size * @return String */ public boolean httpsFind(String str) { if(str.matches("https://.*")) return true; return false; } /** * 휴대폰번호 대시('-') 추가 * 대시 유무 상관없음 * 유효성 맞지 않을시 변환안됨. */ public String addDash(String str) { String regExp = "(^01[016789]{1}|070)([0-9]{3}|[0-9]{4})([0-9]{4})$"; String chgf = "$1-$2-$3"; return str.replaceFirst(regExp, chgf); } public boolean checkReg(String reg, String str) { return Pattern.matches(reg, str); } public static boolean checkRegKor(String str) { return Pattern.matches("[가-힣]+", str); } public boolean checkRegNum(String str) { return Pattern.matches("[0-9]+", str); } public boolean checkRegEng(String str) { return Pattern.matches("[a-zA-z]+", str); } public boolean checkRegEngUp(String str) { return Pattern.matches("[A-z]+", str); } public boolean checkRegEngLo(String str) { return Pattern.matches("[a-z]+", str); } public static boolean checkRegKorEngNum(String str) { return Pattern.matches("[가-힣A-za-z0-9]+", str); } /** * 새 우편번호 변환 */ public String getPost(int area, int num) { String[][] newPosts = { {""} ,{"05050","11787","13636","26475","25517","07988","05048"} ,{"14041","14442","10449","14067","16704","21313"} ,{"46700","44248","51708","52628"} ,{"32840","32840","28684","31158","32839"} ,{"62278","55316","57987","58457"} ,{"41504","36709","37653"} }; return newPosts[area][num]; } public String nl2br() { String returnStr = null; return returnStr; } public static boolean isEmpty(String value) { return (value == null || value.length() == 0); } public static boolean isNotEmpty(String value) { return !isEmpty(value); } public static String trim(String value) { if(value == null) { return null; } return value.trim(); } public static String getOnlyNum(String value) { if(value == null) { return null; } return value.replaceAll("[^0-9]",""); } public static String escapeXml(Object o) { if (o == null) { return null; } StringWriter writer = new StringWriter(); try { writeXmlText(writer, o.toString(), true); } catch (java.io.IOException e) { // this exception cannot be catched. System.out.println("escapeXml IOException Error"); } return writer.toString(); } public static void writeXmlText(Writer writer, String text) throws java.io.IOException { writeXmlText(writer, text, true, false, false); } public static void writeXmlText(Writer writer, String text, boolean escapeXml) throws java.io.IOException { writeXmlText(writer, text, escapeXml, false, false); } public static void writeXmlText(Writer writer, String text, boolean escapeXml, boolean applyBr) throws java.io.IOException { writeXmlText(writer, text, escapeXml, applyBr, false); } public static void writeXmlText(Writer writer, String text, boolean escapeXml, boolean applyBr, boolean escapeNbsp) throws java.io.IOException { if (text == null) { // do nothing. } else if (!escapeXml && !applyBr) { writer.write(text); } else { writeXmlText(writer, text.toCharArray(), text.length(), escapeXml, applyBr, escapeNbsp); } } public static void writeXmlText(Writer writer, char[] buffer, int length, boolean escapeXml, boolean applyBr, boolean escapeNbsp) throws java.io.IOException { int highest = HIGHEST_SPECIAL; int lowest = LOWEST_SPECIAL; int start = 0; int i = 0; char[][] representation = (escapeNbsp ? specialCharactersRepresentationWithNbsp : specialCharactersRepresentation); if (applyBr) { lowest = LOWEST_BR; if (!escapeXml) { highest = HIGHEST_BR; } } else { if (!escapeXml) { i = length; } } for (; i < length; i++) { char c = buffer[i]; if (c <= highest && c >= lowest) { char[] escaped = representation[c]; if (escaped != null) { // add unescaped portion if (start < i) { writer.write(buffer, start, i - start); } // add escaped xml writer.write(escaped); start = i + 1; } } } // add rest of unescaped portion if (start < length) { writer.write(buffer, start, length - start); } } public static void main(String args[]) { try { System.out.print(EgovFileScrty.encryptPassword("1", "kcctest")); } catch (Exception e) { System.out.println("Main Exception Error"); } } /** * 파라미터를 String 타입으로 가져옵니다.
* - null일 경우 빈 문자열을 가져옵니다.
* Get String(if object is null, return empty string). * @param Object * @return String */ public static String getString(Object obj) { if (obj == null) return ""; else return String.valueOf(obj); } /** * 오늘 날짜를 형식에 맞는 문자열로 가져옵니다. * * @param format * @return */ public static String getDateToString(String format) { Date date = new Date(); return getDateToString(date, format); } /** * 날짜 객체를 형식에 맞는 문자열로 가져옵니다. * * @param date * @param format * @return */ public static String getDateToString(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } public static String getRandomKey(int len) { SecureRandom random = new SecureRandom(); String tmp1 = Math.abs(random.nextInt()) + ""; String tmp2 = Math.abs(random.nextInt()) + ""; //String key = (long) (Math.random() * Math.pow(len, len))+""; String key = (tmp1 + tmp2).substring(0, 10); if(key.length()!=len) { return getRandomKey(len); } return key; } public static String getRandomKey(int len, HashMap keyMap) { SecureRandom random = new SecureRandom(); String tmp1 = Math.abs(random.nextInt()) + ""; String tmp2 = Math.abs(random.nextInt()) + ""; //String key = (long) (Math.random() * Math.pow(len, len))+""; String key = (tmp1 + tmp2).substring(0, 10); if(key.length()!=len) { return getRandomKey(len, keyMap); } if(keyMap.containsKey(key)) { return getRandomKey(len, keyMap); } keyMap.put(key, key); return key; } //오즈리포트 직인 이미지 처리를 위한 주소 변환 함수 public static String getUrlString(String url) { String repUrl = ""; if(url.contains("hosts_")) { //운영서버 업무시스템 주소셋팅 repUrl = url.replaceAll("hosts_real", "https://adr.copyright.or.kr"); //repUrl = url.replaceAll("hosts_real", "http://192.168.39.144"); }else{ //개발서버 시스템 주소 셋팅 repUrl = url.replaceAll("host_dev", "http://119.193.215.98:8081"); } repUrl = repUrl.replaceAll("&url&", "/uss/ion/pwm/getImage.do?atchFileId="); return repUrl; } }