/*
* 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 egovframework.com.cmm.util;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
public class StringUtil {
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) {
// TODO Auto-generated catch block
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 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 final boolean unscriptAlert(String data, RedirectAttributes redirectAttributes ) {
if (data == null || data.trim().equals("")) {
return false;
}
String ret = data;
if( ret.contains("<(S|s)(C|c)(R|r)(I|i)(P|p)(T|t)") ||
ret.contains("(S|s)(C|c)(R|r)(I|i)(P|p)(T|t)") ||
ret.contains("(O|o)(B|b)(J|j)(E|e)(C|c)(T|t)") ||
ret.contains("(O|o)(B|b)(J|j)(E|e)(C|c)(T|t)") ||
ret.contains("alert") ||
ret.contains("script") ||
ret.contains("update") ||
ret.contains("delete") ||
ret.contains("!") ||
ret.contains("<") ||
ret.contains(">") ||
ret.contains("<") ||
ret.contains("=") ){
return true;
}
return false;
}
// 오늘 날짜
public static String getTodayDate() throws Exception {
Date toDay = new Date();
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
String rtnDay = date.format(toDay);
return rtnDay;
}
// 한달 전 날짜
public static String getBefore1MonthDate() throws Exception {
Calendar mon = Calendar.getInstance();
mon.add(Calendar.MONTH , -1);
String beforeMonthDay = new java.text.SimpleDateFormat("yyyy-MM-dd").format(mon.getTime());
return beforeMonthDay;
}
}