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 * @see Pattern#compile(String, int)
60 */
61 public PatternFileSelector(final String regex) {
62 this(Pattern.compile(regex));
63 }
64
65 /**
66 * Creates a new selector for the given Pattern and flags.
67 * <p>
68 * See {@link PatternFileSelector} for a specification how the pattern is matched.
69 * </p>
70 *
71 * @param regex The expression to be compiled
72 * @param flags Match flags, a bit mask.
73 * @see Pattern#compile(String, int)
74 */
75 public PatternFileSelector(final String regex, final int flags) {
76 this(Pattern.compile(regex, flags));
77 }
78
79 /**
80 * Determines if a file or folder should be selected.
81 * <p>
82 * See {@link PatternFileSelector} for a specification how the pattern is matched.
83 * </p>
84 *
85 * @param fileInfo The file selection information.
86 * @return true if the file should be selected, false otherwise.
87 */
88 @Override
89 public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
90 return pattern.matcher(fileInfo.getFile().getName().getPath()).matches();
91 }
92
93 @Override
94 public String toString() {
95 return pattern.toString();
96 }
97
98 /**
99 * Determines whether a folder should be traversed.
100 * <p>
101 * This implementation always returns true to make sure all
102 * leafs are inspected.
103 * </p>
104 *
105 * @param fileInfo The file selection information.
106 * @return true if descendants should be traversed, false otherwise.
107 */
108 @Override
109 public boolean traverseDescendents(final FileSelectInfo fileInfo) throws Exception {
110 return true;
111 }
112 }