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;
35 public class CmnUtils{
37 protected static void writeCsv(String _file, int _columnCount, ArrayList<String> _alData) throws Exception{
38 PrintWriter pw = null;
40 debugPrint("Start write csv," + "columnCount=" + _columnCount);
41 pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(_file))));
42 for(int i=0;i<_alData.size();i++){
43 pw.print(_alData.get(i).replaceAll("\n","\r\n"));
44 if (_columnCount == 1){
47 if ((i+1) % _columnCount == 0 && i > 0){
55 debugPrint("End write csv");
62 protected static void printConsole(int _columnCount, ArrayList<String> _alData) throws Exception{
64 debugPrint("Start console print");
65 for(int i=0;i<_alData.size();i++){
66 System.out.print(_alData.get(i).replaceAll("\n","\r\n"));
67 if (_columnCount == 1){
68 System.out.println("");
70 if ((i+1) % _columnCount == 0 && i > 0){
71 System.out.println("");
73 System.out.print(",");
77 debugPrint("End console print");
84 protected static void deepPrint(String _message) throws Exception{
85 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);
88 protected static void debugPrint(String _message) throws Exception{
90 (System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y")) ||
91 (System.getProperty("debug")!=null && System.getProperty("debug").toUpperCase().equals("Y"))
92 )System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
95 protected static void debugPrinting(String _message) throws Exception{
97 (System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y")) ||
98 (System.getProperty("debug")!=null && System.getProperty("debug").toUpperCase().equals("Y"))
99 )System.out.print(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
102 protected static void infoPrint(String _message) throws Exception{
103 System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [INFO] " + _message);
105 protected static void infoPrinting(String _message) throws Exception{
106 System.out.print(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [INFO] " + _message);
109 protected static void errorPrint(String _message) throws Exception{
110 System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [ERROR] " + _message);
113 protected static String getTimestamp() throws Exception{
114 return (new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
117 protected static String getYmdhm() throws Exception{
118 return (new java.text.SimpleDateFormat("yyyyMMddHHmm").format(new Date()));
121 protected static int getArrayRowNo(ArrayList<String> _al, int _alMember, int _seq, String _value) throws Exception {
123 for(int i=0;i<_al.size()/_alMember;i++){
124 if(_al.get(i*_alMember + _seq).equals(_value)){
132 protected static String [] getArrayRowData(ArrayList<String> _al, int _alMember, int _row) throws Exception {
133 String [] rowData = new String[_alMember];
135 for(int i=_row*_alMember;i<_row*_alMember + _alMember;i++){
136 rowData[colNo] = _al.get(i);
142 protected static String [] splitCsv(String _line) throws Exception {
143 return split(_line, ",");
146 protected static String [] splitSpace(String _line) throws Exception {
147 return split(_line, " ");
150 protected static String [] splitDot(String _line) throws Exception {
151 return split(_line, "\\.");
154 protected static String [] splitCrLf(String _line) throws Exception {
155 return split(_line, "\r\n");
158 protected static String [] splitSlash(String _line) throws Exception {
159 return split(_line, "/");
162 protected static String [] splitTab(String _line) throws Exception {
163 return split(_line, "\t");
166 protected static String [] split(String _line, String _delim) throws Exception {
168 strSplit = _line.split(_delim, -1);
172 protected static boolean isLastCrLf(String _str) throws Exception{
174 char[] charArray = _str.toCharArray();
175 if (_str.length() > 1){
176 if (charArray[_str.length() - 2] != '\r' && charArray[_str.length() - 1] != '\n'){
183 protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
184 protected static String bytesToHex(byte[] _bytes){
185 char[] hexChars = new char[_bytes.length * 2];
186 for(int j=0;j<_bytes.length;j++){
187 int v = _bytes[j] & 0xFF;
188 hexChars[j * 2] = hexArray[v >>> 4];
189 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
191 return new String(hexChars);
193 protected static byte[] hexToBytes(String _hex){
194 byte[] bytes = new byte[_hex.length()/2];
195 for(int j=0;j<_hex.length()/2;j++){
196 bytes[j] = (byte)Integer.parseInt(_hex.substring(j*2,(j+1)*2),16);
201 protected static String bytesToBase64(byte[] _bytes){
202 return new String(Base64.encodeBase64(_bytes));
204 protected static byte[] base64ToBytes(String _base64){
205 return Base64.decodeBase64(_base64.getBytes());
208 protected static String clobToString(Clob _clob) throws Exception{
209 return _clob.getSubString(1, (int)_clob.length());
212 protected static String getLineSeparator(){
213 return System.getProperty("line.separator");
216 protected static boolean isMatch(String [] _tables, String _table) throws Exception{
220 if(_tables != null && _tables.length>0){
221 for (int i = 0; i < _tables.length; i++) {
222 String s = _tables[i].replace("*",".*");
223 //debugPrint("s="+s);
224 //p = Pattern.compile(s);
225 //m = p.matches(_table);
226 if(_table.matches(s)){
235 protected static String getYesNo(boolean _bln){
245 protected static boolean isMatch(String [] _tabCols, String _table, String _col) throws Exception{
249 if(_tabCols != null && _tabCols.length>0){
250 for (int i = 0; i < _tabCols.length; i++) {
252 String [] tbCols = splitDot(_tabCols[i]);
253 //infoPrint("length="+tbCols.length);
254 if (tbCols.length == 1){
255 s = _tabCols[i].replace("*",".*");
260 } else if (tbCols.length == 2){
261 if(_table.matches(tbCols[0].replace("*",".*")) && _col.matches(tbCols[1].replace("*",".*"))){
271 protected static boolean isJapanese(String _str) throws Exception{
272 for(int i=0;i<_str.length();i++){
273 char chr = _str.charAt(i);
274 Character.UnicodeBlock unicodeBlock = Character.UnicodeBlock.of(chr);
275 if (Character.UnicodeBlock.HIRAGANA.equals(unicodeBlock))return true;
276 if (Character.UnicodeBlock.KATAKANA.equals(unicodeBlock))return true;
277 if (Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS.equals(unicodeBlock))return true;
278 if (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(unicodeBlock))return true;
279 if (Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION.equals(unicodeBlock))return true;
284 protected static boolean isHankaku(char _chr) throws Exception{
285 if((_chr <= '\u007e') || (_chr == '\u00a5') || (_chr == '\u203e') || (_chr >= '\uff61' && _chr <= '\uff9f')){
292 protected static boolean isColString(String _str) throws Exception{
294 _str.toUpperCase().equals("XML") ||
295 _str.toUpperCase().equals("UNIQUEIDENTIFIER") ||
296 _str.toUpperCase().equals("TEXT") ||
297 _str.toUpperCase().equals("NTEXT") ||
298 _str.toUpperCase().equals("CHAR") ||
299 _str.toUpperCase().equals("NCHAR") ||
300 _str.toUpperCase().equals("VARCHAR") ||
301 _str.toUpperCase().equals("VARCHAR2") ||
302 _str.toUpperCase().equals("NVARCHAR") ||
303 _str.toUpperCase().equals("NVARCHAR2") ||
304 _str.toUpperCase().equals("MVARCHAR")){
311 protected static boolean isColDate(String _str) throws Exception{
313 _str.toUpperCase().equals("DATE")){
320 protected static boolean isColYear(String _str) throws Exception{
322 _str.toUpperCase().equals("YEAR")){
329 protected static boolean isColTime(String _str) throws Exception{
331 _str.toUpperCase().equals("TIME")){
338 protected static boolean isColTimestamp(String _str) throws Exception{
340 _str.toUpperCase().equals("TIMESTAMP") ||
341 _str.toUpperCase().equals("SMALLDATETIME") ||
342 _str.toUpperCase().equals("DATETIME") ||
343 _str.toUpperCase().equals("DATETIME2")){
350 protected static boolean isColBigDecimal(String _str) throws Exception{
352 _str.toUpperCase().equals("NUMERIC") ||
353 _str.toUpperCase().equals("NUMBER") ||
354 _str.toUpperCase().equals("MONEY") ||
355 _str.toUpperCase().equals("SMALLMONEY") ||
356 _str.toUpperCase().equals("DECIMAL")){
363 protected static boolean isColShort(String _str) throws Exception{
365 _str.toUpperCase().equals("TINYINT") ||
366 _str.toUpperCase().equals("TINYINT UNSIGNED") ||
367 _str.toUpperCase().equals("SMALLINT")){
374 protected static boolean isColInt(String _str) throws Exception{
376 _str.toUpperCase().equals("INTEGER") ||
377 _str.toUpperCase().equals("SMALLINT UNSIGNED") ||
378 _str.toUpperCase().equals("MEDIUMINT") ||
379 _str.toUpperCase().equals("INT")){
386 protected static boolean isColLong(String _str) throws Exception{
388 _str.toUpperCase().equals("INTEGER UNSIGNED") ||
389 _str.toUpperCase().equals("MEDIUMINT UNSIGNED") ||
390 _str.toUpperCase().equals("BIGINT UNSIGNED") ||
391 _str.toUpperCase().equals("BIGINT") ||
392 _str.toUpperCase().equals("LONG")){
399 protected static boolean isColFloat(String _str) throws Exception{
401 _str.toUpperCase().equals("REAL") ||
402 _str.toUpperCase().equals("SMALLFLT")){
409 protected static boolean isColDouble(String _str) throws Exception{
411 _str.toUpperCase().equals("FLOAT") ||
412 _str.toUpperCase().equals("DOUBLE PRECISION")){
419 protected static boolean isColBytes(String _str) throws Exception{
421 _str.toUpperCase().equals("RAW") ||
422 _str.toUpperCase().equals("GEOMETRY") ||
423 _str.toUpperCase().equals("BINARY")){
430 protected static boolean isColBlob(String _str) throws Exception{
432 _str.toUpperCase().equals("BLOB") ||
433 _str.toUpperCase().equals("VARBINARY") ||
434 _str.toUpperCase().equals("UDT") ||
435 _str.toUpperCase().equals("IMAGE")){
442 protected static boolean isColClob(String _str) throws Exception{
444 _str.toUpperCase().equals("CLOB") ||
445 _str.toUpperCase().equals("NCLOB")){
452 protected static boolean isLob(String _str) throws Exception{
454 isColClob(_str) || isColBlob(_str)){
461 protected static boolean isEmpty(String _str) throws Exception{
462 if(_str == null || _str.equals("")){
469 protected static boolean isNumeric(String _str) {
470 boolean retVal = true;
472 int val = Integer.parseInt(_str);
473 } catch (Exception e){
479 protected static String[] getSystemProperty(String _str) throws Exception{
480 String [] retValue = null;
481 if(System.getProperty(_str)!=null){
482 retValue = splitCsv(System.getProperty(_str).toUpperCase());
487 protected static String getSystemProperty(String _str, String _strDefault) throws Exception{
488 String retValue = _strDefault;
489 if(System.getProperty(_str)!=null){
490 retValue = System.getProperty(_str).toUpperCase();
495 protected static char getSystemProperty(String _str, char _chrDefault) throws Exception{
496 char retValue = _chrDefault;
497 if(System.getProperty(_str)!=null){
498 retValue = System.getProperty(_str).toUpperCase().charAt(0);
503 protected static boolean getSystemProperty(String _str, boolean _blnDefault) throws Exception{
504 boolean retValue = _blnDefault;
505 if(System.getProperty(_str)!=null){
506 if(System.getProperty(_str).toUpperCase().equals("Y")){
508 } else if (System.getProperty(_str).toUpperCase().equals("N")){
517 protected static String getSeparator(String _str, String _strDefault) throws Exception{
518 String retValue = _strDefault;
519 if(System.getProperty(_str)!=null){
520 String _sprtr = System.getProperty(_str);
521 char [] chr = new char[_sprtr.length()];
523 for(int i=0;i<_sprtr.length();i++){
524 if (i < _sprtr.length() - 1){
525 if (_sprtr.charAt(i) == '\\'){
526 switch(_sprtr.charAt(i + 1)){
533 chr[j] = _sprtr.charAt(i);
538 chr[j] = _sprtr.charAt(i);
542 chr[j] = _sprtr.charAt(i);
546 char [] chr2 = new char[j];
547 for(int i=0;i<j;i++){
550 retValue = String.valueOf(chr2);
555 protected static String getDelimiter(String _str) throws Exception{
556 String retValue = _str;
558 char [] chr = new char[_str.length()];
560 for(int i=0;i<_str.length();i++){
561 if (i < _str.length() - 1){
562 if (_str.charAt(i) == '\\'){
563 switch(_str.charAt(i + 1)){
570 chr[j] = _str.charAt(i);
575 chr[j] = _str.charAt(i);
579 chr[j] = _str.charAt(i);
583 char [] chr2 = new char[j];
584 for(int i=0;i<j;i++){
587 retValue = String.valueOf(chr2);
592 protected static String getLineSeparator(String _str, String _strDefault) throws Exception{
593 String retValue = _strDefault;
594 if(System.getProperty(_str)!=null){
595 String _sprtr = System.getProperty(_str);
596 char [] chr = new char[_sprtr.length()];
598 for(int i=0;i<_sprtr.length();i++){
599 if (i < _sprtr.length() - 1){
600 if (_sprtr.charAt(i) == '\\'){
601 switch(_sprtr.charAt(i + 1)){
616 char [] chr2 = new char[j];
617 for(int i=0;i<j;i++){
620 retValue = String.valueOf(chr2);
625 protected static String getLineSeparator(String _str) throws Exception{
626 String retValue = _str;
628 char [] chr = new char[_str.length()];
630 for(int i=0;i<_str.length();i++){
631 if (i < _str.length() - 1){
632 if (_str.charAt(i) == '\\'){
633 switch(_str.charAt(i + 1)){
648 char [] chr2 = new char[j];
649 for(int i=0;i<j;i++){
652 retValue = String.valueOf(chr2);