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.io.Serializable;
021 import java.util.List;
022
023 import org.apache.commons.io.FilenameUtils;
024
025 /**
026 * Filters files using the supplied wildcards.
027 * <p>
028 * This filter selects files, but not directories, based on one or more wildcards
029 * and using case-sensitive comparison.
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.
035 * See {@link FilenameUtils#wildcardMatch(String, String)} for more information.
036 * <p>
037 * For example:
038 * <pre>
039 * File dir = new File(".");
040 * FileFilter fileFilter = new WildcardFilter("*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: 1004077 $ $Date: 2010-10-04 01:58:42 +0100 (Mon, 04 Oct 2010) $
049 * @since Commons IO 1.1
050 * @deprecated Use WilcardFileFilter. Deprecated as this class performs directory
051 * filtering which it shouldn't do, but that can't be removed due to compatability.
052 */
053 @Deprecated
054 public class WildcardFilter extends AbstractFileFilter implements Serializable {
055
056 /** The wildcards that will be used to match filenames. */
057 private final String[] wildcards;
058
059 /**
060 * Construct a new case-sensitive wildcard filter for a single wildcard.
061 *
062 * @param wildcard the wildcard to match
063 * @throws IllegalArgumentException if the pattern is null
064 */
065 public WildcardFilter(String wildcard) {
066 if (wildcard == null) {
067 throw new IllegalArgumentException("The wildcard must not be null");
068 }
069 this.wildcards = new String[] { wildcard };
070 }
071
072 /**
073 * Construct a new case-sensitive wildcard filter for an array of wildcards.
074 *
075 * @param wildcards the array of wildcards to match
076 * @throws IllegalArgumentException if the pattern array is null
077 */
078 public WildcardFilter(String[] wildcards) {
079 if (wildcards == null) {
080 throw new IllegalArgumentException("The wildcard array must not be null");
081 }
082 this.wildcards = new String[wildcards.length];
083 System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
084 }
085
086 /**
087 * Construct a new case-sensitive wildcard filter for a list of wildcards.
088 *
089 * @param wildcards the list of wildcards to match
090 * @throws IllegalArgumentException if the pattern list is null
091 * @throws ClassCastException if the list does not contain Strings
092 */
093 public WildcardFilter(List<String> wildcards) {
094 if (wildcards == null) {
095 throw new IllegalArgumentException("The wildcard list must not be null");
096 }
097 this.wildcards = wildcards.toArray(new String[wildcards.size()]);
098 }
099
100 //-----------------------------------------------------------------------
101 /**
102 * Checks to see if the filename matches one of the wildcards.
103 *
104 * @param dir the file directory
105 * @param name the filename
106 * @return true if the filename matches one of the wildcards
107 */
108 @Override
109 public boolean accept(File dir, String name) {
110 if (dir != null && new File(dir, name).isDirectory()) {
111 return false;
112 }
113
114 for (String wildcard : wildcards) {
115 if (FilenameUtils.wildcardMatch(name, wildcard)) {
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 @Override
130 public boolean accept(File file) {
131 if (file.isDirectory()) {
132 return false;
133 }
134
135 for (String wildcard : wildcards) {
136 if (FilenameUtils.wildcardMatch(file.getName(), wildcard)) {
137 return true;
138 }
139 }
140
141 return false;
142 }
143
144 }