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.filter;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.commons.vfs2.FileFilter;
25  import org.apache.commons.vfs2.FileSelectInfo;
26  
27  /**
28   * Filters files based on the suffix (what the file name ends with). This is used
29   * in retrieving all the files of a particular type.
30   * <p>
31   * For example, to retrieve and print all <code>*.java</code> files in the
32   * current directory:
33   * </p>
34   *
35   * <pre>
36   * FileSystemManager fsManager = VFS.getManager();
37   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
38   * FileObject[] files = dir.findFiles(new FileFilterSelector(new SuffixFileFilter(&quot;.java&quot;)));
39   * for (int i = 0; i &lt; files.length; i++) {
40   *     System.out.println(files[i]);
41   * }
42   * </pre>
43   *
44   * @author This code was originally ported from Apache Commons IO File Filter
45   * @see "http://commons.apache.org/proper/commons-io/"
46   * @since 2.4
47   */
48  public class SuffixFileFilter implements FileFilter, Serializable {
49  
50      private static final long serialVersionUID = 1L;
51  
52      /** Whether the comparison is case sensitive. */
53      private final IOCase caseSensitivity;
54  
55      /** The file name suffixes to search for. */
56      private final List<String> suffixes;
57  
58      /**
59       * Constructs a new Suffix file filter for a list of suffixes.
60       *
61       * @param suffixes the suffixes to allow, must not be null
62       */
63      public SuffixFileFilter(final List<String> suffixes) {
64          this(IOCase.SENSITIVE, suffixes);
65      }
66  
67      /**
68       * Constructs a new Suffix file filter for a list of suffixes specifying
69       * case-sensitivity.
70       *
71       * @param suffixes        the suffixes to allow, must not be null
72       * @param caseSensitivity how to handle case sensitivity, null means
73       *                        case-sensitive
74       */
75      public SuffixFileFilter(final IOCase caseSensitivity, final List<String> suffixes) {
76          if (suffixes == null) {
77              throw new IllegalArgumentException("The list of suffixes must not be null");
78          }
79          this.suffixes = new ArrayList<>(suffixes);
80          this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
81      }
82  
83      /**
84       * Constructs a new Suffix file filter for an array of suffixes.
85       *
86       * @param suffixes the suffixes to allow, must not be null
87       */
88      public SuffixFileFilter(final String... suffixes) {
89          this(IOCase.SENSITIVE, suffixes);
90      }
91  
92      /**
93       * Constructs a new Suffix file filter for an array of suffixs specifying
94       * case-sensitivity.
95       *
96       * @param suffixes        the suffixes to allow, must not be null
97       * @param caseSensitivity how to handle case sensitivity, null means
98       *                        case-sensitive
99       */
100     public SuffixFileFilter(final IOCase caseSensitivity, final String... suffixes) {
101         if (suffixes == null) {
102             throw new IllegalArgumentException("The array of suffixes must not be null");
103         }
104         this.suffixes = new ArrayList<>(Arrays.asList(suffixes));
105         this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
106     }
107 
108     /**
109      * Checks to see if the file name ends with the suffix.
110      *
111      * @param fileInfo the File to check
112      *
113      * @return true if the file name ends with one of our suffixes
114      */
115     @Override
116     public boolean accept(final FileSelectInfo fileInfo) {
117         final String name = fileInfo.getFile().getName().getBaseName();
118         for (final String suffix : this.suffixes) {
119             if (caseSensitivity.checkEndsWith(name, suffix)) {
120                 return true;
121             }
122         }
123         return false;
124     }
125 
126     /**
127      * Provides a String representation of this file filter.
128      *
129      * @return a String representation
130      */
131     @Override
132     public String toString() {
133         final StringBuilder buffer = new StringBuilder();
134         buffer.append(super.toString());
135         buffer.append("(");
136         if (suffixes != null) {
137             for (int i = 0; i < suffixes.size(); i++) {
138                 if (i > 0) {
139                     buffer.append(",");
140                 }
141                 buffer.append(suffixes.get(i));
142             }
143         }
144         buffer.append(")");
145         return buffer.toString();
146     }
147 
148 }