1 16 package org.apache.commons.jxpath; 17 18 import java.text.DecimalFormatSymbols ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Locale ; 24 25 379 public abstract class JXPathContext { 380 protected JXPathContext parentContext; 381 protected Object contextBean; 382 protected Variables vars; 383 protected Functions functions; 384 protected AbstractFactory factory; 385 private Locale locale; 386 private boolean lenientSet = false; 387 private boolean lenient = false; 388 protected IdentityManager idManager; 389 protected KeyManager keyManager; 390 protected HashMap decimalFormats; 391 392 private static JXPathContextFactory contextFactory; 393 private static JXPathContext compilationContext; 394 395 private static final PackageFunctions GENERIC_FUNCTIONS = 396 new PackageFunctions("", null); 397 398 401 public static JXPathContext newContext(Object contextBean) { 402 return getContextFactory().newContext(null, contextBean); 403 } 404 405 410 public static JXPathContext newContext( 411 JXPathContext parentContext, 412 Object contextBean) 413 { 414 return getContextFactory().newContext(parentContext, contextBean); 415 } 416 417 420 private static JXPathContextFactory getContextFactory () { 421 if (contextFactory == null) { 422 contextFactory = JXPathContextFactory.newInstance(); 423 } 424 return contextFactory; 425 } 426 427 431 protected JXPathContext(JXPathContext parentContext, Object contextBean) { 432 this.parentContext = parentContext; 433 this.contextBean = contextBean; 434 } 435 436 439 public JXPathContext getParentContext() { 440 return parentContext; 441 } 442 443 446 public Object getContextBean() { 447 return contextBean; 448 } 449 450 453 public abstract Pointer getContextPointer(); 454 455 461 public abstract JXPathContext getRelativeContext(Pointer pointer); 462 463 466 public void setVariables(Variables vars) { 467 this.vars = vars; 468 } 469 470 476 public Variables getVariables() { 477 if (vars == null) { 478 vars = new BasicVariables(); 479 } 480 return vars; 481 } 482 483 488 public void setFunctions(Functions functions) { 489 this.functions = functions; 490 } 491 492 495 public Functions getFunctions() { 496 if (functions != null) { 497 return functions; 498 } 499 if (parentContext == null) { 500 return GENERIC_FUNCTIONS; 501 } 502 return null; 503 } 504 505 510 public void setFactory(AbstractFactory factory) { 511 this.factory = factory; 512 } 513 514 519 public AbstractFactory getFactory() { 520 if (factory == null && parentContext != null) { 521 return parentContext.getFactory(); 522 } 523 return factory; 524 } 525 526 532 public void setLocale(Locale locale) { 533 this.locale = locale; 534 } 535 536 541 public Locale getLocale() { 542 if (locale == null) { 543 if (parentContext != null) { 544 return parentContext.getLocale(); 545 } 546 else { 547 locale = Locale.getDefault(); 548 } 549 } 550 return locale; 551 } 552 553 561 public void setDecimalFormatSymbols( 562 String name, 563 DecimalFormatSymbols symbols) 564 { 565 if (decimalFormats == null) { 566 decimalFormats = new HashMap (); 567 } 568 decimalFormats.put(name, symbols); 569 } 570 571 574 public DecimalFormatSymbols getDecimalFormatSymbols(String name) { 575 if (decimalFormats == null) { 576 if (parentContext != null) { 577 return parentContext.getDecimalFormatSymbols(name); 578 } 579 return null; 580 } 581 return (DecimalFormatSymbols ) decimalFormats.get(name); 582 } 583 584 593 public void setLenient(boolean lenient) { 594 this.lenient = lenient; 595 lenientSet = true; 596 } 597 598 601 public boolean isLenient() { 602 if (!lenientSet && parentContext != null) { 603 return parentContext.isLenient(); 604 } 605 return lenient; 606 } 607 608 615 public static CompiledExpression compile(String xpath) { 616 if (compilationContext == null) { 617 compilationContext = JXPathContext.newContext(null); 618 } 619 return compilationContext.compilePath(xpath); 620 } 621 622 626 protected abstract CompiledExpression compilePath(String xpath); 627 628 638 public Object selectSingleNode(String xpath) { 639 Pointer pointer = getPointer(xpath); 640 if (pointer == null) { 641 return null; 642 } 643 return pointer.getNode(); 644 } 645 646 652 public List selectNodes(String xpath) { 653 ArrayList list = new ArrayList (); 654 Iterator iterator = iteratePointers(xpath); 655 while (iterator.hasNext()) { 656 Pointer pointer = (Pointer) iterator.next(); 657 list.add(pointer.getNode()); 658 } 659 return list; 660 } 661 662 666 public abstract Object getValue(String xpath); 667 668 672 public abstract Object getValue(String xpath, Class requiredType); 673 674 682 public abstract void setValue(String xpath, Object value); 683 684 691 public abstract Pointer createPath(String xpath); 692 693 707 public abstract Pointer createPathAndSetValue(String xpath, Object value); 708 709 712 public abstract void removePath(String xpath); 713 714 717 public abstract void removeAll(String xpath); 718 719 724 public abstract Iterator iterate(String xpath); 725 726 732 public abstract Pointer getPointer(String xpath); 733 734 740 public abstract Iterator iteratePointers(String xpath); 741 742 746 public void setIdentityManager(IdentityManager idManager) { 747 this.idManager = idManager; 748 } 749 750 754 public IdentityManager getIdentityManager() { 755 if (idManager == null && parentContext != null) { 756 return parentContext.getIdentityManager(); 757 } 758 return idManager; 759 } 760 761 766 public Pointer getPointerByID(String id) { 767 IdentityManager manager = getIdentityManager(); 768 if (manager != null) { 769 return manager.getPointerByID(this, id); 770 } 771 else { 772 throw new JXPathException( 773 "Cannot find an element by ID - " 774 + "no IdentityManager has been specified"); 775 } 776 } 777 778 782 public void setKeyManager(KeyManager keyManager) { 783 this.keyManager = keyManager; 784 } 785 786 790 public KeyManager getKeyManager() { 791 if (keyManager == null && parentContext != null) { 792 return parentContext.getKeyManager(); 793 } 794 return keyManager; 795 } 796 797 800 public Pointer getPointerByKey(String key, String value) { 801 KeyManager manager = getKeyManager(); 802 if (manager != null) { 803 return manager.getPointerByKey(this, key, value); 804 } 805 else { 806 throw new JXPathException( 807 "Cannot find an element by key - " 808 + "no KeyManager has been specified"); 809 } 810 } 811 812 818 public void registerNamespace(String prefix, String namespaceURI) { 819 throw new UnsupportedOperationException ( 820 "Namespace registration is not implemented by " + getClass()); 821 } 822 823 833 public String getNamespaceURI(String prefix) { 834 throw new UnsupportedOperationException ( 835 "Namespace registration is not implemented by " + getClass()); 836 } 837 838 847 public void setNamespaceContextPointer(Pointer namespaceContextPointer) { 848 throw new UnsupportedOperationException ( 849 "Namespace registration is not implemented by " + getClass()); 850 } 851 852 859 public Pointer getNamespaceContextPointer() { 860 throw new UnsupportedOperationException ( 861 "Namespace registration is not implemented by " + getClass()); 862 } 863 } | Popular Tags |