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 org.apache.commons.vfs2.util.Messages;
020
021/**
022 * A {@link org.apache.commons.vfs2.FileSelector} that selects all children of the given fileObject.
023 * <p>
024 * This is to mimic the {@link FileFilter} interface.
025 * </p>
026 */
027public class FileFilterSelector extends FileDepthSelector {
028
029    /**
030     * The FileFilter.
031     */
032    private final FileFilter fileFilter;
033
034    /**
035     * Constructs a new instance without a FileFilter.
036     */
037    public FileFilterSelector() {
038        this(null);
039    }
040
041    /**
042     * Constructs a new instance with a FileFilter.
043     * @param fileFilter the FileFilter.
044     */
045    public FileFilterSelector(final FileFilter fileFilter) {
046        super(1, 1);
047        this.fileFilter = fileFilter;
048    }
049
050    /**
051     * Determines whether the file should be selected.
052     *
053     * @param fileInfo The file selection information.
054     * @return true if the file should be selected, false otherwise.
055     * @throws Exception Thrown for file system errors or illegal argument exception.
056     */
057    public boolean accept(final FileSelectInfo fileInfo) throws Exception {
058        if (fileFilter != null) {
059            return fileFilter.accept(fileInfo);
060        }
061
062        throw new IllegalArgumentException(Messages.getString("vfs.selectors/filefilter.missing.error"));
063    }
064
065    /**
066     * Determines if a file or folder should be selected.
067     *
068     * @param fileInfo The file selection information.
069     * @return true if the file or folder should be included, false otherwise.
070     */
071    @Override
072    public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
073        return super.includeFile(fileInfo) && accept(fileInfo);
074    }
075}