14fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/*
24fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Licensed to the Apache Software Foundation (ASF) under one or more
34fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * contributor license agreements.  See the NOTICE file distributed with
44fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * this work for additional information regarding copyright ownership.
54fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * The ASF licenses this file to You under the Apache License, Version 2.0
64fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * (the "License"); you may not use this file except in compliance with
74fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * the License.  You may obtain a copy of the License at
84fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
94fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Unless required by applicable law or agreed to in writing, software
124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See the License for the specific language governing permissions and
154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * limitations under the License.
164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypackage org.apache.commons.io.filefilter;
184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.File;
204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.io.Serializable;
214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport java.util.regex.Pattern;
224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
234fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedyimport org.apache.commons.io.IOCase;
244fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
254fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy/**
264fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * Filters files using supplied regular expression(s).
274fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p/>
284fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * See java.util.regex.Pattern for regex matching rules
294fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p/>
304fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
314fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <p/>
324fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * e.g.
334fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * <pre>
344fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * File dir = new File(".");
354fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
364fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * File[] files = dir.listFiles(fileFilter);
374fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * for (int i = 0; i < files.length; i++) {
384fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *   System.out.println(files[i]);
394fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * }
404fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * </pre>
414fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy *
424fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @author Oliver Siegmar
434fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @version $Revision: 606381 $
444fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy * @since Commons IO 1.4
454fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy */
464fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedypublic class RegexFileFilter extends AbstractFileFilter implements Serializable {
474fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
484fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /** The regular expression pattern that will be used to match filenames */
494fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    private final Pattern pattern;
504fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
514fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
524fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Construct a new regular expression filter.
534fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
544fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param pattern regular string expression to match
554fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IllegalArgumentException if the pattern is null
564fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
574fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public RegexFileFilter(String pattern) {
584fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (pattern == null) {
594fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new IllegalArgumentException("Pattern is missing");
604fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
614fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
624fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.pattern = Pattern.compile(pattern);
634fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
644fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
654fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
664fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Construct a new regular expression filter with the specified flags case sensitivity.
674fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
684fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param pattern regular string expression to match
694fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
704fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IllegalArgumentException if the pattern is null
714fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
724fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public RegexFileFilter(String pattern, IOCase caseSensitivity) {
734fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (pattern == null) {
744fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new IllegalArgumentException("Pattern is missing");
754fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
764fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        int flags = 0;
774fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
784fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            flags = Pattern.CASE_INSENSITIVE;
794fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
804fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.pattern = Pattern.compile(pattern, flags);
814fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
824fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
834fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
844fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Construct a new regular expression filter with the specified flags.
854fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
864fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param pattern regular string expression to match
874fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param flags pattern flags - e.g. {@link Pattern#CASE_INSENSITIVE}
884fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IllegalArgumentException if the pattern is null
894fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
904fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public RegexFileFilter(String pattern, int flags) {
914fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (pattern == null) {
924fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new IllegalArgumentException("Pattern is missing");
934fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
944fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.pattern = Pattern.compile(pattern, flags);
954fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
964fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
974fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
984fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Construct a new regular expression filter for a compiled regular expression
994fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1004fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param pattern regular expression to match
1014fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @throws IllegalArgumentException if the pattern is null
1024fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1034fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public RegexFileFilter(Pattern pattern) {
1044fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        if (pattern == null) {
1054fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy            throw new IllegalArgumentException("Pattern is missing");
1064fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        }
1074fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1084fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        this.pattern = pattern;
1094fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1104fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1114fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    /**
1124fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * Checks to see if the filename matches one of the regular expressions.
1134fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     *
1144fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param dir   the file directory
1154fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @param name  the filename
1164fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     * @return true if the filename matches one of the regular expressions
1174fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy     */
1184fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    public boolean accept(File dir, String name) {
1194fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy        return (pattern.matcher(name).matches());
1204fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy    }
1214fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy
1224fa0a3295bcacbdcd6a9e7709cf17aa5adb90356Scott Kennedy}
123