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.hayabusa.taglib;
017    
018    import org.opengion.hayabusa.common.HybsSystemException;
019    import org.opengion.fukurou.util.HybsFileFilter;
020    
021    import static org.opengion.fukurou.util.StringUtil.nval ;
022    
023    /**
024     * fileOption でファイルを検索する場合ã?条件を指定するタグですã?
025     *
026     * @og.formSample
027     * ●形式ï¼?lt;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     */
078    public 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 形式ã? ?˜文字数字文字å?以外にã€?
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    }