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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.io.filefilter;
20
import java.util.List;
22
import org.apache.commons.io.FilenameUtils;
25
* Filters files using the supplied wildcards.
27
* This filter selects files, but not directories, based on one or more wildcards
28
* and using case-sensitive comparison.
30
* The wildcard matcher uses the characters '?' and '*' to represent a
31
* single or multiple wildcard characters.
32
* This is the same as often found on Dos/Unix command lines.
33
* The extension check is case-sensitive.
34
* See {@link FilenameUtils#wildcardMatch} for more information.
38
* File dir = new File(".");
39
* FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
40
* File[] files = dir.listFiles(fileFilter);
41
* for (int i = 0; i < files.length; i++) {
42
* System.out.println(files[i]);
46
* @author Jason Anderson
47
* @version $Revision: 437680 $ $Date: 2006-08-28 04:57:00 -0700 (Mon, 28 Aug 2006) $
48
* @since Commons IO 1.1
49
* @deprecated Use WilcardFileFilter. Deprecated as this class performs directory
50
* filtering which it shouldn't do, but that can't be removed due to compatability.
52
public class WildcardFilter extends AbstractFileFilter {
54
/** The wildcards that will be used to match filenames. */
55
private String[] wildcards;
58
* Construct a new case-sensitive wildcard filter for a single wildcard.
60
* @param wildcard the wildcard to match
61
* @throws IllegalArgumentException if the pattern is null
63
public WildcardFilter(String wildcard) {
64
if (wildcard == null) {
65
throw new IllegalArgumentException("The wildcard must not be null");
67
this.wildcards = new String[] { wildcard };
71
* Construct a new case-sensitive wildcard filter for an array of wildcards.
73
* @param wildcards the array of wildcards to match
74
* @throws IllegalArgumentException if the pattern array is null
76
public WildcardFilter(String[] wildcards) {
77
if (wildcards == null) {
78
throw new IllegalArgumentException("The wildcard array must not be null");
80
this.wildcards = wildcards;
84
* Construct a new case-sensitive wildcard filter for a list of wildcards.
86
* @param wildcards the list of wildcards to match
87
* @throws IllegalArgumentException if the pattern list is null
88
* @throws ClassCastException if the list does not contain Strings
90
public WildcardFilter(List wildcards) {
91
if (wildcards == null) {
92
throw new IllegalArgumentException("The wildcard list must not be null");
94
this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
97
//-----------------------------------------------------------------------
99
* Checks to see if the filename matches one of the wildcards.
101
* @param dir the file directory
102
* @param name the filename
103
* @return true if the filename matches one of the wildcards
105
public boolean accept(File dir, String name) {
106
if (dir != null && new File(dir, name).isDirectory()) {
110
for (int i = 0; i < wildcards.length; i++) {
111
if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
120
* Checks to see if the filename matches one of the wildcards.
122
* @param file the file to check
123
* @return true if the filename matches one of the wildcards
125
public boolean accept(File file) {
126
if (file.isDirectory()) {
130
for (int i = 0; i < wildcards.length; i++) {
131
if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) {