KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > DirectoryFilenameFilter


1 /*
2  * DirectoryFilenameFilter.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: DirectoryFilenameFilter.java,v 1.3 2003/07/04 17:26:44 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import java.util.regex.PatternSyntaxException JavaDoc;
27
28 public final class DirectoryFilenameFilter
29 {
30     private Pattern JavaDoc pattern;
31     private boolean ignoreCase;
32
33     public DirectoryFilenameFilter(String JavaDoc s) throws PatternSyntaxException JavaDoc
34     {
35         ignoreCase = Platform.isPlatformWindows();
36         if (ignoreCase)
37             s = s.toLowerCase();
38         FastStringBuffer sb = new FastStringBuffer();
39         for (int i = 0; i < s.length(); i++) {
40             char c = s.charAt(i);
41             switch (c) {
42                 case '.':
43                     sb.append("\\.");
44                     break;
45                 case '*':
46                     sb.append(".*");
47                     break;
48                 case '?':
49                     sb.append(".?");
50                     break;
51                 default:
52                     sb.append(c);
53                     break;
54             }
55         }
56         try {
57             pattern = Pattern.compile(sb.toString());
58         }
59         catch (PatternSyntaxException JavaDoc e) {
60             Log.debug(e);
61             pattern = null;
62         }
63     }
64
65     public boolean accepts(String JavaDoc name)
66     {
67         if (pattern == null)
68             return false;
69         if (ignoreCase)
70             name = name.toLowerCase();
71         Matcher JavaDoc matcher = pattern.matcher(name);
72         return matcher.matches();
73     }
74 }
75
Popular Tags