Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PathFilter |
|
| 2.3333333333333335;2.333 |
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 | 17 | super(wildcardmatcher, ignoreCase ? IOCase.INSENSITIVE : IOCase.SENSITIVE); |
48 | 17 | this.caseSensitivity = ignoreCase ? IOCase.INSENSITIVE : IOCase.SENSITIVE; |
49 | 17 | this.wildcard = wildcardmatcher; |
50 | 17 | } |
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 | 0 | String path = new File(dir, name).toString(); |
62 | 0 | if (FilenameUtils.wildcardMatch(path, wildcard, caseSensitivity)) { |
63 | 0 | return true; |
64 | } | |
65 | 0 | 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 | 268 | String name = file.getPath(); |
76 | 268 | if (FilenameUtils.wildcardMatch(name, wildcard, caseSensitivity)) { |
77 | 208 | return true; |
78 | } | |
79 | 60 | return false; |
80 | } | |
81 | ||
82 | } |