1 package com.rapide_act;
3 import java.io.BufferedInputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
8 import java.io.FileReader;
9 import java.io.FileWriter;
10 import java.io.BufferedWriter;
11 import java.io.BufferedReader;
12 import java.io.PrintWriter;
13 import java.io.PrintWriter;
14 import java.io.ByteArrayOutputStream;
15 import java.util.Date;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.Properties;
19 import java.util.regex.Pattern;
20 import java.util.regex.Matcher;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.ResultSet;
24 import java.sql.ResultSetMetaData;
25 import java.sql.SQLException;
26 import java.sql.PreparedStatement;
27 import java.sql.Timestamp;
30 import java.math.BigDecimal;
31 import java.math.BigInteger;
32 import java.util.regex.Pattern;
33 import org.apache.commons.codec.binary.Base64;
34 import java.nio.charset.Charset;
36 public class CmnUtils{
38 protected static void writeCsv(String _file, int _columnCount, ArrayList<String> _alData) throws Exception{
39 PrintWriter pw = null;
41 debugPrint("Start write csv," + "columnCount=" + _columnCount);
42 pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(_file))));
43 for(int i=0;i<_alData.size();i++){
44 pw.print(_alData.get(i).replaceAll("\n","\r\n"));
45 if (_columnCount == 1){
48 if ((i+1) % _columnCount == 0 && i > 0){
56 debugPrint("End write csv");
63 protected static void writeSeparator(String _file, int _columnCount, ArrayList<String> _alData, String _sprtr) throws Exception{
64 PrintWriter pw = null;
66 debugPrint("Start write Separator," + "columnCount=" + _columnCount);
67 pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(_file))));
68 for(int i=0;i<_alData.size();i++){
69 pw.print(_alData.get(i).replaceAll("\n","\r\n"));
70 if (_columnCount == 1){
73 if ((i+1) % _columnCount == 0 && i > 0){
81 debugPrint("End write Separator");
88 protected static void printConsole(int _columnCount, ArrayList<String> _alData) throws Exception{
90 debugPrint("Start console print");
91 for(int i=0;i<_alData.size();i++){
92 System.out.print(_alData.get(i).replaceAll("\n","\r\n"));
93 if (_columnCount == 1){
94 System.out.println("");
96 if ((i+1) % _columnCount == 0 && i > 0){
97 System.out.println("");
99 System.out.print(",");
103 debugPrint("End console print");
110 protected static void deepPrint(String _message) throws Exception{
111 if(System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y"))System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
114 protected static void debugPrint(String _message) throws Exception{
116 (System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y")) ||
117 (System.getProperty("debug")!=null && System.getProperty("debug").toUpperCase().equals("Y"))
118 )System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
121 protected static void debugPrinting(String _message) throws Exception{
123 (System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y")) ||
124 (System.getProperty("debug")!=null && System.getProperty("debug").toUpperCase().equals("Y"))
125 )System.out.print(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
128 protected static void infoPrint(String _message) throws Exception{
129 System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [INFO] " + _message);
131 protected static void infoPrinting(String _message) throws Exception{
132 System.out.print(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [INFO] " + _message);
135 protected static void errorPrint(String _message) throws Exception{
136 System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [ERROR] " + _message);
139 protected static String getTimestamp() throws Exception{
140 return (new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
143 protected static String getYmdhm() throws Exception{
144 return (new java.text.SimpleDateFormat("yyyyMMddHHmm").format(new Date()));
147 protected static int getArrayRowNo(ArrayList<String> _al, int _alMember, int _seq, String _value) throws Exception {
149 for(int i=0;i<_al.size()/_alMember;i++){
150 if(_al.get(i*_alMember + _seq).equals(_value)){
158 protected static String [] getArrayRowData(ArrayList<String> _al, int _alMember, int _row) throws Exception {
159 String [] rowData = new String[_alMember];
161 for(int i=_row*_alMember;i<_row*_alMember + _alMember;i++){
162 rowData[colNo] = _al.get(i);
168 protected static String [] splitCsv(String _line) throws Exception {
169 return split(_line, ",");
172 protected static String [] splitSpace(String _line) throws Exception {
173 return split(_line, " ");
176 protected static String [] splitDot(String _line) throws Exception {
177 return split(_line, "\\.");
180 protected static String [] splitCrLf(String _line) throws Exception {
181 return split(_line, "\r\n");
184 protected static String [] splitSlash(String _line) throws Exception {
185 return split(_line, "/");
188 protected static String [] splitTab(String _line) throws Exception {
189 return split(_line, "\t");
192 protected static String [] split(String _line, String _delim) throws Exception {
194 strSplit = _line.split(_delim, -1);
198 protected static boolean isLastCrLf(String _str) throws Exception{
200 char[] charArray = _str.toCharArray();
201 if (_str.length() > 1){
202 if (charArray[_str.length() - 2] != '\r' && charArray[_str.length() - 1] != '\n'){
209 protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
210 protected static String bytesToHex(byte[] _bytes){
211 char[] hexChars = new char[_bytes.length * 2];
212 for(int j=0;j<_bytes.length;j++){
213 int v = _bytes[j] & 0xFF;
214 hexChars[j * 2] = hexArray[v >>> 4];
215 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
217 return new String(hexChars);
219 protected static byte[] hexToBytes(String _hex){
220 byte[] bytes = new byte[_hex.length()/2];
221 for(int j=0;j<_hex.length()/2;j++){
222 bytes[j] = (byte)Integer.parseInt(_hex.substring(j*2,(j+1)*2),16);
227 protected static String bytesToBase64(byte[] _bytes){
228 return new String(Base64.encodeBase64(_bytes));
230 protected static byte[] base64ToBytes(String _base64){
231 return Base64.decodeBase64(_base64.getBytes());
234 protected static String clobToString(Clob _clob) throws Exception{
235 return _clob.getSubString(1, (int)_clob.length());
238 protected static String getLineSeparator(){
239 return System.getProperty("line.separator");
242 protected static boolean isMatch(String [] _tables, String _table) throws Exception{
246 if(_tables != null && _tables.length>0){
247 for (int i = 0; i < _tables.length; i++) {
248 String s = _tables[i].replace("*",".*");
249 //debugPrint("s="+s);
250 //p = Pattern.compile(s);
251 //m = p.matches(_table);
252 if(_table.matches(s)){
261 protected static String getYesNo(boolean _bln){
271 protected static boolean isMatch(String [] _tabCols, String _table, String _col) throws Exception{
275 if(_tabCols != null && _tabCols.length>0){
276 for (int i = 0; i < _tabCols.length; i++) {
278 String [] tbCols = splitDot(_tabCols[i]);
279 //infoPrint("length="+tbCols.length);
280 if (tbCols.length == 1){
281 s = _tabCols[i].replace("*",".*");
286 } else if (tbCols.length == 2){
287 if(_table.matches(tbCols[0].replace("*",".*")) && _col.matches(tbCols[1].replace("*",".*"))){
297 protected static boolean isJapanese(String _str) throws Exception{
298 for(int i=0;i<_str.length();i++){
299 char chr = _str.charAt(i);
300 Character.UnicodeBlock unicodeBlock = Character.UnicodeBlock.of(chr);
301 if (Character.UnicodeBlock.HIRAGANA.equals(unicodeBlock))return true;
302 if (Character.UnicodeBlock.KATAKANA.equals(unicodeBlock))return true;
303 if (Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS.equals(unicodeBlock))return true;
304 if (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(unicodeBlock))return true;
305 if (Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION.equals(unicodeBlock))return true;
310 protected static boolean isHankaku(char _chr) throws Exception{
311 if((_chr <= '\u007e') || (_chr == '\u00a5') || (_chr == '\u203e') || (_chr >= '\uff61' && _chr <= '\uff9f')){
318 protected static boolean isColLength(String _colType) throws Exception{
320 _colType.toUpperCase().equals("RAW") ||
321 _colType.toUpperCase().equals("BINARY") ||
322 _colType.toUpperCase().equals("VARBINARY") ||
323 _colType.toUpperCase().equals("CHAR") ||
324 _colType.toUpperCase().equals("NCHAR") ||
325 _colType.toUpperCase().equals("VARCHAR") ||
326 _colType.toUpperCase().equals("VARCHAR2") ||
327 _colType.toUpperCase().equals("NVARCHAR") ||
328 _colType.toUpperCase().equals("NVARCHAR2")
336 protected static boolean isColPrec(String _colType, String _colPrec) throws Exception{
338 !_colPrec.equals("") &&
339 !_colPrec.equals("0") &&
341 _colType.toUpperCase().equals("DECIMAL") ||
342 _colType.toUpperCase().equals("NUMBER") ||
343 _colType.toUpperCase().equals("NUMERIC")
352 protected static int getColLength(String _colType, String _colLen) throws Exception{
354 _colType.toUpperCase().equals("NCHAR") ||
355 _colType.toUpperCase().equals("NVARCHAR") ||
356 _colType.toUpperCase().equals("NVARCHAR2")){
357 return Integer.parseInt(_colLen)/2;
359 return Integer.parseInt(_colLen);
363 protected static boolean isColString(String _str) throws Exception{
365 _str.toUpperCase().equals("LONG") ||
366 _str.toUpperCase().equals("XML") ||
367 _str.toUpperCase().equals("UNIQUEIDENTIFIER") ||
368 _str.toUpperCase().equals("TEXT") ||
369 _str.toUpperCase().equals("NTEXT") ||
370 _str.toUpperCase().equals("CHARACTER") ||
371 _str.toUpperCase().equals("CHAR") ||
372 _str.toUpperCase().equals("BPCHAR") ||
373 _str.toUpperCase().equals("NCHAR") ||
374 _str.toUpperCase().equals("VARCHAR") ||
375 _str.toUpperCase().equals("VARCHAR2") ||
376 _str.toUpperCase().equals("NVARCHAR") ||
377 _str.toUpperCase().equals("NVARCHAR2") ||
378 _str.toUpperCase().equals("LONG VARCHAR") ||
379 _str.toUpperCase().equals("GRAPHIC") ||
380 _str.toUpperCase().equals("VARGRAPHIC") ||
381 _str.toUpperCase().equals("LONG VARGRAPHIC") ||
382 _str.toUpperCase().equals("MVARCHAR")){
389 protected static boolean isColBoolean(String _str) throws Exception{
391 _str.toUpperCase().equals("BIT") ||
392 _str.toUpperCase().equals("BOOL") ||
393 _str.toUpperCase().equals("BOOLEAN")){
400 protected static boolean isColDate(String _str) throws Exception{
402 _str.toUpperCase().equals("DATE")){
409 protected static boolean isColYear(String _str) throws Exception{
411 _str.toUpperCase().equals("YEAR")){
418 protected static boolean isColTime(String _str) throws Exception{
420 _str.toUpperCase().equals("TIME")){
427 protected static boolean isColTimestamp(String _str) throws Exception{
429 _str.toUpperCase().equals("TIMESTAMP") ||
430 _str.toUpperCase().equals("SMALLDATETIME") ||
431 _str.toUpperCase().equals("DATETIME") ||
432 _str.toUpperCase().equals("DATETIME2")){
439 protected static boolean isColBigDecimal(String _str) throws Exception{
441 _str.toUpperCase().equals("NUMERIC") ||
442 _str.toUpperCase().equals("NUMBER") ||
443 _str.toUpperCase().equals("MONEY") ||
444 _str.toUpperCase().equals("SMALLMONEY") ||
445 _str.toUpperCase().equals("DECFLOAT") ||
446 _str.toUpperCase().equals("DECIMAL")){
453 protected static boolean isColShort(String _str) throws Exception{
455 _str.toUpperCase().equals("TINYINT") ||
456 _str.toUpperCase().equals("TINYINT UNSIGNED") ||
457 _str.toUpperCase().equals("SMALLINT")){
464 protected static boolean isColInt(String _str) throws Exception{
466 _str.toUpperCase().equals("INTEGER") ||
467 _str.toUpperCase().equals("SMALLINT UNSIGNED") ||
468 _str.toUpperCase().equals("MEDIUMINT") ||
469 _str.toUpperCase().equals("SERIAL") ||
470 _str.toUpperCase().equals("INT2") ||
471 _str.toUpperCase().equals("INT4") ||
472 _str.toUpperCase().equals("INT")){
479 protected static boolean isColLong(String _str) throws Exception{
481 _str.toUpperCase().equals("INTEGER UNSIGNED") ||
482 _str.toUpperCase().equals("MEDIUMINT UNSIGNED") ||
483 _str.toUpperCase().equals("BIGINT UNSIGNED") ||
484 _str.toUpperCase().equals("BIGINT") ||
485 _str.toUpperCase().equals("BIGSERIAL") ||
486 _str.toUpperCase().equals("SERIAL8") ||
487 _str.toUpperCase().equals("INT8")){
494 protected static boolean isColFloat(String _str) throws Exception{
496 _str.toUpperCase().equals("REAL") ||
497 _str.toUpperCase().equals("SMALLFLT")){
504 protected static boolean isColDouble(String _str) throws Exception{
506 _str.toUpperCase().equals("FLOAT") ||
507 _str.toUpperCase().equals("DOUBLE") ||
508 _str.toUpperCase().equals("DOUBLE PRECISION")){
515 protected static boolean isColBytes(String _str) throws Exception{
517 _str.toUpperCase().equals("RAW") ||
518 _str.toUpperCase().equals("GEOMETRY") ||
519 _str.toUpperCase().equals("BINARY")){
526 protected static boolean isColBlob(String _str) throws Exception{
528 _str.toUpperCase().equals("BYTEA") ||
529 _str.toUpperCase().equals("BLOB") ||
530 _str.toUpperCase().equals("VARBINARY") ||
531 _str.toUpperCase().equals("UDT") ||
532 _str.toUpperCase().equals("IMAGE")){
539 protected static boolean isColClob(String _str) throws Exception{
541 _str.toUpperCase().equals("CLOB") ||
542 _str.toUpperCase().equals("NCLOB")){
549 protected static boolean isLob(String _str) throws Exception{
551 isColClob(_str) || isColBlob(_str)){
558 protected static boolean isEmpty(String _str) throws Exception{
559 if(_str == null || _str.equals("")){
566 protected static boolean isNumeric(String _str) {
567 boolean retVal = true;
569 int val = Integer.parseInt(_str);
570 } catch (Exception e){
576 protected static String[] getSystemProperty(String _str) throws Exception{
577 String [] retValue = null;
578 if(System.getProperty(_str)!=null){
579 retValue = splitCsv(System.getProperty(_str).toUpperCase());
584 protected static String getSystemProperty(String _str, String _strDefault) throws Exception{
585 String retValue = _strDefault;
586 if(System.getProperty(_str)!=null){
587 retValue = System.getProperty(_str).toUpperCase();
592 protected static char getSystemProperty(String _str, char _chrDefault) throws Exception{
593 char retValue = _chrDefault;
594 if(System.getProperty(_str)!=null){
595 retValue = System.getProperty(_str).toUpperCase().charAt(0);
600 protected static boolean getSystemProperty(String _str, boolean _blnDefault) throws Exception{
601 boolean retValue = _blnDefault;
602 if(System.getProperty(_str)!=null){
603 if(System.getProperty(_str).toUpperCase().equals("Y")){
605 } else if (System.getProperty(_str).toUpperCase().equals("N")){
614 protected static String getSeparator(String _str, String _strDefault) throws Exception{
615 String retValue = _strDefault;
616 if(System.getProperty(_str)!=null){
617 String _sprtr = System.getProperty(_str);
618 char [] chr = new char[_sprtr.length()];
620 for(int i=0;i<_sprtr.length();i++){
621 if (i < _sprtr.length() - 1){
622 if (_sprtr.charAt(i) == '\\'){
623 switch(_sprtr.charAt(i + 1)){
630 chr[j] = _sprtr.charAt(i);
635 chr[j] = _sprtr.charAt(i);
639 chr[j] = _sprtr.charAt(i);
643 char [] chr2 = new char[j];
644 for(int i=0;i<j;i++){
647 retValue = String.valueOf(chr2);
652 protected static String getDelimiter(String _str) throws Exception{
653 String retValue = _str;
655 char [] chr = new char[_str.length()];
657 for(int i=0;i<_str.length();i++){
658 if (i < _str.length() - 1){
659 if (_str.charAt(i) == '\\'){
660 switch(_str.charAt(i + 1)){
667 chr[j] = _str.charAt(i);
672 chr[j] = _str.charAt(i);
676 chr[j] = _str.charAt(i);
680 char [] chr2 = new char[j];
681 for(int i=0;i<j;i++){
684 retValue = String.valueOf(chr2);
689 protected static String getLineSeparator(String _str, String _strDefault) throws Exception{
690 String retValue = _strDefault;
691 if(System.getProperty(_str)!=null){
692 String _sprtr = System.getProperty(_str);
693 char [] chr = new char[_sprtr.length()];
695 for(int i=0;i<_sprtr.length();i++){
696 if (i < _sprtr.length() - 1){
697 if (_sprtr.charAt(i) == '\\'){
698 switch(_sprtr.charAt(i + 1)){
713 char [] chr2 = new char[j];
714 for(int i=0;i<j;i++){
717 retValue = String.valueOf(chr2);
722 protected static String getLineSeparator(String _str) throws Exception{
723 String retValue = _str;
725 char [] chr = new char[_str.length()];
727 for(int i=0;i<_str.length();i++){
728 if (i < _str.length() - 1){
729 if (_str.charAt(i) == '\\'){
730 switch(_str.charAt(i + 1)){
745 char [] chr2 = new char[j];
746 for(int i=0;i<j;i++){
749 retValue = String.valueOf(chr2);
754 protected static boolean isFixString(String _str) throws Exception{
756 _str.toUpperCase().equals("CHARACTER") ||
757 _str.toUpperCase().equals("CHAR") ||
758 _str.toUpperCase().equals("NCHAR")){
765 protected static int getByteLength(String string, Charset charset) {
766 return string.getBytes(charset).length;
769 protected static String padSpace(String str, int len, Charset charset){
770 int bytelen = getByteLength(str, charset);
772 for(int i=0; i<(len - bytelen);i++){