    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.io.filefilter;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.util.List;
    022
    023import org.apache.commons.io.FilenameUtils;
    024
    025/**
    026 * Filters files using the supplied wildcards.
    027 * <p>
    028 * This filter selects files, but not directories, based on one or more wildcards
    029 * and using case-sensitive comparison.
    030 * <p>
    031 * The wildcard matcher uses the characters '?' and '*' to represent a
    032 * single or multiple wildcard characters.
    033 * This is the same as often found on Dos/Unix command lines.
    034 * The extension check is case-sensitive.
    035 * See {@link FilenameUtils#wildcardMatch(String, String)} for more information.
    036 * <p>
    037 * For example:
    038 * <pre>
    039 * File dir = new File(".");
    040 * FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
    041 * File[] files = dir.listFiles(fileFilter);
    042 * for (int i = 0; i &lt; files.length; i++) {
    043 *   System.out.println(files[i]);
    044 * }
    045 * </pre>
    046 *
    047 * @since 1.1
    048 * @deprecated Use WildcardFileFilter. Deprecated as this class performs directory
    049 * filtering which it shouldn't do, but that can't be removed due to compatibility.
    050 */
    051@Deprecated
    052public class WildcardFilter extends AbstractFileFilter implements Serializable {
    053
    054    private static final long serialVersionUID = -5037645902506953517L;
    055    /** The wildcards that will be used to match file names. */
    056    private final String[] wildcards;
    057
    058    /**
    059     * Construct a new case-sensitive wildcard filter for a single wildcard.
    060     *
    061     * @param wildcard  the wildcard to match
    062     * @throws IllegalArgumentException if the pattern is null
    063     */
    064    public WildcardFilter(final String wildcard) {
    065        if (wildcard == null) {
    066            throw new IllegalArgumentException("The wildcard must not be null");
    067        }
    068        this.wildcards = new String[] { wildcard };
    069    }
    070
    071    /**
    072     * Construct a new case-sensitive wildcard filter for an array of wildcards.
    073     *
    074     * @param wildcards  the array of wildcards to match
    075     * @throws IllegalArgumentException if the pattern array is null
    076     */
    077    public WildcardFilter(final String... wildcards) {
    078        if (wildcards == null) {
    079            throw new IllegalArgumentException("The wildcard array must not be null");
    080        }
    081        this.wildcards = new String[wildcards.length];
    082        System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
    083    }
    084
    085    /**
    086     * Construct a new case-sensitive wildcard filter for a list of wildcards.
    087     *
    088     * @param wildcards  the list of wildcards to match
    089     * @throws IllegalArgumentException if the pattern list is null
    090     * @throws ClassCastException if the list does not contain Strings
    091     */
    092    public WildcardFilter(final List<String> wildcards) {
    093        if (wildcards == null) {
    094            throw new IllegalArgumentException("The wildcard list must not be null");
    095        }
    096        this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY);
    097    }
    098
    099    //-----------------------------------------------------------------------
    100    /**
    101     * Checks to see if the file name matches one of the wildcards.
    102     *
    103     * @param dir  the file directory
    104     * @param name  the file name
    105     * @return true if the file name matches one of the wildcards
    106     */
    107    @Override
    108    public boolean accept(final File dir, final String name) {
    109        if (dir != null && new File(dir, name).isDirectory()) {
    110            return false;
    111        }
    112
    113        for (final String wildcard : wildcards) {
    114            if (FilenameUtils.wildcardMatch(name, wildcard)) {
    115                return true;
    116            }
    117        }
    118
    119        return false;
    120    }
    121
    122    /**
    123     * Checks to see if the file name matches one of the wildcards.
    124     *
    125     * @param file the file to check
    126     * @return true if the file name matches one of the wildcards
    127     */
    128    @Override
    129    public boolean accept(final File file) {
    130        if (file.isDirectory()) {
    131            return false;
    132        }
    133
    134        for (final String wildcard : wildcards) {
    135            if (FilenameUtils.wildcardMatch(file.getName(), wildcard)) {
    136                return true;
    137            }
    138        }
    139
    140        return false;
    141    }
    142
    143}