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.vfs2.filter;
18  
19  import java.io.Serializable;
20  import java.util.regex.Pattern;
21  
22  import org.apache.commons.vfs2.FileFilter;
23  import org.apache.commons.vfs2.FileSelectInfo;
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   * For example, to retrieve and print all java files where the name matched the
33   * regular expression in the current directory:
34   * </p>
35   *
36   * <pre>
37   * FileSystemManager fsManager = VFS.getManager();
38   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
39   * FileObject[] files = dir.findFiles(new FileFilterSelector(new RegexFileFilter(&quot;ˆ.*[tT]est(-\\d+)?\\.java$&quot;)));
40   * for (int i = 0; i &lt; files.length; i++) {
41   *     System.out.println(files[i]);
42   * }
43   * </pre>
44   *
45   * @author This code was originally ported from Apache Commons IO File Filter
46   * @see "http://commons.apache.org/proper/commons-io/"
47   * @since 2.4
48   */
49  public class RegexFileFilter implements FileFilter, Serializable {
50  
51      private static final long serialVersionUID = 1L;
52  
53      /** Exception message when no pattern is given in the constructor. */
54      public static final String PATTERN_IS_MISSING = "Pattern is missing";
55  
56      /** The regular expression pattern that will be used to match file names. */
57      private final Pattern pattern;
58  
59      /**
60       * Construct a new regular expression filter for a compiled regular expression.
61       *
62       * @param pattern regular expression to match - Cannot be null
63       */
64      public RegexFileFilter(final Pattern pattern) {
65          if (pattern == null) {
66              throw new IllegalArgumentException(PATTERN_IS_MISSING);
67          }
68  
69          this.pattern = pattern;
70      }
71  
72      /**
73       * Construct a new regular expression filter.
74       *
75       * @param pattern regular string expression to match - Cannot be null
76       */
77      public RegexFileFilter(final String pattern) {
78          if (pattern == null) {
79              throw new IllegalArgumentException(PATTERN_IS_MISSING);
80          }
81  
82          this.pattern = Pattern.compile(pattern);
83      }
84  
85      /**
86       * Construct a new regular expression filter with the specified flags.
87       *
88       * @param pattern regular string expression to match
89       * @param flags   pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
90       */
91      public RegexFileFilter(final String pattern, final int flags) {
92          if (pattern == null) {
93              throw new IllegalArgumentException(PATTERN_IS_MISSING);
94          }
95          this.pattern = Pattern.compile(pattern, flags);
96      }
97  
98      /**
99       * Construct a new regular expression filter with the specified flags case
100      * sensitivity.
101      *
102      * @param pattern         regular string expression to match - Cannot be null
103      * @param caseSensitivity how to handle case sensitivity, null means
104      *                        case-sensitive
105      */
106     public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
107         if (pattern == null) {
108             throw new IllegalArgumentException(PATTERN_IS_MISSING);
109         }
110         int flags = 0;
111         if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
112             flags = Pattern.CASE_INSENSITIVE;
113         }
114         this.pattern = Pattern.compile(pattern, flags);
115     }
116 
117     /**
118      * Checks to see if the file name matches one of the regular expressions.
119      *
120      * @param fileInfo the File to check
121      *
122      * @return true if the file matches one of the regular expressions
123      */
124     @Override
125     public boolean accept(final FileSelectInfo fileInfo) {
126         final String name = fileInfo.getFile().getName().getBaseName();
127         return pattern.matcher(name).matches();
128     }
129 
130 }