View Javadoc

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   */
17  package org.apache.commons.io.filefilter;
18  
19  import java.io.File;
20  import java.util.List;
21  
22  import org.apache.commons.io.FilenameUtils;
23  
24  /**
25   * Filters files using the supplied wildcards.
26   * <p>
27   * This filter selects files, but not directories, based on one or more wildcards
28   * and using case-sensitive comparison.
29   * <p>
30   * The wildcard matcher uses the characters '?' and '*' to represent a
31   * single or multiple wildcard characters.
32   * This is the same as often found on Dos/Unix command lines.
33   * The extension check is case-sensitive.
34   * See {@link FilenameUtils#wildcardMatch} for more information.
35   * <p>
36   * For example:
37   * <pre>
38   * File dir = new File(".");
39   * FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
40   * File[] files = dir.listFiles(fileFilter);
41   * for (int i = 0; i < files.length; i++) {
42   *   System.out.println(files[i]);
43   * }
44   * </pre>
45   *
46   * @author Jason Anderson
47   * @version $Revision: 437680 $ $Date: 2006-08-28 13:57:00 +0200 (Mo, 28 Aug 2006) $
48   * @since Commons IO 1.1
49   * @deprecated Use WilcardFileFilter. Deprecated as this class performs directory
50   * filtering which it shouldn't do, but that can't be removed due to compatability.
51   */
52  public class WildcardFilter extends AbstractFileFilter {
53  
54      /** The wildcards that will be used to match filenames. */
55      private String[] wildcards;
56  
57      /**
58       * Construct a new case-sensitive wildcard filter for a single wildcard.
59       *
60       * @param wildcard  the wildcard to match
61       * @throws IllegalArgumentException if the pattern is null
62       */
63      public WildcardFilter(String wildcard) {
64          if (wildcard == null) {
65              throw new IllegalArgumentException("The wildcard must not be null");
66          }
67          this.wildcards = new String[] { wildcard };
68      }
69  
70      /**
71       * Construct a new case-sensitive wildcard filter for an array of wildcards.
72       *
73       * @param wildcards  the array of wildcards to match
74       * @throws IllegalArgumentException if the pattern array is null
75       */
76      public WildcardFilter(String[] wildcards) {
77          if (wildcards == null) {
78              throw new IllegalArgumentException("The wildcard array must not be null");
79          }
80          this.wildcards = wildcards;
81      }
82  
83      /**
84       * Construct a new case-sensitive wildcard filter for a list of wildcards.
85       *
86       * @param wildcards  the list of wildcards to match
87       * @throws IllegalArgumentException if the pattern list is null
88       * @throws ClassCastException if the list does not contain Strings
89       */
90      public WildcardFilter(List wildcards) {
91          if (wildcards == null) {
92              throw new IllegalArgumentException("The wildcard list must not be null");
93          }
94          this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
95      }
96  
97      //-----------------------------------------------------------------------
98      /**
99       * Checks to see if the filename matches one of the wildcards.
100      *
101      * @param dir  the file directory
102      * @param name  the filename
103      * @return true if the filename matches one of the wildcards
104      */
105     public boolean accept(File dir, String name) {
106         if (dir != null && new File(dir, name).isDirectory()) {
107             return false;
108         }
109         
110         for (int i = 0; i < wildcards.length; i++) {
111             if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
112                 return true;
113             }
114         }
115         
116         return false;
117     }
118 
119     /**
120      * Checks to see if the filename matches one of the wildcards.
121      *
122      * @param file the file to check
123      * @return true if the filename matches one of the wildcards
124      */
125     public boolean accept(File file) {
126         if (file.isDirectory()) {
127             return false;
128         }
129         
130         for (int i = 0; i < wildcards.length; i++) {
131             if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) {
132                 return true;
133             }
134         }
135         
136         return false;
137     }
138 
139 }