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