OSDN Git Service

20170219
[rapideact/rapideact.git] / com / rapide_act / CmnUtils.java
1 package com.rapide_act;
2
3 import java.io.BufferedInputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.File;
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;
28 import java.sql.Blob;
29 import java.sql.Clob;
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
35 public class CmnUtils{
36         
37         protected static void writeCsv(String _file, int _columnCount, ArrayList<String> _alData) throws Exception{
38                 PrintWriter pw = null;
39                 try {
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){
45                                         pw.println("");
46                                 } else {
47                                         if ((i+1) % _columnCount == 0 && i > 0){
48                                                 pw.println("");
49                                         } else {
50                                                 pw.print(",");
51                                         }
52                                 }
53                         }
54                         pw.close();
55                         debugPrint("End write csv");
56                 }
57                 catch (Exception e){
58                         throw e;
59                 }
60         }
61
62         protected static void printConsole(int _columnCount, ArrayList<String> _alData) throws Exception{
63                 try {
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("");
69                                 } else {
70                                         if ((i+1) % _columnCount == 0 && i > 0){
71                                                 System.out.println("");
72                                         } else {
73                                                 System.out.print(",");
74                                         }
75                                 }
76                         }
77                         debugPrint("End console print");
78                 }
79                 catch (Exception e){
80                         throw e;
81                 }
82         }
83
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);
86         }
87
88         protected static void debugPrint(String _message) throws Exception{
89                 if(
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);
93         }
94
95         protected static void debugPrinting(String _message) throws Exception{
96                 if(
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);
100         }
101
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);
104         }
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);
107         }
108
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);
111         }
112
113         protected static String getTimestamp() throws Exception{
114                 return (new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
115         }
116
117         protected static String getYmdhm() throws Exception{
118                 return (new java.text.SimpleDateFormat("yyyyMMddHHmm").format(new Date()));
119         }
120
121         protected static int getArrayRowNo(ArrayList<String> _al, int _alMember, int _seq, String _value) throws Exception {
122                 int ret = -1;
123                 for(int i=0;i<_al.size()/_alMember;i++){
124                         if(_al.get(i*_alMember + _seq).equals(_value)){
125                                 ret = i;
126                                 break;
127                         }
128                 }
129                 return ret;
130         }
131
132         protected static String [] getArrayRowData(ArrayList<String> _al, int _alMember, int _row) throws Exception {
133                 String [] rowData = new String[_alMember];
134                 int colNo = 0;
135                 for(int i=_row*_alMember;i<_row*_alMember + _alMember;i++){
136                         rowData[colNo] = _al.get(i);
137                         colNo++;
138                 }
139                 return rowData;
140         }
141
142         protected static String [] splitCsv(String _line) throws Exception {
143                 return split(_line, ",");
144         }
145
146         protected static String [] splitSpace(String _line) throws Exception {
147                 return split(_line, " ");
148         }
149
150         protected static String [] splitDot(String _line) throws Exception {
151                 return split(_line, "\\.");
152         }
153         
154         protected static String [] splitCrLf(String _line) throws Exception {
155                 return split(_line, "\r\n");
156         }
157
158         protected static String [] splitSlash(String _line) throws Exception {
159                 return split(_line, "/");
160         }
161
162         protected static String [] splitTab(String _line) throws Exception {
163                 return split(_line, "\t");
164         }
165
166         protected static String [] split(String _line, String _delim) throws Exception {
167                 String [] strSplit;
168                 strSplit = _line.split(_delim, -1);
169                 return strSplit;
170         }
171         
172         protected static boolean isLastCrLf(String _str) throws Exception{
173                 boolean rtn = false;
174                 char[] charArray = _str.toCharArray();
175                 if (_str.length() > 1){
176                         if (charArray[_str.length() - 2] != '\r' && charArray[_str.length() - 1] != '\n'){
177                                 rtn = true;
178                         }
179                 }
180                 return rtn;
181         }
182
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];
190                 }
191                 return new String(hexChars);
192         }
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);
197                 }
198                 return bytes;
199         }
200
201         protected static String bytesToBase64(byte[] _bytes){
202                 return new String(Base64.encodeBase64(_bytes));
203         }
204         protected static byte[] base64ToBytes(String _base64){
205                 return Base64.decodeBase64(_base64.getBytes());
206         }
207
208         protected static String clobToString(Clob _clob) throws Exception{
209                 return _clob.getSubString(1, (int)_clob.length());
210         }
211
212         protected static String getLineSeparator(){
213                 return System.getProperty("line.separator");
214         }
215
216         protected static boolean isMatch(String [] _tables, String _table) throws Exception{
217                 boolean rtn = false;
218                 Pattern p = null;
219                 Matcher m = null;
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)){
227                                         rtn = true;
228                                         break;
229                                 }
230                         }
231                 }
232                 return rtn;
233         }
234         
235         protected static String getYesNo(boolean _bln){
236                 String retVal;
237                 if(_bln){
238                         retVal = "Yes";
239                 } else {
240                         retVal = "No";
241                 }
242                 return retVal;
243         }
244
245         protected static boolean isMatch(String [] _tabCols, String _table, String _col) throws Exception{
246                 boolean rtn = false;
247                 Pattern p = null;
248                 Matcher m = null;
249                 if(_tabCols != null && _tabCols.length>0){
250                         for (int i = 0; i < _tabCols.length; i++) {
251                                 String s = null;
252                                 String [] tbCols = splitDot(_tabCols[i]);
253                                 //infoPrint("length="+tbCols.length);
254                                 if (tbCols.length == 1){
255                                         s = _tabCols[i].replace("*",".*");
256                                         if(_col.matches(s)){
257                                                 rtn = true;
258                                                 break;
259                                         }
260                                 } else if (tbCols.length == 2){
261                                         if(_table.matches(tbCols[0].replace("*",".*")) && _col.matches(tbCols[1].replace("*",".*"))){
262                                                 rtn = true;
263                                                 break;
264                                         }
265                                 }
266                         }
267                 }
268                 return rtn;
269         }
270
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;
280                 }
281                 return false;
282         }
283
284         protected static boolean isHankaku(char _chr) throws Exception{
285                 if((_chr <= '\u007e') || (_chr == '\u00a5') || (_chr == '\u203e') || (_chr >= '\uff61' && _chr <= '\uff9f')){
286                         return true;
287                 } else {
288                         return false;
289                 }
290         }
291
292         protected static boolean isColString(String _str) throws Exception{
293                 if(
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")){
305                         return true;
306                 } else {
307                         return false;
308                 }
309         }
310
311         protected static boolean isColDate(String _str) throws Exception{
312                 if(
313                         _str.toUpperCase().equals("DATE")){
314                         return true;
315                 } else {
316                         return false;
317                 }
318         }
319
320         protected static boolean isColYear(String _str) throws Exception{
321                 if(
322                 _str.toUpperCase().equals("YEAR")){
323                         return true;
324                 } else {
325                         return false;
326                 }
327         }
328
329         protected static boolean isColTime(String _str) throws Exception{
330                 if(
331                         _str.toUpperCase().equals("TIME")){
332                         return true;
333                 } else {
334                         return false;
335                 }
336         }
337
338         protected static boolean isColTimestamp(String _str) throws Exception{
339                 if(
340                         _str.toUpperCase().equals("TIMESTAMP") ||
341                         _str.toUpperCase().equals("SMALLDATETIME") ||
342                         _str.toUpperCase().equals("DATETIME") ||
343                         _str.toUpperCase().equals("DATETIME2")){
344                         return true;
345                 } else {
346                         return false;
347                 }
348         }
349
350         protected static boolean isColBigDecimal(String _str) throws Exception{
351                 if(
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")){
357                         return true;
358                 } else {
359                         return false;
360                 }
361         }
362
363         protected static boolean isColShort(String _str) throws Exception{
364                 if(
365                         _str.toUpperCase().equals("TINYINT") ||
366                         _str.toUpperCase().equals("TINYINT UNSIGNED") ||
367                         _str.toUpperCase().equals("SMALLINT")){
368                         return true;
369                 } else {
370                         return false;
371                 }
372         }
373
374         protected static boolean isColInt(String _str) throws Exception{
375                 if(
376                         _str.toUpperCase().equals("INTEGER") ||
377                         _str.toUpperCase().equals("SMALLINT UNSIGNED") ||
378                         _str.toUpperCase().equals("MEDIUMINT") ||
379                         _str.toUpperCase().equals("INT")){
380                         return true;
381                 } else {
382                         return false;
383                 }
384         }
385
386         protected static boolean isColLong(String _str) throws Exception{
387                 if(
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")){
393                         return true;
394                 } else {
395                         return false;
396                 }
397         }
398
399         protected static boolean isColFloat(String _str) throws Exception{
400                 if(
401                         _str.toUpperCase().equals("REAL") ||
402                         _str.toUpperCase().equals("SMALLFLT")){
403                         return true;
404                 } else {
405                         return false;
406                 }
407         }
408
409         protected static boolean isColDouble(String _str) throws Exception{
410                 if(
411                         _str.toUpperCase().equals("FLOAT") ||
412                         _str.toUpperCase().equals("DOUBLE PRECISION")){
413                         return true;
414                 } else {
415                         return false;
416                 }
417         }
418
419         protected static boolean isColBytes(String _str) throws Exception{
420                 if(
421                         _str.toUpperCase().equals("RAW") ||
422                         _str.toUpperCase().equals("GEOMETRY") ||
423                         _str.toUpperCase().equals("BINARY")){
424                         return true;
425                 } else {
426                         return false;
427                 }
428         }
429
430         protected static boolean isColBlob(String _str) throws Exception{
431                 if(
432                         _str.toUpperCase().equals("BLOB") ||
433                         _str.toUpperCase().equals("VARBINARY") ||
434                         _str.toUpperCase().equals("UDT") ||
435                         _str.toUpperCase().equals("IMAGE")){
436                         return true;
437                 } else {
438                         return false;
439                 }
440         }
441
442         protected static boolean isColClob(String _str) throws Exception{
443                 if(
444                         _str.toUpperCase().equals("CLOB") ||
445                         _str.toUpperCase().equals("NCLOB")){
446                         return true;
447                 } else {
448                         return false;
449                 }
450         }
451
452         protected static boolean isLob(String _str) throws Exception{
453                 if(
454                         isColClob(_str) || isColBlob(_str)){
455                         return true;
456                 } else {
457                         return false;
458                 }
459         }
460
461         protected static boolean isEmpty(String _str) throws Exception{
462                 if(_str == null || _str.equals("")){
463                         return true;
464                 } else {
465                         return false;
466                 }
467         }
468
469         protected static boolean isNumeric(String _str) {
470                 boolean retVal = true;
471                 try {
472                         int val = Integer.parseInt(_str);
473                 } catch (Exception e){
474                         retVal = false;
475                 }
476                 return retVal;
477         }
478
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());
483                 }
484                 return retValue;
485         }
486
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();
491                 }
492                 return retValue;
493         }
494
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);
499                 }
500                 return retValue;
501         }
502
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")){
507                                 retValue = true;
508                         } else if (System.getProperty(_str).toUpperCase().equals("N")){
509                                 retValue = false;
510                         } else {
511                                 retValue = true;
512                         }
513                 }
514                 return retValue;
515         }
516
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()];
522                         int j = 0;
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)){
527                                                         case 't':
528                                                                 chr[j] = 0x09;
529                                                                 i++;
530                                                                 j++;
531                                                                 break;
532                                                         default:
533                                                                 chr[j] = _sprtr.charAt(i);
534                                                                 j++;
535                                                                 break;
536                                                 }
537                                         } else {
538                                                 chr[j] = _sprtr.charAt(i);
539                                                 j++;
540                                         }
541                                 } else {
542                                         chr[j] = _sprtr.charAt(i);
543                                         j++;
544                                 }
545                         }
546                         char [] chr2 = new char[j];
547                         for(int i=0;i<j;i++){
548                                 chr2[i] = chr[i];
549                         }
550                         retValue = String.valueOf(chr2);
551                 }
552                 return retValue;
553         }
554
555         protected static String getDelimiter(String _str) throws Exception{
556                 String retValue = _str;
557                 if(_str!=null){
558                         char [] chr = new char[_str.length()];
559                         int j = 0;
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)){
564                                                         case 't':
565                                                                 chr[j] = 0x09;
566                                                                 i++;
567                                                                 j++;
568                                                                 break;
569                                                         default:
570                                                                 chr[j] = _str.charAt(i);
571                                                                 j++;
572                                                                 break;
573                                                 }
574                                         } else {
575                                                 chr[j] = _str.charAt(i);
576                                                 j++;
577                                         }
578                                 } else {
579                                         chr[j] = _str.charAt(i);
580                                         j++;
581                                 }
582                         }
583                         char [] chr2 = new char[j];
584                         for(int i=0;i<j;i++){
585                                 chr2[i] = chr[i];
586                         }
587                         retValue = String.valueOf(chr2);
588                 }
589                 return retValue;
590         }
591
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()];
597                         int j = 0;
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)){
602                                                         case 'r':
603                                                                 chr[j] = 0x0d;
604                                                                 i++;
605                                                                 j++;
606                                                                 break;
607                                                         case 'n':
608                                                                 chr[j] = 0x0a;
609                                                                 i++;
610                                                                 j++;
611                                                                 break;
612                                                 }
613                                         }
614                                 }
615                         }
616                         char [] chr2 = new char[j];
617                         for(int i=0;i<j;i++){
618                                 chr2[i] = chr[i];
619                         }
620                         retValue = String.valueOf(chr2);
621                 }
622                 return retValue;
623         }
624
625         protected static String getLineSeparator(String _str) throws Exception{
626                 String retValue = _str;
627                 if(_str!=null){
628                         char [] chr = new char[_str.length()];
629                         int j = 0;
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)){
634                                                         case 'r':
635                                                                 chr[j] = 0x0d;
636                                                                 i++;
637                                                                 j++;
638                                                                 break;
639                                                         case 'n':
640                                                                 chr[j] = 0x0a;
641                                                                 i++;
642                                                                 j++;
643                                                                 break;
644                                                 }
645                                         }
646                                 }
647                         }
648                         char [] chr2 = new char[j];
649                         for(int i=0;i<j;i++){
650                                 chr2[i] = chr[i];
651                         }
652                         retValue = String.valueOf(chr2);
653                 }
654                 return retValue;
655         }
656
657 }