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.nio.file.FileVisitResult;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.nio.file.attribute.BasicFileAttributes;
025import java.util.List;
026import java.util.Objects;
027
028import org.apache.commons.io.FilenameUtils;
029
030/**
031 * Filters files using the supplied wildcards.
032 * <p>
033 * This filter selects files, but not directories, based on one or more wildcards
034 * and using case-sensitive comparison.
035 * </p>
036 * <p>
037 * The wildcard matcher uses the characters '?' and '*' to represent a
038 * single or multiple wildcard characters.
039 * This is the same as often found on Dos/Unix command lines.
040 * The extension check is case-sensitive.
041 * See {@link FilenameUtils#wildcardMatch(String, String)} for more information.
042 * </p>
043 * <p>
044 * For example:
045 * </p>
046 * <h2>Using Classic IO</h2>
047 * <pre>
048 * File dir = new File(".");
049 * FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
050 * File[] files = dir.listFiles(fileFilter);
051 * for (String file : files) {
052 *     System.out.println(file);
053 * }
054 * </pre>
055 *
056 * <h2>Using NIO</h2>
057 * <pre>
058 * final Path dir = Paths.get("");
059 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new WildcardFilter("*test*.java~*~"));
060 * //
061 * // Walk one dir
062 * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor);
063 * System.out.println(visitor.getPathCounters());
064 * System.out.println(visitor.getFileList());
065 * //
066 * visitor.getPathCounters().reset();
067 * //
068 * // Walk dir tree
069 * Files.<b>walkFileTree</b>(dir, visitor);
070 * System.out.println(visitor.getPathCounters());
071 * System.out.println(visitor.getDirList());
072 * System.out.println(visitor.getFileList());
073 * </pre>
074 *
075 * @since 1.1
076 * @deprecated Use WildcardFileFilter. Deprecated as this class performs directory
077 * filtering which it shouldn't do, but that can't be removed due to compatibility.
078 */
079@Deprecated
080public class WildcardFilter extends AbstractFileFilter implements Serializable {
081
082    private static final long serialVersionUID = -5037645902506953517L;
083    /** The wildcards that will be used to match file names. */
084    private final String[] wildcards;
085
086    /**
087     * Construct a new case-sensitive wildcard filter for a list of wildcards.
088     *
089     * @param wildcards  the list of wildcards to match
090     * @throws IllegalArgumentException if the pattern list is null
091     * @throws ClassCastException if the list does not contain Strings
092     */
093    public WildcardFilter(final List<String> wildcards) {
094        if (wildcards == null) {
095            throw new IllegalArgumentException("The wildcard list must not be null");
096        }
097        this.wildcards = wildcards.toArray(EMPTY_STRING_ARRAY);
098    }
099
100    /**
101     * Construct a new case-sensitive wildcard filter for a single wildcard.
102     *
103     * @param wildcard  the wildcard to match
104     * @throws IllegalArgumentException if the pattern is null
105     */
106    public WildcardFilter(final String wildcard) {
107        if (wildcard == null) {
108            throw new IllegalArgumentException("The wildcard must not be null");
109        }
110        this.wildcards = new String[] { wildcard };
111    }
112
113    /**
114     * Construct a new case-sensitive wildcard filter for an array of wildcards.
115     *
116     * @param wildcards  the array of wildcards to match
117     * @throws IllegalArgumentException if the pattern array is null
118     */
119    public WildcardFilter(final String... wildcards) {
120        if (wildcards == null) {
121            throw new IllegalArgumentException("The wildcard array must not be null");
122        }
123        this.wildcards = new String[wildcards.length];
124        System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
125    }
126
127    /**
128     * Checks to see if the file name matches one of the wildcards.
129     *
130     * @param file the file to check
131     * @return true if the file name matches one of the wildcards
132     */
133    @Override
134    public boolean accept(final File file) {
135        if (file.isDirectory()) {
136            return false;
137        }
138
139        for (final String wildcard : wildcards) {
140            if (FilenameUtils.wildcardMatch(file.getName(), wildcard)) {
141                return true;
142            }
143        }
144
145        return false;
146    }
147
148    /**
149     * Checks to see if the file name matches one of the wildcards.
150     * @param file the file to check
151     *
152     * @return true if the file name matches one of the wildcards
153     * @since 2.9.0
154     */
155    @Override
156    public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
157        if (Files.isDirectory(file)) {
158            return FileVisitResult.TERMINATE;
159        }
160
161        for (final String wildcard : wildcards) {
162            if (FilenameUtils.wildcardMatch(Objects.toString(file.getFileName(), null), wildcard)) {
163                return FileVisitResult.CONTINUE;
164            }
165        }
166
167        return FileVisitResult.TERMINATE;
168    }
169
170    /**
171     * Checks to see if the file name matches one of the wildcards.
172     *
173     * @param dir  the file directory
174     * @param name  the file name
175     * @return true if the file name matches one of the wildcards
176     */
177    @Override
178    public boolean accept(final File dir, final String name) {
179        if (dir != null && new File(dir, name).isDirectory()) {
180            return false;
181        }
182
183        for (final String wildcard : wildcards) {
184            if (FilenameUtils.wildcardMatch(name, wildcard)) {
185                return true;
186            }
187        }
188
189        return false;
190    }
191
192}