1 61 62 63 package org.jaxen; 64 65 import java.io.Serializable ; 66 import java.util.List ; 67 68 import org.jaxen.expr.Expr; 69 import org.jaxen.expr.XPathExpr; 70 import org.jaxen.function.BooleanFunction; 71 import org.jaxen.function.NumberFunction; 72 import org.jaxen.function.StringFunction; 73 import org.jaxen.saxpath.SAXPathException; 74 import org.jaxen.saxpath.XPathReader; 75 import org.jaxen.saxpath.helpers.XPathReaderFactory; 76 import org.jaxen.util.SingletonList; 77 78 99 public class BaseXPath implements XPath, Serializable 100 { 101 102 private String exprText; 103 104 105 private XPathExpr xpath; 106 107 108 private ContextSupport support; 109 110 111 private Navigator navigator; 112 113 120 protected BaseXPath(String xpathExpr) throws JaxenException 121 { 122 try 123 { 124 XPathReader reader = XPathReaderFactory.createReader(); 125 JaxenHandler handler = new JaxenHandler(); 126 reader.setXPathHandler( handler ); 127 reader.parse( xpathExpr ); 128 this.xpath = handler.getXPathExpr(); 129 } 130 catch (org.jaxen.saxpath.XPathSyntaxException e) 131 { 132 throw new org.jaxen.XPathSyntaxException( e ); 133 } 134 catch (SAXPathException e) 135 { 136 throw new JaxenException( e ); 137 } 138 139 this.exprText = xpathExpr; 140 } 141 142 151 public BaseXPath(String xpathExpr, Navigator navigator) throws JaxenException 152 { 153 this( xpathExpr ); 154 this.navigator = navigator; 155 } 156 157 184 public Object evaluate(Object context) throws JaxenException 185 { 186 List answer = selectNodes(context); 187 188 if ( answer != null 189 && 190 answer.size() == 1 ) 191 { 192 Object first = answer.get(0); 193 194 if ( first instanceof String 195 || 196 first instanceof Number 197 || 198 first instanceof Boolean ) 199 { 200 return first; 201 } 202 } 203 return answer; 204 } 205 206 224 public List selectNodes(Object node) throws JaxenException 225 { 226 Context context = getContext( node ); 227 return selectNodesForContext( context ); 228 } 229 230 246 public Object selectSingleNode(Object node) throws JaxenException 247 { 248 List results = selectNodes( node ); 249 250 if ( results.isEmpty() ) 251 { 252 return null; 253 } 254 255 return results.get( 0 ); 256 } 257 258 267 public String valueOf(Object node) throws JaxenException 268 { 269 return stringValueOf( node ); 270 } 271 272 290 public String stringValueOf(Object node) throws JaxenException 291 { 292 Context context = getContext( node ); 293 294 Object result = selectSingleNodeForContext( context ); 295 296 if ( result == null ) 297 { 298 return ""; 299 } 300 301 return StringFunction.evaluate( result, 302 context.getNavigator() ); 303 } 304 305 322 public boolean booleanValueOf(Object node) throws JaxenException 323 { 324 Context context = getContext( node ); 325 List result = selectNodesForContext( context ); 326 if ( result == null ) return false; 327 return BooleanFunction.evaluate( result, context.getNavigator() ).booleanValue(); 328 } 329 330 347 public Number numberValueOf(Object node) throws JaxenException 348 { 349 Context context = getContext( node ); 350 Object result = selectSingleNodeForContext( context ); 351 return NumberFunction.evaluate( result, 352 context.getNavigator() ); 353 } 354 355 357 380 public void addNamespace(String prefix, 381 String uri) throws JaxenException 382 { 383 NamespaceContext nsContext = getNamespaceContext(); 384 if ( nsContext instanceof SimpleNamespaceContext ) 385 { 386 ((SimpleNamespaceContext)nsContext).addNamespace( prefix, 387 uri ); 388 return; 389 } 390 391 throw new JaxenException("Operation not permitted while using a custom namespace context."); 392 } 393 394 395 401 402 416 public void setNamespaceContext(NamespaceContext namespaceContext) 417 { 418 getContextSupport().setNamespaceContext(namespaceContext); 419 } 420 421 435 public void setFunctionContext(FunctionContext functionContext) 436 { 437 getContextSupport().setFunctionContext(functionContext); 438 } 439 440 454 public void setVariableContext(VariableContext variableContext) 455 { 456 getContextSupport().setVariableContext(variableContext); 457 } 458 459 477 public NamespaceContext getNamespaceContext() 478 { 479 NamespaceContext answer = getContextSupport().getNamespaceContext(); 480 if ( answer == null ) { 481 answer = createNamespaceContext(); 482 getContextSupport().setNamespaceContext( answer ); 483 } 484 return answer; 485 } 486 487 505 public FunctionContext getFunctionContext() 506 { 507 FunctionContext answer = getContextSupport().getFunctionContext(); 508 if ( answer == null ) { 509 answer = createFunctionContext(); 510 getContextSupport().setFunctionContext( answer ); 511 } 512 return answer; 513 } 514 515 533 public VariableContext getVariableContext() 534 { 535 VariableContext answer = getContextSupport().getVariableContext(); 536 if ( answer == null ) { 537 answer = createVariableContext(); 538 getContextSupport().setVariableContext( answer ); 539 } 540 return answer; 541 } 542 543 544 558 public Expr getRootExpr() 559 { 560 return xpath.getRootExpr(); 561 } 562 563 567 public String toString() 568 { 569 return this.exprText; 570 } 571 572 578 public String debug() 579 { 580 return this.xpath.toString(); 581 } 582 583 589 590 598 protected Context getContext(Object node) 599 { 600 if ( node instanceof Context ) 601 { 602 return (Context) node; 603 } 604 605 Context fullContext = new Context( getContextSupport() ); 606 607 if ( node instanceof List ) 608 { 609 fullContext.setNodeSet( (List ) node ); 610 } 611 else 612 { 613 List list = new SingletonList(node); 614 fullContext.setNodeSet( list ); 615 } 616 617 return fullContext; 618 } 619 620 627 protected ContextSupport getContextSupport() 628 { 629 if ( support == null ) 630 { 631 support = new ContextSupport( 632 createNamespaceContext(), 633 createFunctionContext(), 634 createVariableContext(), 635 getNavigator() 636 ); 637 } 638 639 return support; 640 } 641 642 647 public Navigator getNavigator() 648 { 649 return navigator; 650 } 651 652 653 654 660 664 protected FunctionContext createFunctionContext() 665 { 666 return XPathFunctionContext.getInstance(); 667 } 668 669 673 protected NamespaceContext createNamespaceContext() 674 { 675 return new SimpleNamespaceContext(); 676 } 677 678 682 protected VariableContext createVariableContext() 683 { 684 return new SimpleVariableContext(); 685 } 686 687 703 protected List selectNodesForContext(Context context) throws JaxenException 704 { 705 List list = this.xpath.asList( context ); 706 return list; 707 708 } 709 710 711 727 protected Object selectSingleNodeForContext(Context context) throws JaxenException 728 { 729 List results = selectNodesForContext( context ); 730 731 if ( results.isEmpty() ) 732 { 733 return null; 734 } 735 736 return results.get( 0 ); 737 } 738 739 } 740 | Popular Tags |