1 16 19 package org.apache.xpath.axes; 20 21 import org.apache.xalan.res.XSLMessages; 22 import org.apache.xml.dtm.DTM; 23 import org.apache.xml.dtm.DTMFilter; 24 import org.apache.xml.dtm.DTMIterator; 25 import org.apache.xml.dtm.DTMManager; 26 import org.apache.xml.utils.PrefixResolver; 27 import org.apache.xpath.ExpressionOwner; 28 import org.apache.xpath.XPathContext; 29 import org.apache.xpath.XPathVisitor; 30 import org.apache.xpath.compiler.Compiler; 31 import org.apache.xpath.objects.XNodeSet; 32 import org.apache.xpath.objects.XObject; 33 import org.apache.xpath.res.XPATHErrorResources; 34 35 47 public abstract class LocPathIterator extends PredicatedNodeTest 48 implements Cloneable , DTMIterator, java.io.Serializable , PathComponent 49 { 50 51 57 protected LocPathIterator() 58 { 59 } 60 61 62 68 protected LocPathIterator(PrefixResolver nscontext) 69 { 70 71 setLocPathIterator(this); 72 m_prefixResolver = nscontext; 73 } 74 75 87 protected LocPathIterator(Compiler compiler, int opPos, int analysis) 88 throws javax.xml.transform.TransformerException 89 { 90 this(compiler, opPos, analysis, true); 91 } 92 93 108 protected LocPathIterator( 109 Compiler compiler, int opPos, int analysis, boolean shouldLoadWalkers) 110 throws javax.xml.transform.TransformerException 111 { 112 setLocPathIterator(this); 113 } 114 115 119 public int getAnalysisBits() 120 { 121 int axis = getAxis(); 122 int bit = WalkerFactory.getAnalysisBitFromAxes(axis); 123 return bit; 124 } 125 126 134 private void readObject(java.io.ObjectInputStream stream) 135 throws java.io.IOException , javax.xml.transform.TransformerException 136 { 137 try 138 { 139 stream.defaultReadObject(); 140 m_clones = new IteratorPool(this); 141 } 142 catch (ClassNotFoundException cnfe) 143 { 144 throw new javax.xml.transform.TransformerException (cnfe); 145 } 146 } 147 148 161 public void setEnvironment(Object environment) 162 { 163 } 165 166 175 public DTM getDTM(int nodeHandle) 176 { 177 return m_execContext.getDTM(nodeHandle); 179 } 180 181 188 public DTMManager getDTMManager() 189 { 190 return m_execContext.getDTMManager(); 191 } 192 193 205 public XObject execute(XPathContext xctxt) 206 throws javax.xml.transform.TransformerException 207 { 208 209 XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance()); 210 211 iter.setRoot(xctxt.getCurrentNode(), xctxt); 212 213 return iter; 214 } 215 216 230 public void executeCharsToContentHandler( 231 XPathContext xctxt, org.xml.sax.ContentHandler handler) 232 throws javax.xml.transform.TransformerException , 233 org.xml.sax.SAXException 234 { 235 LocPathIterator clone = (LocPathIterator)m_clones.getInstance(); 236 237 int current = xctxt.getCurrentNode(); 238 clone.setRoot(current, xctxt); 239 240 int node = clone.nextNode(); 241 DTM dtm = clone.getDTM(node); 242 clone.detach(); 243 244 if(node != DTM.NULL) 245 { 246 dtm.dispatchCharactersEvents(node, handler, false); 247 } 248 } 249 250 265 public DTMIterator asIterator( 266 XPathContext xctxt, int contextNode) 267 throws javax.xml.transform.TransformerException 268 { 269 XNodeSet iter = new XNodeSet((LocPathIterator)m_clones.getInstance()); 270 271 iter.setRoot(contextNode, xctxt); 272 273 return iter; 274 } 275 276 277 282 public boolean isNodesetExpr() 283 { 284 return true; 285 } 286 287 295 public int asNode(XPathContext xctxt) 296 throws javax.xml.transform.TransformerException 297 { 298 DTMIterator iter = (DTMIterator)m_clones.getInstance(); 299 300 int current = xctxt.getCurrentNode(); 301 302 iter.setRoot(current, xctxt); 303 304 int next = iter.nextNode(); 305 iter.detach(); 307 return next; 308 } 309 310 319 public boolean bool(XPathContext xctxt) 320 throws javax.xml.transform.TransformerException 321 { 322 return (asNode(xctxt) != DTM.NULL); 323 } 324 325 326 334 public void setIsTopLevel(boolean b) 335 { 336 m_isTopLevel = b; 337 } 338 339 347 public boolean getIsTopLevel() 348 { 349 return m_isTopLevel; 350 } 351 352 359 public void setRoot(int context, Object environment) 360 { 361 362 m_context = context; 363 364 XPathContext xctxt = (XPathContext)environment; 365 m_execContext = xctxt; 366 m_cdtm = xctxt.getDTM(context); 367 368 m_currentContextNode = context; 370 if(null == m_prefixResolver) 372 m_prefixResolver = xctxt.getNamespaceContext(); 373 374 m_lastFetched = DTM.NULL; 375 m_foundLast = false; 376 m_pos = 0; 377 m_length = -1; 378 379 if (m_isTopLevel) 380 this.m_stackFrame = xctxt.getVarStack().getStackFrame(); 381 382 } 384 385 391 protected void setNextPosition(int next) 392 { 393 assertion(false, "setNextPosition not supported in this iterator!"); 394 } 395 396 405 public final int getCurrentPos() 406 { 407 return m_pos; 408 } 409 410 411 417 public void setShouldCacheNodes(boolean b) 418 { 419 420 assertion(false, "setShouldCacheNodes not supported by this iterater!"); 421 } 422 423 429 public boolean isMutable() 430 { 431 return false; 432 } 433 434 440 public void setCurrentPos(int i) 441 { 442 assertion(false, "setCurrentPos not supported by this iterator!"); 443 } 444 445 448 public void incrementCurrentPos() 449 { 450 m_pos++; 451 } 452 453 454 464 public int size() 465 { 466 assertion(false, "size() not supported by this iterator!"); 467 return 0; 468 } 469 470 479 public int item(int index) 480 { 481 assertion(false, "item(int index) not supported by this iterator!"); 482 return 0; 483 } 484 485 498 public void setItem(int node, int index) 499 { 500 assertion(false, "setItem not supported by this iterator!"); 501 } 502 503 509 public int getLength() 510 { 511 boolean isPredicateTest = (this == m_execContext.getSubContextList()); 513 514 int predCount = getPredicateCount(); 516 517 if (-1 != m_length && isPredicateTest && m_predicateIndex < 1) 521 return m_length; 522 523 if (m_foundLast) 526 return m_pos; 527 528 int pos = (m_predicateIndex >= 0) ? getProximityPosition() : m_pos; 532 533 LocPathIterator clone; 534 535 try 536 { 537 clone = (LocPathIterator) clone(); 538 } 539 catch (CloneNotSupportedException cnse) 540 { 541 return -1; 542 } 543 544 if (predCount > 0 && isPredicateTest) 548 { 549 clone.m_predCount = m_predicateIndex; 551 } 555 556 int next; 557 558 while (DTM.NULL != (next = clone.nextNode())) 559 { 560 pos++; 561 } 562 563 if (isPredicateTest && m_predicateIndex < 1) 564 m_length = pos; 565 566 return pos; 567 } 568 569 576 public boolean isFresh() 577 { 578 return (m_pos == 0); 579 } 580 581 587 public int previousNode() 588 { 589 throw new RuntimeException ( 590 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_ITERATE, null)); } 592 593 606 public int getWhatToShow() 607 { 608 609 return DTMFilter.SHOW_ALL & ~DTMFilter.SHOW_ENTITY_REFERENCE; 611 } 612 613 621 public DTMFilter getFilter() 622 { 623 return null; 624 } 625 626 632 public int getRoot() 633 { 634 return m_context; 635 } 636 637 652 public boolean getExpandEntityReferences() 653 { 654 return true; 655 } 656 657 658 protected boolean m_allowDetach = true; 659 660 666 public void allowDetachToRelease(boolean allowRelease) 667 { 668 m_allowDetach = allowRelease; 669 } 670 671 678 public void detach() 679 { 680 if(m_allowDetach) 681 { 682 m_execContext = null; 685 m_cdtm = null; 687 m_length = -1; 688 m_pos = 0; 689 m_lastFetched = DTM.NULL; 690 m_context = DTM.NULL; 691 m_currentContextNode = DTM.NULL; 692 693 m_clones.freeInstance(this); 694 } 695 } 696 697 700 public void reset() 701 { 702 assertion(false, "This iterator can not reset!"); 703 } 704 705 713 public DTMIterator cloneWithReset() throws CloneNotSupportedException 714 { 715 LocPathIterator clone; 716 clone = (LocPathIterator)m_clones.getInstanceOrThrow(); 718 clone.m_execContext = m_execContext; 719 clone.m_cdtm = m_cdtm; 720 721 clone.m_context = m_context; 722 clone.m_currentContextNode = m_currentContextNode; 723 clone.m_stackFrame = m_stackFrame; 724 725 727 return clone; 728 } 729 730 746 753 public abstract int nextNode(); 754 755 763 protected int returnNextNode(int nextNode) 764 { 765 766 if (DTM.NULL != nextNode) 767 { 768 m_pos++; 769 } 770 771 m_lastFetched = nextNode; 772 773 if (DTM.NULL == nextNode) 774 m_foundLast = true; 775 776 return nextNode; 777 } 778 779 784 public int getCurrentNode() 785 { 786 return m_lastFetched; 787 } 788 789 798 public void runTo(int index) 799 { 800 801 if (m_foundLast || ((index >= 0) && (index <= getCurrentPos()))) 802 return; 803 804 int n; 805 806 if (-1 == index) 807 { 808 while (DTM.NULL != (n = nextNode())); 809 } 810 else 811 { 812 while (DTM.NULL != (n = nextNode())) 813 { 814 if (getCurrentPos() >= index) 815 break; 816 } 817 } 818 } 819 820 825 public final boolean getFoundLast() 826 { 827 return m_foundLast; 828 } 829 830 836 public final XPathContext getXPathContext() 837 { 838 return m_execContext; 839 } 840 841 846 public final int getContext() 847 { 848 return m_context; 849 } 850 851 857 public final int getCurrentContextNode() 858 { 859 return m_currentContextNode; 860 } 861 862 867 public final void setCurrentContextNode(int n) 868 { 869 m_currentContextNode = n; 870 } 871 872 883 889 public final PrefixResolver getPrefixResolver() 890 { 891 if(null == m_prefixResolver) 892 { 893 m_prefixResolver = (PrefixResolver)getExpressionOwner(); 894 } 895 896 return m_prefixResolver; 897 } 898 899 909 919 922 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 923 { 924 if(visitor.visitLocationPath(owner, this)) 925 { 926 visitor.visitStep(owner, this); 927 callPredicateVisitors(visitor); 928 } 929 } 930 931 932 934 939 transient protected IteratorPool m_clones = new IteratorPool(this); 940 941 945 transient protected DTM m_cdtm; 946 947 950 transient int m_stackFrame = -1; 951 952 958 private boolean m_isTopLevel = false; 959 960 961 transient public int m_lastFetched = DTM.NULL; 962 963 967 transient protected int m_context = DTM.NULL; 968 969 975 transient protected int m_currentContextNode = DTM.NULL; 976 977 980 transient protected int m_pos = 0; 981 982 transient protected int m_length = -1; 983 984 989 private PrefixResolver m_prefixResolver; 990 991 995 transient protected XPathContext m_execContext; 996 997 1003 public boolean isDocOrdered() 1004 { 1005 return true; 1006 } 1007 1008 1014 public int getAxis() 1015 { 1016 return -1; 1017 } 1018 1019 1020 1030 public int getLastPos(XPathContext xctxt) 1031 { 1032 return getLength(); 1033 } 1034 1035} 1036 | Popular Tags |