View Javadoc
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 java.util.Arrays;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  /**
25   * A {@link FileSelector} that selects based on file extensions.
26   * <p>
27   * The extension comparison is case insensitive.
28   * </p>
29   * <p>
30   * The selector makes a copy of a given Collection or array. Changing the object passed in the constructors will not
31   * affect the selector.
32   * </p>
33   *
34   * @since 2.1
35   */
36  public class FileExtensionSelector implements FileSelector
37  {
38  
39      /**
40       * The extensions to select.
41       */
42      private final Set<String> extensions = new HashSet<String>();
43  
44      /**
45       * Creates a new selector for the given extensions.
46       *
47       * @param extensions
48       *            The extensions to be included by this selector.
49       */
50      public FileExtensionSelector(final Collection<String> extensions)
51      {
52          if (extensions != null)
53          {
54              this.extensions.addAll(extensions);
55          }
56      }
57  
58      /**
59       * Creates a new selector for the given extensions.
60       *
61       * @param extensions
62       *            The extensions to be included by this selector.
63       */
64      public FileExtensionSelector(final String... extensions)
65      {
66          if (extensions != null)
67          {
68              this.extensions.addAll(Arrays.asList(extensions));
69          }
70      }
71  
72      /**
73       * Determines if a file or folder should be selected.
74       *
75       * @param fileInfo
76       *            The file selection information.
77       * @return true if the file should be selected, false otherwise.
78       */
79      @Override
80      public boolean includeFile(final FileSelectInfo fileInfo)
81      {
82          if (this.extensions == null)
83          {
84              return false;
85          }
86          for (final String extension : this.extensions)
87          {
88              if (fileInfo.getFile().getName().getExtension().equalsIgnoreCase(extension))
89              {
90                  return true;
91              }
92          }
93          return false;
94      }
95  
96      /**
97       * Determines whether a folder should be traversed.
98       *
99       * @param fileInfo
100      *            The file selection information.
101      * @return true if descendants should be traversed, fase otherwise.
102      */
103     @Override
104     public boolean traverseDescendents(final FileSelectInfo fileInfo)
105     {
106         return true;
107     }
108 }