From 36c298588bcc0d2f39e008325af66fddd5c38024 Mon Sep 17 00:00:00 2001 From: leebj Date: Mon, 11 Nov 2024 08:51:59 +0900 Subject: [PATCH] =?UTF-8?q?=ED=95=9C=EA=B8=80=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 + .../java/cokr/xit/applib/AppCmmnUtil.java | 118 ++++++++++ src/main/java/cokr/xit/applib/Hangul.java | 222 ++++++++++++++++++ 3 files changed, 347 insertions(+) create mode 100644 src/main/java/cokr/xit/applib/Hangul.java diff --git a/pom.xml b/pom.xml index 36b657c..429daf3 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,13 @@ xit-docs 23.04.01-SNAPSHOT + + + + com.ibm.icu + icu4j + 75.1 + diff --git a/src/main/java/cokr/xit/applib/AppCmmnUtil.java b/src/main/java/cokr/xit/applib/AppCmmnUtil.java index ab2e62f..88e6b3e 100644 --- a/src/main/java/cokr/xit/applib/AppCmmnUtil.java +++ b/src/main/java/cokr/xit/applib/AppCmmnUtil.java @@ -5,6 +5,56 @@ import java.util.regex.Pattern; public class AppCmmnUtil { + /** + * 전화번호(유선,무선) 형식인지 체크한다. + * @param 문자열 + * @return boolean + */ + public static boolean isTelno(String str) { + + //01X + String REGEXP_PHONE = "^(01)\\d{8,9}$"; + if(Pattern.matches(REGEXP_PHONE, str)) { + return true; + } + + //서울 + String REGEXP_TEL = "^(02)\\d{7,8}$"; + if(Pattern.matches(REGEXP_TEL, str)) { + return true; + } + + //지방 + REGEXP_TEL = "^(0)(3|4|5|6)\\d{8,9}$"; + if(Pattern.matches(REGEXP_TEL, str)) { + return true; + } + + //070 + REGEXP_TEL = "^(070)\\d{7,8}$"; + if(Pattern.matches(REGEXP_TEL, str)) { + return true; + } + + //전국대표번호(0000-0000) + REGEXP_TEL = "^\\d{8}$"; + if(Pattern.matches(REGEXP_TEL, str)) { + return true; + } + + return false; + } + + /** + * 문자수신 가능한 휴대폰번호인지 체크한다. + * @param 문자열 + * @return boolean + */ + public static boolean isReceivePhone(String str) { + String REGEXP_PHONE = "^(01)\\d{8,9}$"; + return Pattern.matches(REGEXP_PHONE, str); + } + /** * 문자열이 차량번호인지 체크한다. * @param 문자열 @@ -35,4 +85,72 @@ public class AppCmmnUtil { return false; } } + + /** + * 문자열에서 차량번호를 추출한다. + * @param 문자열 + * @return String 차량번호 + */ + public static String extractCarNumber(String carNum){ + + try{ + String regex = "[서울|부산|대구|인천|대전|광주|울산|제주|경기|강원|충남|전남|전북|경남|경북|세종]{2}\\d{2}[가|나|다|라|마|거|너|더|러|머|버|서|어|저|고|노|도|로|모|보|소|오|조|구|누|두|루|무|부|수|우|주|바|사|아|자|허|배|호|하\\x20]\\d{4}"; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(carNum); + if (m.find()) { + return m.group(); + } + + + regex = "\\d{3}[가|나|다|라|마|거|너|더|러|머|버|서|어|저|고|노|도|로|모|보|소|오|조|구|누|두|루|무|부|수|우|주|바|사|아|자|허|배|호|하\\x20]\\d{4}"; + p = Pattern.compile(regex); + m = p.matcher(carNum); + if (m.find()) { + return m.group(); + } + + regex = "\\d{2}[가|나|다|라|마|거|너|더|러|머|버|서|어|저|고|노|도|로|모|보|소|오|조|구|누|두|루|무|부|수|우|주|바|사|아|자|허|배|호|하\\x20]\\d{4}"; + p = Pattern.compile(regex); + m = p.matcher(carNum); + if (m.find()) { + return m.group(); + } + + return ""; + + }catch(Exception e){ + return ""; + } + } + + /** + * "연-월-일 시:분:초" 형식 여부를 체크한다. + * @param 문자열 + * @return boolean + */ + public static boolean isDateTimePattern(String str){ + String DATETIME_PATTERN = "(19|20)\\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]"; + Pattern datetimePattern = Pattern.compile(DATETIME_PATTERN); + + if(datetimePattern.matcher(str).matches()) { + return true; + } else { + return false; + } + } + + /** + * "연월일시분초" 14자리 형식 여부를 체크한다. + * @param 문자열 + * @return boolean + */ + public static boolean isDateTimeDigitPattern(String str){ + String DATETIME_PATTERN = "^\\d{4}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])([0-1][0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$"; + if(Pattern.matches(DATETIME_PATTERN, str)) { + return true; + } else { + return false; + } + } + } diff --git a/src/main/java/cokr/xit/applib/Hangul.java b/src/main/java/cokr/xit/applib/Hangul.java new file mode 100644 index 0000000..d1fcc4b --- /dev/null +++ b/src/main/java/cokr/xit/applib/Hangul.java @@ -0,0 +1,222 @@ +package cokr.xit.applib; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.apache.commons.io.IOUtils; + +import com.ibm.icu.text.CharsetDetector; +import com.ibm.icu.text.CharsetMatch; + +public class Hangul { + public Hangul(int hangulIsNByte){ + this.hangulIsNByte = hangulIsNByte; + } + + private int hangulIsNByte; + + public int is() { + return this.hangulIsNByte; + } + + /** + * 문자열의 바이트 수 구하기 + * + * @param str + * @return + */ + public int getByteLength(String str) { + if(str == null) { + return 0; + } + int byteLen = 0; + + for(int i=0;i 127) || (ch < 0)) { + byteLen += this.is(); + } else { + byteLen += 1; + } + } + + return byteLen; + } + + /** + * 문자열을 바이트 단위로 패딩 + * + * @param str + * @param byteLen + * @param ch + * @return + */ + public String lpadByte(String str, int byteLen, String ch) { + String result = str; + + int strLen = this.getByteLength(str); + + for(int i=0; i < byteLen - strLen ; i++) { + result = ch + result; + } + + return result; + } + + /** + * 문자열을 바이트 단위로 패딩 + * + * @param str + * @param byteLen + * @param ch + * @return + */ + public String rpadByte(String str, int byteLen, String ch) { + String result = str; + + int strLen = this.getByteLength(str); + + for(int i=0; i < byteLen - strLen ; i++) { + result += ch; + } + + return result; + } + + /** + * 문자열을 바이트 단위로 substring하기 + * + * @param str + * @param beginBytes + * @param endBytes + * @return + */ + public String substringByBytes(String str, int beginBytes, int endBytes) { + if (str == null || str.length() == 0) { + return ""; + } + + if (beginBytes < 0) { + beginBytes = 0; + } + + if (endBytes < 1) { + return ""; + } + + int len = str.length(); + + int beginIndex = -1; + int endIndex = 0; + + int curBytes = 0; + String ch = null; + for (int i = 0; i < len; i++) { + ch = str.substring(i, i + 1); + curBytes += this.getByteLength(ch); + + + if (beginIndex == -1 && curBytes >= beginBytes) { + beginIndex = i; + } + + if (curBytes > endBytes) { + break; + } else { + endIndex = i + 1; + } + } + + return str.substring(beginIndex, endIndex); + } + + /** + * 문자열을 바이트 단위로 substring하기 + * + * @param str + * @param beginBytes + * @return + */ + public String substringByBytes(String str, int beginBytes) { + if (str == null || str.length() == 0) { + return ""; + } + + if (beginBytes < 0) { + beginBytes = 0; + } + + int len = str.length(); + + int beginIndex = -1; + + int curBytes = 0; + String ch = null; + for (int i = 0; i < len; i++) { + ch = str.substring(i, i + 1); + curBytes += this.getByteLength(ch); + + + if (beginIndex == -1 && curBytes >= beginBytes) { + beginIndex = i; + } + + } + + return str.substring(beginIndex); + } + + /** + * 텍스트파일 인코딩 확인 + * + * @param path 파일경로 + * @return 캐릭터셋 + */ + public String encodingDetect(String path) throws IOException { + File f = new File(path); + + return encodingDetect(f); + } + + /** + * 텍스트파일 인코딩 확인 + * + * @param file 파일 + * @return 캐릭터셋 + */ + public String encodingDetect(File file) throws IOException { + CharsetDetector detector; + CharsetMatch match; + + FileInputStream fis = null; + try { + String result = ""; + + fis = new FileInputStream(file); + + byte[] byteData = new byte[(int) file.length()]; + + fis.read(byteData); + fis.close(); + detector = new CharsetDetector(); + + detector.setText(byteData); + match = detector.detect(); + + System.out.println("encoding is \"" + match.getName() + "\""); + + if(match.getName().equals("UTF-8") || match.getName().equals("EUC-KR")) { + result = match.getName(); + } else { + result = "EUC-KR"; + } + + return result; + } + finally { + IOUtils.closeQuietly(fis); + } + } +}