KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > ExtensionFilter


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.syncclient.common;
19
20 import java.io.File JavaDoc;
21 import java.io.FilenameFilter JavaDoc;
22
23 /**
24  *
25  * @author Stefano Fornari
26  * @version $Id: ExtensionFilter.java,v 1.2 2005/01/19 11:18:36 fabius Exp $
27  */

28 public class ExtensionFilter implements FilenameFilter JavaDoc {
29     private String JavaDoc extension;
30
31     public ExtensionFilter(String JavaDoc extension) {
32         setExtension(extension);
33     }
34
35     public void setExtension(String JavaDoc extension) {
36         if ((extension == null) || (extension.length() == 0)) {
37             extension = ".*";
38         }
39
40         this.extension = (extension.startsWith("."))
41                        ? extension
42                        : '.' + extension
43                        ;
44         extension = extension.toLowerCase();
45     }
46
47     /**
48      * Accepts:
49      * <ul>
50      * <li>all directories
51      * <li>if extension is .* all files
52      * <li>if extension is not .* all files ending with extension
53      * </ul>
54      *
55      * @param dir the file parent directory
56      * @param name the file name
57      *
58      * @return true if the file must be included in the list, false otherwise
59      */

60     public boolean accept(File JavaDoc dir, String JavaDoc name) {
61         if (extension.equals(".*")) {
62             return true;
63         }
64
65         if (new File JavaDoc(dir, name).isDirectory()) {
66             return true;
67         }
68
69         return name.toLowerCase().endsWith(extension);
70     }
71 }
Popular Tags