001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.fukurou.process; 017 018import org.opengion.fukurou.security.HybsCryptography; 019import org.opengion.fukurou.util.Argument; 020import org.opengion.fukurou.util.StringUtil; 021import org.opengion.fukurou.util.LogWriter; 022 023import java.util.Map ; 024import java.util.LinkedHashMap ; 025 026/** 027 * Process_StringUtil は、上流から受け取ったデータをStringUtilクラスの特定の 028 * メソッドでデータ変換する、CainProcess インターフェースの実装クラスです。 029 * 030 * 上流(プロセスチェインのデータは上流から下流へと渡されます。)から 031 * LineModel を元に、指定のカラムの文字を、変換します。 032 * 033 * 現時点で利用できるStringUtil のメソッドは、下記の通りです。 034 * urlEncode : UTF-8 で、URLエンコードを行う。 035 * rTrim : 文字列の後ろのスペースを削除 036 * htmlFilter : HTML上のエスケープ文字を変換 037 * code39 : CODE39 の 文字列を作成(チェックデジット付き) 038 * getUnicodeEscape : HTML のエスケープ記号(&#xZZZZ;)に変換 039 * getReplaceEscape : HTML のエスケープ記号(&#xZZZZ;)を戻す 040 * spanCut : 引数からspanタグを取り除く 041 * 042 * HybsCryptography のメソッドも呼び出せます。 043 * getMD5 : MessageDigestにより、MD5 でハッシュした文字に変換 044 * encrypt : Hybs独自の暗号化を行います(Hybs内部設定の秘密鍵) 045 * decrypt : Hybs独自の復号化を行います(Hybs内部設定の秘密鍵) 046 * 047 * 引数文字列中にスペースを含む場合は、ダブルコーテーション("") で括って下さい。 048 * 引数文字列の 『=』の前後には、スペースは挟めません。必ず、-key=value の様に 049 * 繋げてください。 050 * 051 * @og.formSample 052 * Process_StringUtil -action=getMD5|encrypt|decrypt|code39|getUnicodeEscape|getReplaceEscape|・・・ -keys=AA,BB,CC 053 * 054 * -action=ESC|REV :StringUtilクラスの特定のメソッド名を指定します(必須)。 055 * urlEncode|rTrim|htmlFilter|getMD5|code39|getUnicodeEscape|getReplaceEscape|spanCut 056 * -keys=AA,BB,CC :変換するカラムをCSV形式で複数指定できます(必須)。 057 * [ -display=[false/true]] :結果を標準出力に表示する(true)かしない(false)か(初期値:false[表示しない]) 058 * [ -debug=[false/true] ] :デバッグ情報を標準出力に表示する(true)かしない(false)か(初期値:false[表示しない]) 059 * 060 * @og.rev 5.0.0.2 (2009/09/15) 新規クラス作成 061 * 062 * @version 0.9.0 2004/02/27 063 * @author Kazuhiko Hasegawa 064 * @since JDK5.0, 065 */ 066public class Process_StringUtil extends AbstractProcess implements ChainProcess { 067 068 private static final String STR_ACTION_BASE = "org.opengion.fukurou.process.Process_StringUtil$SU_" ; 069 070 private String keys = null; // 変換するカラム名配列のアドレス 071 private int[] clmNos = null; // 変換するカラム名配列のアドレス 072 private boolean display = false; // 表示しない 073 private boolean debug = false; // 5.7.3.0 (2014/02/07) デバッグ情報 074 075 private boolean firstRow = true; // 最初の一行目 076 private int count = 0; 077 private StrAction stAction = null; // Ver 5.0.0.2 (2009/09/15) 078 079 private static final Map<String,String> mustProparty ; // [プロパティ]必須チェック用 Map 080 private static final Map<String,String> usableProparty ; // [プロパティ]整合性チェック Map 081 082 static { 083 mustProparty = new LinkedHashMap<String,String>(); 084 mustProparty.put( "action", "StringUtilの特定のメソッドを指定します(必須)" + 085 CR + "urlEncode , rTrim , htmlFilter , getMD5 , encrypt , decrypt , code39 , getUnicodeEscape , getReplaceEscape , spanCut" ); 086 087 mustProparty.put( "keys", "変換するカラムをCSV形式で複数指定できます(必須)。" ); 088 089 usableProparty = new LinkedHashMap<String,String>(); 090 usableProparty.put( "display", "結果を標準出力に表示する(true)かしない(false)か" + 091 CR + " (初期値:false[表示しない])" ); 092 usableProparty.put( "debug", "デバッグ情報を標準出力に表示する(true)かしない(false)か" + 093 CR + "(初期値:false:表示しない)" ); // 5.7.3.0 (2014/02/07) デバッグ情報 094 } 095 096 /** 097 * デフォルトコンストラクター。 098 * このクラスは、動的作成されます。デフォルトコンストラクターで、 099 * super クラスに対して、必要な初期化を行っておきます。 100 * 101 */ 102 public Process_StringUtil() { 103 super( "org.opengion.fukurou.process.Process_StringUtil",mustProparty,usableProparty ); 104 } 105 106 /** 107 * プロセスの初期化を行います。初めに一度だけ、呼び出されます。 108 * 初期処理(ファイルオープン、DBオープン等)に使用します。 109 * 110 * @param paramProcess データベースの接続先情報などを持っているオブジェクト 111 */ 112 public void init( final ParamProcess paramProcess ) { 113 Argument arg = getArgument(); 114 115 keys = arg.getProparty( "keys",keys ); 116 display = arg.getProparty( "display",display ); 117 debug = arg.getProparty("debug",debug); // 5.7.3.0 (2014/02/07) デバッグ情報 118// if( debug ) { println( arg.toString() ); } // 5.7.3.0 (2014/02/07) デバッグ情報 119 120 String act = arg.getProparty( "action" ); 121 122 stAction = (StrAction)StringUtil.newInstance( STR_ACTION_BASE + act ); 123 } 124 125 /** 126 * 引数の LineModel を処理するメソッドです。 127 * 変換処理後の LineModel を返します。 128 * 後続処理を行わない場合(データのフィルタリングを行う場合)は、 129 * null データを返します。つまり、null データは、後続処理を行わない 130 * フラグの代わりにも使用しています。 131 * なお、変換処理後の LineModel と、オリジナルの LineModel が、 132 * 同一か、コピー(クローン)かは、各処理メソッド内で決めています。 133 * ドキュメントに明記されていない場合は、副作用が問題になる場合は、 134 * 各処理ごとに自分でコピー(クローン)して下さい。 135 * 136 * @param data オリジナルのLineModel 137 * 138 * @return 処理変換後のLineModel 139 */ 140 public LineModel action( final LineModel data ) { 141 count++ ; 142 try { 143 if( firstRow ) { 144 makeColumnNos( data ); 145 firstRow = false; 146 if( display ) { println( data.nameLine() ); } // 5.7.3.0 (2014/02/07) デバッグ情報 147 } 148 149 if( debug ) { println( "Before:" + data.dataLine() ); } // 5.1.2.0 (2010/01/01) display の条件変更 150 for( int i=0; i<clmNos.length; i++ ) { 151 String val = (String)data.getValue( clmNos[i] ) ; 152 data.setValue( clmNos[i],stAction.change( val ) ); 153 } 154 155// if( display ) { printKey( count,data ); } 156 if( debug ) { println( "After :" + data.dataLine() ); } // 5.1.2.0 (2010/01/01) display の条件変更 157 else if( display ) { println( data.dataLine() ); } // 5.1.2.0 (2010/01/01) display の条件変更 158 } 159 catch( Throwable ex ) { 160 String errMsg = "row=[" + count + "]" + CR + 161 " data=[" + data + "]" + CR ; 162 throw new RuntimeException( errMsg,ex ); 163 } 164 return data; 165 } 166 167 /** 168 * プロセスの終了を行います。最後に一度だけ、呼び出されます。 169 * 終了処理(ファイルクローズ、DBクローズ等)に使用します。 170 * 171 * @param isOK トータルで、OKだったかどうか[true:成功/false:失敗] 172 */ 173 public void end( final boolean isOK ) { 174 keys = null; // 変換するカラム名配列のアドレス 175 clmNos = null; // 変換するカラム名配列のアドレス 176 } 177 178 /** 179 * プロセスの処理結果のレポート表現を返します。 180 * 処理プログラム名、入力件数、出力件数などの情報です。 181 * この文字列をそのまま、標準出力に出すことで、結果レポートと出来るような 182 * 形式で出してください。 183 * 184 * @return 処理結果のレポート 185 */ 186 public String report() { 187 String report = "[" + getClass().getName() + "]" + CR 188 + TAB + "Output Count : " + count ; 189 190 return report ; 191 } 192 193 /** 194 * カラム番号配列を取得します。 195 * 繰返し処理を行う場合に、事前にアドレスでアクセスできるように処理するカラム番号を 196 * キャッシュしておきます。 197 * 198 * @param data LineModelオブジェクト 199 */ 200 private void makeColumnNos( final LineModel data ) { 201 String[] clms = StringUtil.csv2Array( keys ); 202 int size = clms.length; 203 clmNos = new int[size]; 204 for( int i=0; i<size; i++ ) { 205 clmNos[i] = data.getColumnNo( clms[i] ); 206 } 207 } 208 209 /** 210 * 画面出力用のフォーマットを作成します。 211 * 212 * @og.rev 5.7.3.0 (2014/02/07) 表示方法の変更のため、廃止 213 * 214 * @param rowNo データ読み取り件数 215 * @param data LineModelオブジェクト 216 */ 217// private void printKey( final int rowNo , final LineModel data ) { 218// StringBuilder buf = new StringBuilder(); 219// 220// buf.append( "row=[" ).append( rowNo ).append( "] : " ); 221// for( int i=0; i < clmNos.length; i++ ) { 222// buf.append( data.getName( clmNos[i] ) ); 223// buf.append( " ⇒ " ); 224// buf.append( data.getValue( clmNos[i] ) ); 225// buf.append( " , " ); 226// } 227// 228// println( buf.toString() ); 229// } 230 231 /** 232 * このクラスの使用方法を返します。 233 * 234 * @return このクラスの使用方法 235 */ 236 public String usage() { 237 StringBuilder buf = new StringBuilder(); 238 239 buf.append( "Process_StringUtil は、上流から受け取ったデータをStringUtilクラスの特定の" ).append( CR ); 240 buf.append( "メソッドでデータ変換する、CainProcess インターフェースの実装クラスです。" ).append( CR ); 241 buf.append( CR ); 242 buf.append( "上流(プロセスチェインのデータは上流から下流へと渡されます。)から" ).append( CR ); 243 buf.append( " LineModel を元に、指定のカラムの文字を、変換します。" ).append( CR ); 244 buf.append( CR ); 245 buf.append( "現時点で利用できるStringUtil のメソッドは、下記の通りです。" ).append( CR ); 246 buf.append( " urlEncode : UTF-8 で、URLエンコードを行う。" ).append( CR ); 247 buf.append( " rTrim : 文字列の後ろのスペースを削除" ).append( CR ); 248 buf.append( " htmlFilter : HTML上のエスケープ文字を変換" ).append( CR ); 249 buf.append( " code39 : CODE39 の 文字列を作成(チェックデジット付き)" ).append( CR ); 250 buf.append( " getUnicodeEscape : HTML のエスケープ記号(&#xZZZZ;)に変換" ).append( CR ); 251 buf.append( " getReplaceEscape : HTML のエスケープ記号(&#xZZZZ;)を戻す" ).append( CR ); 252 buf.append( " spanCut : 引数からspanタグを取り除く" ).append( CR ); 253 buf.append( CR ); 254 buf.append( "HybsCryptography のメソッドも呼び出せます。" ).append( CR ); 255 buf.append( " getMD5 : MessageDigestにより、MD5 でハッシュした文字に変換" ).append( CR ); 256 buf.append( " encrypt : Hybs独自の暗号化を行います(Hybs内部設定の秘密鍵)" ).append( CR ); 257 buf.append( " decrypt : Hybs独自の復号化を行います(Hybs内部設定の秘密鍵)" ).append( CR ); 258 buf.append( CR ); 259 buf.append( "引数文字列中に空白を含む場合は、ダブルコーテーション(\"\") で括って下さい。" ).append( CR ); 260 buf.append( "引数文字列の 『=』の前後には、空白は挟めません。必ず、-key=value の様に" ).append( CR ); 261 buf.append( "繋げてください。" ).append( CR ); 262 buf.append( CR ).append( CR ); 263 buf.append( getArgument().usage() ).append( CR ); 264 265 return buf.toString(); 266 } 267 268 /** 269 * このクラスは、main メソッドから実行できません。 270 * 271 * @param args コマンド引数配列 272 */ 273 public static void main( final String[] args ) { 274 LogWriter.log( new Process_StringUtil().usage() ); 275 } 276 277 /** 278 * インナークラスとして、共通メソッドを定義します(I/Fの代わり)。 279 */ 280 public static class StrAction { 281 /** 282 * 引数を変換します。 283 * 284 * @param val 引数 285 * @return 変換された文字列 286 */ 287 public String change( final String val ) { 288 return val; 289 } 290 } 291 292 /** 293 * UTF-8 で、URLエンコードを行います。 294 */ 295 public static class SU_urlEncode extends StrAction { 296 /** 297 * 引数を変換します。 298 * 299 * @param val 引数 300 * @return 変換された文字列 301 */ 302 @Override 303 public String change( final String val ) { 304 return StringUtil.urlEncode( val ); 305 } 306 } 307 308 /** 309 * 文字列の後ろのスペースを削除します。 310 * 注意:'\u0020' (スペース文字) より小さい文字を切り取ります。 311 */ 312 public static class SU_rTrim extends StrAction { 313 /** 314 * 引数を変換します。 315 * 316 * @param val 引数 317 * @return 変換された文字列 318 */ 319 @Override 320 public String change( final String val ) { 321 return StringUtil.rTrim( val ); 322 } 323 } 324 325 /** 326 * HTML上のエスケープ文字を変換します。 327 */ 328 public static class SU_htmlFilter extends StrAction { 329 /** 330 * 引数を変換します。 331 * 332 * @param val 引数 333 * @return 変換された文字列 334 */ 335 @Override 336 public String change( final String val ) { 337 return StringUtil.htmlFilter( val ); 338 } 339 } 340 341 /** 342 * CODE39 の 文字列を作成します。(チェックデジット付き) 343 */ 344 public static class SU_code39 extends StrAction { 345 /** 346 * 引数を変換します。 347 * 348 * @param val 引数 349 * @return 変換された文字列 350 */ 351 @Override 352 public String change( final String val ) { 353 return StringUtil.code39( val,true ); 354 } 355 } 356 357 /** 358 * Unicode文字列の値を HTML のエスケープ記号(&#xZZZZ;)に変換します。 359 */ 360 public static class SU_getUnicodeEscape extends StrAction { 361 /** 362 * 引数を変換します。 363 * 364 * @param val 引数 365 * @return 変換された文字列 366 */ 367 @Override 368 public String change( final String val ) { 369 return StringUtil.getUnicodeEscape( val ); 370 } 371 } 372 373 /** 374 * HTML のエスケープ記号(&#xZZZZ;)をUnicode文字列に戻します。 375 */ 376 public static class SU_getReplaceEscape extends StrAction { 377 /** 378 * 引数を変換します。 379 * 380 * @param val 引数 381 * @return 変換された文字列 382 */ 383 @Override 384 public String change( final String val ) { 385 return StringUtil.getReplaceEscape( val ); 386 } 387 } 388 389 /** 390 * 引数からspanタグを取り除いて返します。 391 */ 392 public static class SU_spanCut extends StrAction { 393 /** 394 * 引数を変換します。 395 * 396 * @param val 引数 397 * @return 変換された文字列 398 */ 399 @Override 400 public String change( final String val ) { 401 return StringUtil.spanCut( val ); 402 } 403 } 404 405 /** 406 * MessageDigestにより、MD5 でハッシュした文字に変換します。 407 * 408 * @og.rev 5.2.2.0 (2010/11/01) util.StringUtil から security.HybsCryptography へ移動 409 * 410 */ 411 public static class SU_getMD5 extends StrAction { 412 /** 413 * 引数を変換します。 414 * 415 * @param val 引数 416 * @return 変換された文字列 417 */ 418 @Override 419 public String change( final String val ) { 420 return HybsCryptography.getMD5( val ); 421 } 422 } 423 424 /** 425 * Hybs独自の暗号化を行います(Hybs内部設定の秘密鍵) 426 * 427 * @og.rev 5.2.2.0 (2010/11/01) 新規追加 428 */ 429 public static class SU_encrypt extends StrAction { 430 private HybsCryptography crpt = null; 431 432 /** 433 * 引数を変換します。 434 * 435 * @param val 引数 436 * @return 変換された文字列 437 */ 438 @Override 439 public String change( final String val ) { 440 if( crpt == null ) { 441 crpt = new HybsCryptography(); 442 } 443 return crpt.encrypt( val ); 444 } 445 } 446 447 /** 448 * Hybs独自の復号化を行います(Hybs内部設定の秘密鍵) 449 * 450 * @og.rev 5.2.2.0 (2010/11/01) 新規追加 451 */ 452 public static class SU_decrypt extends StrAction { 453 private HybsCryptography crpt = null; 454 455 /** 456 * 引数を変換します。 457 * 458 * @param val 引数 459 * @return 変換された文字列 460 */ 461 @Override 462 public String change( final String val ) { 463 if( crpt == null ) { 464 crpt = new HybsCryptography(); 465 } 466 return crpt.decrypt( val ); 467 } 468 } 469}