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.util.resource.impl;
17  
18  import org.seasar.uruma.util.resource.ResourceFilter;
19  
20  /**
21   * 任意の拡張子にマッチする {@link ResourceFilter} です。<br />
22   * 
23   * @author y-komori
24   */
25  public class ExtResourceFilter implements ResourceFilter {
26      private static final String PERIOD = ".";
27  
28      private static final String NULL_STRING = "";
29  
30      private String suffix;
31  
32      private String ext;
33  
34      private String match = NULL_STRING;
35  
36      /**
37       * {@link ExtResourceFilter} を構築します。<br />
38       * <code>ext</code> で指定した拡張子にマッチします。<br />
39       * たとえば、<code>xml</code> という文字列を指定した場合、<code>.xml</code>
40       * で終わるパスにマッチします。<br />
41       * 
42       * @param ext
43       *            拡張子
44       */
45      public ExtResourceFilter(final String ext) {
46          this.ext = ext;
47  
48          createMatchString();
49      }
50  
51      /**
52       * {@link ExtResourceFilter} を構築します。<br />
53       * ファイル名部分が <code>suffix</code> で終了し、拡張子 <code>ext</code> をもつパスにマッチします。<br />
54       * たとえば、 <code>suffix</code> に <code>View</code>、<code>ext</code> に
55       * <code>xml</code> を指定すると、 <code>*View.xml</code> にマッチします。
56       * 
57       * @param suffix
58       *            ファイル名サフィックス
59       * @param ext
60       *            拡張子
61       */
62      public ExtResourceFilter(final String suffix, final String ext) {
63          this.ext = ext;
64          this.suffix = suffix;
65          createMatchString();
66      }
67  
68      private void createMatchString() {
69          StringBuilder builder = new StringBuilder();
70          if (suffix != null) {
71              builder.append(suffix);
72              builder.append(PERIOD);
73          }
74  
75          if (ext != null) {
76              builder.append(ext);
77          }
78  
79          match = builder.toString();
80      }
81  
82      /*
83       * @see org.seasar.uruma.util.resource.ResourceFilter#accept(java.lang.String)
84       */
85      public boolean accept(final String path) {
86          if (path != null) {
87              return path.endsWith(match) ? true : false;
88          } else {
89              return false;
90          }
91      }
92  }