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 */
17package org.apache.commons.io.filefilter;
18
19import java.io.File;
20import java.io.Serializable;
21import java.util.List;
22
23import org.apache.commons.io.FilenameUtils;
24import org.apache.commons.io.IOCase;
25
26/**
27 * Filters files using the supplied wildcards.
28 * <p>
29 * This filter selects files and directories based on one or more wildcards.
30 * Testing is case-sensitive by default, but this can be configured.
31 * <p>
32 * The wildcard matcher uses the characters '?' and '*' to represent a
33 * single or multiple wildcard characters.
34 * This is the same as often found on Dos/Unix command lines.
35 * The extension check is case-sensitive by .
36 * See {@link FilenameUtils#wildcardMatchOnSystem} for more information.
37 * <p>
38 * For example:
39 * <pre>
40 * File dir = new File(".");
41 * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
42 * File[] files = dir.listFiles(fileFilter);
43 * for (int i = 0; i < files.length; i++) {
44 *   System.out.println(files[i]);
45 * }
46 * </pre>
47 *
48 * @author Jason Anderson
49 * @version $Revision: 155419 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
50 * @since Commons IO 1.3
51 */
52public class WildcardFileFilter extends AbstractFileFilter implements Serializable {
53
54    /** The wildcards that will be used to match filenames. */
55    private final String[] wildcards;
56    /** Whether the comparison is case sensitive. */
57    private final IOCase caseSensitivity;
58
59    /**
60     * Construct a new case-sensitive wildcard filter for a single wildcard.
61     *
62     * @param wildcard  the wildcard to match
63     * @throws IllegalArgumentException if the pattern is null
64     */
65    public WildcardFileFilter(String wildcard) {
66        this(wildcard, null);
67    }
68
69    /**
70     * Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
71     *
72     * @param wildcard  the wildcard to match, not null
73     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
74     * @throws IllegalArgumentException if the pattern is null
75     */
76    public WildcardFileFilter(String wildcard, IOCase caseSensitivity) {
77        if (wildcard == null) {
78            throw new IllegalArgumentException("The wildcard must not be null");
79        }
80        this.wildcards = new String[] { wildcard };
81        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
82    }
83
84    /**
85     * Construct a new case-sensitive wildcard filter for an array of wildcards.
86     * <p>
87     * The array is not cloned, so could be changed after constructing the
88     * instance. This would be inadvisable however.
89     *
90     * @param wildcards  the array of wildcards to match
91     * @throws IllegalArgumentException if the pattern array is null
92     */
93    public WildcardFileFilter(String[] wildcards) {
94        this(wildcards, null);
95    }
96
97    /**
98     * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
99     * <p>
100     * The array is not cloned, so could be changed after constructing the
101     * instance. This would be inadvisable however.
102     *
103     * @param wildcards  the array of wildcards to match, not null
104     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
105     * @throws IllegalArgumentException if the pattern array is null
106     */
107    public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) {
108        if (wildcards == null) {
109            throw new IllegalArgumentException("The wildcard array must not be null");
110        }
111        this.wildcards = wildcards;
112        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
113    }
114
115    /**
116     * Construct a new case-sensitive wildcard filter for a list of wildcards.
117     *
118     * @param wildcards  the list of wildcards to match, not null
119     * @throws IllegalArgumentException if the pattern list is null
120     * @throws ClassCastException if the list does not contain Strings
121     */
122    public WildcardFileFilter(List<String> wildcards) {
123        this(wildcards, null);
124    }
125
126    /**
127     * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
128     *
129     * @param wildcards  the list of wildcards to match, not null
130     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
131     * @throws IllegalArgumentException if the pattern list is null
132     * @throws ClassCastException if the list does not contain Strings
133     */
134    public WildcardFileFilter(List<String> wildcards, IOCase caseSensitivity) {
135        if (wildcards == null) {
136            throw new IllegalArgumentException("The wildcard list must not be null");
137        }
138        this.wildcards = wildcards.toArray(new String[wildcards.size()]);
139        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
140    }
141
142    //-----------------------------------------------------------------------
143    /**
144     * Checks to see if the filename matches one of the wildcards.
145     *
146     * @param dir  the file directory
147     * @param name  the filename
148     * @return true if the filename matches one of the wildcards
149     */
150    @Override
151    public boolean accept(File dir, String name) {
152        for (int i = 0; i < wildcards.length; i++) {
153            if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
154                return true;
155            }
156        }
157        return false;
158    }
159
160    /**
161     * Checks to see if the filename matches one of the wildcards.
162     *
163     * @param file  the file to check
164     * @return true if the filename matches one of the wildcards
165     */
166    @Override
167    public boolean accept(File file) {
168        String name = file.getName();
169        for (int i = 0; i < wildcards.length; i++) {
170            if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
171                return true;
172            }
173        }
174        return false;
175    }
176
177    /**
178     * Provide a String representaion of this file filter.
179     *
180     * @return a String representaion
181     */
182    @Override
183    public String toString() {
184        StringBuffer buffer = new StringBuffer();
185        buffer.append(super.toString());
186        buffer.append("(");
187        if (wildcards != null) {
188            for (int i = 0; i < wildcards.length; i++) {
189                if (i > 0) {
190                    buffer.append(",");
191                }
192                buffer.append(wildcards[i]);
193            }
194        }
195        buffer.append(")");
196        return buffer.toString();
197    }
198
199}
200