01: /*
02: * DirectoryFilenameFilter.java
03: *
04: * Copyright (C) 1998-2003 Peter Graves
05: * $Id: DirectoryFilenameFilter.java,v 1.3 2003/07/04 17:26:44 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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;
25: import java.util.regex.Pattern;
26: import java.util.regex.PatternSyntaxException;
27:
28: public final class DirectoryFilenameFilter {
29: private Pattern pattern;
30: private boolean ignoreCase;
31:
32: public DirectoryFilenameFilter(String s)
33: throws PatternSyntaxException {
34: ignoreCase = Platform.isPlatformWindows();
35: if (ignoreCase)
36: s = s.toLowerCase();
37: FastStringBuffer sb = new FastStringBuffer();
38: for (int i = 0; i < s.length(); i++) {
39: char c = s.charAt(i);
40: switch (c) {
41: case '.':
42: sb.append("\\.");
43: break;
44: case '*':
45: sb.append(".*");
46: break;
47: case '?':
48: sb.append(".?");
49: break;
50: default:
51: sb.append(c);
52: break;
53: }
54: }
55: try {
56: pattern = Pattern.compile(sb.toString());
57: } catch (PatternSyntaxException e) {
58: Log.debug(e);
59: pattern = null;
60: }
61: }
62:
63: public boolean accepts(String name) {
64: if (pattern == null)
65: return false;
66: if (ignoreCase)
67: name = name.toLowerCase();
68: Matcher matcher = pattern.matcher(name);
69: return matcher.matches();
70: }
71: }
|