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;
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  /**
23   * A {@link FileSelector} that selects based on regular expressions.
24   * <p>
25   * The regular expression specified in one of the constructors is
26   * {@linkplain Matcher#matches() matched} against {@link FileName#getPath()}
27   * of all candidate files. If you want to match only against the base file name,
28   * make sure to prefix the pattern with {@code ".*\\/"}.
29   * </p>
30   *
31   * @since 2.1
32   */
33  public class PatternFileSelector implements FileSelector {
34  
35      /**
36       * The extensions to select.
37       */
38      private final Pattern pattern;
39  
40      /**
41       * Creates a new selector for the given pattern.
42       * <p>
43       * See {@link PatternFileSelector} for a specification how the pattern is matched.
44       * </p>
45       *
46       * @param pattern The regular expressed used by this selector.
47       */
48      public PatternFileSelector(final Pattern pattern) {
49          this.pattern = pattern;
50      }
51  
52      /**
53       * Creates a new selector for the given pattern.
54       * <p>
55       * See {@link PatternFileSelector} for a specification how the pattern is matched.
56       * </p>
57       *
58       * @param regex The regular expressed used by this selector.
59       *
60       * @see Pattern#compile(String, int)
61       */
62      public PatternFileSelector(final String regex) {
63          this(Pattern.compile(regex));
64      }
65  
66      /**
67       * Creates a new selector for the given Pattern and flags.
68       * <p>
69       * See {@link PatternFileSelector} for a specification how the pattern is matched.
70       * </p>
71       *
72       * @param regex The expression to be compiled
73       * @param flags Match flags, a bit mask.
74       *
75       * @see Pattern#compile(String, int)
76       */
77      public PatternFileSelector(final String regex, final int flags) {
78          this(Pattern.compile(regex, flags));
79      }
80  
81      /**
82       * Determines if a file or folder should be selected.
83       * <p>
84       * See {@link PatternFileSelector} for a specification how the pattern is matched.
85       * </p>
86       *
87       * @param fileInfo The file selection information.
88       * @return true if the file should be selected, false otherwise.
89       */
90      @Override
91      public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
92          return this.pattern.matcher(fileInfo.getFile().getName().getPath()).matches();
93      }
94  
95      @Override
96      public String toString() {
97          return this.pattern.toString();
98      }
99  
100     /**
101      * Determines whether a folder should be traversed.
102      * <p>
103      * This implementation always returns true to make sure all
104      * leafs are inspected.
105      * </p>
106      *
107      * @param fileInfo The file selection information.
108      * @return true if descendants should be traversed, false otherwise.
109      */
110     @Override
111     public boolean traverseDescendents(final FileSelectInfo fileInfo) throws Exception {
112         return true;
113     }
114 }