1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
package com.stevesoft.pat; |
9 |
|
|
10 |
|
import java.io.File; |
11 |
|
import java.util.StringTokenizer; |
12 |
|
import java.util.Vector; |
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
|
|
| 0% |
Uncovered Elements: 167 (167) |
Complexity: 43 |
Complexity Density: 0.43 |
|
70 |
|
public class FileRegex extends Regex |
71 |
|
{ |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
0 |
public FileRegex()... |
74 |
|
{ |
75 |
0 |
dirflag = EITHER; |
76 |
|
} |
77 |
|
|
78 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
79 |
0 |
public FileRegex(String s)... |
80 |
|
{ |
81 |
0 |
super(s); |
82 |
0 |
dirflag = EITHER; |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
@exception |
89 |
|
|
90 |
|
@see |
91 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
92 |
0 |
public void compile(String s) throws RegSyntax... |
93 |
|
{ |
94 |
0 |
String npat = toFileRegex(s); |
95 |
0 |
super.compile(npat); |
96 |
0 |
if (File.separatorChar == '\\') |
97 |
|
{ |
98 |
0 |
ignoreCase = true; |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 6 |
Complexity Density: 0.86 |
|
114 |
0 |
public boolean accept(File dir, String s)... |
115 |
|
{ |
116 |
0 |
if (dirflag != EITHER) |
117 |
|
{ |
118 |
0 |
File f = new File(s); |
119 |
0 |
if (f.isDirectory() && dirflag == NONDIR) |
120 |
|
{ |
121 |
0 |
return false; |
122 |
|
} |
123 |
0 |
if (!f.isDirectory() && dirflag == DIR) |
124 |
|
{ |
125 |
0 |
return false; |
126 |
|
} |
127 |
|
} |
128 |
0 |
return matchAt(s, 0); |
129 |
|
} |
130 |
|
|
131 |
|
int dirflag = 0; |
132 |
|
|
133 |
|
final static int EITHER = 0, DIR = 1, NONDIR = 2; |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
0 |
public static String[] list(String f)... |
146 |
|
{ |
147 |
0 |
return list(f, EITHER); |
148 |
|
} |
149 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
150 |
0 |
static String[] list(String f, int df)... |
151 |
|
{ |
152 |
|
|
153 |
0 |
StringTokenizer st = new StringTokenizer(f, File.pathSeparator); |
154 |
0 |
Vector v = new Vector(); |
155 |
0 |
while (st.hasMoreTokens()) |
156 |
|
{ |
157 |
0 |
String path = st.nextToken(); |
158 |
0 |
list1(path, v, df, true); |
159 |
|
} |
160 |
0 |
String[] sa = new String[v.size()]; |
161 |
0 |
v.copyInto(sa); |
162 |
0 |
return sa; |
163 |
|
} |
164 |
|
|
165 |
|
final static Regex root = new Regex( |
166 |
|
File.separatorChar == '/' ? "/$" : "(?:.:|)\\\\$"); |
167 |
|
|
|
|
| 0% |
Uncovered Elements: 39 (39) |
Complexity: 9 |
Complexity Density: 0.36 |
|
168 |
0 |
static void list1(String path, Vector v, int df, boolean rec)... |
169 |
|
{ |
170 |
|
|
171 |
0 |
if (root.matchAt(path, 0)) |
172 |
|
{ |
173 |
0 |
v.addElement(path + "."); |
174 |
0 |
return; |
175 |
|
} |
176 |
0 |
File f = new File(path); |
177 |
0 |
if (f.getParent() != null && rec) |
178 |
|
{ |
179 |
0 |
Vector v2 = new Vector(); |
180 |
0 |
list1(f.getParent(), v2, DIR, true); |
181 |
0 |
for (int i = 0; i < v2.size(); i++) |
182 |
|
{ |
183 |
0 |
String path2 = ((String) v2.elementAt(i)) + File.separator |
184 |
|
+ f.getName(); |
185 |
0 |
list1(path2, v, df, false); |
186 |
|
} |
187 |
|
} |
188 |
|
else |
189 |
|
{ |
190 |
0 |
File base = new File(path); |
191 |
|
|
192 |
0 |
String dir_s = base.getParent(); |
193 |
0 |
if (dir_s == null) |
194 |
|
{ |
195 |
0 |
dir_s = "."; |
196 |
|
} |
197 |
0 |
File dir = new File(dir_s); |
198 |
|
|
199 |
0 |
FileRegex fr = new FileRegex(base.getName()); |
200 |
0 |
if (fr.isLiteral()) |
201 |
|
{ |
202 |
0 |
v.addElement(dir_s + File.separator + base.getName()); |
203 |
0 |
return; |
204 |
|
} |
205 |
0 |
fr.dirflag = df; |
206 |
0 |
String[] sa = dir.list(fr); |
207 |
0 |
if (sa == null) |
208 |
|
{ |
209 |
0 |
return; |
210 |
|
} |
211 |
0 |
for (int i = 0; i < sa.length; i++) |
212 |
|
{ |
213 |
0 |
v.addElement(dir_s + File.separator + sa[i]); |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
|
|
| 0% |
Uncovered Elements: 71 (71) |
Complexity: 16 |
Complexity Density: 0.37 |
|
222 |
0 |
public static String toFileRegex(String s)... |
223 |
|
{ |
224 |
0 |
StrPos sp = new StrPos(s, 0); |
225 |
0 |
StringBuffer sb = new StringBuffer(); |
226 |
0 |
if (sp.incMatch("{?e=")) |
227 |
|
{ |
228 |
0 |
char e = sp.thisChar(); |
229 |
0 |
sp.inc(); |
230 |
0 |
if (sp.incMatch("}")) |
231 |
|
{ |
232 |
0 |
sb.append("(?e=" + e + ")^"); |
233 |
|
} |
234 |
|
else |
235 |
|
{ |
236 |
0 |
sb.append("^(?e="); |
237 |
|
} |
238 |
0 |
sp.esc = e; |
239 |
|
} |
240 |
0 |
int ParenLvl = 0; |
241 |
0 |
while (!sp.eos()) |
242 |
|
{ |
243 |
0 |
if (File.separatorChar == '\\') |
244 |
|
{ |
245 |
0 |
if (sp.escaped()) |
246 |
|
{ |
247 |
0 |
sb.append("\\\\"); |
248 |
|
} |
249 |
0 |
sp.dontMatch = false; |
250 |
|
} |
251 |
0 |
if (sp.incMatch("?")) |
252 |
|
{ |
253 |
0 |
sb.append("."); |
254 |
|
} |
255 |
0 |
else if (sp.incMatch(".")) |
256 |
|
{ |
257 |
0 |
sb.append(sp.esc); |
258 |
0 |
sb.append('.'); |
259 |
|
} |
260 |
0 |
else if (sp.incMatch("{??")) |
261 |
|
{ |
262 |
0 |
sb.append("(??"); |
263 |
0 |
ParenLvl++; |
264 |
|
|
265 |
|
} |
266 |
0 |
else if (sp.incMatch("{?!")) |
267 |
|
{ |
268 |
0 |
sb.append("(?!"); |
269 |
0 |
ParenLvl++; |
270 |
|
|
271 |
|
} |
272 |
0 |
else if (sp.incMatch("{?=")) |
273 |
|
{ |
274 |
0 |
sb.append("(?="); |
275 |
0 |
ParenLvl++; |
276 |
|
} |
277 |
0 |
else if (sp.incMatch("{")) |
278 |
|
{ |
279 |
0 |
sb.append("(?:"); |
280 |
0 |
ParenLvl++; |
281 |
|
} |
282 |
0 |
else if (sp.incMatch("}")) |
283 |
|
{ |
284 |
0 |
sb.append(')'); |
285 |
0 |
ParenLvl--; |
286 |
|
} |
287 |
0 |
else if (ParenLvl != 0 && sp.incMatch(",")) |
288 |
|
{ |
289 |
0 |
sb.append('|'); |
290 |
|
} |
291 |
0 |
else if (sp.incMatch("*")) |
292 |
|
{ |
293 |
0 |
sb.append(".*"); |
294 |
|
} |
295 |
|
else |
296 |
|
{ |
297 |
0 |
sb.append(sp.thisChar()); |
298 |
0 |
sp.inc(); |
299 |
|
} |
300 |
|
} |
301 |
0 |
sb.append("$"); |
302 |
0 |
return sb.toString(); |
303 |
|
} |
304 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 5 |
Complexity Density: 0.56 |
|
305 |
0 |
public boolean isLiteral()... |
306 |
|
{ |
307 |
0 |
Pattern x = thePattern; |
308 |
0 |
while (x != null && !(x instanceof End)) |
309 |
|
{ |
310 |
0 |
if (x instanceof oneChar) |
311 |
|
{ |
312 |
0 |
; |
313 |
|
} |
314 |
0 |
else if (x instanceof Skipped) |
315 |
|
{ |
316 |
0 |
; |
317 |
|
} |
318 |
|
else |
319 |
|
{ |
320 |
0 |
return false; |
321 |
|
} |
322 |
0 |
x = x.next; |
323 |
|
} |
324 |
0 |
return true; |
325 |
|
} |
326 |
|
} |