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.io.Serializable;
21  import java.util.regex.Pattern;
22  
23  import org.apache.commons.io.IOCase;
24  
25  /**
26   * Filters files using supplied regular expression(s).
27   * <p/>
28   * See java.util.regex.Pattern for regex matching rules
29   * <p/>
30   *
31   * <p/>
32   * e.g.
33   * <pre>
34   * File dir = new File(".");
35   * FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
36   * File[] files = dir.listFiles(fileFilter);
37   * for (int i = 0; i < files.length; i++) {
38   *   System.out.println(files[i]);
39   * }
40   * </pre>
41   *
42   * @author Oliver Siegmar
43   * @version $Revision: 606381 $
44   * @since Commons IO 1.4
45   */
46  public class RegexFileFilter extends AbstractFileFilter implements Serializable {
47  
48      /** The regular expression pattern that will be used to match filenames */
49      private final Pattern pattern;
50  
51      /**
52       * Construct a new regular expression filter.
53       *
54       * @param pattern regular string expression to match
55       * @throws IllegalArgumentException if the pattern is null
56       */
57      public RegexFileFilter(String pattern) {
58          if (pattern == null) {
59              throw new IllegalArgumentException("Pattern is missing");
60          }
61  
62          this.pattern = Pattern.compile(pattern);
63      }
64  
65      /**
66       * Construct a new regular expression filter with the specified flags case sensitivity.
67       *
68       * @param pattern regular string expression to match
69       * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
70       * @throws IllegalArgumentException if the pattern is null
71       */
72      public RegexFileFilter(String pattern, IOCase caseSensitivity) {
73          if (pattern == null) {
74              throw new IllegalArgumentException("Pattern is missing");
75          }
76          int flags = 0;
77          if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
78              flags = Pattern.CASE_INSENSITIVE;
79          }
80          this.pattern = Pattern.compile(pattern, flags);
81      }
82  
83      /**
84       * Construct a new regular expression filter with the specified flags.
85       *
86       * @param pattern regular string expression to match
87       * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
88       * @throws IllegalArgumentException if the pattern is null
89       */
90      public RegexFileFilter(String pattern, int flags) {
91          if (pattern == null) {
92              throw new IllegalArgumentException("Pattern is missing");
93          }
94          this.pattern = Pattern.compile(pattern, flags);
95      }
96  
97      /**
98       * Construct a new regular expression filter for a compiled regular expression
99       *
100      * @param pattern regular expression to match
101      * @throws IllegalArgumentException if the pattern is null
102      */
103     public RegexFileFilter(Pattern pattern) {
104         if (pattern == null) {
105             throw new IllegalArgumentException("Pattern is missing");
106         }
107 
108         this.pattern = pattern;
109     }
110 
111     /**
112      * Checks to see if the filename matches one of the regular expressions.
113      *
114      * @param dir   the file directory
115      * @param name  the filename
116      * @return true if the filename matches one of the regular expressions
117      */
118     public boolean accept(File dir, String name) {
119         return (pattern.matcher(name).matches());
120     }
121 
122 }