1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
package com.stevesoft.pat; |
9 |
|
|
10 |
|
import java.util.Hashtable; |
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
@see |
24 |
|
@see |
25 |
|
@see |
26 |
|
@see |
27 |
|
@see |
28 |
|
@see |
29 |
|
|
|
|
| 10.5% |
Uncovered Elements: 204 (228) |
Complexity: 69 |
Complexity Density: 0.54 |
|
30 |
|
public abstract class ReplaceRule |
31 |
|
{ |
32 |
|
|
33 |
|
protected ReplaceRule next = null; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public abstract void apply(StringBufferLike sb, RegRes r); |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
46 |
0 |
public Object clone1()... |
47 |
|
{ |
48 |
0 |
return new RuleHolder(this); |
49 |
|
} |
50 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
51 |
0 |
public final Object clone()... |
52 |
|
{ |
53 |
0 |
ReplaceRule x = (ReplaceRule) clone1(); |
54 |
0 |
ReplaceRule xsav = x; |
55 |
0 |
ReplaceRule y = this; |
56 |
0 |
while (y.next != null) |
57 |
|
{ |
58 |
0 |
x.next = (ReplaceRule) y.next.clone1(); |
59 |
0 |
x.name = y.name; |
60 |
0 |
x = x.next; |
61 |
0 |
y = y.next; |
62 |
|
} |
63 |
0 |
return xsav; |
64 |
|
} |
65 |
|
|
|
|
| 50% |
Uncovered Elements: 3 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
66 |
135 |
static ReplaceRule add(ReplaceRule head, ReplaceRule adding)... |
67 |
|
{ |
68 |
135 |
if (head == null) |
69 |
|
{ |
70 |
135 |
return head = adding; |
71 |
|
} |
72 |
0 |
head.addRule(adding); |
73 |
0 |
return head; |
74 |
|
} |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
0 |
public ReplaceRule add(ReplaceRule adding)... |
77 |
|
{ |
78 |
0 |
return add(this, adding); |
79 |
|
} |
80 |
|
|
81 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
82 |
0 |
public void addRule(ReplaceRule r)... |
83 |
|
{ |
84 |
0 |
if (next == null) |
85 |
|
{ |
86 |
0 |
next = r; |
87 |
|
} |
88 |
|
else |
89 |
|
{ |
90 |
0 |
next.addRule(r); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
|
static Regex getvar = null; |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
96 |
135 |
final static Regex getv()... |
97 |
|
{ |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
135 |
if (getvar != null) |
103 |
|
{ |
104 |
134 |
return (Regex) getvar.clone(); |
105 |
|
} |
106 |
1 |
getvar = new Regex("(?:\\\\(\\d+)|" + |
107 |
|
"\\$(?:" + "(\\d+)|" + |
108 |
|
"(\\w+)|" + |
109 |
|
"([&'`])|" + |
110 |
|
"\\{(?:(\\d+)|" + |
111 |
|
"([^\n}\\\\]+))}" + |
112 |
|
")|" + "\\\\([nrbtaef])|" + |
113 |
|
"\\\\c([\u0000-\uFFFF])|" + |
114 |
|
"\\\\x([A-Fa-f0-9]{2})|" + |
115 |
|
"\\\\([\u0000-\uFFFF])" + |
116 |
|
")"); |
117 |
1 |
getvar.optimize(); |
118 |
1 |
return getvar; |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
|
|
| 7.1% |
Uncovered Elements: 144 (155) |
Complexity: 45 |
Complexity Density: 0.53 |
|
126 |
135 |
public static ReplaceRule perlCode(String s)... |
127 |
|
{ |
128 |
|
|
129 |
|
|
130 |
135 |
try |
131 |
|
{ |
132 |
135 |
int mf = 0, mt = 0; |
133 |
135 |
Regex gv = getv(); |
134 |
135 |
ReplaceRule head = null; |
135 |
135 |
Object tmp = null; |
136 |
135 |
while (gv.searchFrom(s, mt)) |
137 |
|
{ |
138 |
0 |
int off = Regex.BackRefOffset - 1; |
139 |
0 |
mf = gv.matchedFrom(); |
140 |
0 |
if (mf > mt) |
141 |
|
{ |
142 |
0 |
head = add(head, new StringRule(s.substring(mt, mf))); |
143 |
|
} |
144 |
0 |
String var = null; |
145 |
0 |
if ((var = gv.stringMatched(1 + off)) != null |
146 |
|
|| (var = gv.stringMatched(2 + off)) != null |
147 |
|
|| (var = gv.stringMatched(5 + off)) != null) |
148 |
|
{ |
149 |
0 |
int d = 0; |
150 |
0 |
for (int i = 0; i < var.length(); i++) |
151 |
|
{ |
152 |
0 |
d = 8 * d + (var.charAt(i) - '0'); |
153 |
|
} |
154 |
0 |
if (var.length() == 1) |
155 |
|
{ |
156 |
0 |
head = add(head, new BackRefRule(d)); |
157 |
|
} |
158 |
|
else |
159 |
|
{ |
160 |
0 |
head = new StringRule("" + (char) d); |
161 |
|
} |
162 |
|
} |
163 |
0 |
else if ((var = gv.stringMatched(10 + off)) != null) |
164 |
|
{ |
165 |
0 |
if ("QELlUu".indexOf(var) >= 0) |
166 |
|
{ |
167 |
0 |
head = add(head, new CodeRule(var.charAt(0))); |
168 |
|
} |
169 |
|
else |
170 |
|
{ |
171 |
0 |
head = add(head, new StringRule(var)); |
172 |
|
} |
173 |
|
} |
174 |
0 |
else if ((var = gv.stringMatched(3 + off)) != null |
175 |
|
|| (var = gv.stringMatched(4 + off)) != null |
176 |
|
|| (var = gv.stringMatched(6 + off)) != null) |
177 |
|
{ |
178 |
0 |
String arg = ""; |
179 |
0 |
int pc; |
180 |
0 |
if ((pc = var.indexOf(':')) > 0) |
181 |
|
{ |
182 |
0 |
arg = var.substring(pc + 1); |
183 |
0 |
var = var.substring(0, pc); |
184 |
|
} |
185 |
0 |
if (var.equals("&") || var.equals("MATCH")) |
186 |
|
{ |
187 |
0 |
head = add(head, new AmpersandRule()); |
188 |
|
} |
189 |
0 |
else if (var.equals("`") || var.equals("PREMATCH")) |
190 |
|
{ |
191 |
0 |
head = add(head, new LeftRule()); |
192 |
|
} |
193 |
0 |
else if (var.equals("'") || var.equals("POSTMATCH")) |
194 |
|
{ |
195 |
0 |
head = add(head, new RightRule()); |
196 |
|
} |
197 |
0 |
else if (var.equals("WANT_MORE_TEXT")) |
198 |
|
{ |
199 |
0 |
head = add(head, new WantMoreTextReplaceRule()); |
200 |
|
} |
201 |
0 |
else if (var.equals("POP")) |
202 |
|
{ |
203 |
0 |
head = add(head, new PopRule()); |
204 |
|
} |
205 |
0 |
else if (var.startsWith("+") |
206 |
|
&& (tmp = defs.get(var.substring(1))) != null) |
207 |
|
{ |
208 |
0 |
if (tmp instanceof Regex) |
209 |
|
{ |
210 |
0 |
head = add(head, new PushRule(var.substring(1), (Regex) tmp)); |
211 |
|
} |
212 |
0 |
else if (tmp instanceof Transformer) |
213 |
|
{ |
214 |
0 |
head = add(head, |
215 |
|
new PushRule(var.substring(1), (Transformer) tmp)); |
216 |
|
} |
217 |
|
else |
218 |
|
{ |
219 |
0 |
head = add(head, new StringRule("${" + var + "}")); |
220 |
|
} |
221 |
|
} |
222 |
0 |
else if (var.startsWith("=") |
223 |
|
&& (tmp = defs.get(var.substring(1))) != null) |
224 |
|
{ |
225 |
0 |
if (tmp instanceof Regex) |
226 |
|
{ |
227 |
0 |
head = add(head, |
228 |
|
new ChangeRule(var.substring(1), (Regex) tmp)); |
229 |
|
} |
230 |
0 |
else if (tmp instanceof Transformer) |
231 |
|
{ |
232 |
0 |
head = add(head, |
233 |
|
new ChangeRule(var.substring(1), (Transformer) tmp)); |
234 |
|
} |
235 |
|
else |
236 |
|
{ |
237 |
0 |
head = add(head, new StringRule("${" + var + "}")); |
238 |
|
} |
239 |
|
} |
240 |
0 |
else if ((tmp = defs.get(var)) != null) |
241 |
|
{ |
242 |
0 |
if (tmp instanceof ReplaceRule) |
243 |
|
{ |
244 |
0 |
ReplaceRule alt = ((ReplaceRule) tmp).arg(arg); |
245 |
0 |
if (alt == null) |
246 |
|
{ |
247 |
0 |
alt = ((ReplaceRule) tmp); |
248 |
|
} |
249 |
0 |
head = add(head, (ReplaceRule) (alt.clone())); |
250 |
|
} |
251 |
|
} |
252 |
|
else |
253 |
|
|
254 |
|
{ |
255 |
0 |
head = add(head, new StringRule("${" + var + "}")); |
256 |
|
} |
257 |
|
} |
258 |
0 |
else if ((var = gv.stringMatched(7 + off)) != null) |
259 |
|
{ |
260 |
0 |
char c = var.charAt(0); |
261 |
0 |
if (c == 'n') |
262 |
|
{ |
263 |
0 |
head = add(head, new StringRule("\n")); |
264 |
|
} |
265 |
0 |
else if (c == 't') |
266 |
|
{ |
267 |
0 |
head = add(head, new StringRule("\t")); |
268 |
|
} |
269 |
0 |
else if (c == 'r') |
270 |
|
{ |
271 |
0 |
head = add(head, new StringRule("\r")); |
272 |
|
} |
273 |
0 |
else if (c == 'b') |
274 |
|
{ |
275 |
0 |
head = add(head, new StringRule("\r")); |
276 |
|
} |
277 |
0 |
else if (c == 'a') |
278 |
|
{ |
279 |
0 |
head = add(head, new StringRule("" + (char) 7)); |
280 |
|
} |
281 |
0 |
else if (c == 'e') |
282 |
|
{ |
283 |
0 |
head = add(head, new StringRule("" + (char) 27)); |
284 |
|
} |
285 |
0 |
else if (c == 'f') |
286 |
|
{ |
287 |
0 |
head = add(head, new StringRule("" + (char) 12)); |
288 |
|
} |
289 |
|
} |
290 |
0 |
else if ((var = gv.stringMatched(8 + off)) != null) |
291 |
|
{ |
292 |
0 |
char c = var.charAt(0); |
293 |
0 |
if (c < Ctrl.cmap.length) |
294 |
|
{ |
295 |
0 |
c = Ctrl.cmap[c]; |
296 |
|
} |
297 |
0 |
head = add(head, new StringRule("" + c)); |
298 |
|
} |
299 |
0 |
else if ((var = gv.stringMatched(9 + off)) != null) |
300 |
|
{ |
301 |
0 |
int d = 16 * getHexDigit(var.charAt(0)) |
302 |
|
+ getHexDigit(var.charAt(1)); |
303 |
0 |
head = add(head, new StringRule("" + (char) d)); |
304 |
|
} |
305 |
0 |
mt = gv.matchedTo(); |
306 |
|
} |
307 |
135 |
if (mt <= s.length()) |
308 |
|
{ |
309 |
135 |
head = add(head, new StringRule(s.substring(mt))); |
310 |
|
} |
311 |
135 |
return head; |
312 |
|
} finally |
313 |
|
{ |
314 |
|
|
315 |
|
|
316 |
|
} |
317 |
|
} |
318 |
|
|
319 |
|
static Hashtable defs = new Hashtable(); |
320 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
321 |
0 |
public static boolean isDefined(String s)... |
322 |
|
{ |
323 |
0 |
return defs.get(s) != null; |
324 |
|
} |
325 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
326 |
0 |
public static void define(String s, Regex r)... |
327 |
|
{ |
328 |
0 |
defs.put(s, r); |
329 |
|
} |
330 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
331 |
0 |
public static void define(String s, ReplaceRule r)... |
332 |
|
{ |
333 |
0 |
defs.put(s, r); |
334 |
0 |
r.name = s; |
335 |
|
} |
336 |
|
|
337 |
|
String name = getClass().getName(); |
338 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
339 |
0 |
public static void define(String s, Transformer t)... |
340 |
|
{ |
341 |
0 |
defs.put(s, t); |
342 |
|
} |
343 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
344 |
0 |
public static void undefine(String s)... |
345 |
|
{ |
346 |
0 |
defs.remove(s); |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
354 |
0 |
public String toString1()... |
355 |
|
{ |
356 |
0 |
return "${" + name + "}"; |
357 |
|
} |
358 |
|
|
359 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
360 |
0 |
public final String toString()... |
361 |
|
{ |
362 |
0 |
StringBuffer sb = new StringBuffer(); |
363 |
0 |
sb.append(toString1()); |
364 |
0 |
ReplaceRule rr = this.next; |
365 |
0 |
while (rr != null) |
366 |
|
{ |
367 |
0 |
sb.append(rr.toString1()); |
368 |
0 |
rr = rr.next; |
369 |
|
} |
370 |
0 |
return sb.toString(); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
@see |
382 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
383 |
0 |
public ReplaceRule arg(String s)... |
384 |
|
{ |
385 |
0 |
return null; |
386 |
|
} |
387 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
388 |
0 |
static int getHexDigit(char c)... |
389 |
|
{ |
390 |
0 |
if (c >= '0' && c <= '9') |
391 |
|
{ |
392 |
0 |
return c - '0'; |
393 |
|
} |
394 |
0 |
if (c >= 'a' && c <= 'f') |
395 |
|
{ |
396 |
0 |
return c - 'a' + 10; |
397 |
|
} |
398 |
0 |
return c - 'A' + 10; |
399 |
|
} |
400 |
|
} |