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