WildcardFileFilter.java revision 8978aac1977408b05e386ae846c30920c7faa0a6
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 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 wildcards, IOCase caseSensitivity) { 135 if (wildcards == null) { 136 throw new IllegalArgumentException("The wildcard list must not be null"); 137 } 138 this.wildcards = (String[]) 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 public boolean accept(File dir, String name) { 151 for (int i = 0; i < wildcards.length; i++) { 152 if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) { 153 return true; 154 } 155 } 156 return false; 157 } 158 159 /** 160 * Checks to see if the filename matches one of the wildcards. 161 * 162 * @param file the file to check 163 * @return true if the filename matches one of the wildcards 164 */ 165 public boolean accept(File file) { 166 String name = file.getName(); 167 for (int i = 0; i < wildcards.length; i++) { 168 if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) { 169 return true; 170 } 171 } 172 return false; 173 } 174 175 /** 176 * Provide a String representaion of this file filter. 177 * 178 * @return a String representaion 179 */ 180 public String toString() { 181 StringBuffer buffer = new StringBuffer(); 182 buffer.append(super.toString()); 183 buffer.append("("); 184 if (wildcards != null) { 185 for (int i = 0; i < wildcards.length; i++) { 186 if (i > 0) { 187 buffer.append(","); 188 } 189 buffer.append(wildcards[i]); 190 } 191 } 192 buffer.append(")"); 193 return buffer.toString(); 194 } 195 196} 197