Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RegexFilter |
|
| 1.2;1.2 |
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 java.util.regex.Pattern; | |
21 | import org.apache.commons.io.filefilter.AbstractFileFilter; | |
22 | ||
23 | /** | |
24 | * <code>IOFileFilter</code> implementation that uses regular | |
25 | * expression matching on the file path. | |
26 | * | |
27 | * @version $Id: RegexFilter.java 437543 2006-08-28 05:47:51Z bayard $ | |
28 | * @since 0.1 | |
29 | */ | |
30 | public class RegexFilter extends AbstractFileFilter { | |
31 | ||
32 | private boolean ignoreCase; | |
33 | private String regex; | |
34 | private Pattern pattern; | |
35 | ||
36 | /** | |
37 | * Construct a new <code>IOFileFilter</code> specifying the | |
38 | * regular expresion criteria. | |
39 | * | |
40 | * @param regex The regular expression. | |
41 | * @param ignoreCase Whether the case should be ignored. | |
42 | */ | |
43 | public RegexFilter(String regex, boolean ignoreCase) { | |
44 | 2 | super(); |
45 | 2 | this.regex = regex; |
46 | 2 | this.ignoreCase = ignoreCase; |
47 | 2 | if (ignoreCase) { |
48 | 1 | pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); |
49 | 1 | } else { |
50 | 1 | pattern = Pattern.compile(regex); |
51 | } | |
52 | 2 | } |
53 | ||
54 | /** | |
55 | * Indicates whether the case should be ignored. | |
56 | * | |
57 | * @return <code>true</code> if the case should be ignored, | |
58 | * otherwise <code>false</code> | |
59 | */ | |
60 | public boolean isIgnoreCase() { | |
61 | 0 | return ignoreCase; |
62 | } | |
63 | ||
64 | /** | |
65 | * Returnt the regular expression. | |
66 | * | |
67 | * @return The regular expression. | |
68 | */ | |
69 | public String getRegex() { | |
70 | 0 | return regex; |
71 | } | |
72 | ||
73 | /** | |
74 | * Tests whether the {@link File} can | |
75 | * matches a specified regular expression. | |
76 | * | |
77 | * @param file The {@link File} to test. | |
78 | * @return <code>true</code> if the {@link File} | |
79 | * matches the regular expression, otherwise | |
80 | * <code>false</code>. | |
81 | */ | |
82 | public boolean accept(File file) { | |
83 | 8 | return pattern.matcher(file.getPath()).matches(); |
84 | } | |
85 | ||
86 | /** | |
87 | * Return a String representation of this class. | |
88 | * @return a String representation of the class. | |
89 | */ | |
90 | public String toString() { | |
91 | 0 | return "RefexFilter{regex=[" + regex + "]" + |
92 | ", ignoreCase=" + ignoreCase + "}"; | |
93 | } | |
94 | } |