1 16 19 package org.apache.xpath; 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.NodeVector; 27 import org.apache.xpath.res.XPATHErrorResources; 28 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.w3c.dom.traversal.NodeIterator; 32 33 34 58 public class NodeSetDTM extends NodeVector 59 implements DTMIterator, 60 Cloneable 61 { 62 63 66 public NodeSetDTM(DTMManager dtmManager) 67 { 68 super(); 69 m_manager = dtmManager; 70 } 71 72 78 public NodeSetDTM(int blocksize, int dummy, DTMManager dtmManager) 79 { 80 super(blocksize); 81 m_manager = dtmManager; 82 } 83 84 99 105 public NodeSetDTM(NodeSetDTM nodelist) 106 { 107 108 super(); 109 m_manager = nodelist.getDTMManager(); 110 m_root = nodelist.getRoot(); 111 112 addNodes((DTMIterator) nodelist); 113 } 114 115 121 public NodeSetDTM(DTMIterator ni) 122 { 123 124 super(); 125 126 m_manager = ni.getDTMManager(); 127 m_root = ni.getRoot(); 128 addNodes(ni); 129 } 130 131 137 public NodeSetDTM(NodeIterator iterator, XPathContext xctxt) 138 { 139 140 super(); 141 142 Node node; 143 m_manager = xctxt.getDTMManager(); 144 145 while (null != (node = iterator.nextNode())) 146 { 147 int handle = xctxt.getDTMHandleFromNode(node); 148 addNodeInDocOrder(handle, xctxt); 149 } 150 } 151 152 158 public NodeSetDTM(NodeList nodeList, XPathContext xctxt) 159 { 160 161 super(); 162 163 m_manager = xctxt.getDTMManager(); 164 165 int n = nodeList.getLength(); 166 for (int i = 0; i < n; i++) 167 { 168 Node node = nodeList.item(i); 169 int handle = xctxt.getDTMHandleFromNode(node); 170 addNode(handle); } 173 } 174 175 176 181 public NodeSetDTM(int node, DTMManager dtmManager) 182 { 183 184 super(); 185 m_manager = dtmManager; 186 187 addNode(node); 188 } 189 190 203 public void setEnvironment(Object environment) 204 { 205 } 207 208 209 213 public int getRoot() 214 { 215 if(DTM.NULL == m_root) 216 { 217 if(size() > 0) 218 return item(0); 219 else 220 return DTM.NULL; 221 } 222 else 223 return m_root; 224 } 225 226 233 public void setRoot(int context, Object environment) 234 { 235 } 237 238 250 public Object clone() throws CloneNotSupportedException 251 { 252 253 NodeSetDTM clone = (NodeSetDTM) super.clone(); 254 255 return clone; 256 } 257 258 268 public DTMIterator cloneWithReset() throws CloneNotSupportedException 269 { 270 271 NodeSetDTM clone = (NodeSetDTM) clone(); 272 273 clone.reset(); 274 275 return clone; 276 } 277 278 281 public void reset() 282 { 283 m_next = 0; 284 } 285 286 298 public int getWhatToShow() 299 { 300 return DTMFilter.SHOW_ALL & ~DTMFilter.SHOW_ENTITY_REFERENCE; 301 } 302 303 316 public DTMFilter getFilter() 317 { 318 return null; 319 } 320 321 337 public boolean getExpandEntityReferences() 338 { 339 return true; 340 } 341 342 351 public DTM getDTM(int nodeHandle) 352 { 353 354 return m_manager.getDTM(nodeHandle); 355 } 356 357 358 DTMManager m_manager; 359 360 367 public DTMManager getDTMManager() 368 { 369 370 return m_manager; 371 } 372 373 383 public int nextNode() 384 { 385 386 if ((m_next) < this.size()) 387 { 388 int next = this.elementAt(m_next); 389 390 m_next++; 391 392 return next; 393 } 394 else 395 return DTM.NULL; 396 } 397 398 409 public int previousNode() 410 { 411 412 if (!m_cacheNodes) 413 throw new RuntimeException ( 414 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_ITERATE, null)); 416 if ((m_next - 1) > 0) 417 { 418 m_next--; 419 420 return this.elementAt(m_next); 421 } 422 else 423 return DTM.NULL; 424 } 425 426 437 public void detach(){} 438 439 445 public void allowDetachToRelease(boolean allowRelease) 446 { 447 } 449 450 451 459 public boolean isFresh() 460 { 461 return (m_next == 0); 462 } 463 464 476 public void runTo(int index) 477 { 478 479 if (!m_cacheNodes) 480 throw new RuntimeException ( 481 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_INDEX, null)); 483 if ((index >= 0) && (m_next < m_firstFree)) 484 m_next = index; 485 else 486 m_next = m_firstFree - 1; 487 } 488 489 501 public int item(int index) 502 { 503 504 runTo(index); 505 506 return this.elementAt(index); 507 } 508 509 517 public int getLength() 518 { 519 520 runTo(-1); 521 522 return this.size(); 523 } 524 525 533 public void addNode(int n) 534 { 535 536 if (!m_mutable) 537 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 539 this.addElement(n); 540 } 541 542 551 public void insertNode(int n, int pos) 552 { 553 554 if (!m_mutable) 555 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 557 insertElementAt(n, pos); 558 } 559 560 567 public void removeNode(int n) 568 { 569 570 if (!m_mutable) 571 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 573 this.removeElement(n); 574 } 575 576 610 636 644 public void addNodes(DTMIterator iterator) 645 { 646 647 if (!m_mutable) 648 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 650 if (null != iterator) { 652 int obj; 653 654 while (DTM.NULL != (obj = iterator.nextNode())) 655 { 656 addElement(obj); 657 } 658 } 659 660 } 662 663 692 701 public void addNodesInDocOrder(DTMIterator iterator, XPathContext support) 702 { 703 704 if (!m_mutable) 705 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 707 int node; 708 709 while (DTM.NULL != (node = iterator.nextNode())) 710 { 711 addNodeInDocOrder(node, support); 712 } 713 } 714 715 780 793 public int addNodeInDocOrder(int node, boolean test, XPathContext support) 794 { 795 796 if (!m_mutable) 797 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 799 int insertIndex = -1; 800 801 if (test) 802 { 803 804 int size = size(), i; 808 809 for (i = size - 1; i >= 0; i--) 810 { 811 int child = elementAt(i); 812 813 if (child == node) 814 { 815 i = -2; 817 break; 818 } 819 820 DTM dtm = support.getDTM(node); 821 if (!dtm.isNodeAfter(node, child)) 822 { 823 break; 824 } 825 } 826 827 if (i != -2) 828 { 829 insertIndex = i + 1; 830 831 insertElementAt(node, insertIndex); 832 } 833 } 834 else 835 { 836 insertIndex = this.size(); 837 838 boolean foundit = false; 839 840 for (int i = 0; i < insertIndex; i++) 841 { 842 if (i == node) 843 { 844 foundit = true; 845 846 break; 847 } 848 } 849 850 if (!foundit) 851 addElement(node); 852 } 853 854 return insertIndex; 856 } 858 871 public int addNodeInDocOrder(int node, XPathContext support) 872 { 873 874 if (!m_mutable) 875 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 877 return addNodeInDocOrder(node, true, support); 878 } 880 885 public int size() 886 { 887 return super.size(); 888 } 889 890 897 public void addElement(int value) 898 { 899 900 if (!m_mutable) 901 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 903 super.addElement(value); 904 } 905 906 917 public void insertElementAt(int value, int at) 918 { 919 920 if (!m_mutable) 921 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 923 super.insertElementAt(value, at); 924 } 925 926 933 public void appendNodes(NodeVector nodes) 934 { 935 936 if (!m_mutable) 937 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 939 super.appendNodes(nodes); 940 } 941 942 950 public void removeAllElements() 951 { 952 953 if (!m_mutable) 954 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 956 super.removeAllElements(); 957 } 958 959 972 public boolean removeElement(int s) 973 { 974 975 if (!m_mutable) 976 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 978 return super.removeElement(s); 979 } 980 981 991 public void removeElementAt(int i) 992 { 993 994 if (!m_mutable) 995 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 997 super.removeElementAt(i); 998 } 999 1000 1012 public void setElementAt(int node, int index) 1013 { 1014 1015 if (!m_mutable) 1016 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 1018 super.setElementAt(node, index); 1019 } 1020 1021 1029 public void setItem(int node, int index) 1030 { 1031 1032 if (!m_mutable) 1033 throw new RuntimeException (XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); 1035 super.setElementAt(node, index); 1036 } 1037 1038 1045 public int elementAt(int i) 1046 { 1047 1048 runTo(i); 1049 1050 return super.elementAt(i); 1051 } 1052 1053 1060 public boolean contains(int s) 1061 { 1062 1063 runTo(-1); 1064 1065 return super.contains(s); 1066 } 1067 1068 1079 public int indexOf(int elem, int index) 1080 { 1081 1082 runTo(-1); 1083 1084 return super.indexOf(elem, index); 1085 } 1086 1087 1097 public int indexOf(int elem) 1098 { 1099 1100 runTo(-1); 1101 1102 return super.indexOf(elem); 1103 } 1104 1105 1107 transient protected int m_next = 0; 1108 1109 1117 public int getCurrentPos() 1118 { 1119 return m_next; 1120 } 1121 1122 1128 public void setCurrentPos(int i) 1129 { 1130 1131 if (!m_cacheNodes) 1132 throw new RuntimeException ( 1133 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_CANNOT_INDEX, null)); 1135 m_next = i; 1136 } 1137 1138 1145 public int getCurrentNode() 1146 { 1147 1148 if (!m_cacheNodes) 1149 throw new RuntimeException ( 1150 "This NodeSetDTM can not do indexing or counting functions!"); 1151 1152 int saved = m_next; 1153 int current = (m_next > 0) ? m_next-1 : m_next; 1157 int n = (current < m_firstFree) ? elementAt(current) : DTM.NULL; 1158 m_next = saved; return n; 1160 } 1161 1162 1163 transient protected boolean m_mutable = true; 1164 1165 1167 transient protected boolean m_cacheNodes = true; 1168 1169 1170 protected int m_root = DTM.NULL; 1171 1172 1178 public boolean getShouldCacheNodes() 1179 { 1180 return m_cacheNodes; 1181 } 1182 1183 1194 public void setShouldCacheNodes(boolean b) 1195 { 1196 1197 if (!isFresh()) 1198 throw new RuntimeException ( 1199 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CANNOT_CALL_SETSHOULDCACHENODE, null)); 1201 m_cacheNodes = b; 1202 m_mutable = true; 1203 } 1204 1205 1211 public boolean isMutable() 1212 { 1213 return m_mutable; 1214 } 1215 1216 transient private int m_last = 0; 1217 1218 public int getLast() 1219 { 1220 return m_last; 1221 } 1222 1223 public void setLast(int last) 1224 { 1225 m_last = last; 1226 } 1227 1228 1234 public boolean isDocOrdered() 1235 { 1236 return true; 1237 } 1238 1239 1245 public int getAxis() 1246 { 1247 return -1; 1248 } 1249 1250 1251} 1252 | Popular Tags |