001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.filefilter;
018:
019: import java.io.File;
020: import java.util.List;
021:
022: import org.apache.commons.io.FilenameUtils;
023: import org.apache.commons.io.IOCase;
024:
025: /**
026: * Filters files using the supplied wildcards.
027: * <p>
028: * This filter selects files and directories based on one or more wildcards.
029: * Testing is case-sensitive by default, but this can be configured.
030: * <p>
031: * The wildcard matcher uses the characters '?' and '*' to represent a
032: * single or multiple wildcard characters.
033: * This is the same as often found on Dos/Unix command lines.
034: * The extension check is case-sensitive by .
035: * See {@link FilenameUtils#wildcardMatchOnSystem} for more information.
036: * <p>
037: * For example:
038: * <pre>
039: * File dir = new File(".");
040: * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
041: * File[] files = dir.listFiles(fileFilter);
042: * for (int i = 0; i < files.length; i++) {
043: * System.out.println(files[i]);
044: * }
045: * </pre>
046: *
047: * @author Jason Anderson
048: * @version $Revision: 155419 $ $Date: 2006-08-28 13:57:00 +0200 (Mo, 28 Aug 2006) $
049: * @since Commons IO 1.3
050: */
051: public class WildcardFileFilter extends AbstractFileFilter {
052:
053: /** The wildcards that will be used to match filenames. */
054: private String[] wildcards;
055: /** Whether the comparison is case sensitive. */
056: private IOCase caseSensitivity;
057:
058: /**
059: * Construct a new case-sensitive wildcard filter for a single wildcard.
060: *
061: * @param wildcard the wildcard to match
062: * @throws IllegalArgumentException if the pattern is null
063: */
064: public WildcardFileFilter(String wildcard) {
065: this (wildcard, null);
066: }
067:
068: /**
069: * Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
070: *
071: * @param wildcard the wildcard to match, not null
072: * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
073: * @throws IllegalArgumentException if the pattern is null
074: */
075: public WildcardFileFilter(String wildcard, IOCase caseSensitivity) {
076: if (wildcard == null) {
077: throw new IllegalArgumentException(
078: "The wildcard must not be null");
079: }
080: this .wildcards = new String[] { wildcard };
081: this .caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE
082: : caseSensitivity);
083: }
084:
085: /**
086: * Construct a new case-sensitive wildcard filter for an array of wildcards.
087: * <p>
088: * The array is not cloned, so could be changed after constructing the
089: * instance. This would be inadvisable however.
090: *
091: * @param wildcards the array of wildcards to match
092: * @throws IllegalArgumentException if the pattern array is null
093: */
094: public WildcardFileFilter(String[] wildcards) {
095: this (wildcards, null);
096: }
097:
098: /**
099: * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
100: * <p>
101: * The array is not cloned, so could be changed after constructing the
102: * instance. This would be inadvisable however.
103: *
104: * @param wildcards the array of wildcards to match, not null
105: * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
106: * @throws IllegalArgumentException if the pattern array is null
107: */
108: public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) {
109: if (wildcards == null) {
110: throw new IllegalArgumentException(
111: "The wildcard array must not be null");
112: }
113: this .wildcards = wildcards;
114: this .caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE
115: : caseSensitivity);
116: }
117:
118: /**
119: * Construct a new case-sensitive wildcard filter for a list of wildcards.
120: *
121: * @param wildcards the list of wildcards to match, not null
122: * @throws IllegalArgumentException if the pattern list is null
123: * @throws ClassCastException if the list does not contain Strings
124: */
125: public WildcardFileFilter(List wildcards) {
126: this (wildcards, null);
127: }
128:
129: /**
130: * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
131: *
132: * @param wildcards the list of wildcards to match, not null
133: * @param caseSensitivity how to handle case sensitivity, null means case-sensitive
134: * @throws IllegalArgumentException if the pattern list is null
135: * @throws ClassCastException if the list does not contain Strings
136: */
137: public WildcardFileFilter(List wildcards, IOCase caseSensitivity) {
138: if (wildcards == null) {
139: throw new IllegalArgumentException(
140: "The wildcard list must not be null");
141: }
142: this .wildcards = (String[]) wildcards
143: .toArray(new String[wildcards.size()]);
144: this .caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE
145: : caseSensitivity);
146: }
147:
148: //-----------------------------------------------------------------------
149: /**
150: * Checks to see if the filename matches one of the wildcards.
151: *
152: * @param dir the file directory
153: * @param name the filename
154: * @return true if the filename matches one of the wildcards
155: */
156: public boolean accept(File dir, String name) {
157: for (int i = 0; i < wildcards.length; i++) {
158: if (FilenameUtils.wildcardMatch(name, wildcards[i],
159: caseSensitivity)) {
160: return true;
161: }
162: }
163: return false;
164: }
165:
166: /**
167: * Checks to see if the filename matches one of the wildcards.
168: *
169: * @param file the file to check
170: * @return true if the filename matches one of the wildcards
171: */
172: public boolean accept(File file) {
173: String name = file.getName();
174: for (int i = 0; i < wildcards.length; i++) {
175: if (FilenameUtils.wildcardMatch(name, wildcards[i],
176: caseSensitivity)) {
177: return true;
178: }
179: }
180: return false;
181: }
182:
183: }
|