1 16 19 package org.apache.xpath; 20 21 import java.lang.reflect.Method ; 22 import java.util.Stack ; 23 import java.util.Vector ; 24 25 import javax.xml.transform.ErrorListener ; 26 import javax.xml.transform.SourceLocator ; 27 import javax.xml.transform.TransformerException ; 28 import javax.xml.transform.URIResolver ; 29 30 import org.apache.xalan.extensions.ExpressionContext; 31 import org.apache.xalan.res.XSLMessages; 32 import org.apache.xml.dtm.Axis; 33 import org.apache.xml.dtm.DTM; 34 import org.apache.xml.dtm.DTMFilter; 35 import org.apache.xml.dtm.DTMIterator; 36 import org.apache.xml.dtm.DTMManager; 37 import org.apache.xml.dtm.DTMWSFilter; 38 import org.apache.xml.dtm.ref.sax2dtm.SAX2RTFDTM; 39 import org.apache.xml.utils.IntStack; 40 import org.apache.xml.utils.NodeVector; 41 import org.apache.xml.utils.ObjectStack; 42 import org.apache.xml.utils.PrefixResolver; 43 import org.apache.xml.utils.SAXSourceLocator; 44 import org.apache.xml.utils.XMLString; 45 import org.apache.xpath.axes.SubContextList; 46 import org.apache.xpath.objects.XObject; 47 import org.apache.xpath.objects.XString; 48 import org.apache.xpath.res.XPATHErrorResources; 49 50 import org.xml.sax.XMLReader ; 51 52 58 public class XPathContext extends DTMManager { 60 IntStack m_last_pushed_rtfdtm=new IntStack(); 61 71 private Vector m_rtfdtm_stack=null; 72 73 private int m_which_rtfdtm=-1; 74 75 79 private SAX2RTFDTM m_global_rtfdtm=null; 80 81 82 87 protected DTMManager m_dtmManager = DTMManager.newInstance( 88 org.apache.xpath.objects.XMLStringFactoryImpl.getFactory()); 89 90 96 public DTMManager getDTMManager() 97 { 98 return m_dtmManager; 99 } 100 101 123 public DTM getDTM(javax.xml.transform.Source source, boolean unique, 124 DTMWSFilter wsfilter, 125 boolean incremental, 126 boolean doIndexing) 127 { 128 return m_dtmManager.getDTM(source, unique, wsfilter, 129 incremental, doIndexing); 130 } 131 132 139 public DTM getDTM(int nodeHandle) 140 { 141 return m_dtmManager.getDTM(nodeHandle); 142 } 143 144 152 public int getDTMHandleFromNode(org.w3c.dom.Node node) 153 { 154 return m_dtmManager.getDTMHandleFromNode(node); 155 } 156 161 public int getDTMIdentity(DTM dtm) 162 { 163 return m_dtmManager.getDTMIdentity(dtm); 164 } 165 170 public DTM createDocumentFragment() 171 { 172 return m_dtmManager.createDocumentFragment(); 173 } 174 184 public boolean release(DTM dtm, boolean shouldHardDelete) 185 { 186 if(m_rtfdtm_stack!=null && m_rtfdtm_stack.contains(dtm)) 192 { 193 return false; 194 } 195 196 return m_dtmManager.release(dtm, shouldHardDelete); 197 } 198 199 211 public DTMIterator createDTMIterator(Object xpathCompiler, int pos) 212 { 213 return m_dtmManager.createDTMIterator(xpathCompiler, pos); 214 } 215 229 public DTMIterator createDTMIterator(String xpathString, 230 PrefixResolver presolver) 231 { 232 return m_dtmManager.createDTMIterator(xpathString, presolver); 233 } 234 252 public DTMIterator createDTMIterator(int whatToShow, 253 DTMFilter filter, boolean entityReferenceExpansion) 254 { 255 return m_dtmManager.createDTMIterator(whatToShow, filter, entityReferenceExpansion); 256 } 257 258 265 public DTMIterator createDTMIterator(int node) 266 { 267 DTMIterator iter = new org.apache.xpath.axes.OneStepIteratorForward(Axis.SELF); 269 iter.setRoot(node, this); 270 return iter; 271 } 273 274 277 public XPathContext() 278 { 279 m_prefixResolvers.push(null); 280 m_currentNodes.push(DTM.NULL); 281 m_currentExpressionNodes.push(DTM.NULL); 282 m_saxLocations.push(null); 283 } 284 285 290 public XPathContext(Object owner) 291 { 292 m_owner = owner; 293 try { 294 m_ownerGetErrorListener = m_owner.getClass().getMethod("getErrorListener", new Class [] {}); 295 } 296 catch (NoSuchMethodException nsme) {} 297 m_prefixResolvers.push(null); 298 m_currentNodes.push(DTM.NULL); 299 m_currentExpressionNodes.push(DTM.NULL); 300 m_saxLocations.push(null); 301 } 302 303 306 public void reset() 307 { 308 if(m_rtfdtm_stack!=null) 310 for (java.util.Enumeration e = m_rtfdtm_stack.elements() ; e.hasMoreElements() ;) 311 m_dtmManager.release((DTM)e.nextElement(), true); 312 313 m_rtfdtm_stack=null; m_which_rtfdtm=-1; 315 316 if(m_global_rtfdtm!=null) 317 m_dtmManager.release(m_global_rtfdtm,true); 318 m_global_rtfdtm=null; 319 320 m_dtmManager = DTMManager.newInstance( 321 org.apache.xpath.objects.XMLStringFactoryImpl.getFactory()); 322 323 m_saxLocations.removeAllElements(); 324 m_axesIteratorStack.removeAllElements(); 325 m_contextNodeLists.removeAllElements(); 326 m_currentExpressionNodes.removeAllElements(); 327 m_currentNodes.removeAllElements(); 328 m_iteratorRoots.RemoveAllNoClear(); 329 m_predicatePos.removeAllElements(); 330 m_predicateRoots.RemoveAllNoClear(); 331 m_prefixResolvers.removeAllElements(); 332 333 m_prefixResolvers.push(null); 334 m_currentNodes.push(DTM.NULL); 335 m_currentExpressionNodes.push(DTM.NULL); 336 m_saxLocations.push(null); 337 } 338 339 340 ObjectStack m_saxLocations = new ObjectStack(RECURSIONLIMIT); 341 342 347 public void setSAXLocator(SourceLocator location) 348 { 349 m_saxLocations.setTop(location); 350 } 351 352 357 public void pushSAXLocator(SourceLocator location) 358 { 359 m_saxLocations.push(location); 360 } 361 362 368 public void pushSAXLocatorNull() 369 { 370 m_saxLocations.push(null); 371 } 372 373 374 377 public void popSAXLocator() 378 { 379 m_saxLocations.pop(); 380 } 381 382 387 public SourceLocator getSAXLocator() 388 { 389 return (SourceLocator ) m_saxLocations.peek(); 390 } 391 392 395 private Object m_owner; 396 397 400 private Method m_ownerGetErrorListener; 401 402 408 public Object getOwnerObject() 409 { 410 return m_owner; 411 } 412 413 415 419 private VariableStack m_variableStacks = new VariableStack(); 420 421 427 public final VariableStack getVarStack() 428 { 429 return m_variableStacks; 430 } 431 432 438 public final void setVarStack(VariableStack varStack) 439 { 440 m_variableStacks = varStack; 441 } 442 443 445 447 private SourceTreeManager m_sourceTreeManager = new SourceTreeManager(); 448 449 454 public final SourceTreeManager getSourceTreeManager() 455 { 456 return m_sourceTreeManager; 457 } 458 459 465 public void setSourceTreeManager(SourceTreeManager mgr) 466 { 467 m_sourceTreeManager = mgr; 468 } 469 470 472 473 private ErrorListener m_errorListener; 474 475 478 private ErrorListener m_defaultErrorListener; 479 480 485 public final ErrorListener getErrorListener() 486 { 487 488 if (null != m_errorListener) 489 return m_errorListener; 490 491 ErrorListener retval = null; 492 493 try { 494 if (null != m_ownerGetErrorListener) 495 retval = (ErrorListener ) m_ownerGetErrorListener.invoke(m_owner, new Object [] {}); 496 } 497 catch (Exception e) {} 498 499 if (null == retval) 500 { 501 if (null == m_defaultErrorListener) 502 m_defaultErrorListener = new org.apache.xml.utils.DefaultErrorHandler(); 503 retval = m_defaultErrorListener; 504 } 505 506 return retval; 507 } 508 509 514 public void setErrorListener(ErrorListener listener) throws IllegalArgumentException 515 { 516 if (listener == null) 517 throw new IllegalArgumentException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NULL_ERROR_HANDLER, null)); m_errorListener = listener; 519 } 520 521 522 524 526 private URIResolver m_uriResolver; 527 528 533 public final URIResolver getURIResolver() 534 { 535 return m_uriResolver; 536 } 537 538 544 public void setURIResolver(URIResolver resolver) 545 { 546 m_uriResolver = resolver; 547 } 548 549 551 552 public XMLReader m_primaryReader; 553 554 559 public final XMLReader getPrimaryReader() 560 { 561 return m_primaryReader; 562 } 563 564 569 public void setPrimaryReader(XMLReader reader) 570 { 571 m_primaryReader = reader; 572 } 573 574 576 577 578 580 589 private void assertion(boolean b, String msg) throws javax.xml.transform.TransformerException 590 { 591 if (!b) 592 { 593 ErrorListener errorHandler = getErrorListener(); 594 595 if (errorHandler != null) 596 { 597 errorHandler.fatalError( 598 new TransformerException ( 599 XSLMessages.createMessage( 600 XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION, 601 new Object []{ msg }), (SAXSourceLocator)this.getSAXLocator())); 602 } 603 } 604 } 605 606 610 613 private Stack m_contextNodeLists = new Stack (); 614 615 public Stack getContextNodeListsStack() { return m_contextNodeLists; } 616 public void setContextNodeListsStack(Stack s) { m_contextNodeLists = s; } 617 618 624 public final DTMIterator getContextNodeList() 625 { 626 627 if (m_contextNodeLists.size() > 0) 628 return (DTMIterator) m_contextNodeLists.peek(); 629 else 630 return null; 631 } 632 633 640 public final void pushContextNodeList(DTMIterator nl) 641 { 642 m_contextNodeLists.push(nl); 643 } 644 645 649 public final void popContextNodeList() 650 { 651 if(m_contextNodeLists.isEmpty()) 652 System.err.println("Warning: popContextNodeList when stack is empty!"); 653 else 654 m_contextNodeLists.pop(); 655 } 656 657 661 public static final int RECURSIONLIMIT = (1024*4); 662 663 667 private IntStack m_currentNodes = new IntStack(RECURSIONLIMIT); 668 669 671 public IntStack getCurrentNodeStack() {return m_currentNodes; } 672 public void setCurrentNodeStack(IntStack nv) { m_currentNodes = nv; } 673 674 679 public final int getCurrentNode() 680 { 681 return m_currentNodes.peek(); 682 } 683 684 690 public final void pushCurrentNodeAndExpression(int cn, int en) 691 { 692 m_currentNodes.push(cn); 693 m_currentExpressionNodes.push(cn); 694 } 695 696 699 public final void popCurrentNodeAndExpression() 700 { 701 m_currentNodes.quickPop(1); 702 m_currentExpressionNodes.quickPop(1); 703 } 704 705 712 public final void pushExpressionState(int cn, int en, PrefixResolver nc) 713 { 714 m_currentNodes.push(cn); 715 m_currentExpressionNodes.push(cn); 716 m_prefixResolvers.push(nc); 717 } 718 719 722 public final void popExpressionState() 723 { 724 m_currentNodes.quickPop(1); 725 m_currentExpressionNodes.quickPop(1); 726 m_prefixResolvers.pop(); 727 } 728 729 730 731 736 public final void pushCurrentNode(int n) 737 { 738 m_currentNodes.push(n); 739 } 740 741 744 public final void popCurrentNode() 745 { 746 m_currentNodes.quickPop(1); 747 } 748 749 752 public final void pushPredicateRoot(int n) 753 { 754 m_predicateRoots.push(n); 755 } 756 757 760 public final void popPredicateRoot() 761 { 762 m_predicateRoots.popQuick(); 763 } 764 765 768 public final int getPredicateRoot() 769 { 770 return m_predicateRoots.peepOrNull(); 771 } 772 773 776 public final void pushIteratorRoot(int n) 777 { 778 m_iteratorRoots.push(n); 779 } 780 781 784 public final void popIteratorRoot() 785 { 786 m_iteratorRoots.popQuick(); 787 } 788 789 792 public final int getIteratorRoot() 793 { 794 return m_iteratorRoots.peepOrNull(); 795 } 796 797 798 private NodeVector m_iteratorRoots = new NodeVector(); 799 800 801 private NodeVector m_predicateRoots = new NodeVector(); 802 803 804 private IntStack m_currentExpressionNodes = new IntStack(RECURSIONLIMIT); 805 806 807 public IntStack getCurrentExpressionNodeStack() { return m_currentExpressionNodes; } 808 public void setCurrentExpressionNodeStack(IntStack nv) { m_currentExpressionNodes = nv; } 809 810 private IntStack m_predicatePos = new IntStack(); 811 812 public final int getPredicatePos() 813 { 814 return m_predicatePos.peek(); 815 } 816 817 public final void pushPredicatePos(int n) 818 { 819 m_predicatePos.push(n); 820 } 821 822 public final void popPredicatePos() 823 { 824 m_predicatePos.pop(); 825 } 826 827 832 public final int getCurrentExpressionNode() 833 { 834 return m_currentExpressionNodes.peek(); 835 } 836 837 842 public final void pushCurrentExpressionNode(int n) 843 { 844 m_currentExpressionNodes.push(n); 845 } 846 847 851 public final void popCurrentExpressionNode() 852 { 853 m_currentExpressionNodes.quickPop(1); 854 } 855 856 private ObjectStack m_prefixResolvers 857 = new ObjectStack(RECURSIONLIMIT); 858 859 865 public final PrefixResolver getNamespaceContext() 866 { 867 return (PrefixResolver) m_prefixResolvers.peek(); 868 } 869 870 876 public final void setNamespaceContext(PrefixResolver pr) 877 { 878 m_prefixResolvers.setTop(pr); 879 } 880 881 887 public final void pushNamespaceContext(PrefixResolver pr) 888 { 889 m_prefixResolvers.push(pr); 890 } 891 892 896 public final void pushNamespaceContextNull() 897 { 898 m_prefixResolvers.push(null); 899 } 900 901 904 public final void popNamespaceContext() 905 { 906 m_prefixResolvers.pop(); 907 } 908 909 913 916 private Stack m_axesIteratorStack = new Stack (); 917 918 public Stack getAxesIteratorStackStacks() { return m_axesIteratorStack; } 919 public void setAxesIteratorStackStacks(Stack s) { m_axesIteratorStack = s; } 920 921 927 public final void pushSubContextList(SubContextList iter) 928 { 929 m_axesIteratorStack.push(iter); 930 } 931 932 936 public final void popSubContextList() 937 { 938 m_axesIteratorStack.pop(); 939 } 940 941 947 public SubContextList getSubContextList() 948 { 949 return m_axesIteratorStack.isEmpty() 950 ? null : (SubContextList) m_axesIteratorStack.peek(); 951 } 952 953 960 961 public org.apache.xpath.axes.SubContextList getCurrentNodeList() 962 { 963 return m_axesIteratorStack.isEmpty() 964 ? null : (SubContextList) m_axesIteratorStack.elementAt(0); 965 } 966 970 974 public final int getContextNode() 975 { 976 return this.getCurrentNode(); 977 } 978 979 984 public final DTMIterator getContextNodes() 985 { 986 987 try 988 { 989 DTMIterator cnl = getContextNodeList(); 990 991 if (null != cnl) 992 return cnl.cloneWithReset(); 993 else 994 return null; } 996 catch (CloneNotSupportedException cnse) 997 { 998 return null; } 1000 } 1001 1002 XPathExpressionContext expressionContext = new XPathExpressionContext(); 1003 1004 1009 public ExpressionContext getExpressionContext() 1010 { 1011 return expressionContext; 1012 } 1013 1014 public class XPathExpressionContext implements ExpressionContext 1015 { 1016 1023 public XPathContext getXPathContext() 1024 { 1025 return XPathContext.this; 1026 } 1027 1028 1034 public DTMManager getDTMManager() 1035 { 1036 return m_dtmManager; 1037 } 1038 1039 1043 public org.w3c.dom.Node getContextNode() 1044 { 1045 int context = getCurrentNode(); 1046 1047 return getDTM(context).getNode(context); 1048 } 1049 1050 1055 public org.w3c.dom.traversal.NodeIterator getContextNodes() 1056 { 1057 return new org.apache.xml.dtm.ref.DTMNodeIterator(getContextNodeList()); 1058 } 1059 1060 1064 public ErrorListener getErrorListener() 1065 { 1066 return XPathContext.this.getErrorListener(); 1067 } 1068 1069 1074 public double toNumber(org.w3c.dom.Node n) 1075 { 1076 int nodeHandle = getDTMHandleFromNode(n); 1078 DTM dtm = getDTM(nodeHandle); 1079 XString xobj = (XString)dtm.getStringValue(nodeHandle); 1080 return xobj.num(); 1081 } 1082 1083 1088 public String toString(org.w3c.dom.Node n) 1089 { 1090 int nodeHandle = getDTMHandleFromNode(n); 1092 DTM dtm = getDTM(nodeHandle); 1093 XMLString strVal = dtm.getStringValue(nodeHandle); 1094 return strVal.toString(); 1095 } 1096 1097 1103 1104 public final XObject getVariableOrParam(org.apache.xml.utils.QName qname) 1105 throws javax.xml.transform.TransformerException 1106 { 1107 return m_variableStacks.getVariableOrParam(XPathContext.this, qname); 1108 } 1109 1110 } 1111 1112 1128 public DTM getGlobalRTFDTM() 1129 { 1130 1140 if( m_global_rtfdtm==null || m_global_rtfdtm.isTreeIncomplete() ) 1144 { 1145 m_global_rtfdtm=(SAX2RTFDTM)m_dtmManager.getDTM(null,true,null,false,false); 1146 } 1147 return m_global_rtfdtm; 1148 } 1149 1150 1151 1152 1153 1162 public DTM getRTFDTM() 1163 { 1164 SAX2RTFDTM rtfdtm; 1165 1166 1176 if(m_rtfdtm_stack==null) 1177 { 1178 m_rtfdtm_stack=new Vector (); 1179 rtfdtm=(SAX2RTFDTM)m_dtmManager.getDTM(null,true,null,false,false); 1180 m_rtfdtm_stack.addElement(rtfdtm); 1181 ++m_which_rtfdtm; 1182 } 1183 else if(m_which_rtfdtm<0) 1184 { 1185 rtfdtm=(SAX2RTFDTM)m_rtfdtm_stack.elementAt(++m_which_rtfdtm); 1186 } 1187 else 1188 { 1189 rtfdtm=(SAX2RTFDTM)m_rtfdtm_stack.elementAt(m_which_rtfdtm); 1190 1191 if(rtfdtm.isTreeIncomplete()) 1198 { 1199 if(++m_which_rtfdtm < m_rtfdtm_stack.size()) 1200 rtfdtm=(SAX2RTFDTM)m_rtfdtm_stack.elementAt(m_which_rtfdtm); 1201 else 1202 { 1203 rtfdtm=(SAX2RTFDTM)m_dtmManager.getDTM(null,true,null,false,false); 1204 m_rtfdtm_stack.addElement(rtfdtm); 1205 } 1206 } 1207 } 1208 1209 return rtfdtm; 1210 } 1211 1212 1216 public void pushRTFContext() 1217 { 1218 m_last_pushed_rtfdtm.push(m_which_rtfdtm); 1219 if(null!=m_rtfdtm_stack) 1220 ((SAX2RTFDTM)(getRTFDTM())).pushRewindMark(); 1221 } 1222 1223 1237 public void popRTFContext() 1238 { 1239 int previous=m_last_pushed_rtfdtm.pop(); 1240 if(null==m_rtfdtm_stack) 1241 return; 1242 1243 if(m_which_rtfdtm==previous) 1244 { 1245 if(previous>=0) { 1247 boolean isEmpty=((SAX2RTFDTM)(m_rtfdtm_stack.elementAt(previous))).popRewindMark(); 1248 } 1249 } 1250 else while(m_which_rtfdtm!=previous) 1251 { 1252 boolean isEmpty=((SAX2RTFDTM)(m_rtfdtm_stack.elementAt(m_which_rtfdtm))).popRewindMark(); 1256 --m_which_rtfdtm; 1257 } 1258 } 1259} 1260 | Popular Tags |