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.hayabusa.common.HybsSystemException; 019import org.opengion.fukurou.util.HybsFileFilter; 020 021import static org.opengion.fukurou.util.StringUtil.nval ; 022 023/** 024 * fileOption でファイルを検索する場合の条件を指定するタグです。 025 * 026 * @og.formSample 027 * ●形式:<og:fileWhere name="…" startsWith="…" endsWith="…" … /> 028 * ●body:なし 029 * 030 * ●Tag定義: 031 * <og:fileWhere 032 * startsWith 【TAG】指定された接頭辞で始まる場合、スルー(選択)されます 033 * endsWith 【TAG】指定された接頭辞で終わる場合、スルー(選択)されます 034 * instr 【TAG】指定された文字列がファイル名に含まれる場合、スルー(選択)されます 035 * equals 【TAG】ファイル名が一致する場合、スルー(選択)されます 036 * matches 【TAG】ファイル名が、指定された正規表現と一致する場合、スルー(選択)されます 037 * unMatches 【TAG】ファイル名が、指定された正規表現と一致しない場合、スルー(選択)されます 038 * lastModified 【TAG】指定のタイムスタンプ以後に変更されている場合、スルー(選択)されます 039 * isLarger 【TAG】指定の大きさより大きいファイルの場合、スルー(選択)されます 040 * isSmaller 【TAG】指定の大きさより小さいファイルの場合、スルー(選択)されます 041 * isHidden 【TAG】true:HIDDENファイルのみ/false:NORMALファイルのみスルー(選択)されます 042 * useDir 【TAG】フィルタによる対象の絞込みをディレクトリにも適用するかどうかを指定します 043 * debug 【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false) 044 * /> 045 * 046 * ●使用例 047 * <og:fileOption select="NAME" from="./jsp" > 048 * <og:fileWhere 049 * startsWith = "File" :指定された接頭辞で始まる場合、選択される 050 * endsWith = ".java" :指定された接頭辞で終わる場合、選択される 051 * instr = "Tag" :指定された文字列がファイル名に含まれる場合、選択される 052 * fileEquals = "FileWhereTag.java" :ファイル名が一致する場合、選択される(大文字小文字は区別しない) 053 * matches = "File*Tag" :ファイル名が、指定された正規表現と一致する場合、選択される(大文字小文字は区別しない) 054 * unMatches = "File*Tag" :ファイル名が、指定された正規表現と一致しない場合、選択される(大文字小文字は区別しない) 055 * lastModified = "20050101" :指定のタイムスタンプ以後に変更された場合、選択される 056 * 日付けの指定は、YYYYMMDD 形式, TODAY , YESTERDAY が使用できます。 057 * isLarger = "1024" :指定の大きさ(バイト単位)より大きいファイルの場合、選択される 058 * isSmaller = "1024" :指定の大きさ(バイト単位)より小さいファイルの場合、選択される 059 * isHidden = "true" :true:HIDDENファイルのみ選択される 060 * useDir = "false" :フィルタによる対象の絞込みをディレクトリにも適用するかどうか 061 * /> 062 * </og:fileOption> 063 * 064 * ・検索条件が入力された時 065 * 条件を満足して全てのファイルを列挙します。 066 * 067 * ・検索条件が入力されなかった時 068 * 該当ディレクトリ内の全てのファイルを列挙します。 069 * 070 * @og.rev 2.1.1.0 (2002/11/11) 新規作成 071 * @og.rev 4.0.0.0 (2005/01/31) 内部ロジックの全面見直し 072 * @og.group その他入力 073 * 074 * @version 4.0 075 * @author Kazuhiko Hasegawa 076 * @since JDK5.0, 077 */ 078public class FileWhereTag extends CommonTagSupport { 079 //* このプログラムのVERSION文字列を設定します。 {@value} */ 080 private static final String VERSION = "5.7.5.0 (2014/04/04)" ; 081 082 private static final long serialVersionUID = 575020140404L ; 083 084 private String startsWith = null; // 指定された接頭辞で始まる場合、選択される 085 private String endsWith = null; // 指定された接頭辞で終わる場合、選択される 086 private String instr = null; // 指定された文字列がファイル名に含まれる場合、選択される 087 private String fileEquals = null; // ファイル名が一致する場合、選択される(大文字小文字は区別しない) 088 private String matches = null; // ファイル名が、指定された正規表現と一致する場合、選択される(大文字小文字は区別しない) 089 private String unMatches = null; // ファイル名が、指定された正規表現と一致しない場合、選択される(大文字小文字は区別しない) 090 private String lastModified = null; // 指定のタイムスタンプ以後に変更された場合、選択される 091// private int isLarger = -1; // 指定の大きさ(バイト単位)より大きいファイルの場合、選択される 092// private int isSmaller = -1; // 指定の大きさ(バイト単位)より小さいファイルの場合、選択される 093 private String isLarger = null; // 指定の大きさ(バイト単位)より大きいファイルの場合、選択される 094 private String isSmaller = null; // 指定の大きさ(バイト単位)より小さいファイルの場合、選択される 095 private String isHidden = null; // 5.7.5.0 (2014/04/04) true:HIDDENファイルのみ/false:NORMALファイルのみスルー(選択)されます 096 private boolean useDir = false; // 5.3.9.0 (2011/09/01) フィルタによる対象の絞込みをディレクトリにも適用するかどうか 097 098 /** 099 * Taglibの終了タグが見つかったときに処理する doEndTag() を オーバーライドします。 100 * 101 * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。 102 * @og.rev 5.1.2.0 (2010/01/01) unMatches メソッドで反転属性の追加 103 * @og.rev 5.3.9.0 (2011/09/01) useDir属性を追加 104 * @og.rev 5.6.6.0 (2013/07/05) unMatchesの引数間違いの訂正 105 * @og.rev 5.7.5.0 (2014/04/04) isHidden属性を追加します。 106 * 107 * @return 後続処理の指示 108 */ 109 @Override 110 public int doEndTag() { 111 debugPrint(); // 4.0.0 (2005/02/28) 112 113 // 5.3.9.0 (2011/09/01) useDir属性を追加 114// HybsFileFilter filter = new HybsFileFilter(); 115 HybsFileFilter filter = new HybsFileFilter( useDir ); 116 filter.startsWith( startsWith ); 117 filter.endsWith( endsWith ); 118 filter.instr( instr ); 119 filter.fileEquals( fileEquals ); 120 filter.matches( matches ); 121// filter.matches( matches , true ); // 5.1.2.0 (2010/01/01) 反転属性の追加 122 filter.matches( unMatches , true ); // 5.6.6.0 (2013/07/05) 引数間違いの訂正 123// filter.unMatches( unMatches ); 124 filter.lastModified( lastModified ); 125 filter.isLarger( isLarger ); 126 filter.isSmaller( isSmaller ); 127 filter.isHidden( isHidden ); // 5.7.5.0 (2014/04/04) 追加 128 129 // ファイルフィルタを追加 130 FileQueryTag fileQuery = (FileQueryTag)findAncestorWithClass( this, FileQueryTag.class ); 131 if( fileQuery != null ) { 132 fileQuery.setFileFilter( filter ); 133 return EVAL_PAGE ; 134 } 135 136 FileOptionTag fileOption = (FileOptionTag)findAncestorWithClass( this, FileOptionTag.class ); 137 if( fileOption != null ) { 138 fileOption.setFileFilter( filter ); 139 return EVAL_PAGE ; 140 } 141 142 // どちらにも属していなかった場合は、エラーになる。 143 String errMsg = "この、FileWhereTag タグは、FileQueryTag か、FileOption の" 144 + "BODY 部にのみ、配置できます。(それ以外の箇所での使用はできません。)" ; 145 throw new HybsSystemException( errMsg ); 146 } 147 148 /** 149 * タグリブオブジェクトをリリースします。 150 * キャッシュされて再利用されるので、フィールドの初期設定を行います。 151 * 152 * @og.rev 2.0.0.4 (2002/09/27) カスタムタグの release() メソッドを、追加 153 * @og.rev 3.1.1.2 (2003/04/04) Tomcat4.1 対応。release2() を doEndTag()で呼ぶ。 154 * @og.rev 4.0.0.0 (2005/01/31) 内部ロジックの全面見直し 155 * @og.rev 5.3.9.0 (2011/09/01) useDir属性追加 156 * @og.rev 5.7.4.3 (2014/03/28) isLarger,isSmaller属性を文字列に変更 157 * @og.rev 5.7.5.0 (2014/04/04) isHidden属性を追加 158 */ 159 @Override 160 protected void release2() { 161 super.release2(); 162 startsWith = null; 163 endsWith = null; 164 instr = null; 165 fileEquals = null; 166 matches = null; 167 unMatches = null; 168 lastModified = null; 169// isLarger = -1; 170// isSmaller = -1; 171 isLarger = null; // 5.7.4.3 (2014/03/28) isLarger,isSmaller属性を文字列に変更 172 isSmaller = null; // 5.7.4.3 (2014/03/28) isLarger,isSmaller属性を文字列に変更 173 isHidden = null; // 5.7.5.0 (2014/04/04) isHidden属性を追加 174 useDir = false; // 5.3.9.0 (2011/09/01) useDir属性追加 175 } 176 177 /** 178 * 【TAG】指定された接頭辞で始まる場合、スルー(選択)されます。 179 * @og.tag 180 * 引数が null の場合は、追加しません。 181 * 182 * @param prefix 接頭辞 183 * @see java.lang.String#startsWith(String) 184 */ 185 public void setStartsWith( final String prefix ) { 186 startsWith = nval( getRequestParameter( prefix ),startsWith ); 187 } 188 189 /** 190 * 【TAG】指定された接頭辞で終わる場合、スルー(選択)されます。 191 * @og.tag 192 * 引数が null の場合は、追加しません。 193 * 194 * @param suffix 接尾辞 195 * @see java.lang.String#endsWith(String) 196 */ 197 public void setEndsWith( final String suffix ) { 198 endsWith = nval( getRequestParameter( suffix ),endsWith ); 199 } 200 201 /** 202 * 【TAG】指定された文字列がファイル名に含まれる場合、スルー(選択)されます。 203 * @og.tag 204 * 引数が null の場合は、追加しません。 205 * 206 * @param str 指定の部分文字列 207 */ 208 public void setInstr( final String str ) { 209 instr = nval( getRequestParameter( str ),instr ); 210 } 211 212 /** 213 * 【TAG】ファイル名が一致する場合、スルー(選択)されます。 214 * @og.tag 215 * 大文字小文字は区別しません。 216 * 引数が null の場合は、追加しません。 217 * 218 * @param str ファイル名文字列 219 * @see java.lang.String#equals(Object) 220 */ 221 public void setEquals( final String str ) { 222 fileEquals = nval( getRequestParameter( str ),fileEquals ); 223 } 224 225 /** 226 * 【TAG】ファイル名が、指定された正規表現と一致する場合、スルー(選択)されます。 227 * @og.tag 228 * 大文字小文字は区別しません。 229 * Pattern.compile( str,Pattern.CASE_INSENSITIVE ) ; 230 * pattern.matcher( pathname.getName() ).find() == true と同じ結果が得られます。 231 * 引数が null の場合は、追加しません。 232 * 233 * @param str ファイル名文字列(正規表現) 234 * @see java.util.regex.Pattern#compile(String,int) 235 * @see java.util.regex.Matcher#find() 236 */ 237 public void setMatches( final String str ) { 238 matches = nval( getRequestParameter( str ),matches ); 239 } 240 241 /** 242 * 【TAG】ファイル名が、指定された正規表現と一致しない場合、スルー(選択)されます。 243 * @og.tag 244 * 大文字小文字は区別しません。 245 * Pattern.compile( str,Pattern.CASE_INSENSITIVE ) ; 246 * pattern.matcher( pathname.getName() ).find() == false と同じ結果が得られます。 247 * 引数が null の場合は、追加しません。 248 * 249 * @param str ファイル名文字列(正規表現) とマッチしない 250 * @see java.util.regex.Pattern#compile(String,int) 251 * @see java.util.regex.Matcher#find() 252 */ 253 public void setUnMatches( final String str ) { 254 unMatches = nval( getRequestParameter( str ),unMatches ); 255 } 256 257 /** 258 * 【TAG】指定のタイムスタンプ以後に変更されている場合、スルー(選択)されます。 259 * @og.tag 260 * ディレクトリは、ここの判定では無視します。(必ず true を返します) 261 * 日付けの指定に、YYYYMMDD 形式の 8文字数字文字列以外に、 262 * TODAY や YESTERDAY なども使用できます。 263 * TODAY は、実行日の 00:00:00 を基準時刻とし、YESTERDAY は、その前日になります。 264 * 引数が null の場合は、追加しません。 265 * 266 * YYYYMMDD YYYYMMDD形式の指定日の 00:00:00 を基準時刻 267 * TODAY 実行日の 00:00:00 を基準時刻 268 * YESTERDAY 実行日前日の 00:00:00 を基準時刻 269 * LAST_WEEK 実行日の先週(7日前) 00:00:00 を基準時刻 270 * MONTH 実行月の 1日 00:00:00 を基準時刻 271 * LAST_MONTH 実行前月の 同日 00:00:00 を基準時刻 272 * LAST_YEAR 実行前年の 同月同日 00:00:00 を基準時刻 273 * 274 * @param modify YYYYMMDD形式の指定日 275 */ 276 public void setLastModified( final String modify ) { 277 lastModified = nval( getRequestParameter( modify ),lastModified ); 278 } 279 280 /** 281 * 【TAG】指定の大きさより大きいファイルの場合、スルー(選択)されます。 282 * 283 * @og.tag 284 * ファイルの大きさ(バイト単位)は同値を含みます。 285 * 未指定の場合は、スルー(選択)されます。 286 * 287 * 指定はバイト単位ですが、**KB , **MB , **GB などの単位を付ける事も可能です。 288 * 現時点では、K , KB , M , MB , G , GB のみ指定可能です。 289 * 290 * @param len ファイルの大きさ(バイト単位)。同値を含む。 291 */ 292 public void setIsLarger( final String len ) { 293 isLarger = nval( getRequestParameter( len ),isLarger ); 294 } 295 296 /** 297 * 【TAG】指定の大きさより小さいファイルの場合、スルー(選択)されます。 298 * 299 * @og.tag 300 * ファイルの大きさ(バイト単位)は同値を含みません。 301 * 未指定の場合は、スルー(選択)されます。 302 * 303 * 指定はバイト単位ですが、**KB , **MB , **GB などの単位を付ける事も可能です。 304 * 現時点では、K , KB , M , MB , G , GB のみ指定可能です。 305 * 306 * @param len ファイルの大きさ(バイト単位)。同値を含まない。 307 */ 308 public void setIsSmaller( final String len ) { 309 isSmaller = nval( getRequestParameter( len ),isSmaller ); 310 } 311 312 313 /** 314 * 【TAG】対象に隠しファイル(hidden)を、スルー(選択)する場合、true を指定します(初期値:null)。 315 * @og.tag 316 * trueを指定すると、隠しファイル(hidden)のみ選択します。 317 * falseを指定すると、隠しファイル(hidden)以外を選択します。 318 * 隠しファイルかどうかの定義はOSに従います。 319 * 初期値は、両方とも選択する為、null (trueでもfalseでもない)です。 320 * 321 * @og.rev 5.7.5.0 (2014/04/04) 新規作成 322 * 323 * @param flg 隠しファイルを対象にするかどうか true:する/false:しない] 324 */ 325 public void setIsHidden( final String flg ) { 326 isHidden = nval( getRequestParameter( flg ), isHidden ); 327 } 328 329 /** 330 * 【TAG】フィルタによる対象の絞込みをディレクトリにも適用するかどうかを指定します。 331 * @og.tag 初期値は、false(ディレクトリはフィルタによる絞込みをしない)です。 332 * 333 * @og.rev 5.3.9.0 (2011/09/01) 新規作成 334 * 335 * @param flg フィルタによる対象の絞込みをディレクトリにも適用するかどうか(初期値:false) 336 */ 337 public void setUseDir( final String flg ) { 338 useDir = nval( getRequestParameter( flg ),useDir ); 339 } 340 341 /** 342 * このオブジェクトの文字列表現を返します。 343 * 基本的にデバッグ目的に使用します。 344 * 345 * @return String型。このクラスの文字列表現 346 */ 347 @Override 348 public String toString() { 349 return org.opengion.fukurou.util.ToString.title( this.getClass().getName() ) 350 .println( "VERSION" ,VERSION ) 351 .println( "startsWith" ,startsWith ) 352 .println( "endsWith" ,endsWith ) 353 .println( "instr" ,instr ) 354 .println( "fileEquals" ,fileEquals ) 355 .println( "matches" ,matches ) 356 .println( "unMatches" ,unMatches ) 357 .println( "lastModified" ,lastModified ) 358 .println( "isLarger" ,isLarger ) 359 .println( "isSmaller" ,isSmaller ) 360 .println( "useDir" ,useDir ) 361 .println( "Other..." ,getAttributes().getAttribute() ) 362 .fixForm().toString() ; 363 } 364}