View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.core.io;
17  
18  import java.io.File;
19  import java.io.FileFilter;
20  
21  /**
22   * 任意の拡張子にマッチする {@link FileFilter} です。<br />
23   * 
24   * @author y-komori
25   */
26  public class ExtFileFilter implements FileFilter {
27      private static final String PERIOD = ".";
28  
29      private static final String NULL_STRING = "";
30  
31      private String suffix;
32  
33      private String ext;
34  
35      private String match = NULL_STRING;
36  
37      /**
38       * {@link ExtFileFilter} を構築します。<br />
39       * <code>ext</code> で指定した拡張子にマッチします。<br />
40       * たとえば、<code>xml</code> という文字列を指定した場合、<code>.xml</code>
41       * で終わるパスにマッチします。<br />
42       * 
43       * @param ext
44       *            拡張子
45       */
46      public ExtFileFilter(final String ext) {
47          this.ext = ext;
48  
49          createMatchString();
50      }
51  
52      /**
53       * {@link ExtFileFilter} を構築します。<br />
54       * ファイル名部分が <code>suffix</code> で終了し、拡張子 <code>ext</code> をもつパスにマッチします。<br />
55       * たとえば、 <code>suffix</code> に <code>View</code>、<code>ext</code> に
56       * <code>xml</code> を指定すると、 <code>*View.xml</code> にマッチします。
57       * 
58       * @param suffix
59       *            ファイル名サフィックス
60       * @param ext
61       *            拡張子
62       */
63      public ExtFileFilter(final String suffix, final String ext) {
64          this.ext = ext;
65          this.suffix = suffix;
66          createMatchString();
67      }
68  
69      private void createMatchString() {
70          StringBuilder builder = new StringBuilder();
71          if (suffix != null) {
72              builder.append(suffix);
73              builder.append(PERIOD);
74          }
75  
76          if (ext != null) {
77              builder.append(ext);
78          }
79  
80          match = builder.toString();
81      }
82  
83      /*
84       * @see java.io.FileFilter#accept(java.io.File)
85       */
86      public boolean accept(final File pathname) {
87          String path = pathname.getPath();
88          if (path != null) {
89              return path.endsWith(match) ? true : false;
90          } else {
91              return false;
92          }
93      }
94  
95  }