176 lines
4.0 KiB
Java
176 lines
4.0 KiB
Java
package nlib.util;
|
|
|
|
import java.net.URL;
|
|
import java.sql.Timestamp;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Base64;
|
|
import java.util.Base64.Decoder;
|
|
import java.util.Base64.Encoder;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.Locale;
|
|
|
|
import egovframework.com.utl.fcc.service.EgovStringUtil;
|
|
import nlib.user.web.LoginController;
|
|
|
|
/**
|
|
* <pre>
|
|
* @Class Name : StringUtil.java
|
|
*
|
|
* @Description : 문자열 관련 공통 함수 모음
|
|
*
|
|
*
|
|
* @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
|
|
*
|
|
* </pre>
|
|
*
|
|
* @ ------------ -------- ---------------------------
|
|
* @ 수정일 수정자 수정내용
|
|
* @ ------------ -------- ---------------------------
|
|
* @ 2021. 6. 22. KNKIM 최초 생성
|
|
*
|
|
*
|
|
* @author 이씨플라자 * DIGITALSHIP KNKIM
|
|
* @since 2021. 6. 22.
|
|
* @version 1.0
|
|
*
|
|
*/
|
|
public class StringUtil extends EgovStringUtil {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(StringUtil.class);
|
|
|
|
/**
|
|
* Null 이거나, 빈문자열(공백 포함)인 경우, true를 리턴한다.
|
|
*
|
|
* @param str
|
|
* @return
|
|
*/
|
|
public static boolean isEmpty(String str) {
|
|
return str == null || str.trim().length() == 0;
|
|
}
|
|
|
|
/**
|
|
* Null 이거나, 빈문자열(공백 포함)인 경우, true를 리턴한다.
|
|
*
|
|
* @param str
|
|
* @return
|
|
*/
|
|
public static String getString(String str, String replaceStr) {
|
|
if(isEmpty(str)) return replaceStr;
|
|
else return str;
|
|
}
|
|
|
|
/**
|
|
* Null이 아니고 빈문자열(공백 포함)이 아닌 일반 문자가 존재하는 , true를 리턴한다.
|
|
*
|
|
* @param str
|
|
* @return
|
|
*/
|
|
public static boolean isNotEmpty(String str) {
|
|
return !isEmpty(str);
|
|
}
|
|
|
|
/**
|
|
* 문자열을 정수형으로 변환하여 리턴한다. 만일 문자열이 null 또는 빈 문자열인 경우, -1을 리턴한다.
|
|
*
|
|
* @param str
|
|
* @return
|
|
*/
|
|
public static int toNumber(String str) {
|
|
return toNumber(str, -1);
|
|
}
|
|
|
|
/**
|
|
* 문자열을 정수형으로 변환하여 리턴한다. 만일 문자열이 null 또는 빈 문자열인 경우, 기본값 정보 defaultInt를 리턴한다.
|
|
*
|
|
* @param str
|
|
* @param defaultInt
|
|
* @return
|
|
*/
|
|
public static int toNumber(String str, int defaultInt) {
|
|
if(isEmpty(str)) return defaultInt;
|
|
|
|
try {
|
|
return Integer.parseInt(str.trim());
|
|
} catch(Exception e) {}
|
|
|
|
return defaultInt;
|
|
}
|
|
|
|
/**
|
|
* 현재 시각을 yyyyMMddhhmmssSSS 형식으로 리턴한다.
|
|
*
|
|
* @return
|
|
*/
|
|
public static String getTimeStamp() {
|
|
|
|
String rtnStr = null;
|
|
|
|
// 문자열로 변환하기 위한 패턴 설정(년도-월-일 시:분:초:초(자정이후 초))
|
|
String pattern = "yyyyMMddhhmmssSSS";
|
|
|
|
SimpleDateFormat sdfCurrent = new SimpleDateFormat(pattern, Locale.KOREA);
|
|
Timestamp ts = new Timestamp(System.currentTimeMillis());
|
|
|
|
rtnStr = sdfCurrent.format(ts.getTime());
|
|
|
|
return rtnStr;
|
|
}
|
|
|
|
|
|
/**
|
|
* Base64로 인코딩한다.
|
|
*
|
|
* @return
|
|
*/
|
|
public static String encodeBase64(String str) {
|
|
|
|
if(str == null) return null;
|
|
|
|
Encoder encoder = Base64.getEncoder();
|
|
return new String(encoder.encode(str.getBytes()));
|
|
}
|
|
|
|
/**
|
|
* Base64로 디코딩한다.
|
|
*
|
|
* @return
|
|
*/
|
|
public static String decodeBase64(String str) {
|
|
|
|
if(str == null) return null;
|
|
|
|
Decoder decoder = Base64.getDecoder();
|
|
return new String(decoder.decode(str.getBytes()));
|
|
}
|
|
|
|
/**
|
|
* URL 주소에서 호스트명을 리턴한다.
|
|
*
|
|
* 프로토콜://호스트.도메인/URI 에서 호스트명 리턴
|
|
* (ex) https://seoul.nculture.org/abc/def.do -> seoul 을 리턴한다.
|
|
*
|
|
* @param url
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static String getHostName(String url) {
|
|
if(isEmpty(url)) return "";
|
|
|
|
String hostName = "";
|
|
|
|
try {
|
|
final URL urlObj = new URL(url);
|
|
hostName = urlObj.getHost();
|
|
if(isEmpty(hostName)) return "";
|
|
} catch(Exception e) {
|
|
log.error("[ERROR] getHostName(..) : " + e.toString());
|
|
return "";
|
|
}
|
|
|
|
return (hostName.split("\\."))[0];
|
|
}
|
|
}
|