KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > file > FileSuffixFilter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software 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 software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.file;
23
24 import java.io.File JavaDoc;
25 import java.io.FileFilter JavaDoc;
26
27 /**
28  * A <em>suffix</em> based file filter.
29  *
30  * @version <tt>$Revision: 1958 $</tt>
31  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
32  */

33 public class FileSuffixFilter
34    implements FileFilter JavaDoc
35 {
36    /** A list of suffixes which files must have to be accepted. */
37    protected final String JavaDoc suffixes[];
38
39    /** Flag to signal that we want to ignore the case. */
40    protected final boolean ignoreCase;
41
42    /**
43     * Construct a <tt>FileSuffixFilter</tt>.
44     *
45     * @param suffixes A list of suffixes which files mut have to be accepted.
46     * @param ignoreCase <tt>True</tt> if the filter should be case-insensitive.
47     */

48    public FileSuffixFilter(final String JavaDoc suffixes[],
49                            final boolean ignoreCase)
50    {
51       this.ignoreCase = ignoreCase;
52       if (ignoreCase) {
53          this.suffixes = new String JavaDoc[suffixes.length];
54          for (int i=0; i<suffixes.length; i++) {
55             this.suffixes[i] = suffixes[i].toLowerCase();
56          }
57       }
58       else {
59          this.suffixes = suffixes;
60       }
61    }
62
63    /**
64     * Construct a <tt>FileSuffixFilter</tt>.
65     *
66     * @param suffixes A list of suffixes which files mut have to be accepted.
67     */

68    public FileSuffixFilter(final String JavaDoc suffixes[])
69    {
70       this(suffixes, false);
71    }
72
73    /**
74     * Construct a <tt>FileSuffixFilter</tt>.
75     *
76     * @param suffix The suffix which files must have to be accepted.
77     * @param ignoreCase <tt>True</tt> if the filter should be case-insensitive.
78     */

79    public FileSuffixFilter(final String JavaDoc suffix,
80                            final boolean ignoreCase)
81    {
82       this(new String JavaDoc[] { suffix }, ignoreCase);
83    }
84
85    /**
86     * Construct a case sensitive <tt>FileSuffixFilter</tt>.
87     *
88     * @param suffix The suffix which files must have to be accepted.
89     */

90    public FileSuffixFilter(final String JavaDoc suffix) {
91       this(suffix, false);
92    }
93
94    /**
95     * Check if a file is acceptible.
96     *
97     * @param file The file to check.
98     * @return <tt>true</tt> if the file is acceptable.
99     */

100    public boolean accept(final File JavaDoc file) {
101       boolean success = false;
102
103       for (int i=0; i<suffixes.length && !success; i++) {
104          if (ignoreCase)
105             success = file.getName().toLowerCase().endsWith(suffixes[i]);
106          else
107             success = file.getName().endsWith(suffixes[i]);
108       }
109
110       return success;
111    }
112 }
113
Popular Tags