View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.finder.filters;
18  
19  import java.io.File;
20  import org.apache.commons.io.filefilter.WildcardFileFilter;
21  import org.apache.commons.io.FilenameUtils;
22  import org.apache.commons.io.IOCase;
23  
24  /**
25   * <code>IOFileFilter</code> implementation that uses wildcard matching on the file's path.
26   * 
27   * @version $Id: PathFilter.java 437543 2006-08-28 05:47:51Z bayard $
28   * @since 0.1
29   */
30  public class PathFilter extends WildcardFileFilter {
31  
32      // Ideally this would just use the same variable in the parent, but it's
33      // not accessible
34      private IOCase caseSensitivity;
35  
36      // Same here
37      private String wildcard;
38  
39      /**
40       * Construct a new <code>IOFileFilter</code> specifying whether the
41       * wildcard criteria.
42       * 
43       * @param wildcardmatcher the wildcard string to match against
44       * @param ignoreCase whether to ignore the case
45       */
46      public PathFilter(String wildcardmatcher, boolean ignoreCase) {
47          super(wildcardmatcher, ignoreCase ? IOCase.INSENSITIVE : IOCase.SENSITIVE);
48          this.caseSensitivity = ignoreCase ? IOCase.INSENSITIVE : IOCase.SENSITIVE;
49          this.wildcard = wildcardmatcher;
50      }
51  
52  
53      /**
54       * Checks to see if the filename matches the wildcard.
55       *
56       * @param dir  the file directory
57       * @param name  the full filename
58       * @return true if the full filename matches the wildcard
59       */
60      public boolean accept(File dir, String name) {
61          String path = new File(dir, name).toString();
62          if (FilenameUtils.wildcardMatch(path, wildcard, caseSensitivity)) {
63              return true;
64          }
65          return false;
66      }
67  
68      /**
69       * Checks to see if the full filename matches the wildcard.
70       *
71       * @param file  the file to check
72       * @return true if the full filename matches the wildcard
73       */
74      public boolean accept(File file) {
75          String name = file.getPath();
76          if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) {
77              return true;
78          }
79          return false;
80      }
81  
82  }