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