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 */ 016 package org.opengion.fukurou.process; 017 018 import org.opengion.fukurou.util.Argument; 019 import org.opengion.fukurou.util.FileUtil; 020 import org.opengion.fukurou.util.Closer ; 021 import org.opengion.fukurou.util.LogWriter; 022 023 import java.util.Map ; 024 import java.util.LinkedHashMap ; 025 026 import java.io.File; 027 import java.io.PrintWriter; 028 import java.io.BufferedReader; 029 import java.io.IOException; 030 031 /** 032 * Process_FileCopy は、上流から受け取っ?FileLineModel を??る? 033 * ChainProcess インターフェースの実?ラスです? 034 * 035 * 上流から受け取っ?FileLineModel の ファイルから、inPath の共通パス 036 * 以下?ファイルを?outPath の共通パス以下にコピ?します? 037 * コピ?の種類?、バイナリか??ストで、テキスト?場合?、エンコー? 038 * 変換も行うことが可能です? 039 * inPath と outPath が同じ?また?、outPath が未設定?場合?、?力と出力が 040 * 同じです?で、???身のエンコード変換処?行うことになります? 041 * 042 * コピ?されるファイルのファイル名?、?力ファイル名と同?す?保存される 043 * フォル?異なります?(同?することも可能です?) 044 * 045 * 上流?ロセスでは、Name 属?として、?File』を持ち、?は、Fileオブジェク? 046 * である、Process_FileSearch を使用するのが?便利です?それ以外?クラス? 047 * 使用する場合でも?Name属?と、File オブジェクトを持つ LineModel を受け渡? 048 * できれば、使用可能です? 049 * 050 * 引数??中に空白を含??合?、ダブルコー??ション("") で括って下さ?? 051 * 引数??の ?』?前後には、空白は挟めません。??key=value の様に 052 * 繋げてください? 053 * 054 * @og.formSample 055 * Process_FileCopy -inPath=入力?通パス -inEncode=Windows-31J -outPath=出力?通パス -outEncode=UTF-8 056 * 057 * -inPath=入力?通パス ?上流で検索されたファイルパスの共通部? 058 * [-inEncode=入力エンコー? ] ??力ファイルのエンコードタイ? 059 * [-outPath=出力?通パス ] ??力するファイルパスの共通部? 060 * [-outEncode=出力エンコー? ] ??力ファイルのエンコードタイ? 061 * [-binary=[false/true] ] ?trueは、バイナリファイルのコピ?(初期値:false) 062 * [-changeCrLf=[false/true] ] ?trueは、バイナリファイルのコピ?時にCR+LFに変換しま?初期値:false) 063 * [-keepTimeStamp=[false/true]] ?trueは、コピ???ファイルのタイ?タンプで作?しま?初期値:false) 064 * [-display=[false/true] ] ?trueは、コピ?状況を表示しま?初期値:false) 065 * 066 * @version 4.0 067 * @author Kazuhiko Hasegawa 068 * @since JDK5.0, 069 */ 070 public class Process_FileCopy extends AbstractProcess implements ChainProcess { 071 private File tempFile = null; 072 073 private String inPath = null; 074 private String inEncode = null; 075 private String outPath = null; 076 private String outEncode = null; 077 private boolean binary = false; 078 private boolean changeCrLf = false; // 4.2.2.0 (2008/05/10) 079 private boolean keepTimeStamp = false; // 5.1.5.0 (2010/04/01) 080 private boolean display = false; 081 082 private int inPathLen = 0; 083 private boolean isEquals = false; 084 private int inCount = 0; 085 086 private static final Map<String,String> mustProparty ; // ?プロパティ???チェ?用 Map 087 private static final Map<String,String> usableProparty ; // ?プロパティ?整合?チェ? Map 088 089 static { 090 mustProparty = new LinkedHashMap<String,String>(); 091 mustProparty.put( "inPath", "コピ???ファイル基準パス" ); 092 093 usableProparty = new LinkedHashMap<String,String>(); 094 usableProparty.put( "inEncode", "コピ???ファイルのエンコードタイ? ); 095 usableProparty.put( "outPath", "コピ?先?ファイル基準パス" ); 096 usableProparty.put( "outEncode", "コピ?先?ファイルのエンコードタイ? ); 097 usableProparty.put( "binary", "trueは、バイナリファイルをコピ?しま?初期値:false)" ); 098 usableProparty.put( "changeCrLf", "trueは、バイナリファイルのコピ?時にCR+LFに変換しま?初期値:false)" ); // 4.2.2.0 (2008/05/10) 099 usableProparty.put( "keepTimeStamp", "trueは、コピ???ファイルのタイ?タンプで作?しま?初期値:false)" ); // 5.1.5.0 (2010/04/01) 100 usableProparty.put( "display", "trueは、コピ?状況を表示しま?初期値:false)" ); 101 } 102 103 /** 104 * ?ォルトコンストラクター? 105 * こ?クラスは、動??されます??ォルトコンストラクターで? 106 * super クラスに対して、?な初期化を行っておきます? 107 * 108 */ 109 public Process_FileCopy() { 110 super( "org.opengion.fukurou.process.Process_FileCopy",mustProparty,usableProparty ); 111 } 112 113 /** 114 * プロセスの初期化を行います?初めに??、呼び出されます? 115 * 初期処?ファイルオープン??オープン?に使用します? 116 * 117 * @og.rev 4.2.2.0 (2008/05/10) changeCrLf 属?対? 118 * @og.rev 5.1.5.0 (2010/04/01) keepTimeStamp 属?の追? 119 * 120 * @param paramProcess ??タベ?スの接続???などを持って?オブジェク? 121 */ 122 public void init( final ParamProcess paramProcess ) { 123 Argument arg = getArgument(); 124 125 inPath = arg.getProparty("inPath" ); 126 outPath = arg.getProparty("outPath" ); 127 inEncode = arg.getProparty("inEncode" ,System.getProperty("file.encoding")); 128 outEncode = arg.getProparty("outEncode",System.getProperty("file.encoding")); 129 binary = arg.getProparty("binary" ,binary); 130 changeCrLf = arg.getProparty("changeCrLf" ,changeCrLf); // 4.2.2.0 (2008/05/10) 131 keepTimeStamp = arg.getProparty("keepTimeStamp" ,keepTimeStamp); // 5.1.5.0 (2010/04/01) 132 display = arg.getProparty("display",display); 133 134 inPathLen = inPath.length(); 135 136 // 入力と出力が同じか? 137 isEquals = outPath == null || inPath.equalsIgnoreCase( outPath ); 138 139 if( binary ) { 140 // 4.2.2.0 (2008/05/10) 判定ミスの修正 141 if( ! inEncode.equalsIgnoreCase( outEncode ) ) { 142 String errMsg = "バイナリコピ?時には、?出力?エンコード?同じ?があります?" + CR 143 + " inEncode=[" + inEncode + "] , outEncode=[" + outEncode + "]" ; 144 throw new RuntimeException( errMsg ); 145 } 146 if( isEquals ) { 147 String errMsg = "入出力が同じファイルのバイナリコピ?はできません? + CR 148 + " inPath=[" + inPath + "] , outPath=[" + outPath + "]" ; 149 throw new RuntimeException( errMsg ); 150 } 151 } 152 153 // 入力と出力が同じ場合?、中間ファイルを作?します? 154 if( isEquals ) { 155 try { 156 tempFile = File.createTempFile( "X", ".tmp", new File( outPath ) ); 157 tempFile.deleteOnExit(); 158 } 159 catch( IOException ex ) { 160 String errMsg = "中間ファイル作?でエラーが発生しました? + CR 161 + " outPath=[" + outPath + "]" ; 162 throw new RuntimeException( errMsg,ex ); 163 } 164 } 165 } 166 167 /** 168 * プロセスの終?行います??に??、呼び出されます? 169 * 終???ファイルクローズ??クローズ?に使用します? 170 * 171 * @param isOK ト?タルで、OK?たかど?[true:成功/false:失敗] 172 */ 173 public void end( final boolean isOK ) { 174 tempFile = null; 175 } 176 177 /** 178 * 引数の LineModel を??るメソ?です? 179 * 変換処?? LineModel を返します? 180 * 後続??行わな?????タのフィルタリングを行う場?は? 181 * null ??タを返します?つまり?null ??タは、後続??行わな? 182 * フラグの代わりにも使用して?す? 183 * なお?変換処?? LineModel と、オリジナルの LineModel が? 184 * 同?、コピ?(クローン)か?、各処?ソ??決めて?す? 185 * ドキュメントに明記されて???合?、副作用が問題になる?合?? 186 * ???とに自?コピ?(クローン)して下さ?? 187 * 188 * @og.rev 4.0.0.0 (2007/11/28) メソ?の戻り?をチェ?します? 189 * @og.rev 4.2.2.0 (2008/05/10) changeCrLf 属?対? 190 * @og.rev 4.2.3.0 (2008/05/26) LineModel ?FileLineModel でな??合?処? 191 * @og.rev 5.1.5.0 (2010/04/01) keepTimeStamp 属?の追? 192 * @og.rev 5.1.6.0 (2010/05/01) changeCrLf 属?が?.FileUtil#changeCrLfcopy メソ?への移動に伴?? 193 * 194 * @param data オリジナルのLineModel 195 * 196 * @return 処?換後?LineModel 197 */ 198 public LineModel action( final LineModel data ) { 199 inCount++ ; 200 final FileLineModel fileData ; 201 if( data instanceof FileLineModel ) { 202 fileData = (FileLineModel)data ; 203 } 204 else { 205 // LineModel ?FileLineModel でな??合?オブジェクトを作?します? 206 fileData = new FileLineModel( data ); 207 // String errMsg = "??タ?FileLineModel オブジェクトではありません? + CR ; 208 // throw new RuntimeException( errMsg ); 209 } 210 211 File inFile = fileData.getFile() ; 212 if( ! inFile.isFile() ) { 213 if( display ) { println( data.dataLine() ); } // 5.1.2.0 (2010/01/01) display の条件変更 214 return data; 215 } 216 217 // ファイル名を作?します? 218 // ファイル名?、引数ファイル?から、inPath を引き、outPath を加えます? 219 File outFile = new File( outPath, inFile.getAbsolutePath().substring( inPathLen ) ); 220 fileData.setFile( outFile ); 221 222 // if( display ) { println( inFile + " => " + outFile ); } 223 224 // 入出力が異なる?? 225 if( !isEquals ) { 226 tempFile = outFile; 227 File parent = outFile.getParentFile(); 228 if( parent != null && ! parent.exists() && !parent.mkdirs() ) { 229 String errMsg = "??フォル?作?できませんでした?" + parent + "]" ; 230 throw new RuntimeException( errMsg ); 231 } 232 } 233 234 if( binary ) { 235 // FileUtil.copy( inFile,tempFile ); 236 // FileUtil.copy( inFile,tempFile,changeCrLf ); // 4.2.2.0 (2008/05/10) 237 // 5.1.6.0 (2010/05/01) changeCrLfcopy 対? 238 if( changeCrLf ) { FileUtil.changeCrLfcopy( inFile,tempFile ); } 239 else { FileUtil.copy( inFile,tempFile,keepTimeStamp ); } 240 } 241 else { 242 BufferedReader reader = FileUtil.getBufferedReader( inFile ,inEncode ); 243 PrintWriter writer = FileUtil.getPrintWriter( tempFile ,outEncode ); 244 245 try { 246 String line1; 247 while((line1 = reader.readLine()) != null) { 248 writer.println( line1 ); 249 } 250 } 251 catch( IOException ex ) { 252 String errMsg = "ファイルコピ?中に例外が発生しました?" + data.getRowNo() + "]件目" + CR 253 + " inFile=[" + inFile + "] , tempFile=[" + tempFile + "]" ; 254 throw new RuntimeException( errMsg,ex ); 255 } 256 finally { 257 Closer.ioClose( reader ) ; 258 Closer.ioClose( writer ) ; 259 } 260 } 261 262 if( isEquals ) { 263 if( !outFile.delete() ) { 264 String errMsg = "??ファイルを削除できませんでした?" + outFile + "]" ; 265 throw new RuntimeException( errMsg ); 266 } 267 268 if( !tempFile.renameTo( outFile ) ) { 269 String errMsg = "??ファイルをリネ??きませんでした?" + tempFile + "]" ; 270 throw new RuntimeException( errMsg ); 271 } 272 } 273 274 // 5.1.5.0 (2010/04/01) keepTimeStamp 属?の追? 275 if( keepTimeStamp ) { 276 if( !outFile.setLastModified( inFile.lastModified() ) ) { 277 String errMsg = "lastModified 時間の設定が、できませんでした?" + outFile + "]" ; 278 throw new RuntimeException( errMsg ); 279 } 280 } 281 282 if( display ) { println( data.dataLine() ); } // 5.1.2.0 (2010/01/01) display の条件変更 283 return data ; 284 } 285 286 /** 287 * プロセスの処?果のレポ?ト表現を返します? 288 * 処??ログラ?、?力件数、?力件数などの??です? 289 * こ???をそのまま、標準?力に出すことで、結果レポ?トと出来るよ? 290 * 形式で出してください? 291 * 292 * @return 処?果のレポ?? 293 */ 294 public String report() { 295 String report = "[" + getClass().getName() + "]" + CR 296 + TAB + "Copy Count : " + inCount + CR 297 + TAB + "inPath : " + inPath + CR 298 + TAB + "inEncode : " + inEncode + CR 299 + TAB + "outPath : " + outPath + CR 300 + TAB + "outEncode : " + outEncode + CR 301 + TAB + "binary : " + binary ; 302 303 return report ; 304 } 305 306 /** 307 * こ?クラスの使用方法を返します? 308 * 309 * @return こ?クラスの使用方? 310 */ 311 public String usage() { 312 StringBuilder buf = new StringBuilder(); 313 314 buf.append( "Process_FileCopy は、上流から受け取っ?FileLineModelを??る?" ).append( CR ); 315 buf.append( "ChainProcess インターフェースの実?ラスです?" ).append( CR ); 316 buf.append( CR ); 317 buf.append( "上流から受け取っ?FileLineModel の ファイルから、inPath の共通パス" ).append( CR ); 318 buf.append( "以下?ファイルを?outPath の共通パス以下にコピ?します?" ).append( CR ); 319 buf.append( "コピ?の種類?、バイナリか??ストで、テキスト?場合?、エンコー? ).append( CR ); 320 buf.append( "変換も行うことが可能です?" ).append( CR ); 321 buf.append( "inPath と outPath が同じ?また?、outPath が未設定?場合?、?力と出力が" ).append( CR ); 322 buf.append( "同じです?で、???身のエンコード変換処?行うことになります?" ).append( CR ); 323 buf.append( CR ); 324 buf.append( "コピ?されるファイルのファイル名?、?力ファイル名と同?す?保存される" ).append( CR ); 325 buf.append( "フォル?異なります?(同?することも可能です?)" ).append( CR ); 326 buf.append( CR ); 327 buf.append( "上流?ロセスでは、Name 属?として、?File』を持ち、?は、Fileオブジェク? ).append( CR ); 328 buf.append( "である、Process_FileSearch を使用するのが?便利です?それ以外?クラス? ).append( CR ); 329 buf.append( "使用する場合でも?Name属?と、File オブジェクトを持つ LineModel を受け渡? ).append( CR ); 330 buf.append( "できれば、使用可能です?" ).append( CR ); 331 buf.append( CR ); 332 buf.append( "引数??中に空白を含??合?、ダブルコー??ション(\"\") で括って下さ??" ).append( CR ); 333 buf.append( "引数??の ?』?前後には、空白は挟めません。??key=value の様に" ).append( CR ); 334 buf.append( "繋げてください? ).append( CR ); 335 buf.append( CR ).append( CR ); 336 337 buf.append( getArgument().usage() ).append( CR ); 338 339 return buf.toString(); 340 } 341 342 /** 343 * こ?クラスは、main メソ?から実行できません? 344 * 345 * @param args コマンド引数配? 346 */ 347 public static void main( final String[] args ) { 348 LogWriter.log( new Process_FileCopy().usage() ); 349 } 350 }