KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > server > JFileFilter


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: JFileFilter.java,v 1.2 2004/05/25 15:13:29 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.server;
28
29 //import java
30
import java.util.StringTokenizer JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FileFilter JavaDoc;
33
34 /**
35  * This class implements a FileFilter, used to list files of a directory.
36  * @author Ludovic Bert
37  * @author Florent Benoit
38  */

39 public class JFileFilter implements FileFilter JavaDoc {
40
41     /**
42      * The extensions that match in the file filter.
43      */

44     private String JavaDoc[] extensions = null;
45
46     /**
47      * Contruct a new JFileFilter with the specified filter.
48      * @param filter the filter to use. A comma-separated list of the allowed
49      * extensions. For instance ".jar,.dtd", note that ".*" is allowed.
50      */

51     public JFileFilter(String JavaDoc filter) {
52         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(filter, ",");
53         int tokenCount = st.countTokens();
54         extensions = new String JavaDoc[tokenCount];
55         for (int i = 0; i < tokenCount; i++) {
56             extensions[i] = st.nextToken();
57         }
58     }
59
60     /**
61      * Tests whether or not the specified abstract pathname should be included
62      * in a pathname list.
63      * @param pathname the abstract pathname to be tested.
64      * @return true if and only if pathname should be included.
65      */

66     public boolean accept(File JavaDoc pathname) {
67         if (pathname.isDirectory()) {
68             return true;
69         } else {
70             for (int i = 0; i < extensions.length; i++) {
71                 if (extensions[i].equals(".*")) {
72                     return true;
73                 } else {
74                     if (pathname.getName().endsWith(extensions[i])) {
75                         return true;
76                     }
77                 }
78             }
79             return false;
80         }
81     }
82 }
Popular Tags