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} 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: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $ 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 public class WildcardFilter extends AbstractFileFilter implements Serializable { 054 055 /** The wildcards that will be used to match filenames. */ 056 private final String[] wildcards; 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 WildcardFilter(String wildcard) { 065 if (wildcard == null) { 066 throw new IllegalArgumentException("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("The wildcard array must not be null"); 080 } 081 this.wildcards = wildcards; 082 } 083 084 /** 085 * Construct a new case-sensitive wildcard filter for a list of wildcards. 086 * 087 * @param wildcards the list of wildcards to match 088 * @throws IllegalArgumentException if the pattern list is null 089 * @throws ClassCastException if the list does not contain Strings 090 */ 091 public WildcardFilter(List wildcards) { 092 if (wildcards == null) { 093 throw new IllegalArgumentException("The wildcard list must not be null"); 094 } 095 this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]); 096 } 097 098 //----------------------------------------------------------------------- 099 /** 100 * Checks to see if the filename matches one of the wildcards. 101 * 102 * @param dir the file directory 103 * @param name the filename 104 * @return true if the filename matches one of the wildcards 105 */ 106 public boolean accept(File dir, String name) { 107 if (dir != null && new File(dir, name).isDirectory()) { 108 return false; 109 } 110 111 for (int i = 0; i < wildcards.length; i++) { 112 if (FilenameUtils.wildcardMatch(name, wildcards[i])) { 113 return true; 114 } 115 } 116 117 return false; 118 } 119 120 /** 121 * Checks to see if the filename matches one of the wildcards. 122 * 123 * @param file the file to check 124 * @return true if the filename matches one of the wildcards 125 */ 126 public boolean accept(File file) { 127 if (file.isDirectory()) { 128 return false; 129 } 130 131 for (int i = 0; i < wildcards.length; i++) { 132 if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) { 133 return true; 134 } 135 } 136 137 return false; 138 } 139 140 }