1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
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 |
|
package org.jaxen; |
65 |
|
|
66 |
|
import java.util.Iterator; |
67 |
|
import java.util.LinkedList; |
68 |
|
|
69 |
|
import org.jaxen.expr.DefaultXPathFactory; |
70 |
|
import org.jaxen.expr.Expr; |
71 |
|
import org.jaxen.expr.FilterExpr; |
72 |
|
import org.jaxen.expr.FunctionCallExpr; |
73 |
|
import org.jaxen.expr.LocationPath; |
74 |
|
import org.jaxen.expr.Predicate; |
75 |
|
import org.jaxen.expr.Predicated; |
76 |
|
import org.jaxen.expr.Step; |
77 |
|
import org.jaxen.expr.XPathExpr; |
78 |
|
import org.jaxen.expr.XPathFactory; |
79 |
|
import org.jaxen.saxpath.Operator; |
80 |
|
import org.jaxen.saxpath.XPathHandler; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
public class JaxenHandler implements XPathHandler |
89 |
|
{ |
90 |
|
private XPathFactory xpathFactory; |
91 |
|
private XPathExpr xpath; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
protected boolean simplified; |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
protected LinkedList stack; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
public JaxenHandler() |
106 |
10722 |
{ |
107 |
10722 |
this.stack = new LinkedList(); |
108 |
10722 |
this.xpathFactory = new DefaultXPathFactory(); |
109 |
10722 |
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public void setXPathFactory(XPathFactory xpathFactory) |
117 |
|
{ |
118 |
150 |
this.xpathFactory = xpathFactory; |
119 |
150 |
} |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
public XPathFactory getXPathFactory() |
127 |
|
{ |
128 |
93258 |
return this.xpathFactory; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
public XPathExpr getXPathExpr() |
141 |
|
{ |
142 |
10716 |
return getXPathExpr( true ); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
public XPathExpr getXPathExpr(boolean shouldSimplify) |
156 |
|
{ |
157 |
10938 |
if ( shouldSimplify && ! this.simplified ) |
158 |
|
{ |
159 |
10716 |
this.xpath.simplify(); |
160 |
10716 |
this.simplified = true; |
161 |
|
} |
162 |
|
|
163 |
10938 |
return this.xpath; |
164 |
|
} |
165 |
|
|
166 |
|
public void startXPath() throws JaxenException |
167 |
|
{ |
168 |
10962 |
this.simplified = false; |
169 |
10962 |
pushFrame(); |
170 |
10962 |
} |
171 |
|
|
172 |
|
public void endXPath() throws JaxenException |
173 |
|
{ |
174 |
10764 |
this.xpath = getXPathFactory().createXPath( (Expr) pop() ); |
175 |
10764 |
popFrame(); |
176 |
10764 |
} |
177 |
|
|
178 |
|
public void startPathExpr() throws JaxenException |
179 |
|
{ |
180 |
23316 |
pushFrame(); |
181 |
23316 |
} |
182 |
|
|
183 |
|
public void endPathExpr() throws JaxenException |
184 |
|
{ |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
FilterExpr filterExpr; |
200 |
|
LocationPath locationPath; |
201 |
|
|
202 |
|
Object popped; |
203 |
|
|
204 |
23094 |
if ( stackSize() == 2 ) |
205 |
|
{ |
206 |
162 |
locationPath = (LocationPath) pop(); |
207 |
162 |
filterExpr = (FilterExpr) pop(); |
208 |
|
} |
209 |
|
else |
210 |
|
{ |
211 |
22932 |
popped = pop(); |
212 |
|
|
213 |
22932 |
if ( popped instanceof LocationPath ) |
214 |
|
{ |
215 |
9660 |
locationPath = (LocationPath) popped; |
216 |
9660 |
filterExpr = null; |
217 |
|
} |
218 |
|
else |
219 |
|
{ |
220 |
13272 |
locationPath = null; |
221 |
13272 |
filterExpr = (FilterExpr) popped; |
222 |
|
} |
223 |
|
} |
224 |
23094 |
popFrame(); |
225 |
|
|
226 |
23094 |
push( getXPathFactory().createPathExpr( filterExpr, |
227 |
|
locationPath ) ); |
228 |
23094 |
} |
229 |
|
|
230 |
|
public void startAbsoluteLocationPath() throws JaxenException |
231 |
|
{ |
232 |
6036 |
pushFrame(); |
233 |
|
|
234 |
6036 |
push( getXPathFactory().createAbsoluteLocationPath() ); |
235 |
6036 |
} |
236 |
|
|
237 |
|
public void endAbsoluteLocationPath() throws JaxenException |
238 |
|
{ |
239 |
5904 |
endLocationPath(); |
240 |
5904 |
} |
241 |
|
|
242 |
|
public void startRelativeLocationPath() throws JaxenException |
243 |
|
{ |
244 |
3930 |
pushFrame(); |
245 |
|
|
246 |
3930 |
push( getXPathFactory().createRelativeLocationPath() ); |
247 |
3930 |
} |
248 |
|
|
249 |
|
public void endRelativeLocationPath() throws JaxenException |
250 |
|
{ |
251 |
3918 |
endLocationPath(); |
252 |
3918 |
} |
253 |
|
|
254 |
|
protected void endLocationPath() throws JaxenException |
255 |
|
{ |
256 |
9822 |
LocationPath path = (LocationPath) peekFrame().removeFirst(); |
257 |
|
|
258 |
9822 |
addSteps( path, |
259 |
|
popFrame().iterator() ); |
260 |
|
|
261 |
9822 |
push( path ); |
262 |
9822 |
} |
263 |
|
|
264 |
|
protected void addSteps(LocationPath locationPath, |
265 |
|
Iterator stepIter) |
266 |
|
{ |
267 |
25686 |
while ( stepIter.hasNext() ) |
268 |
|
{ |
269 |
15864 |
locationPath.addStep( (Step) stepIter.next() ); |
270 |
|
} |
271 |
9822 |
} |
272 |
|
|
273 |
|
public void startNameStep(int axis, |
274 |
|
String prefix, |
275 |
|
String localName) throws JaxenException |
276 |
|
{ |
277 |
13158 |
pushFrame(); |
278 |
|
|
279 |
13158 |
push( getXPathFactory().createNameStep( axis, |
280 |
|
prefix, |
281 |
|
localName ) ); |
282 |
13158 |
} |
283 |
|
|
284 |
|
public void endNameStep() throws JaxenException |
285 |
|
{ |
286 |
13110 |
endStep(); |
287 |
13110 |
} |
288 |
|
|
289 |
|
public void startTextNodeStep(int axis) throws JaxenException |
290 |
|
{ |
291 |
|
|
292 |
240 |
pushFrame(); |
293 |
|
|
294 |
240 |
push( getXPathFactory().createTextNodeStep( axis ) ); |
295 |
240 |
} |
296 |
|
|
297 |
|
public void endTextNodeStep() throws JaxenException |
298 |
|
{ |
299 |
240 |
endStep(); |
300 |
240 |
} |
301 |
|
|
302 |
|
public void startCommentNodeStep(int axis) throws JaxenException |
303 |
|
{ |
304 |
150 |
pushFrame(); |
305 |
|
|
306 |
150 |
push( getXPathFactory().createCommentNodeStep( axis ) ); |
307 |
150 |
} |
308 |
|
|
309 |
|
public void endCommentNodeStep() throws JaxenException |
310 |
|
{ |
311 |
150 |
endStep(); |
312 |
150 |
} |
313 |
|
|
314 |
|
public void startAllNodeStep(int axis) throws JaxenException |
315 |
|
{ |
316 |
2268 |
pushFrame(); |
317 |
|
|
318 |
2268 |
push( getXPathFactory().createAllNodeStep( axis ) ); |
319 |
2268 |
} |
320 |
|
|
321 |
|
public void endAllNodeStep() throws JaxenException |
322 |
|
{ |
323 |
2268 |
endStep(); |
324 |
2268 |
} |
325 |
|
|
326 |
|
public void startProcessingInstructionNodeStep(int axis, |
327 |
|
String name) throws JaxenException |
328 |
|
{ |
329 |
210 |
pushFrame(); |
330 |
|
|
331 |
210 |
push( getXPathFactory().createProcessingInstructionNodeStep( axis, |
332 |
|
name ) ); |
333 |
210 |
} |
334 |
|
|
335 |
|
public void endProcessingInstructionNodeStep() throws JaxenException |
336 |
|
{ |
337 |
210 |
endStep(); |
338 |
210 |
} |
339 |
|
|
340 |
|
protected void endStep() |
341 |
|
{ |
342 |
15978 |
Step step = (Step) peekFrame().removeFirst(); |
343 |
|
|
344 |
15978 |
addPredicates( step, |
345 |
|
popFrame().iterator() ); |
346 |
|
|
347 |
15978 |
push( step ); |
348 |
15978 |
} |
349 |
|
|
350 |
|
public void startPredicate() throws JaxenException |
351 |
|
{ |
352 |
2364 |
pushFrame(); |
353 |
2364 |
} |
354 |
|
|
355 |
|
public void endPredicate() throws JaxenException |
356 |
|
{ |
357 |
2316 |
Predicate predicate = getXPathFactory().createPredicate( (Expr) pop() ); |
358 |
|
|
359 |
2316 |
popFrame(); |
360 |
|
|
361 |
2316 |
push( predicate ); |
362 |
2316 |
} |
363 |
|
|
364 |
|
public void startFilterExpr() throws JaxenException |
365 |
|
{ |
366 |
13494 |
pushFrame(); |
367 |
13494 |
} |
368 |
|
|
369 |
|
public void endFilterExpr() throws JaxenException |
370 |
|
{ |
371 |
13446 |
Expr expr = (Expr) peekFrame().removeFirst(); |
372 |
|
|
373 |
13446 |
FilterExpr filter = getXPathFactory().createFilterExpr( expr ); |
374 |
|
|
375 |
13446 |
Iterator predIter = popFrame().iterator(); |
376 |
|
|
377 |
13446 |
addPredicates( filter, |
378 |
|
predIter ); |
379 |
|
|
380 |
13446 |
push( filter ); |
381 |
13446 |
} |
382 |
|
|
383 |
|
protected void addPredicates(Predicated obj, |
384 |
|
Iterator predIter) |
385 |
|
{ |
386 |
31740 |
while ( predIter.hasNext() ) |
387 |
|
{ |
388 |
2316 |
obj.addPredicate( (Predicate) predIter.next() ); |
389 |
|
} |
390 |
29424 |
} |
391 |
|
|
392 |
|
protected void returnExpr() |
393 |
|
{ |
394 |
0 |
Expr expr = (Expr) pop(); |
395 |
0 |
popFrame(); |
396 |
0 |
push( expr ); |
397 |
0 |
} |
398 |
|
|
399 |
|
public void startOrExpr() throws JaxenException |
400 |
|
{ |
401 |
19260 |
} |
402 |
|
|
403 |
|
public void endOrExpr(boolean create) throws JaxenException |
404 |
|
{ |
405 |
|
|
406 |
19038 |
if ( create ) |
407 |
|
{ |
408 |
30 |
Expr rhs = (Expr) pop(); |
409 |
30 |
Expr lhs = (Expr) pop(); |
410 |
|
|
411 |
30 |
push( getXPathFactory().createOrExpr( lhs, |
412 |
|
rhs ) ); |
413 |
|
} |
414 |
19038 |
} |
415 |
|
|
416 |
|
public void startAndExpr() throws JaxenException |
417 |
|
{ |
418 |
19650 |
} |
419 |
|
|
420 |
|
public void endAndExpr(boolean create) throws JaxenException |
421 |
|
{ |
422 |
|
|
423 |
19428 |
if ( create ) |
424 |
|
{ |
425 |
|
|
426 |
390 |
Expr rhs = (Expr) pop(); |
427 |
390 |
Expr lhs = (Expr) pop(); |
428 |
|
|
429 |
390 |
push( getXPathFactory().createAndExpr( lhs, |
430 |
|
rhs ) ); |
431 |
|
} |
432 |
19428 |
} |
433 |
|
|
434 |
|
public void startEqualityExpr() throws JaxenException |
435 |
|
{ |
436 |
1902 |
} |
437 |
|
|
438 |
|
public void endEqualityExpr(int operator) throws JaxenException |
439 |
|
{ |
440 |
|
|
441 |
1902 |
if ( operator != Operator.NO_OP ) |
442 |
|
{ |
443 |
|
|
444 |
1902 |
Expr rhs = (Expr) pop(); |
445 |
1902 |
Expr lhs = (Expr) pop(); |
446 |
|
|
447 |
1902 |
push( getXPathFactory().createEqualityExpr( lhs, |
448 |
|
rhs, |
449 |
|
operator ) ); |
450 |
|
} |
451 |
1902 |
} |
452 |
|
|
453 |
|
public void startRelationalExpr() throws JaxenException |
454 |
|
{ |
455 |
600 |
} |
456 |
|
|
457 |
|
public void endRelationalExpr(int operator) throws JaxenException |
458 |
|
{ |
459 |
|
|
460 |
600 |
if ( operator != Operator.NO_OP ) |
461 |
|
{ |
462 |
|
|
463 |
600 |
Expr rhs = (Expr) pop(); |
464 |
600 |
Expr lhs = (Expr) pop(); |
465 |
|
|
466 |
600 |
push( getXPathFactory().createRelationalExpr( lhs, |
467 |
|
rhs, |
468 |
|
operator ) ); |
469 |
|
} |
470 |
600 |
} |
471 |
|
|
472 |
|
public void startAdditiveExpr() throws JaxenException |
473 |
|
{ |
474 |
654 |
} |
475 |
|
|
476 |
|
public void endAdditiveExpr(int operator) throws JaxenException |
477 |
|
{ |
478 |
|
|
479 |
648 |
if ( operator != Operator.NO_OP ) |
480 |
|
{ |
481 |
|
|
482 |
648 |
Expr rhs = (Expr) pop(); |
483 |
648 |
Expr lhs = (Expr) pop(); |
484 |
|
|
485 |
648 |
push( getXPathFactory().createAdditiveExpr( lhs, |
486 |
|
rhs, |
487 |
|
operator ) ); |
488 |
|
} |
489 |
648 |
} |
490 |
|
|
491 |
|
public void startMultiplicativeExpr() throws JaxenException |
492 |
|
{ |
493 |
510 |
} |
494 |
|
|
495 |
|
public void endMultiplicativeExpr(int operator) throws JaxenException |
496 |
|
{ |
497 |
|
|
498 |
510 |
if ( operator != Operator.NO_OP ) |
499 |
|
{ |
500 |
|
|
501 |
510 |
Expr rhs = (Expr) pop(); |
502 |
510 |
Expr lhs = (Expr) pop(); |
503 |
|
|
504 |
510 |
push( getXPathFactory().createMultiplicativeExpr( lhs, |
505 |
|
rhs, |
506 |
|
operator ) ); |
507 |
|
} |
508 |
510 |
} |
509 |
|
|
510 |
|
public void startUnaryExpr() throws JaxenException |
511 |
|
{ |
512 |
240 |
} |
513 |
|
|
514 |
|
public void endUnaryExpr(int operator) throws JaxenException |
515 |
|
{ |
516 |
|
|
517 |
240 |
if ( operator != Operator.NO_OP ) |
518 |
|
{ |
519 |
240 |
push( getXPathFactory().createUnaryExpr( (Expr) pop(), |
520 |
|
operator ) ); |
521 |
|
} |
522 |
240 |
} |
523 |
|
|
524 |
|
public void startUnionExpr() throws JaxenException |
525 |
|
{ |
526 |
23316 |
} |
527 |
|
|
528 |
|
public void endUnionExpr(boolean create) throws JaxenException |
529 |
|
{ |
530 |
|
|
531 |
23094 |
if ( create ) |
532 |
|
{ |
533 |
|
|
534 |
120 |
Expr rhs = (Expr) pop(); |
535 |
120 |
Expr lhs = (Expr) pop(); |
536 |
|
|
537 |
120 |
push( getXPathFactory().createUnionExpr( lhs, |
538 |
|
rhs ) ); |
539 |
|
} |
540 |
23094 |
} |
541 |
|
|
542 |
|
public void number(int number) throws JaxenException |
543 |
|
{ |
544 |
4266 |
push( getXPathFactory().createNumberExpr( number ) ); |
545 |
4266 |
} |
546 |
|
|
547 |
|
public void number(double number) throws JaxenException |
548 |
|
{ |
549 |
276 |
push( getXPathFactory().createNumberExpr( number ) ); |
550 |
276 |
} |
551 |
|
|
552 |
|
public void literal(String literal) throws JaxenException |
553 |
|
{ |
554 |
3906 |
push( getXPathFactory().createLiteralExpr( literal ) ); |
555 |
3906 |
} |
556 |
|
|
557 |
|
public void variableReference(String prefix, |
558 |
|
String variableName) throws JaxenException |
559 |
|
{ |
560 |
126 |
push( getXPathFactory().createVariableReferenceExpr( prefix, |
561 |
|
variableName ) ); |
562 |
126 |
} |
563 |
|
|
564 |
|
public void startFunction(String prefix, |
565 |
|
String functionName) throws JaxenException |
566 |
|
{ |
567 |
4632 |
pushFrame(); |
568 |
4632 |
push( getXPathFactory().createFunctionCallExpr( prefix, |
569 |
|
functionName ) ); |
570 |
4632 |
} |
571 |
|
|
572 |
|
public void endFunction() throws JaxenException |
573 |
|
{ |
574 |
4608 |
FunctionCallExpr function = (FunctionCallExpr) peekFrame().removeFirst(); |
575 |
|
|
576 |
4608 |
addParameters( function, |
577 |
|
popFrame().iterator() ); |
578 |
|
|
579 |
4608 |
push( function ); |
580 |
4608 |
} |
581 |
|
|
582 |
|
protected void addParameters(FunctionCallExpr function, |
583 |
|
Iterator paramIter) |
584 |
|
{ |
585 |
10080 |
while ( paramIter.hasNext() ) |
586 |
|
{ |
587 |
5472 |
function.addParameter( (Expr) paramIter.next() ); |
588 |
|
} |
589 |
4608 |
} |
590 |
|
|
591 |
|
protected int stackSize() |
592 |
|
{ |
593 |
23094 |
return peekFrame().size(); |
594 |
|
} |
595 |
|
|
596 |
|
protected void push(Object obj) |
597 |
|
{ |
598 |
112902 |
peekFrame().addLast( obj ); |
599 |
112902 |
} |
600 |
|
|
601 |
|
protected Object pop() |
602 |
|
{ |
603 |
44976 |
return peekFrame().removeLast(); |
604 |
|
} |
605 |
|
|
606 |
|
protected boolean canPop() |
607 |
|
{ |
608 |
0 |
return ( peekFrame().size() > 0 ); |
609 |
|
} |
610 |
|
|
611 |
|
protected void pushFrame() |
612 |
|
{ |
613 |
80760 |
this.stack.addLast( new LinkedList() ); |
614 |
80760 |
} |
615 |
|
|
616 |
|
protected LinkedList popFrame() |
617 |
|
{ |
618 |
80028 |
return (LinkedList) this.stack.removeLast(); |
619 |
|
} |
620 |
|
|
621 |
|
protected LinkedList peekFrame() |
622 |
|
{ |
623 |
224826 |
return (LinkedList) this.stack.getLast(); |
624 |
|
} |
625 |
|
} |