1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.vfs2;
18
19 import org.apache.commons.vfs2.util.Messages;
20
21 /**
22 * A {@link org.apache.commons.vfs2.FileSelector} that selects all children of the given fileObject.
23 * <p>
24 * This is to mimic the {@link FileFilter} interface.
25 * </p>
26 */
27 public class FileFilterSelector extends FileDepthSelector {
28
29 /**
30 * The FileFilter.
31 */
32 private final FileFilter fileFilter;
33
34 /**
35 * Constructs a new instance without a FileFilter.
36 */
37 public FileFilterSelector() {
38 this(null);
39 }
40
41 /**
42 * Constructs a new instance with a FileFilter.
43 * @param fileFilter the FileFilter.
44 */
45 public FileFilterSelector(final FileFilter fileFilter) {
46 super(1, 1);
47 this.fileFilter = fileFilter;
48 }
49
50 /**
51 * Determines whether the file should be selected.
52 *
53 * @param fileInfo The file selection information.
54 * @return true if the file should be selected, false otherwise.
55 * @throws Exception Thrown for file system errors or illegal argument exception.
56 */
57 public boolean accept(final FileSelectInfo fileInfo) throws Exception {
58 if (fileFilter != null) {
59 return fileFilter.accept(fileInfo);
60 }
61
62 throw new IllegalArgumentException(Messages.getString("vfs.selectors/filefilter.missing.error"));
63 }
64
65 /**
66 * Determines if a file or folder should be selected.
67 *
68 * @param fileInfo The file selection information.
69 * @return true if the file or folder should be included, false otherwise.
70 */
71 @Override
72 public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
73 return super.includeFile(fileInfo) && accept(fileInfo);
74 }
75 }