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 printConsole(int _columnCount, ArrayList<String> _alData) throws Exception{
65 debugPrint("Start console print");
66 for(int i=0;i<_alData.size();i++){
67 System.out.print(_alData.get(i).replaceAll("\n","\r\n"));
68 if (_columnCount == 1){
69 System.out.println("");
71 if ((i+1) % _columnCount == 0 && i > 0){
72 System.out.println("");
74 System.out.print(",");
78 debugPrint("End console print");
85 protected static void deepPrint(String _message) throws Exception{
86 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);
89 protected static void debugPrint(String _message) throws Exception{
91 (System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y")) ||
92 (System.getProperty("debug")!=null && System.getProperty("debug").toUpperCase().equals("Y"))
93 )System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
96 protected static void debugPrinting(String _message) throws Exception{
98 (System.getProperty("deep")!=null && System.getProperty("deep").toUpperCase().equals("Y")) ||
99 (System.getProperty("debug")!=null && System.getProperty("debug").toUpperCase().equals("Y"))
100 )System.out.print(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [DEBUG] " + _message);
103 protected static void infoPrint(String _message) throws Exception{
104 System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [INFO] " + _message);
106 protected static void infoPrinting(String _message) throws Exception{
107 System.out.print(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [INFO] " + _message);
110 protected static void errorPrint(String _message) throws Exception{
111 System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + " [ERROR] " + _message);
114 protected static String getTimestamp() throws Exception{
115 return (new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
118 protected static String getYmdhm() throws Exception{
119 return (new java.text.SimpleDateFormat("yyyyMMddHHmm").format(new Date()));
122 protected static int getArrayRowNo(ArrayList<String> _al, int _alMember, int _seq, String _value) throws Exception {
124 for(int i=0;i<_al.size()/_alMember;i++){
125 if(_al.get(i*_alMember + _seq).equals(_value)){
133 protected static String [] getArrayRowData(ArrayList<String> _al, int _alMember, int _row) throws Exception {
134 String [] rowData = new String[_alMember];
136 for(int i=_row*_alMember;i<_row*_alMember + _alMember;i++){
137 rowData[colNo] = _al.get(i);
143 protected static String [] splitCsv(String _line) throws Exception {
144 return split(_line, ",");
147 protected static String [] splitSpace(String _line) throws Exception {
148 return split(_line, " ");
151 protected static String [] splitDot(String _line) throws Exception {
152 return split(_line, "\\.");
155 protected static String [] splitCrLf(String _line) throws Exception {
156 return split(_line, "\r\n");
159 protected static String [] splitSlash(String _line) throws Exception {
160 return split(_line, "/");
163 protected static String [] splitTab(String _line) throws Exception {
164 return split(_line, "\t");
167 protected static String [] split(String _line, String _delim) throws Exception {
169 strSplit = _line.split(_delim, -1);
173 protected static boolean isLastCrLf(String _str) throws Exception{
175 char[] charArray = _str.toCharArray();
176 if (_str.length() > 1){
177 if (charArray[_str.length() - 2] != '\r' && charArray[_str.length() - 1] != '\n'){
184 protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
185 protected static String bytesToHex(byte[] _bytes){
186 char[] hexChars = new char[_bytes.length * 2];
187 for(int j=0;j<_bytes.length;j++){
188 int v = _bytes[j] & 0xFF;
189 hexChars[j * 2] = hexArray[v >>> 4];
190 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
192 return new String(hexChars);
194 protected static byte[] hexToBytes(String _hex){
195 byte[] bytes = new byte[_hex.length()/2];
196 for(int j=0;j<_hex.length()/2;j++){
197 bytes[j] = (byte)Integer.parseInt(_hex.substring(j*2,(j+1)*2),16);
202 protected static String bytesToBase64(byte[] _bytes){
203 return new String(Base64.encodeBase64(_bytes));
205 protected static byte[] base64ToBytes(String _base64){
206 return Base64.decodeBase64(_base64.getBytes());
209 protected static String clobToString(Clob _clob) throws Exception{
210 return _clob.getSubString(1, (int)_clob.length());
213 protected static String getLineSeparator(){
214 return System.getProperty("line.separator");
217 protected static boolean isMatch(String [] _tables, String _table) throws Exception{
221 if(_tables != null && _tables.length>0){
222 for (int i = 0; i < _tables.length; i++) {
223 String s = _tables[i].replace("*",".*");
224 //debugPrint("s="+s);
225 //p = Pattern.compile(s);
226 //m = p.matches(_table);
227 if(_table.matches(s)){
236 protected static String getYesNo(boolean _bln){
246 protected static boolean isMatch(String [] _tabCols, String _table, String _col) throws Exception{
250 if(_tabCols != null && _tabCols.length>0){
251 for (int i = 0; i < _tabCols.length; i++) {
253 String [] tbCols = splitDot(_tabCols[i]);
254 //infoPrint("length="+tbCols.length);
255 if (tbCols.length == 1){
256 s = _tabCols[i].replace("*",".*");
261 } else if (tbCols.length == 2){
262 if(_table.matches(tbCols[0].replace("*",".*")) && _col.matches(tbCols[1].replace("*",".*"))){
272 protected static boolean isJapanese(String _str) throws Exception{
273 for(int i=0;i<_str.length();i++){
274 char chr = _str.charAt(i);
275 Character.UnicodeBlock unicodeBlock = Character.UnicodeBlock.of(chr);
276 if (Character.UnicodeBlock.HIRAGANA.equals(unicodeBlock))return true;
277 if (Character.UnicodeBlock.KATAKANA.equals(unicodeBlock))return true;
278 if (Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS.equals(unicodeBlock))return true;
279 if (Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(unicodeBlock))return true;
280 if (Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION.equals(unicodeBlock))return true;
285 protected static boolean isHankaku(char _chr) throws Exception{
286 if((_chr <= '\u007e') || (_chr == '\u00a5') || (_chr == '\u203e') || (_chr >= '\uff61' && _chr <= '\uff9f')){
293 protected static boolean isColString(String _str) throws Exception{
295 _str.toUpperCase().equals("XML") ||
296 _str.toUpperCase().equals("UNIQUEIDENTIFIER") ||
297 _str.toUpperCase().equals("TEXT") ||
298 _str.toUpperCase().equals("NTEXT") ||
299 _str.toUpperCase().equals("CHARACTER") ||
300 _str.toUpperCase().equals("CHAR") ||
301 _str.toUpperCase().equals("BPCHAR") ||
302 _str.toUpperCase().equals("NCHAR") ||
303 _str.toUpperCase().equals("VARCHAR") ||
304 _str.toUpperCase().equals("VARCHAR2") ||
305 _str.toUpperCase().equals("NVARCHAR") ||
306 _str.toUpperCase().equals("NVARCHAR2") ||
307 _str.toUpperCase().equals("LONG VARCHAR") ||
308 _str.toUpperCase().equals("GRAPHIC") ||
309 _str.toUpperCase().equals("VARGRAPHIC") ||
310 _str.toUpperCase().equals("LONG VARGRAPHIC") ||
311 _str.toUpperCase().equals("MVARCHAR")){
318 protected static boolean isColBoolean(String _str) throws Exception{
320 _str.toUpperCase().equals("BIT") ||
321 _str.toUpperCase().equals("BOOL") ||
322 _str.toUpperCase().equals("BOOLEAN")){
329 protected static boolean isColDate(String _str) throws Exception{
331 _str.toUpperCase().equals("DATE")){
338 protected static boolean isColYear(String _str) throws Exception{
340 _str.toUpperCase().equals("YEAR")){
347 protected static boolean isColTime(String _str) throws Exception{
349 _str.toUpperCase().equals("TIME")){
356 protected static boolean isColTimestamp(String _str) throws Exception{
358 _str.toUpperCase().equals("TIMESTAMP") ||
359 _str.toUpperCase().equals("SMALLDATETIME") ||
360 _str.toUpperCase().equals("DATETIME") ||
361 _str.toUpperCase().equals("DATETIME2")){
368 protected static boolean isColBigDecimal(String _str) throws Exception{
370 _str.toUpperCase().equals("NUMERIC") ||
371 _str.toUpperCase().equals("NUMBER") ||
372 _str.toUpperCase().equals("MONEY") ||
373 _str.toUpperCase().equals("SMALLMONEY") ||
374 _str.toUpperCase().equals("DECFLOAT") ||
375 _str.toUpperCase().equals("DECIMAL")){
382 protected static boolean isColShort(String _str) throws Exception{
384 _str.toUpperCase().equals("TINYINT") ||
385 _str.toUpperCase().equals("TINYINT UNSIGNED") ||
386 _str.toUpperCase().equals("SMALLINT")){
393 protected static boolean isColInt(String _str) throws Exception{
395 _str.toUpperCase().equals("INTEGER") ||
396 _str.toUpperCase().equals("SMALLINT UNSIGNED") ||
397 _str.toUpperCase().equals("MEDIUMINT") ||
398 _str.toUpperCase().equals("SERIAL") ||
399 _str.toUpperCase().equals("INT2") ||
400 _str.toUpperCase().equals("INT4") ||
401 _str.toUpperCase().equals("INT")){
408 protected static boolean isColLong(String _str) throws Exception{
410 _str.toUpperCase().equals("INTEGER UNSIGNED") ||
411 _str.toUpperCase().equals("MEDIUMINT UNSIGNED") ||
412 _str.toUpperCase().equals("BIGINT UNSIGNED") ||
413 _str.toUpperCase().equals("BIGINT") ||
414 _str.toUpperCase().equals("BIGSERIAL") ||
415 _str.toUpperCase().equals("SERIAL8") ||
416 _str.toUpperCase().equals("INT8") ||
417 _str.toUpperCase().equals("LONG")){
424 protected static boolean isColFloat(String _str) throws Exception{
426 _str.toUpperCase().equals("REAL") ||
427 _str.toUpperCase().equals("SMALLFLT")){
434 protected static boolean isColDouble(String _str) throws Exception{
436 _str.toUpperCase().equals("FLOAT") ||
437 _str.toUpperCase().equals("DOUBLE") ||
438 _str.toUpperCase().equals("DOUBLE PRECISION")){
445 protected static boolean isColBytes(String _str) throws Exception{
447 _str.toUpperCase().equals("RAW") ||
448 _str.toUpperCase().equals("GEOMETRY") ||
449 _str.toUpperCase().equals("BINARY")){
456 protected static boolean isColBlob(String _str) throws Exception{
458 _str.toUpperCase().equals("BYTEA") ||
459 _str.toUpperCase().equals("BLOB") ||
460 _str.toUpperCase().equals("VARBINARY") ||
461 _str.toUpperCase().equals("UDT") ||
462 _str.toUpperCase().equals("IMAGE")){
469 protected static boolean isColClob(String _str) throws Exception{
471 _str.toUpperCase().equals("CLOB") ||
472 _str.toUpperCase().equals("NCLOB")){
479 protected static boolean isLob(String _str) throws Exception{
481 isColClob(_str) || isColBlob(_str)){
488 protected static boolean isEmpty(String _str) throws Exception{
489 if(_str == null || _str.equals("")){
496 protected static boolean isNumeric(String _str) {
497 boolean retVal = true;
499 int val = Integer.parseInt(_str);
500 } catch (Exception e){
506 protected static String[] getSystemProperty(String _str) throws Exception{
507 String [] retValue = null;
508 if(System.getProperty(_str)!=null){
509 retValue = splitCsv(System.getProperty(_str).toUpperCase());
514 protected static String getSystemProperty(String _str, String _strDefault) throws Exception{
515 String retValue = _strDefault;
516 if(System.getProperty(_str)!=null){
517 retValue = System.getProperty(_str).toUpperCase();
522 protected static char getSystemProperty(String _str, char _chrDefault) throws Exception{
523 char retValue = _chrDefault;
524 if(System.getProperty(_str)!=null){
525 retValue = System.getProperty(_str).toUpperCase().charAt(0);
530 protected static boolean getSystemProperty(String _str, boolean _blnDefault) throws Exception{
531 boolean retValue = _blnDefault;
532 if(System.getProperty(_str)!=null){
533 if(System.getProperty(_str).toUpperCase().equals("Y")){
535 } else if (System.getProperty(_str).toUpperCase().equals("N")){
544 protected static String getSeparator(String _str, String _strDefault) throws Exception{
545 String retValue = _strDefault;
546 if(System.getProperty(_str)!=null){
547 String _sprtr = System.getProperty(_str);
548 char [] chr = new char[_sprtr.length()];
550 for(int i=0;i<_sprtr.length();i++){
551 if (i < _sprtr.length() - 1){
552 if (_sprtr.charAt(i) == '\\'){
553 switch(_sprtr.charAt(i + 1)){
560 chr[j] = _sprtr.charAt(i);
565 chr[j] = _sprtr.charAt(i);
569 chr[j] = _sprtr.charAt(i);
573 char [] chr2 = new char[j];
574 for(int i=0;i<j;i++){
577 retValue = String.valueOf(chr2);
582 protected static String getDelimiter(String _str) throws Exception{
583 String retValue = _str;
585 char [] chr = new char[_str.length()];
587 for(int i=0;i<_str.length();i++){
588 if (i < _str.length() - 1){
589 if (_str.charAt(i) == '\\'){
590 switch(_str.charAt(i + 1)){
597 chr[j] = _str.charAt(i);
602 chr[j] = _str.charAt(i);
606 chr[j] = _str.charAt(i);
610 char [] chr2 = new char[j];
611 for(int i=0;i<j;i++){
614 retValue = String.valueOf(chr2);
619 protected static String getLineSeparator(String _str, String _strDefault) throws Exception{
620 String retValue = _strDefault;
621 if(System.getProperty(_str)!=null){
622 String _sprtr = System.getProperty(_str);
623 char [] chr = new char[_sprtr.length()];
625 for(int i=0;i<_sprtr.length();i++){
626 if (i < _sprtr.length() - 1){
627 if (_sprtr.charAt(i) == '\\'){
628 switch(_sprtr.charAt(i + 1)){
643 char [] chr2 = new char[j];
644 for(int i=0;i<j;i++){
647 retValue = String.valueOf(chr2);
652 protected static String getLineSeparator(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)){
675 char [] chr2 = new char[j];
676 for(int i=0;i<j;i++){
679 retValue = String.valueOf(chr2);
684 protected static boolean isFixString(String _str) throws Exception{
686 _str.toUpperCase().equals("CHARACTER") ||
687 _str.toUpperCase().equals("CHAR") ||
688 _str.toUpperCase().equals("NCHAR")){
695 protected static int getByteLength(String string, Charset charset) {
696 return string.getBytes(charset).length;
699 protected static String padSpace(String str, int len, Charset charset){
700 int bytelen = getByteLength(str, charset);
702 for(int i=0; i<(len - bytelen);i++){