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.hayabusa.taglib;
017
018import org.opengion.fukurou.system.OgRuntimeException ;         // 6.4.2.0 (2016/01/29)
019import org.opengion.hayabusa.common.HybsSystem;
020import org.opengion.hayabusa.common.HybsSystemException;
021import org.opengion.fukurou.util.FileUtil;
022import org.opengion.fukurou.util.StringUtil ;
023import org.opengion.fukurou.util.ToString;                                              // 6.1.1.0 (2015/01/17)
024import org.opengion.fukurou.util.ZipArchive;                                    // 5.9.2.2 (2015/11/12)
025import org.opengion.fukurou.util.ArraySet;                                              // 6.4.3.4 (2016/03/11)
026import org.opengion.fukurou.model.POIUtil;                                              // 6.2.3.0 (2015/05/01)
027
028import org.opengion.hayabusa.io.HybsFileOperationFactory;               // 8.0.0.1 (2021/10/08)
029import org.opengion.fukurou.model.FileOperation;                                // 8.0.0.1 (2021/10/08)
030
031import static org.opengion.fukurou.util.StringUtil.nval ;
032
033import java.io.File;
034import java.io.IOException;
035import java.util.List;                                                                                  // 6.2.3.0 (2015/05/01)
036import java.util.Set;                                                                                   // 6.4.3.4 (2016/03/11)
037import java.util.ArrayList;                                                                             // 6.2.3.0 (2015/05/01)
038import java.util.Locale ;
039
040import jakarta.servlet.ServletContext;                                                  // 6.3.6.1 (2015/08/28)
041
042/**
043 * 各種アクションを指定して、ファイル関連の操作をおこなうタグです。
044 *
045 * 各種アクション に応じた振る舞いを行います。
046 * 結果については、false の場合は、body 要素を表示して、終了します。
047 * これは、BODYにエラーメッセージを書いておくことを想定した作りになっています。
048
049 * 判定結果を反転したい場合は、notEquals 属性を使用してください。また、
050 * 結果に応じて、処理を止めたくない場合は、useStop 属性を false に指定することで、
051 * 後続処理を実行できます。
052 *
053 * [各種アクション]
054 *       canRead          読み込めるかどうかを判定。
055 *       canWrite         変更できるかどうか判定。
056 *       createNewFile    空の新しいファイルを不可分 (atomic) に生成。(そのファイルがまだ存在しない場合だけ)
057 *       delete           ファイルまたはディレクトリを削除(ディレクトリ階層をすべて削除)。
058 *       renameTo         ファイルまたはディレクトリ名を変更。
059 *       exists           ファイルが存在するかどうか判定。
060 *       isDirectory      ファイルがディレクトリであるかどうか判定。
061 *       isFile           ファイルが普通のファイルかどうか判定。
062 *       isHidden         ファイルが隠しファイルかどうか判定。
063 *       mkdir            ディレクトリを生成。
064 *       mkdirs           ディレクトリを複数生成。
065 *       read             ファイルを読み込んで、パラメータにセットします(6.2.3.0 (2015/05/01))。
066 *       existsLength     ファイルサイズが0Byte以上のファイルが存在するかどうか判定。
067 *       copy             ファイルまたはディレクトリをコピー(file1 ⇒ file2 にコピー)。
068 *       copyST           ストリームファイルをコピー(file1(アドレス) ⇒ file2 にコピー)。
069 *       list             ファイルリストをListオブジェクトにコピーして、パラメータにセットします(6.2.3.0 (2015/05/01))。
070 *       zip              ファイルまたはディレクトリをZIPファイルに圧縮します。(file1⇒file2)
071 *       timeStamp        ファイルのタイムスタンプを現在時刻に変更します。(file1のみ)
072 *
073 * @og.formSample
074 * ●形式:<og:file action="…" fileURL="…" >・・・</og:file>
075 * ●body:あり(EVAL_BODY_INCLUDE:BODYをインクルードし、{@XXXX} は解析しません)
076 *
077 * ●Tag定義:
078 *   <og:file
079 *       action           ○【TAG】アクション(canRead,canWrite,createNewFile,delete,renameTo,exists,isDirectory,isFile,isHidden,mkdir,mkdirs,read,existsLength,copy,copyST,list,zip,timeStamp)を指定します(必須)。
080 *       fileURL            【TAG】操作するファイルのディレクトリを指定します (初期値:FILE_URL[=filetemp/])
081 *       file1              【TAG】基準となるファイル名を指定します(コマンドの左辺のファイル名です)
082 *       file2              【TAG】処理結果となるファイル名を指定します(コマンドの右辺のファイル名です)
083 *       notEquals          【TAG】判定結果を反転させるかどうか[true/false]を指定します(初期値:false)
084 *       useStop            【TAG】エラー時BODYを処理後に停止するかどうか[true/false]を指定します(初期値:true)
085 *       encode             【TAG】ファイルを読み込む(action="read")際のエンコードを指定します(初期値:OS依存文字コード)
086 *       fromStorage        【TAG】テンプレートファイルの読み込み元ストレージタイプを指定します。8.0.0.1 (2021/10/08)
087 *       fromBucket         【TAG】テンプレートファイルの読み込み元バケット名を指定します。8.0.0.1 (2021/10/08)
088 *       toStorage          【TAG】生成したファイルの出力先ストレージタイプを指定します。8.0.0.1 (2021/10/08)
089 *       toBucket           【TAG】生成したファイルの出力先バケット名を指定します。8.0.0.1 (2021/10/08)
090 *       useLocal           【TAG】システム定数でクラウド設定されていても、クラウド環境を使用しない場合、trueを指定します(初期値:false) 8.0.1.0 (2021/10/29)
091 *       caseKey            【TAG】このタグ自体を利用するかどうかの条件キーを指定します(初期値:null) 5.7.7.2 (2014/06/20)
092 *       caseVal            【TAG】このタグ自体を利用するかどうかの条件値を指定します(初期値:null) 5.7.7.2 (2014/06/20)
093 *       caseNN             【TAG】指定の値が、null/ゼロ文字列 でない場合(Not Null=NN)は、このタグは使用されます(初期値:判定しない) 5.7.7.2 (2014/06/20)
094 *       caseNull           【TAG】指定の値が、null/ゼロ文字列 の場合は、このタグは使用されます(初期値:判定しない) 5.7.7.2 (2014/06/20)
095 *       caseIf             【TAG】指定の値が、true/TRUE文字列の場合は、このタグは使用されます(初期値:判定しない)
096 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
097 *   >   ... Body ...
098 *   </og:file>
099 *
100 * ●使用例
101 *    ・ファイルの存在チェック→存在しなければエラーメッセージを表示。
102 *        <og:file action="exists" fileURL="N:/CIR/" file1="{@USER.LKISB}/{@USER.LDNO1KAI}.cir/001.sht">
103 *            <og:message lbl="RKE_0157" comment="回路図が存在しません。" />
104 *        </og:file>
105 *
106 *    ・N:/Filetemp/にユーザーディレクトリが存在しなければ作成。→失敗した場合エラーメッセージを表示。
107 *        <og:file action="mkdir" fileURL="N:/Filetemp/{@USER.ID}" >
108 *            <og:message comment="エラーが発生しました。システム管理者に連絡してください。" />
109 *        </og:file>
110 *
111 *    ・N:/Filetemp/test.txt ファイルの削除。ファイルが存在しなくても処理を続ける。
112 *        <og:file action="delete" fileURL="N:/Filetemp/" file1="test.txt" useStop="false" >
113 *            <og:message comment="ファイルは存在しませんでした。" />
114 *        </og:file>
115 *
116 * @og.group その他部品
117 *
118 * @version  4.0
119 * @author       Kazuhiko Hasegawa
120 * @since    JDK5.0,
121 */
122public class FileTag extends CommonTagSupport {
123        /** このプログラムのVERSION文字列を設定します。 {@value} */
124        private static final String VERSION = "8.0.1.0 (2021/10/29)" ;
125        private static final long serialVersionUID = 801020211029L ;
126
127        /** action 引数に渡す事の出来る アクションコマンド  読み込めるかどうか {@value} */
128        public static final String ACT_CANREAD  = "canRead" ;
129        /** action 引数に渡す事の出来る アクションコマンド  変更できるかどうか {@value} */
130        public static final String ACT_CANWRITE    = "canWrite" ;
131        /** action 引数に渡す事の出来る アクションコマンド  空の新しいファイルを不可分 (atomic) に生成します (そのファイルがまだ存在しない場合だけ {@value} */
132        public static final String ACT_CREATENEWFILE    = "createNewFile" ;
133        /** action 引数に渡す事の出来る アクションコマンド  ファイルまたはディレクトリを削除{@value}     */
134        public static final String ACT_DELETE    = "delete" ;
135        /** action 引数に渡す事の出来る アクションコマンド  ファイルが存在するかどうか {@value} */
136        public static final String ACT_EXISTS    = "exists" ;
137        /** action 引数に渡す事の出来る アクションコマンド  ファイルがディレクトリであるかどうか{@value}   */
138        public static final String ACT_ISDIRECTORY        = "isDirectory" ;
139        /** action 引数に渡す事の出来る アクションコマンド  ファイルが普通のファイルかどうか{@value}      */
140        public static final String ACT_ISFILE    = "isFile" ;
141        /** action 引数に渡す事の出来る アクションコマンド  ファイルが隠しファイルかどうか {@value} */
142        public static final String ACT_ISHIDDEN    = "isHidden" ;
143        /** action 引数に渡す事の出来る アクションコマンド  ディレクトリを生成します。 {@value} */
144        public static final String ACT_MKDIR    = "mkdir" ;
145        /** action 引数に渡す事の出来る アクションコマンド  ディレクトリを生成します。 {@value} */
146        public static final String ACT_MKDIRS   = "mkdirs" ;
147        /** action 引数に渡す事の出来る アクションコマンド  ファイル名を変更します。 {@value} */
148        public static final String ACT_RENAMETO = "renameTo" ;          // 3.5.6.5 (2004/08/09)
149        /** action 引数に渡す事の出来る アクションコマンド  ファイルを読み込んで表示します。 {@value} */
150        public static final String ACT_READ     = "read" ;              // 3.6.0.0 (2004/09/25)
151        /** action 引数に渡す事の出来る アクションコマンド  ファイルサイズが0Byte以上のファイルが存在するかどうか判定。 {@value} */
152        public static final String ACT_EXISTSLENGTH      = "existsLength" ;             // 3.8.5.2 (2006/05/31)
153        /** action 引数に渡す事の出来る アクションコマンド  ファイルまたはディレクトリをコピーします。 {@value} */
154        public static final String ACT_COPY      = "copy" ;             // 5.3.6.0 (2011/06/01)
155        /** action 引数に渡す事の出来る アクションコマンド  ストリームファイルをコピーします。 {@value} */
156        public static final String ACT_COPYST = "copyST" ;      // 6.3.6.1 (2015/08/28)
157        /** action 引数に渡す事の出来る アクションコマンド  ディレクトリのファイルリストをListオブジェクトにコピーします。 {@value} */
158        public static final String ACT_LIST      = "list" ;             // 6.2.3.0 (2015/05/01)
159        /** action 引数に渡す事の出来る アクションコマンド  ZIP圧縮します。 {@value} */
160        public static final String ACT_ZIP       = "zip" ;              // 5.9.2.2 (2015/11/20)
161        /** action 引数に渡す事の出来る アクションコマンド  タイムスタンプを現在時刻に変更します。 {@value} */
162        public static final String ACT_TIMESTAMP         = "timeStamp" ;                // 6.7.0.0 (2016/12/28)
163
164        // 6.4.3.4 (2016/03/11) String配列 から、Setに置き換えます。
165        private static final Set<String> ACTION_SET = new ArraySet<>(
166                                                                                                        ACT_CANREAD , ACT_CANWRITE , ACT_CREATENEWFILE , ACT_DELETE , ACT_EXISTS , ACT_ISDIRECTORY ,
167                                                                                                        ACT_ISFILE , ACT_ISHIDDEN , ACT_MKDIR , ACT_MKDIRS , ACT_RENAMETO , ACT_READ ,
168                                                                                                        ACT_EXISTSLENGTH , ACT_COPY , ACT_COPYST , ACT_LIST , ACT_ZIP , ACT_TIMESTAMP );
169
170        private String  fileURL         = HybsSystem.sys( "FILE_URL" );
171        private String  file1           = "";
172        private String  file2           ;
173        private String  action          ;
174        private boolean rtnCode         ;
175
176        private boolean notEquals       ;                       // 3.8.5.2 (2006/05/31) 判定結果を反転させて処理します。
177        private boolean useStop         = true;         // 3.8.5.2 (2006/05/31) エラー時BODYを処理後に停止(true)するかどうか
178
179        private String encode           ;                       // 5.1.9.0 (2010/08/01) READ時のエンコード指定
180
181        private String fromStorage      ;                       // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
182        private String fromBucket       ;                       // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
183        private String toStorage        ;                       // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
184        private String toBucket         ;                       // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
185        private boolean useLocal        ;                       // 8.0.1.0 (2021/10/29) クラウド設定を使用しない場合は、true
186
187        /**
188         * デフォルトコンストラクター
189         *
190         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
191         */
192        public FileTag() { super(); }           // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
193
194        /**
195         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
196         *
197         * @og.rev 3.6.0.0 (2004/09/25) file オブジェクトの作成を actionExec 移動
198         * @og.rev 3.8.5.2 (2006/05/31) notEquals追加。 判定結果を反転させて処理します。
199         * @og.rev 5.7.7.2 (2014/06/20) caseKey,caseVal,caseNN,caseNull 属性を追加
200         *
201         * @return      後続処理の指示
202         */
203        @Override
204        public int doStartTag() {
205                // 5.7.7.2 (2014/06/20) caseKey,caseVal,caseNN,caseNull 属性を追加
206                if( useTag() ) {
207                        if( useLocal ) {        // 8.0.1.0 (2021/10/29) クラウド設定を使用しない場合は、true
208                                fromBucket = "LOCAL" ;
209                                toBucket   = "LOCAL" ;
210                        }
211
212                        try {
213                                rtnCode = notEquals ^ actionExec( action );             // 3.8.5.2 (2006/05/31) 排他的論理和(XOR)
214                        }
215                        catch( final IOException ex ) {
216                                final String errMsg = "指定のアクションは実行できません。アクションエラー" + CR
217                                                                + "\t  action=["        + action        + "]"   + CR
218                                                                + "\t  fileURL=["       + fileURL       + "]"   + CR
219                                                                + "\t  file1=["         + file1         + "]"   + CR
220                                                                + "\t  file2=["         + file2         + "]"   + CR ;
221                                throw new HybsSystemException( errMsg,ex );             // 3.5.5.4 (2004/04/15) 引数の並び順変更
222                        }
223
224                        if( rtnCode ) { return SKIP_BODY ; }                    // Body を評価しない
225                        else {                  return EVAL_BODY_INCLUDE ; }    // Body インクルード( extends TagSupport 時)
226                }
227
228                return SKIP_BODY ;
229        }
230
231        /**
232         * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。
233         *
234         * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。
235         * @og.rev 3.8.5.2 (2006/05/31) useStop 追加。 エラー時BODYを処理後に停止(true)するかどうか
236         * @og.rev 5.7.7.2 (2014/06/20) caseKey,caseVal,caseNN,caseNull 属性を追加
237         *
238         * @return      後続処理の指示
239         */
240        @Override
241        public int doEndTag() {
242                debugPrint();           // 4.0.0 (2005/02/28)
243
244                // 6.4.1.1 (2016/01/16) PMD refactoring. A method should have only one exit point, and that should be the last statement in the method
245                return useTag() && useStop && !rtnCode  ? SKIP_PAGE : EVAL_PAGE ;
246        }
247
248        /**
249         * タグリブオブジェクトをリリースします。
250         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
251         *
252         * @og.rev 2.0.0.4 (2002/09/27) カスタムタグの release() メソッドを、追加
253         * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。
254         * @og.rev 3.6.0.0 (2004/09/24) columns 、tableId 、file 削除
255         * @og.rev 3.8.5.2 (2006/05/31) notEquals 、useStop 追加
256         * @og.rev 5.1.9.0 (2010/08/01) READ時のエンコード指定
257         * @og.rev 8.0.0.1 (2021/10/08) fromStorage,fromBucket,toStorage,toBucket 追加
258         * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加。
259         */
260        @Override
261        protected void release2() {
262                super.release2();
263                fileURL         = HybsSystem.sys( "FILE_URL" );
264                file1           = "";
265                file2           = null;
266                action          = null;
267                rtnCode         = false;
268                notEquals       = false;        // 3.8.5.2 (2006/05/31) 判定結果を反転させて処理します。
269                useStop         = true;         // 3.8.5.2 (2006/05/31) エラー時BODYを処理後に停止(true)するかどうか
270                encode          = null;         // 5.1.9.0 (2010/08/01) READ時のエンコード指定
271                fromStorage     = null;         // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
272                fromBucket      = null;         // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
273                toStorage       = null;         // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
274                toBucket        = null;         // 5.10.9.0 (2019/03/01) 8.0.0.1 (2021/10/08)
275                useLocal        = false;        // 8.0.1.0 (2021/10/29) クラウド設定を使用しない場合は、true
276        }
277
278        /**
279         * アクションを実行します。
280         * アクションは,指定のアクションコマンドに対応する処理を入力データに
281         * 対して行います。
282         *
283         * @og.rev 3.0.0.0 (2002/12/25) ACTION_LIST のチェックを削除
284         * @og.rev 3.6.0.0 (2004/09/25) ACT_read を追加 , file オブジェクトを移動
285         * @og.rev 3.8.5.2 (2006/05/31) existsLength 追加
286         * @og.rev 4.0.0.0 (2007/11/28) メソッドの戻り値をチェックします。
287         * @og.rev 5.1.9.0 (2010/08/01) READ時のエンコード指定
288         * @og.rev 5.3.6.0 (2011/06/01) ACT_COPY 対応
289         * @og.rev 5.7.1.1 (2013/12/13) copy元(file1)のファイルが存在しなければ、エラーにします。
290         * @og.rev 6.2.3.0 (2015/05/01) list アクション追加、delete 処理は、フォルダ階層を丸ごと削除します。
291         * @og.rev 6.3.6.1 (2015/08/28) ACT_COPYST 対応( copyST アクション時に、copy元に、アドレス指定できるようにする)。
292         * @og.rev 5.9.2.2 (2015/11/12) ACT_ZIP追加
293         * @og.rev 6.7.0.0 (2016/12/28) ACT_TIMESTAMP追加
294         * @og.rev 6.8.0.0 (2017/06/02) zipFile の親ディレクトリを作成します。
295         * @og.rev 6.9.8.0 (2018/05/28) FindBugs:例外的戻り値を無視しているメソッド(mkdirs)
296         * @og.rev 7.2.6.3 (2020/08/07) mkdirs は、先に存在チェックを入れて、存在していればtrueにする。
297         * @og.rev 8.0.0.1 (2021/10/08) fromStorage,fromBucket,toStorage,toBucket 追加
298         * @og.rev 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動
299         *
300         * @param       action アクション (public static final 宣言されている文字列)
301         *
302         * @return      実行後のデータ
303         */
304        private boolean actionExec( final String action ) throws IOException {
305                // 6.4.1.1 (2016/01/16) PMD refactoring. Avoid if (x != y) ..; else ..;
306                if( action == null ) {
307                        final String errMsg = "アクションが指定されていません。アクション NULL エラー" + CR
308                                                        + "\t  file=["          + file1         + "]"   + CR
309                                                        + "\t  matches=["       + file2         + "]"   + CR ;
310                        throw new HybsSystemException( errMsg );
311                }
312
313                final String directory = HybsSystem.url2dir( fileURL );
314                // 8.0.0.1 (2021/10/08) fromStorage,fromBucket,toStorage,toBucket 追加
315//              final File file = new File( StringUtil.urlAppend( directory,file1 ) );
316                final File file = HybsFileOperationFactory.create(fromStorage, fromBucket, StringUtil.urlAppend(directory, file1));
317
318                boolean rtnVal = false;
319                        if(      ACT_CANREAD.equalsIgnoreCase(           action ) ) { rtnVal = file.canRead(); }
320                        else if( ACT_CANWRITE.equalsIgnoreCase(          action ) ) { rtnVal = file.canWrite(); }
321                        else if( ACT_CREATENEWFILE.equalsIgnoreCase( action ) ) { rtnVal = file.createNewFile(); }
322                        else if( ACT_DELETE.equalsIgnoreCase(            action ) ) { rtnVal = FileUtil.deleteFiles( file ); }
323                        else if( ACT_EXISTS.equalsIgnoreCase(            action ) ) { rtnVal = file.exists(); }
324                        else if( ACT_ISDIRECTORY.equalsIgnoreCase(       action ) ) { rtnVal = file.isDirectory(); }
325                        else if( ACT_ISFILE.equalsIgnoreCase(            action ) ) { rtnVal = file.isFile(); }
326                        else if( ACT_ISHIDDEN.equalsIgnoreCase(          action ) ) { rtnVal = file.isHidden(); }
327                        else if( ACT_MKDIR.equalsIgnoreCase(             action ) ) {
328                                if( file.isDirectory() ) { rtnVal = true; }
329                                else { rtnVal = file.mkdir(); }
330                        }
331                        // 7.2.6.3 (2020/08/07) mkdirs は、先に存在チェックを入れて、存在していればtrueにする。
332//                      else if( ACT_MKDIRS.equalsIgnoreCase(            action ) ) { rtnVal = file.mkdirs(); }
333                        else if( ACT_MKDIRS.equalsIgnoreCase(            action ) ) {           // 7.2.6.3 (2020/08/07)
334                                if( file.isDirectory() ) { rtnVal = true; }
335                                else { rtnVal = file.mkdirs(); }
336                        }
337                        else if( ACT_RENAMETO.equalsIgnoreCase(          action ) ) {
338                                if( file2 != null ) {
339                                        // 8.0.0.1 (2021/10/08) fromStorage,fromBucket,toStorage,toBucket 追加
340//                                      final File newFile = new File( StringUtil.urlAppend( directory,file2 ) );
341                                        final File newFile = HybsFileOperationFactory.create(fromStorage, fromBucket, StringUtil.urlAppend(directory, file2));
342                                        if( newFile.exists() && !newFile.delete() ) {
343                                                final String errMsg = "所定のファイルを削除できませんでした。[" + newFile + "]" ;
344                                                throw new OgRuntimeException( errMsg );
345                                        }
346                                        rtnVal = file.renameTo( newFile );
347                                }
348                        }
349                        // 3.6.0.0 (2004/09/25) ACT_read を追加
350                        else if( ACT_READ.equalsIgnoreCase(                      action ) ) {
351                                if( file.isFile() ) {
352                                        // 6.2.3.0 (2015/05/01)
353                                        String text ;
354                                        if( POIUtil.isPOI( file ) ) {
355                                                text = POIUtil.extractor( file );
356                                        }
357                                        else {
358                                                final String enc = encode == null ? "UTF-8" : encode ;
359                                                text = POIUtil.extractor( file,enc );
360                                        }
361
362                                        rtnVal = true;
363                                        setRequestAttribute( "read",text );
364                                }
365                                else {
366                                        final String errMsg = "ファイルが存在しないか、ファイルではありません。" + CR
367                                                        + "\t  action=["        + action        + "]"   + CR
368                                                        + "\t  fileURL=["       + fileURL       + "]"   + CR
369                                                        + "\t  directory=[" + directory + "]"   + CR
370                                                        + "\t  file1=["         + file1         + "]"   + CR ;
371                                        throw new HybsSystemException( errMsg );
372                                }
373                        }
374                        // 3.8.5.2 (2006/05/31) ファイルサイズが0Byte以上のファイルが存在するかどうか判定。
375                        else if( ACT_EXISTSLENGTH.equalsIgnoreCase( action ) ) {
376                                rtnVal = file.exists() && file.length() > 0L ;
377                        }
378                        // 5.3.6.0 (2011/06/01) ファイルコピー対応
379                        // 6.0.0.1 (2014/04/25) These nested if statements could be combined
380                        // 6.3.6.1 (2015/08/28) フォルダとファイルの自動判定(FileUtil.copy側で判断する)
381                        else if( ACT_COPY.equalsIgnoreCase( action ) && file2 != null ) {
382                                // 8.0.0.1 (2021/10/08) fromStorage,fromBucket,toStorage,toBucket 追加
383//                              final File newFile = new File( StringUtil.urlAppend( directory,file2 ) );
384                                final File newFile = HybsFileOperationFactory.create(toStorage, toBucket, StringUtil.urlAppend(directory, file2));
385
386                                if( file.exists() ) {
387                                        // FileUtil.copy 側で、toFile のフォルダを作成します。
388                                        // 6.3.6.1 (2015/08/28) で、fromFile がディレクトリでも処理できるように変更。
389                                        rtnVal = FileUtil.copy( file, newFile );
390                                }
391                                else {
392                                        final String errMsg = "copy元(file1)のファイルが存在しません。" + CR
393                                                        + "\t  action=["        + action        + "]"   + CR
394                                                        + "\t  fileURL=["       + fileURL       + "]"   + CR
395                                                        + "\t  directory=[" + directory + "]"   + CR
396                                                        + "\t  file1=["         + file1         + "]"   + CR
397                                                        + "\t  file2=["         + file2         + "]"   + CR ;
398                                        throw new HybsSystemException( errMsg );
399                                }
400                        }
401                        // 6.3.6.1 (2015/08/28) copyST アクション時に、copy元に、アドレス指定できるようにする。
402                        else if( ACT_COPYST.equalsIgnoreCase( action ) && file2 != null ) {
403                                // 8.0.0.1 (2021/10/08) fromStorage,fromBucket,toStorage,toBucket 追加
404                                final String toFile = StringUtil.urlAppend( directory,file2 );
405//                              final File newFile = new File( StringUtil.urlAppend( directory,file2 ) );
406                                final File newFile = new File( toFile );
407
408                                // ※ getResourceAsStream は、/gf/jsp/・・・ ではなく、/jsp/・・・ でのアドレス指定で取得します。
409                                final ServletContext application = pageContext.getServletContext();
410                                rtnVal = FileUtil.copy( application.getResourceAsStream( file1 ), newFile );
411
412                                if( !rtnVal ) {
413                                        final String errMsg = action + " アクションが実行できませんでした。" + CR
414                                                        + "\t  action=["        + action        + "]"   + CR
415                                                        + "\t  fileURL=["       + fileURL       + "]"   + CR
416                                                        + "\t  directory=[" + directory + "]"   + CR
417                                                        + "\t  file1=["         + file1         + "]"   + CR
418                                                        + "\t  file2=["         + file2         + "]"   + CR ;
419                                        throw new HybsSystemException( errMsg );
420                                }
421
422                                // 8.0.0.1 (2021/10/08) ストリームからクラウドファイルへのコピーが上手くいかない。
423                                // 一旦、ローカルに出力したファイルをクラウドへコピーしています。
424                                // 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動。 toStorage, toBucket 有りなので、他と方法が異なる。
425                                final FileOperation cloudFile = HybsFileOperationFactory.create( toStorage, toBucket, toFile );
426//                              if(!cloudFile.isLocal()){
427                                if( cloudFile.isCloud() ){
428                                        FileUtil.copy( newFile,cloudFile );
429                                        newFile.delete();
430                                }
431                        }
432                        // 6.2.3.0 (2015/05/01)
433                        else if( ACT_LIST.equalsIgnoreCase( action ) ) {
434                                final String[] files = file.isDirectory() ? file.list() : new String[] { file.getName() };
435                                final List<String> list = new ArrayList<>();
436
437                                // 6.3.9.0 (2015/11/06) null になっている可能性がある(findbugs)
438                                if( files != null ) {
439                                        final boolean use = file2 == null || file2.isEmpty();
440                                        for( final String nm : files ) {
441                                                if( use || nm.matches( file2 ) ) {
442                                                        list.add( nm );
443                                                }
444                                        }
445                                }
446
447                                setRequestAttribute( "list",list );
448                                rtnVal = true;
449                        }
450                        // 5.9.2.2 (2015/11/20) zip追加
451                        else if( ACT_ZIP.equalsIgnoreCase( action ) && file2 != null ) {
452                                //あるかないかだけ見る
453                                // 6.4.1.1 (2016/01/16) PMD refactoring. When doing a String.toLowerCase()/toUpperCase() call, use a Locale
454                                if( file2.toLowerCase(Locale.JAPAN).indexOf( ".zip" ) < 0 ){
455                                        file2 = file2 + ".zip";
456                                }
457                                final File zipFile = new File( StringUtil.urlAppend( directory,file2 ) );
458                                // 6.9.8.0 (2018/05/28) FindBugs:例外的戻り値を無視しているメソッド
459//                              zipFile.getParentFile().mkdirs();                                                                               // 6.8.0.0 (2017/06/02) zipFile の親ディレクトリを作成します。
460                                final File parent = zipFile.getParentFile();            // 親フォルダを取得。nullもありえる
461                                if( parent == null || !parent.exists() && !parent.mkdirs() ) {
462                                        final String errMsg = "zipFileファイルの親フォルダを作成できませんでした。"  + CR
463                                                                                + " zipFile=[" + zipFile + "]"  + CR ;
464                                        throw new HybsSystemException( errMsg );
465                                }
466
467                                final List<File> ziplist = ZipArchive.compress( file , zipFile );               // 6.7.0.0 (2016/12/28) 返り値で、正誤判定します。
468                                rtnVal = !ziplist.isEmpty();                    // 6.7.0.0 (2016/12/28) true/false の判定を入れます。
469                        }
470                        // 6.7.0.0 (2016/12/28) timeStamp追加。タイムスタンプを現在時刻に変更します。
471                        else if( ACT_TIMESTAMP.equalsIgnoreCase( action ) ) {
472                                rtnVal = file.setLastModified( System.currentTimeMillis() );
473                        }
474
475                return rtnVal;
476        }
477
478        /**
479         * 【TAG】アクション(canRead,canWrite,createNewFile,delete,renameTo,exists,isDirectory,isFile,isHidden,mkdir,mkdirs,read,existsLength,copy,copyST,list,zip,timeStamp)を指定します。
480         *
481         * @og.tag
482         * アクションは,HTMLから(get/post)指定されますので,ACT_xxx で設定される
483         * フィールド定数値のいづれかを、指定できます。
484         * 処理の結果が、false の場合は、body 要素を表示して終了します。
485         * useStop 属性と、notEquals 属性によって、上記の振る舞いをけることが可能です。
486         *
487         *       canRead          読み込めるかどうかを判定。
488         *       canWrite         変更できるかどうか判定。
489         *       createNewFile    空の新しいファイルを不可分 (atomic) に生成。(そのファイルがまだ存在しない場合だけ)
490         *       delete           ファイルまたはディレクトリを削除(ディレクトリ階層をすべて削除)。
491         *       renameTo         ファイルまたはディレクトリ名を変更。
492         *       exists           ファイルが存在するかどうか判定。
493         *       isDirectory      ファイルがディレクトリであるかどうか判定。
494         *       isFile           ファイルが普通のファイルかどうか判定。
495         *       isHidden         ファイルが隠しファイルかどうか判定。
496         *       mkdir            ディレクトリを生成。
497         *       mkdirs           ディレクトリを複数生成。
498         *       read             ファイルを読み込んで、パラメータにセットします(6.2.3.0 (2015/05/01))。
499         *       existsLength     ファイルサイズが0Byte以上のファイルが存在するかどうか判定。
500         *       copy             ファイルまたはディレクトリをコピー(file1 ⇒ file2 にコピー)。
501         *       copyST           ストリームファイルをコピー(file1(アドレス) ⇒ file2 にコピー)。
502         *       list             ファイルリストをListオブジェクトにコピーして、パラメータにセットします(6.2.3.0 (2015/05/01))。
503         *       zip              ファイルまたはディレクトリをZIPファイルに圧縮します。(file1⇒file2)
504         *       timeStamp        ファイルのタイムスタンプを現在時刻に変更します。(file1のみ)
505         *
506         * ※1 copyST の file1 は、getResourceAsStream を使用の為、/gf/jsp/・・・ ではなく、/jsp/・・・ でのアドレス指定で取得します。
507         *
508         * @og.rev 3.0.0.0 (2002/12/25) ACTION_LIST のチェックを導入
509         * @og.rev 3.5.6.2 (2004/07/05) 文字列の連結にStringBuilderを使用します。
510         * @og.rev 6.3.4.0 (2015/08/01) エラーメッセージを、変更。
511         * @og.rev 6.3.6.1 (2015/08/28) copyST アクション追加
512         * @og.rev 6.4.3.4 (2016/03/11) String配列 から、Setに置き換えます。
513         *
514         * @param       cmd アクション文字列
515         * @see         <a href="../../../../constant-values.html#org.opengion.hayabusa.taglib.FileTag.ACT_canRead">アクション定数</a>
516         */
517        public void setAction( final String cmd ) {
518                action = getRequestParameter( cmd );
519
520                if( ! check( action, ACTION_SET ) ) {
521                        final String errMsg = "指定のアクションは実行できません。アクションエラー"       + CR
522                                                        + "\t  action=["        + action        + "]"   + CR
523                                                        + "\t  actionList=" + String.join( ", " , ACTION_SET ) ;
524                        throw new HybsSystemException( errMsg );
525                }
526        }
527
528        /**
529         * 【TAG】操作するファイルのディレクトリを指定します
530         *              (初期値:FILE_URL[={@og.value SystemData#FILE_URL}])。
531         *
532         * @og.tag
533         * この属性で指定されるディレクトリのファイルを操作します。
534         * 指定方法は、通常の fileURL 属性と同様に、先頭が、'/' (UNIX) または、2文字目が、
535         * ":" (Windows)の場合は、指定のURLそのままのディレクトリに、そうでない場合は、
536         * (初期値:システム定数のFILE_URL[={@og.value SystemData#FILE_URL}])。
537         *
538         * @og.rev 4.0.0.0 (2005/01/31) urlAppend メソッドの利用
539         * @og.rev 4.0.0.0 (2007/11/20) 指定されたディレクトリ名の最後が"\"or"/"で終わっていない場合に、"/"を付加する。
540         * @og.rev 6.4.2.1 (2016/02/05) URLの最後に、"/" を追加する処理を廃止。
541         *
542         * @param       url ファイルURL
543         * @see         org.opengion.hayabusa.common.SystemData#FILE_URL
544         */
545        public void setFileURL( final String url ) {
546                final String furl = nval( getRequestParameter( url ),null );
547                if( furl != null ) {
548                        fileURL = StringUtil.urlAppend( fileURL,furl );
549                }
550        }
551
552        /**
553         * 【TAG】基準となるファイル名を指定します(コマンドの左辺のファイル名です)。
554         *
555         * @og.tag
556         * コマンドの左辺のファイル名です。
557         *
558         * ※ 6.3.6.1 (2015/08/28)
559         *    copyST アクション時に、copy元に、アドレス指定できるようにする。
560         *
561         * @param       fname ファイル名1
562         */
563        public void setFile1( final String fname ) {
564                file1 = nval( getRequestParameter( fname ),file1 );
565        }
566
567        /**
568         * 【TAG】処理結果となるファイル名を指定します(コマンドの右辺のファイル名です)。
569         *
570         * @og.tag
571         * コマンドの右辺のファイル名です。
572         * ただし、action="list" の時は、file2 を、フィルターとして使用します。
573         *
574         * @param       fname ファイル名2
575         */
576        public void setFile2( final String fname ) {
577                file2 = nval( getRequestParameter( fname ),file2 );
578        }
579
580        /**
581         * 【TAG】判定結果を反転させるかどうか[true/false]を指定します(初期値:false)。
582         *
583         * @og.tag
584         * 通常の判定結果において、不成立(false)の場合に、BODY を実行します。
585         * 通常の処理結果の正反対の処理を行います。
586         * 初期値は、通常 (true 以外)です。
587         *
588         * @og.rev 3.8.5.2 (2006/05/31) 新規追加
589         *
590         * @param   flag 判定結果反転 [true:反転する/それ以外:通常]
591         */
592        public void setNotEquals( final String flag ) {
593                notEquals = nval( getRequestParameter( flag ),notEquals );
594        }
595
596        /**
597         * 【TAG】エラー時BODYを処理後に停止するかどうか[true/false]を指定します(初期値:true)。
598         *
599         * @og.tag
600         * 処理結果などに応じて、以下の処理を停止したい場合に、使用します。
601         * 通常は、条件を判定後、false の場合に、BODY部を出力(処理)した後に、
602         * 処理を停止します。(useStop="true")
603         * false を指定すると、判定結果に無関係に、以下の処理を実行します。
604         * 処理は継続したいが、警告表示する場合に、useStop="false" を指定します。
605         * 初期値は、停止する ("true")です。
606         *
607         * @og.rev 3.8.5.2 (2006/05/31) 新規追加
608         *
609         * @param   flag 処理後停止 [true:する/それ以外:しない]
610         */
611        public void setUseStop( final String flag ) {
612                useStop = nval( getRequestParameter( flag ),useStop );
613        }
614
615        /**
616         * 【TAG】ファイルを読み込む(action="read")際のエンコードを指定します(初期値:OS依存文字コード)。
617         *
618         * @og.tag
619         * ファイルを読み込む(action="read")際のエンコードを指定します。
620         * action="read"以外場合には、この属性値は利用されません。
621         * 指定しない場合は、OS依存文字コードで読み込まれます。
622         *
623         * @og.rev 5.1.9.0 (2010/08/01) 新規作成
624         *
625         * @param   enc ファイル読み込みのエンコード
626         */
627        public void setEncode( final String enc ) {
628                encode = nval( getRequestParameter( enc ),encode );
629        }
630
631        /**
632         * 【TAG】テンプレートファイルの読み込み元ストレージタイプを指定します。
633         *
634         * @og.tag
635         * フクラウド読み込み時の、from に当たるストレージタイプを指定します。
636         * 初期値は、システム定数の『CLOUD_TARGET』です。(例:AWS)
637         * 強制的に、ローカルファイルを指定する場合は、空文字列ではなく、"LOCAL" を指定します。
638         *
639         * @og.rev 8.0.1.0 (2021/10/29) 新規作成
640         *
641         * @param   fromType 元ストレージタイプ
642         */
643        public void setFromStorage( final String fromType ) {
644                fromStorage = nval( getRequestParameter( fromType ),fromStorage );
645        }
646
647        /**
648         * 【TAG】テンプレートファイルの読み込み元バケット名を指定します。
649         *
650         * @og.tag
651         * フクラウド読み込み時の、from に当たるバケット名を指定します。
652         * 初期値は、システム定数の『CLOUD_BUCKET』です。
653         * 強制的に、ローカルファイルを指定する場合は、空文字列ではなく、"LOCAL" を指定します。
654         *
655         * @og.rev 8.0.1.0 (2021/10/29) 新規作成
656         *
657         * @param   fromBkt 元バケット名
658         */
659        public void setFromBucket( final String fromBkt ) {
660                fromBucket = nval( getRequestParameter( fromBkt ),fromBucket );
661        }
662
663        /**
664         * 【TAG】生成したファイルの出力先ストレージタイプを指定します。
665         *
666         * @og.tag
667         * フクラウド書き込み時の、to に当たるストレージタイプを指定します。
668         * 初期値は、システム定数の『CLOUD_TARGET』です。
669         * 強制的に、ローカルファイルを指定する場合は、空文字列ではなく、"LOCAL" を指定します。
670         *
671         * @og.rev 8.0.1.0 (2021/10/29) 新規作成
672         *
673         * @param   toType 出力先ストレージタイプ
674         */
675        public void setToStorage( final String toType ) {
676                toStorage = nval( getRequestParameter( toType ),toStorage );
677        }
678
679        /**
680         * 【TAG】生成したファイルの出力先バケット名を指定します。
681         *
682         * @og.tag
683         * フクラウド書き込み時の、to に当たるバケット名を指定します。
684         * 初期値は、システム定数の『CLOUD_TARGET』です。
685         * 強制的に、ローカルファイルを指定する場合は、空文字列ではなく、"LOCAL" を指定します。
686         *
687         * @og.rev 8.0.1.0 (2021/10/29) 新規作成
688         *
689         * @param   toBkt 出力先バケット名
690         */
691        public void setToBucket( final String toBkt ) {
692                toBucket = nval( getRequestParameter( toBkt ),toBucket );
693        }
694
695        /**
696         * 【TAG】システム定数でクラウド設定されていても、クラウド環境を使用しない場合、trueを指定します(初期値:false)。
697         *
698         * @og.tag
699         * クラウド設定は、システム定数の『CLOUD_TARGET』と『CLOUD_BUCKET』の設定で自動的に使用しますが、
700         * どうしてもローカルでのみ使いたい場合は、この属性を true に設定します。
701         * 標準はfalse:設定どおりとなります。
702         * fromStorage,fromBucket,toStorage,toBucket
703         * from や to だけを強制的に、ローカルファイルを指定する場合は、空文字列ではなく、
704         * fromStorage,fromBucket や、toStorage,toBucket に、"LOCAL" を指定します。
705         * storegeとbucketは、どちらかが、LOCAL の場合は、ローカルファイル を使用します。
706         *
707         * true/false以外を指定した場合はfalse扱いとします。
708         *
709         * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加。
710         *
711         * @param flag ローカル環境のみ [true:ローカルのみ/false:設定どおり]
712         */
713        public void setUseLocal( final String flag ) {
714                useLocal = nval( getRequestParameter( flag ),useLocal );
715        }
716
717        /**
718         * このオブジェクトの文字列表現を返します。
719         * 基本的にデバッグ目的に使用します。
720         *
721         * @return このクラスの文字列表現
722         * @og.rtnNotNull
723         */
724        @Override
725        public String toString() {
726                return ToString.title( this.getClass().getName() )
727                                .println( "VERSION"             ,VERSION        )
728                                .println( "fileURL"             ,fileURL        )
729                                .println( "file1"               ,file1          )
730                                .println( "file2"               ,file2          )
731                                .println( "action"              ,action         )
732                                .println( "rtnCode"             ,rtnCode        )
733                                .println( "notEquals"   ,notEquals      )
734                                .println( "useStop"             ,useStop        )
735                                .println( "Other..."    ,getAttributes().getAttribute() )
736                                .fixForm().toString() ;
737        }
738}