KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webman > stager > FileNameMatcher


1 package webman.stager;
2
3 import java.io.*;
4 import java.util.*;
5
6 /**
7  * a simple matcher class for patterns with asterisks and question marks
8  * results will be hold for an infinite time
9  * @author $Author: torsten $
10  * @version $Revision: 1.11 $
11  */

12 public class FileNameMatcher
13 {
14     Hashtable results = new Hashtable();
15
16     public FileNameMatcher()
17     {
18         // special case: pattern is * and name has length 0
19
Hashtable h = new Hashtable();
20         results.put("*", h);
21         h.put("", new String JavaDoc[]{""});
22     }
23
24     /**
25      * returns the substitution for the nth occurence of '*' or '?'
26      * in pattern for filename,
27      * n is 0 for first occurence
28      * returns null if there is no nth occurence of '*' or '?'
29      * or the filename doesn't match the filename
30      */

31     public String JavaDoc getMatchResult(String JavaDoc pattern, String JavaDoc filename, int n)
32     {
33         // try to get stored result
34
Hashtable h = (Hashtable)results.get(pattern);
35         if ( h == null && !matches(pattern, filename) )
36         {
37             return null;
38         }
39         else
40         {
41             h = (Hashtable)results.get(pattern);
42         }
43         String JavaDoc[] res = (String JavaDoc[])h.get(filename);
44         if ( res == null && !matches(pattern, filename) )
45         {
46             return null;
47         }
48         else
49         {
50             res = (String JavaDoc[])h.get(filename);
51         }
52         // ok, there's a match
53
if ( n < 0 || n >= res.length )
54         {
55             return null;
56         }
57         return res[n];
58     }
59
60     /**
61      * possible wildcards in pattern are * and ?
62      */

63     public boolean matches(String JavaDoc pattern, String JavaDoc filename)
64     {
65         //von mir eingefügt, damit whitespaces das matching nicht behindern:
66
filename = filename.trim();
67         // try to get stored result
68
Hashtable h = (Hashtable)results.get(pattern);
69         if ( h != null && h.get(filename) != null )
70         {
71             return true;
72         }
73         // replace ** with *
74
int i;
75         while ( (i = pattern.indexOf("**")) >= 0 )
76         {
77             pattern = pattern.substring(0, i) + pattern.substring(i + 1, pattern.length());
78         }
79         // count number of * and ?
80
int index = 0;
81         int num = 0;
82         while ( (i = pattern.indexOf('*', index)) >= 0 )
83         {
84             index = i + 1;
85             num++;
86         }
87         index = 0;
88         while ( (i = pattern.indexOf('?', index)) >= 0 )
89         {
90             index = i + 1;
91             num++;
92         }
93         // contains matches from filename to * and ?
94
String JavaDoc[] result = new String JavaDoc[num];
95
96         if ( matches(pattern.toCharArray(), 0, filename.toCharArray(), 0, result, 0) )
97         {
98             h = (Hashtable)results.get(pattern);
99             if ( h == null )
100             {
101                 h = new Hashtable();
102             }
103             h.put(filename, result);
104             results.put(pattern, h);
105             return true;
106         }
107         return false;
108     }
109
110     boolean matches(char[] t, int tp, char[] name, int np, String JavaDoc[] res, int rp)
111     {
112         // successfull reached the end
113
if ( tp == t.length && np == name.length )
114         {
115             // save result
116
return true;
117         }
118         // no more tokens but not some chars in name left
119
if ( tp == t.length )
120         {
121             return false;
122         }
123         // some tokens left but name is longer
124
if ( np == name.length )
125         {
126             return false;
127         }
128
129         
130         // nu gehts los
131
int next_tp = tp + 1;
132         if ( t[tp] == '*' )
133         {
134             //boolean result = false;
135
// try to match any number of chars of name with *
136
for ( int i = np; i <= name.length; i++ )
137             {
138                 if ( matches(t, next_tp, name, i, res, rp + 1) )
139                 {
140                     res[rp] = (new String JavaDoc(name)).substring(np, i);
141                     return true;
142                 }
143             }
144             return false;
145         }
146         else if ( t[tp] == '?' )
147         {
148             // match next char from name with ?
149
if ( matches(t, next_tp, name, np + 1, res, rp + 1) )
150             {
151                 res[rp] = (new String JavaDoc(name)).substring(np, np + 1);
152                 return true;
153             }
154             return false;
155         }
156         else
157         {
158             // must be some other char
159
return (t[tp] == name[np] && matches(t, next_tp, name, np + 1, res, rp));
160         }
161     }
162 }
Popular Tags