001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2;
018
019import java.util.regex.Matcher;
020import java.util.regex.Pattern;
021
022/**
023 * A {@link FileSelector} that selects based on regular expressions.
024 * <p>
025 * The regular expression specified in one of the constructors is
026 * {@linkplain Matcher#matches() matched} against {@link FileName#getPath()}
027 * of all candidate files. If you want to match only against the base file name,
028 * make sure to prefix the pattern with {@code ".*\\/"}.
029 * </p>
030 *
031 * @since 2.1
032 */
033public class PatternFileSelector implements FileSelector {
034
035    /**
036     * The extensions to select.
037     */
038    private final Pattern pattern;
039
040    /**
041     * Creates a new selector for the given pattern.
042     * <p>
043     * See {@link PatternFileSelector} for a specification how the pattern is matched.
044     * </p>
045     *
046     * @param pattern The regular expressed used by this selector.
047     */
048    public PatternFileSelector(final Pattern pattern) {
049        this.pattern = pattern;
050    }
051
052    /**
053     * Creates a new selector for the given pattern.
054     * <p>
055     * See {@link PatternFileSelector} for a specification how the pattern is matched.
056     * </p>
057     *
058     * @param regex The regular expressed used by this selector.
059     * @see Pattern#compile(String, int)
060     */
061    public PatternFileSelector(final String regex) {
062        this(Pattern.compile(regex));
063    }
064
065    /**
066     * Creates a new selector for the given Pattern and flags.
067     * <p>
068     * See {@link PatternFileSelector} for a specification how the pattern is matched.
069     * </p>
070     *
071     * @param regex The expression to be compiled
072     * @param flags Match flags, a bit mask.
073     * @see Pattern#compile(String, int)
074     */
075    public PatternFileSelector(final String regex, final int flags) {
076        this(Pattern.compile(regex, flags));
077    }
078
079    /**
080     * Determines if a file or folder should be selected.
081     * <p>
082     * See {@link PatternFileSelector} for a specification how the pattern is matched.
083     * </p>
084     *
085     * @param fileInfo The file selection information.
086     * @return true if the file should be selected, false otherwise.
087     */
088    @Override
089    public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
090        return pattern.matcher(fileInfo.getFile().getName().getPath()).matches();
091    }
092
093    @Override
094    public String toString() {
095        return pattern.toString();
096    }
097
098    /**
099     * 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}