View Javadoc

1   /*
2    * Copyright (C) 2004 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2.1 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, visit
19   * http://www.gnu.org/licenses/lgpl.html or write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21   */
22  package de.fu_berlin.ties.io;
23  
24  import java.io.File;
25  import java.io.FileFilter;
26  import java.util.Collections;
27  import java.util.Set;
28  
29  import org.apache.commons.lang.builder.ToStringBuilder;
30  
31  /***
32   * A file filter that checks the extension to decide whether to accept a file.
33   * Don't prepend a dot when specifying extensions -- this happens automatically.
34   *
35   * @author Christian Siefkes
36   * @version $Revision: 1.2 $, $Date: 2004/09/06 17:23:31 $, $Author: siefkes $
37   */
38  public class ExtensionFilter implements FileFilter {
39  
40      /***
41       * Whether case is ignored when matching the extension.
42       */
43      private final boolean caseIgnored;
44  
45      /***
46       * The immutable set of extensions (Strings) to accept.
47       */
48      private final Set extensions;
49  
50      /***
51       * Creates a new instance that accepts only a single extension.
52       *
53       * @param extension the extension to accept
54       * @param ignoreCase whether case is ignored when matching the extension;
55       * if <code>true</code>, <code>extension</code> must be lower-case
56       * or matching will fail (file extensions are converted to lower-case
57       * prior to comparing)
58       */
59      public ExtensionFilter(final String extension, final boolean ignoreCase) {
60          this(Collections.singleton(extension), ignoreCase);
61      }
62  
63      /***
64       * Creates a new instance.
65       *
66       * @param extensionSet the set of extensions (Strings) to accept
67       * @param ignoreCase whether case is ignored when matching the extension;
68       * if <code>true</code>, <code>extensionSet</code> must contain only
69       * lower-case entries or matching will fail (file extensions are converted
70       * to lower-case, but the contents of the set are used as is)
71       */
72      public ExtensionFilter(final Set<String> extensionSet,
73              final boolean ignoreCase) {
74          super();
75          caseIgnored = ignoreCase;
76  
77          // make set immutable
78          extensions = Collections.unmodifiableSet(extensionSet);
79      }
80  
81      /***
82       * Tests whether or not the specified abstract pathname should be included
83       * in a pathname list.
84       *
85       * @param pathname the abstract pathname to be tested
86       * @return <code>true</code> iff the {@linkplain IOUtils#getExtension(File)
87       * extension} of the <code>pathname</code> is contained in the
88       */
89      public boolean accept(final File pathname) {
90          String extension = IOUtils.getExtension(pathname);
91  
92          if (caseIgnored) {
93              // convert to lower case
94              extension = extension.toLowerCase();
95          }
96  
97          return extensions.contains(extension);
98      }
99  
100     /***
101      * The immutable set of extensions (Strings) accepted by this filter.
102      * @return the value of the attribute
103      */
104     public Set getExtensions() {
105         return extensions;
106     }
107 
108     /***
109      * Whether case is ignored when matching the extension.
110      * If <code>true</code>, file extensions are converted to lower case prior
111      * to comparing them.
112      *
113      * @return the value of the attribute
114      */
115     public boolean isCaseIgnored() {
116         return caseIgnored;
117     }
118 
119     /***
120      * Returns a string representation of this object.
121      *
122      * @return a textual representation
123      */
124     public String toString() {
125         return new ToStringBuilder(this)
126             .append("extension set", extensions)
127             .append("ignore case", caseIgnored)
128             .toString();
129     }
130 
131 }