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:
024: /**
025: * Filters files using the supplied wildcards.
026: * <p>
027: * This filter selects files, but not directories, based on one or more wildcards
028: * and using case-sensitive comparison.
029: * <p>
030: * The wildcard matcher uses the characters '?' and '*' to represent a
031: * single or multiple wildcard characters.
032: * This is the same as often found on Dos/Unix command lines.
033: * The extension check is case-sensitive.
034: * See {@link FilenameUtils#wildcardMatch} for more information.
035: * <p>
036: * For example:
037: * <pre>
038: * File dir = new File(".");
039: * FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
040: * File[] files = dir.listFiles(fileFilter);
041: * for (int i = 0; i < files.length; i++) {
042: * System.out.println(files[i]);
043: * }
044: * </pre>
045: *
046: * @author Jason Anderson
047: * @version $Revision: 437680 $ $Date: 2006-08-28 13:57:00 +0200 (Mo, 28 Aug 2006) $
048: * @since Commons IO 1.1
049: * @deprecated Use WilcardFileFilter. Deprecated as this class performs directory
050: * filtering which it shouldn't do, but that can't be removed due to compatability.
051: */
052: public class WildcardFilter extends AbstractFileFilter {
053:
054: /** The wildcards that will be used to match filenames. */
055: private String[] wildcards;
056:
057: /**
058: * Construct a new case-sensitive wildcard filter for a single wildcard.
059: *
060: * @param wildcard the wildcard to match
061: * @throws IllegalArgumentException if the pattern is null
062: */
063: public WildcardFilter(String wildcard) {
064: if (wildcard == null) {
065: throw new IllegalArgumentException(
066: "The wildcard must not be null");
067: }
068: this .wildcards = new String[] { wildcard };
069: }
070:
071: /**
072: * Construct a new case-sensitive wildcard filter for an array of wildcards.
073: *
074: * @param wildcards the array of wildcards to match
075: * @throws IllegalArgumentException if the pattern array is null
076: */
077: public WildcardFilter(String[] wildcards) {
078: if (wildcards == null) {
079: throw new IllegalArgumentException(
080: "The wildcard array must not be null");
081: }
082: this .wildcards = wildcards;
083: }
084:
085: /**
086: * Construct a new case-sensitive wildcard filter for a list of wildcards.
087: *
088: * @param wildcards the list of wildcards to match
089: * @throws IllegalArgumentException if the pattern list is null
090: * @throws ClassCastException if the list does not contain Strings
091: */
092: public WildcardFilter(List wildcards) {
093: if (wildcards == null) {
094: throw new IllegalArgumentException(
095: "The wildcard list must not be null");
096: }
097: this .wildcards = (String[]) wildcards
098: .toArray(new String[wildcards.size()]);
099: }
100:
101: //-----------------------------------------------------------------------
102: /**
103: * Checks to see if the filename matches one of the wildcards.
104: *
105: * @param dir the file directory
106: * @param name the filename
107: * @return true if the filename matches one of the wildcards
108: */
109: public boolean accept(File dir, String name) {
110: if (dir != null && new File(dir, name).isDirectory()) {
111: return false;
112: }
113:
114: for (int i = 0; i < wildcards.length; i++) {
115: if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
116: return true;
117: }
118: }
119:
120: return false;
121: }
122:
123: /**
124: * Checks to see if the filename matches one of the wildcards.
125: *
126: * @param file the file to check
127: * @return true if the filename matches one of the wildcards
128: */
129: public boolean accept(File file) {
130: if (file.isDirectory()) {
131: return false;
132: }
133:
134: for (int i = 0; i < wildcards.length; i++) {
135: if (FilenameUtils.wildcardMatch(file.getName(),
136: wildcards[i])) {
137: return true;
138: }
139: }
140:
141: return false;
142: }
143:
144: }
|