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             * @og.rev 5.7.2.2 (2014/01/24) エラー時に??タも?力します?
194             *
195             * @param       data    オリジナルのLineModel
196             *
197             * @return      処?換後?LineModel
198             */
199            public LineModel action( final LineModel data ) {
200                    inCount++ ;
201                    final FileLineModel fileData ;
202                    if( data instanceof FileLineModel ) {
203                            fileData = (FileLineModel)data ;
204                    }
205                    else {
206                            // LineModel ?FileLineModel でな??合?オブジェクトを作?します?
207                            fileData = new FileLineModel( data );
208    //                      String errMsg = "??タ?FileLineModel オブジェクトではありません? + CR ;
209    //                      throw new RuntimeException( errMsg );
210                    }
211    
212                    File inFile = fileData.getFile() ;
213                    if( ! inFile.isFile() ) {
214                            if( display ) { println( data.dataLine() ); }           // 5.1.2.0 (2010/01/01) display の条件変更
215                            return data;
216                    }
217    
218                    // ファイル名を作?します?
219                    // ファイル名?、引数ファイル?から、inPath を引き、outPath を加えます?
220                    File outFile = new File( outPath, inFile.getAbsolutePath().substring( inPathLen ) );
221                    fileData.setFile( outFile );
222    
223    //              if( display ) { println( inFile + " => " + outFile ); }
224    
225                    // 入出力が異なる??
226                    if( !isEquals ) {
227                            tempFile = outFile;
228                            File parent = outFile.getParentFile();
229                            if( parent != null && ! parent.exists() && !parent.mkdirs() ) {
230                                    String errMsg = "??フォル?作?できませんでした?" + parent + "]" + CR
231                                                            + " inCount=[" + inCount + "]件" + CR
232                                                            + " data=[" + data.dataLine() + "]" + CR ;              // 5.7.2.2 (2014/01/24) エラー時に??タも?力します?
233                                    throw new RuntimeException( errMsg );
234                            }
235                    }
236    
237                    if( binary ) {
238    //                      FileUtil.copy( inFile,tempFile );
239    //                      FileUtil.copy( inFile,tempFile,changeCrLf );    // 4.2.2.0 (2008/05/10)
240                            // 5.1.6.0 (2010/05/01) changeCrLfcopy 対?
241                            if( changeCrLf ) { FileUtil.changeCrLfcopy( inFile,tempFile ); }
242                            else             { FileUtil.copy( inFile,tempFile,keepTimeStamp ); }
243                    }
244                    else {
245                            BufferedReader reader = FileUtil.getBufferedReader( inFile ,inEncode  );
246                            PrintWriter    writer = FileUtil.getPrintWriter( tempFile  ,outEncode );
247    
248                            try {
249                                    String line1;
250                                    while((line1 = reader.readLine()) != null) {
251                                            writer.println( line1 );
252                                    }
253                            }
254                            catch( IOException ex ) {
255                                    String errMsg = "ファイルコピ?中に例外が発生しました?" + data.getRowNo() + "]件目" + CR
256                                                            + " inFile=[" + inFile + "] , tempFile=[" + tempFile + "]" + CR
257                                                            + " data=[" + data.dataLine() + "]" + CR ;              // 5.7.2.2 (2014/01/24) エラー時に??タも?力します?
258                                    throw new RuntimeException( errMsg,ex );
259                            }
260                            finally {
261                                    Closer.ioClose( reader ) ;
262                                    Closer.ioClose( writer ) ;
263                            }
264                    }
265    
266                    if( isEquals ) {
267                            if( !outFile.delete() ) {
268                                    String errMsg = "??ファイルを削除できませんでした?" + outFile + "]" + CR
269                                                            + " inCount=[" + inCount + "]件" + CR
270                                                            + " data=[" + data.dataLine() + "]" + CR ;              // 5.7.2.2 (2014/01/24) エラー時に??タも?力します?
271                                    throw new RuntimeException( errMsg );
272                            }
273    
274                            if( !tempFile.renameTo( outFile ) ) {
275                                    String errMsg = "??ファイルをリネ??きませんでした?" + tempFile + "]" + CR
276                                                            + " inCount=[" + inCount + "]件" + CR
277                                                            + " data=[" + data.dataLine() + "]" + CR ;              // 5.7.2.2 (2014/01/24) エラー時に??タも?力します?
278                                    throw new RuntimeException( errMsg );
279                            }
280                    }
281    
282                    // 5.1.5.0 (2010/04/01) keepTimeStamp 属?の追?
283                    if( keepTimeStamp ) {
284                            if( !outFile.setLastModified( inFile.lastModified() ) ) {
285                                    String errMsg = "lastModified 時間の設定が、できませんでした?" + outFile + "]" + CR
286                                                            + " inCount=[" + inCount + "]件" + CR
287                                                            + " data=[" + data.dataLine() + "]" + CR ;              // 5.7.2.2 (2014/01/24) エラー時に??タも?力します?
288                                    throw new RuntimeException( errMsg );
289                            }
290                    }
291    
292                    if( display ) { println( data.dataLine() ); }           // 5.1.2.0 (2010/01/01) display の条件変更
293                    return data ;
294            }
295    
296            /**
297             * プロセスの処?果のレポ?ト表現を返します?
298             * 処??ログラ?、?力件数、?力件数などの??です?
299             * こ???をそのまま、標準?力に出すことで、結果レポ?トと出来るよ?
300             * 形式で出してください?
301             *
302             * @return   処?果のレポ??
303             */
304            public String report() {
305                    String report = "[" + getClass().getName() + "]" + CR
306                                    + TAB + "Copy Count : " + inCount   + CR
307                                    + TAB + "inPath     : " + inPath    + CR
308                                    + TAB + "inEncode   : " + inEncode  + CR
309                                    + TAB + "outPath    : " + outPath   + CR
310                                    + TAB + "outEncode  : " + outEncode + CR
311                                    + TAB + "binary     : " + binary ;
312    
313                    return report ;
314            }
315    
316            /**
317             * こ?クラスの使用方法を返します?
318             *
319             * @return      こ?クラスの使用方?
320             */
321            public String usage() {
322                    StringBuilder buf = new StringBuilder();
323    
324                    buf.append( "Process_FileCopy は、上流から受け取っ?FileLineModelを??る?"                                ).append( CR );
325                    buf.append( "ChainProcess インターフェースの実?ラスです?"                                                              ).append( CR );
326                    buf.append( CR );
327                    buf.append( "上流から受け取っ?FileLineModel の ファイルから、inPath の共通パス"                       ).append( CR );
328                    buf.append( "以下?ファイルを?outPath の共通パス以下にコピ?します?"                                          ).append( CR );
329                    buf.append( "コピ?の種類?、バイナリか??ストで、テキスト?場合?、エンコー?                  ).append( CR );
330                    buf.append( "変換も行うことが可能です?"                                                                                                     ).append( CR );
331                    buf.append( "inPath と outPath が同じ?また?、outPath が未設定?場合?、?力と出力が"            ).append( CR );
332                    buf.append( "同じです?で、???身のエンコード変換処?行うことになります?"                            ).append( CR );
333                    buf.append( CR );
334                    buf.append( "コピ?されるファイルのファイル名?、?力ファイル名と同?す?保存される"              ).append( CR );
335                    buf.append( "フォル?異なります?(同?することも可能です?)"                                                         ).append( CR );
336                    buf.append( CR );
337                    buf.append( "上流?ロセスでは、Name 属?として、?File』を持ち、?は、Fileオブジェク?    ).append( CR );
338                    buf.append( "である、Process_FileSearch を使用するのが?便利です?それ以外?クラス?           ).append( CR );
339                    buf.append( "使用する場合でも?Name属?と、File オブジェクトを持つ LineModel を受け渡?  ).append( CR );
340                    buf.append( "できれば、使用可能です?"                                                                                                            ).append( CR );
341                    buf.append( CR );
342                    buf.append( "引数??中に空白を含??合?、ダブルコー??ション(\"\") で括って下さ??"         ).append( CR );
343                    buf.append( "引数??の ?』?前後には、空白は挟めません。??key=value の様に"             ).append( CR );
344                    buf.append( "繋げてください?                                                                                                                              ).append( CR );
345                    buf.append( CR ).append( CR );
346    
347                    buf.append( getArgument().usage() ).append( CR );
348    
349                    return buf.toString();
350            }
351    
352            /**
353             * こ?クラスは、main メソ?から実行できません?
354             *
355             * @param       args    コマンド引数配?
356             */
357            public static void main( final String[] args ) {
358                    LogWriter.log( new Process_FileCopy().usage() );
359            }
360    }