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  import org.apache.commons.io.IOCase;
24  
25  /**
26   * Filters files using the supplied wildcards.
27   * <p>
28   * This filter selects files and directories based on one or more wildcards.
29   * Testing is case-sensitive by default, but this can be configured.
30   * <p>
31   * The wildcard matcher uses the characters '?' and '*' to represent a
32   * single or multiple wildcard characters.
33   * This is the same as often found on Dos/Unix command lines.
34   * The extension check is case-sensitive by .
35   * See {@link FilenameUtils#wildcardMatchOnSystem} for more information.
36   * <p>
37   * For example:
38   * <pre>
39   * File dir = new File(".");
40   * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
41   * File[] files = dir.listFiles(fileFilter);
42   * for (int i = 0; i < files.length; i++) {
43   *   System.out.println(files[i]);
44   * }
45   * </pre>
46   *
47   * @author Jason Anderson
48   * @version $Revision: 155419 $ $Date: 2006-08-28 13:57:00 +0200 (Mo, 28 Aug 2006) $
49   * @since Commons IO 1.3
50   */
51  public class WildcardFileFilter extends AbstractFileFilter {
52  
53      /** The wildcards that will be used to match filenames. */
54      private String[] wildcards;
55      /** Whether the comparison is case sensitive. */
56      private IOCase caseSensitivity;
57  
58      /**
59       * Construct a new case-sensitive wildcard filter for a single wildcard.
60       *
61       * @param wildcard  the wildcard to match
62       * @throws IllegalArgumentException if the pattern is null
63       */
64      public WildcardFileFilter(String wildcard) {
65          this(wildcard, null);
66      }
67  
68      /**
69       * Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
70       *
71       * @param wildcard  the wildcard to match, not null
72       * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
73       * @throws IllegalArgumentException if the pattern is null
74       */
75      public WildcardFileFilter(String wildcard, IOCase caseSensitivity) {
76          if (wildcard == null) {
77              throw new IllegalArgumentException("The wildcard must not be null");
78          }
79          this.wildcards = new String[] { wildcard };
80          this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
81      }
82  
83      /**
84       * Construct a new case-sensitive wildcard filter for an array of wildcards.
85       * <p>
86       * The array is not cloned, so could be changed after constructing the
87       * instance. This would be inadvisable however.
88       *
89       * @param wildcards  the array of wildcards to match
90       * @throws IllegalArgumentException if the pattern array is null
91       */
92      public WildcardFileFilter(String[] wildcards) {
93          this(wildcards, null);
94      }
95  
96      /**
97       * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
98       * <p>
99       * The array is not cloned, so could be changed after constructing the
100      * instance. This would be inadvisable however.
101      *
102      * @param wildcards  the array of wildcards to match, not null
103      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
104      * @throws IllegalArgumentException if the pattern array is null
105      */
106     public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) {
107         if (wildcards == null) {
108             throw new IllegalArgumentException("The wildcard array must not be null");
109         }
110         this.wildcards = wildcards;
111         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
112     }
113 
114     /**
115      * Construct a new case-sensitive wildcard filter for a list of wildcards.
116      *
117      * @param wildcards  the list of wildcards to match, not null
118      * @throws IllegalArgumentException if the pattern list is null
119      * @throws ClassCastException if the list does not contain Strings
120      */
121     public WildcardFileFilter(List wildcards) {
122         this(wildcards, null);
123     }
124 
125     /**
126      * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
127      *
128      * @param wildcards  the list of wildcards to match, not null
129      * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
130      * @throws IllegalArgumentException if the pattern list is null
131      * @throws ClassCastException if the list does not contain Strings
132      */
133     public WildcardFileFilter(List wildcards, IOCase caseSensitivity) {
134         if (wildcards == null) {
135             throw new IllegalArgumentException("The wildcard list must not be null");
136         }
137         this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
138         this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
139     }
140 
141     //-----------------------------------------------------------------------
142     /**
143      * Checks to see if the filename matches one of the wildcards.
144      *
145      * @param dir  the file directory
146      * @param name  the filename
147      * @return true if the filename matches one of the wildcards
148      */
149     public boolean accept(File dir, String name) {
150         for (int i = 0; i < wildcards.length; i++) {
151             if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
152                 return true;
153             }
154         }
155         return false;
156     }
157 
158     /**
159      * Checks to see if the filename matches one of the wildcards.
160      *
161      * @param file  the file to check
162      * @return true if the filename matches one of the wildcards
163      */
164     public boolean accept(File file) {
165         String name = file.getName();
166         for (int i = 0; i < wildcards.length; i++) {
167             if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
168                 return true;
169             }
170         }
171         return false;
172     }
173 
174 }