KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > WildCardFilter


1 // $Id: WildCardFilter.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // Wild card file name filter
4

5 /*
6  * Copyright 1999 by John D. Ramsdell and Olivier Refalo
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 import java.io.*;
26
27 public final class WildCardFilter
28 implements FilenameFilter
29 {
30   private /* final */ String JavaDoc pattern;
31   private /* final */ char wildCard;
32   private /* final */ int[] wildIndex; // Contains the index of each occurrence
33
private /* final */ String JavaDoc prefix; // of the wild card in the pattern
34
private /* final */ String JavaDoc suffix;
35
36   public WildCardFilter(String JavaDoc pattern, char wildCard) {
37     this.pattern = pattern;
38     this.wildCard = wildCard;
39     int wilds = 0;
40     for (int i = 0; i < pattern.length(); i++)
41       if (wildCard == pattern.charAt(i))
42     wilds++;
43     wildIndex = new int[wilds];
44     int j = 0;
45     for (int i = 0; j < wilds; i++)
46      if (wildCard == pattern.charAt(i))
47        wildIndex[j++] = i;
48     if (wilds == 0) {
49       prefix = null;
50       suffix = null;
51     }
52     else {
53       prefix = pattern.substring(0, wildIndex[0]);
54       suffix = pattern.substring(wildIndex[wilds - 1] + 1);
55     }
56   }
57
58   public boolean accept(File dir, String JavaDoc name) {
59     if (wildIndex.length == 0)
60       return pattern.equals(name);
61     else if (!name.startsWith(prefix)
62          || !name.endsWith(suffix))
63       return false;
64     else if (wildIndex.length == 1)
65       return true;
66     else {
67       int flen = name.length() - suffix.length();
68       int j = wildIndex[0];
69       int f = j; // index into file
70
for (int i = 1; i < wildIndex.length; i++) {
71     /* i is the index into wildIndex */
72     /* j is wildIndex[i - 1] at loop start */
73     /* pattern matched is pattern.substring(j + 1, wildIndex[i]) */
74     int pstart = j + 1;
75     j = wildIndex[i];
76     int plen = j - pstart;
77     for (;;) { // Find pattern in rest of name
78
if (plen + f > flen)
79         return false;
80       /* Is inlining regionMatches is faster that calling it? */
81       else if (name.regionMatches(f, pattern, pstart, plen))
82         break;
83       else
84         f++;
85     }
86     f += plen;
87       }
88       return true;
89     }
90   }
91
92   //Filter tests
93
static private void test(String JavaDoc pattern, String JavaDoc file) {
94     WildCardFilter f = new WildCardFilter(pattern, '*');
95     String JavaDoc match = f.accept(null, file) ? "matches" : "does not match";
96     System.out.println("String \"" + file + "\" " + match +
97                " pattern \"" + pattern + "\".");
98   }
99
100   public static void main(String JavaDoc args[]) {
101     if (args.length == 2)
102       test(args[0], args[1]);
103     else {
104       test("*", "toto");
105       test("toto.java", "tutu.java");
106       test("12345", "1234");
107       test("1234", "12345");
108       test("*f", "");
109       test("***", "toto");
110       test("*.java", "toto.");
111       test("*.java", "toto.jav");
112       test("*.java", "toto.java");
113       test("abc*", "");
114       test("a*c", "abbbbbccccc");
115       test("abc*xyz", "abcxxxyz");
116       test("*xyz", "abcxxxyz");
117       test("abc**xyz", "abcxxxyz");
118       test("abc**x", "abcxxx");
119       test("*a*b*c**x", "aaabcxxx");
120       test("abc*x*yz", "abcxxxyz");
121       test("abc*x*yz*", "abcxxxyz");
122       test("a*b*c*x*yf*z*", "aabbccxxxeeyffz");
123       test("a*b*c*x*yf*zze", "aabbccxxxeeyffz");
124       test("a*b*c*x*yf*ze", "aabbccxxxeeyffz");
125       test("a*b*c*x*yf*ze", "aabbccxxxeeyfze");
126       test("*LogServerInterface*.java", "_LogServerInterfaceImpl.java");
127       test("abc*xyz", "abcxyxyz");
128     }
129   }
130 }
131
Popular Tags