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.filter;
018
019import java.io.Serializable;
020import java.util.regex.Pattern;
021
022import org.apache.commons.vfs2.FileFilter;
023import org.apache.commons.vfs2.FileSelectInfo;
024
025/**
026 * Filters files using supplied regular expression(s).
027 * <p>
028 * See java.util.regex.Pattern for regex matching rules.
029 * </p>
030 *
031 * <p>
032 * For example, to retrieve and print all Java files where the name matched the
033 * regular expression in the current directory:
034 * </p>
035 *
036 * <pre>
037 * FileSystemManager fsManager = VFS.getManager();
038 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
039 * FileObject[] files = dir.findFiles(new FileFilterSelector(new RegexFileFilter(&quot;ˆ.*[tT]est(-\\d+)?\\.java$&quot;)));
040 * for (int i = 0; i &lt; files.length; i++) {
041 *     System.out.println(files[i]);
042 * }
043 * </pre>
044 *
045 * @author This code was originally ported from Apache Commons IO File Filter
046 * @see "https://commons.apache.org/proper/commons-io/"
047 * @since 2.4
048 */
049public class RegexFileFilter implements FileFilter, Serializable {
050
051    /** Exception message when no pattern is given in the constructor. */
052    public static final String PATTERN_IS_MISSING = "Pattern is missing";
053
054    private static final long serialVersionUID = 1L;
055
056    /** The regular expression pattern that will be used to match file names. */
057    private final Pattern pattern;
058
059    /**
060     * Constructs a new regular expression filter for a compiled regular expression.
061     *
062     * @param pattern regular expression to match - Cannot be null
063     */
064    public RegexFileFilter(final Pattern pattern) {
065        if (pattern == null) {
066            throw new IllegalArgumentException(PATTERN_IS_MISSING);
067        }
068
069        this.pattern = pattern;
070    }
071
072    /**
073     * Constructs a new regular expression filter.
074     *
075     * @param pattern regular string expression to match - Cannot be null
076     */
077    public RegexFileFilter(final String pattern) {
078        if (pattern == null) {
079            throw new IllegalArgumentException(PATTERN_IS_MISSING);
080        }
081
082        this.pattern = Pattern.compile(pattern);
083    }
084
085    /**
086     * Constructs a new regular expression filter with the specified flags.
087     *
088     * @param pattern regular string expression to match
089     * @param flags   pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
090     */
091    public RegexFileFilter(final String pattern, final int flags) {
092        if (pattern == null) {
093            throw new IllegalArgumentException(PATTERN_IS_MISSING);
094        }
095        this.pattern = Pattern.compile(pattern, flags);
096    }
097
098    /**
099     * Constructs 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 fileSelectInfo the File to check
121     * @return true if the file matches one of the regular expressions
122     */
123    @Override
124    public boolean accept(final FileSelectInfo fileSelectInfo) {
125        final String name = fileSelectInfo.getFile().getName().getBaseName();
126        return pattern.matcher(name).matches();
127    }
128
129}