1 16 19 package org.apache.xml.dtm.ref; 20 21 import org.apache.xml.dtm.*; 22 23 import javax.xml.transform.Source ; 24 25 import org.apache.xml.utils.XMLStringFactory; 26 27 import org.apache.xml.res.XMLErrorResources; 28 import org.apache.xml.res.XMLMessages; 29 30 31 34 public abstract class DTMDefaultBaseIterators extends DTMDefaultBaseTraversers 35 { 36 37 50 public DTMDefaultBaseIterators(DTMManager mgr, Source source, 51 int dtmIdentity, 52 DTMWSFilter whiteSpaceFilter, 53 XMLStringFactory xstringfactory, 54 boolean doIndexing) 55 { 56 super(mgr, source, dtmIdentity, whiteSpaceFilter, 57 xstringfactory, doIndexing); 58 } 59 60 76 public DTMDefaultBaseIterators(DTMManager mgr, Source source, 77 int dtmIdentity, 78 DTMWSFilter whiteSpaceFilter, 79 XMLStringFactory xstringfactory, 80 boolean doIndexing, 81 int blocksize, 82 boolean usePrevsib, 83 boolean newNameTable) 84 { 85 super(mgr, source, dtmIdentity, whiteSpaceFilter, 86 xstringfactory, doIndexing, blocksize, usePrevsib, newNameTable); 87 } 88 89 100 public DTMAxisIterator getTypedAxisIterator(int axis, int type) 101 { 102 103 DTMAxisIterator iterator = null; 104 105 109 110 { 119 switch (axis) 120 { 121 case Axis.SELF : 122 iterator = new TypedSingletonIterator(type); 123 break; 124 case Axis.CHILD : 125 iterator = new TypedChildrenIterator(type); 126 break; 127 case Axis.PARENT : 128 return (new ParentIterator().setNodeType(type)); 129 case Axis.ANCESTOR : 130 return (new TypedAncestorIterator(type)); 131 case Axis.ANCESTORORSELF : 132 return ((new TypedAncestorIterator(type)).includeSelf()); 133 case Axis.ATTRIBUTE : 134 return (new TypedAttributeIterator(type)); 135 case Axis.DESCENDANT : 136 iterator = new TypedDescendantIterator(type); 137 break; 138 case Axis.DESCENDANTORSELF : 139 iterator = (new TypedDescendantIterator(type)).includeSelf(); 140 break; 141 case Axis.FOLLOWING : 142 iterator = new TypedFollowingIterator(type); 143 break; 144 case Axis.PRECEDING : 145 iterator = new TypedPrecedingIterator(type); 146 break; 147 case Axis.FOLLOWINGSIBLING : 148 iterator = new TypedFollowingSiblingIterator(type); 149 break; 150 case Axis.PRECEDINGSIBLING : 151 iterator = new TypedPrecedingSiblingIterator(type); 152 break; 153 case Axis.NAMESPACE : 154 iterator = new TypedNamespaceIterator(type); 155 break; 156 case Axis.ROOT : 157 iterator = new TypedRootIterator(type); 158 break; 159 default : 160 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_TYPED_ITERATOR_AXIS_NOT_IMPLEMENTED, new Object []{Axis.names[axis]})); } 163 } 164 165 return (iterator); 166 } 167 168 178 public DTMAxisIterator getAxisIterator(final int axis) 179 { 180 181 DTMAxisIterator iterator = null; 182 183 switch (axis) 184 { 185 case Axis.SELF : 186 iterator = new SingletonIterator(); 187 break; 188 case Axis.CHILD : 189 iterator = new ChildrenIterator(); 190 break; 191 case Axis.PARENT : 192 return (new ParentIterator()); 193 case Axis.ANCESTOR : 194 return (new AncestorIterator()); 195 case Axis.ANCESTORORSELF : 196 return ((new AncestorIterator()).includeSelf()); 197 case Axis.ATTRIBUTE : 198 return (new AttributeIterator()); 199 case Axis.DESCENDANT : 200 iterator = new DescendantIterator(); 201 break; 202 case Axis.DESCENDANTORSELF : 203 iterator = (new DescendantIterator()).includeSelf(); 204 break; 205 case Axis.FOLLOWING : 206 iterator = new FollowingIterator(); 207 break; 208 case Axis.PRECEDING : 209 iterator = new PrecedingIterator(); 210 break; 211 case Axis.FOLLOWINGSIBLING : 212 iterator = new FollowingSiblingIterator(); 213 break; 214 case Axis.PRECEDINGSIBLING : 215 iterator = new PrecedingSiblingIterator(); 216 break; 217 case Axis.NAMESPACE : 218 iterator = new NamespaceIterator(); 219 break; 220 case Axis.ROOT : 221 iterator = new RootIterator(); 222 break; 223 default : 224 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_ITERATOR_AXIS_NOT_IMPLEMENTED, new Object []{Axis.names[axis]})); } 227 228 return (iterator); 229 } 230 231 239 public abstract class InternalAxisIteratorBase extends DTMAxisIteratorBase 240 { 241 242 250 255 protected int _currentNode; 256 257 262 public void setMark() 263 { 264 _markedNode = _currentNode; 265 } 266 267 272 public void gotoMark() 273 { 274 _currentNode = _markedNode; 275 } 276 277 } 279 282 public final class ChildrenIterator extends InternalAxisIteratorBase 283 { 284 285 297 public DTMAxisIterator setStartNode(int node) 298 { 299 if (node == DTMDefaultBase.ROOTNODE) 301 node = getDocument(); 302 if (_isRestartable) 303 { 304 _startNode = node; 305 _currentNode = (node == DTM.NULL) ? DTM.NULL 306 : _firstch(makeNodeIdentity(node)); 307 308 return resetPosition(); 309 } 310 311 return this; 312 } 313 314 320 public int next() 321 { 322 if (_currentNode != NULL) { 323 int node = _currentNode; 324 _currentNode = _nextsib(node); 325 return returnNode(makeNodeHandle(node)); 326 } 327 328 return END; 329 } 330 } 332 337 public final class ParentIterator extends InternalAxisIteratorBase 338 { 339 340 341 private int _nodeType = -1; 342 343 351 public DTMAxisIterator setStartNode(int node) 352 { 353 if (node == DTMDefaultBase.ROOTNODE) 355 node = getDocument(); 356 if (_isRestartable) 357 { 358 _startNode = node; 359 _currentNode = getParent(node); 360 361 return resetPosition(); 362 } 363 364 return this; 365 } 366 367 377 public DTMAxisIterator setNodeType(final int type) 378 { 379 380 _nodeType = type; 381 382 return this; 383 } 384 385 391 public int next() 392 { 393 int result = _currentNode; 394 395 if (_nodeType >= DTM.NTYPES) { 396 if (_nodeType != getExpandedTypeID(_currentNode)) { 397 result = END; 398 } 399 } else if (_nodeType != NULL) { 400 if (_nodeType != getNodeType(_currentNode)) { 401 result = END; 402 } 403 } 404 405 _currentNode = END; 406 407 return returnNode(result); 408 } 409 } 411 417 public final class TypedChildrenIterator extends InternalAxisIteratorBase 418 { 419 420 421 private final int _nodeType; 422 423 429 public TypedChildrenIterator(int nodeType) 430 { 431 _nodeType = nodeType; 432 } 433 434 442 public DTMAxisIterator setStartNode(int node) 443 { 444 if (node == DTMDefaultBase.ROOTNODE) 446 node = getDocument(); 447 if (_isRestartable) 448 { 449 _startNode = node; 450 _currentNode = (node == DTM.NULL) 451 ? DTM.NULL 452 : _firstch(makeNodeIdentity(_startNode)); 453 454 return resetPosition(); 455 } 456 457 return this; 458 } 459 460 465 public int next() 466 { 467 int eType; 468 int node = _currentNode; 469 470 int nodeType = _nodeType; 471 472 if (nodeType >= DTM.NTYPES) { 473 while (node != DTM.NULL && _exptype(node) != nodeType) { 474 node = _nextsib(node); 475 } 476 } else { 477 while (node != DTM.NULL) { 478 eType = _exptype(node); 479 if (eType < DTM.NTYPES) { 480 if (eType == nodeType) { 481 break; 482 } 483 } else if (m_expandedNameTable.getType(eType) == nodeType) { 484 break; 485 } 486 node = _nextsib(node); 487 } 488 } 489 490 if (node == DTM.NULL) { 491 _currentNode = DTM.NULL; 492 return DTM.NULL; 493 } else { 494 _currentNode = _nextsib(node); 495 return returnNode(makeNodeHandle(node)); 496 } 497 498 } 499 } 501 507 public final class NamespaceChildrenIterator 508 extends InternalAxisIteratorBase 509 { 510 511 512 private final int _nsType; 513 514 520 public NamespaceChildrenIterator(final int type) 521 { 522 _nsType = type; 523 } 524 525 533 public DTMAxisIterator setStartNode(int node) 534 { 535 if (node == DTMDefaultBase.ROOTNODE) 537 node = getDocument(); 538 if (_isRestartable) 539 { 540 _startNode = node; 541 _currentNode = (node == DTM.NULL) ? DTM.NULL : NOTPROCESSED; 542 543 return resetPosition(); 544 } 545 546 return this; 547 } 548 549 554 public int next() 555 { 556 if (_currentNode != DTM.NULL) { 557 for (int node = (NOTPROCESSED == _currentNode) 558 ? _firstch(makeNodeIdentity(_startNode)) 559 : _nextsib(_currentNode); 560 node != END; 561 node = _nextsib(node)) { 562 if (m_expandedNameTable.getNamespaceID(_exptype(node)) == _nsType) { 563 _currentNode = node; 564 565 return returnNode(node); 566 } 567 } 568 } 569 570 return END; 571 } 572 } 574 578 public class NamespaceIterator 579 extends InternalAxisIteratorBase 580 { 581 582 585 public NamespaceIterator() 586 { 587 588 super(); 589 } 590 591 599 public DTMAxisIterator setStartNode(int node) 600 { 601 if (node == DTMDefaultBase.ROOTNODE) 603 node = getDocument(); 604 if (_isRestartable) 605 { 606 _startNode = node; 607 _currentNode = getFirstNamespaceNode(node, true); 608 609 return resetPosition(); 610 } 611 612 return this; 613 } 614 615 620 public int next() 621 { 622 623 int node = _currentNode; 624 625 if (DTM.NULL != node) 626 _currentNode = getNextNamespaceNode(_startNode, node, true); 627 628 return returnNode(node); 629 } 630 } 632 636 public class TypedNamespaceIterator extends NamespaceIterator 637 { 638 639 640 private final int _nodeType; 641 642 648 public TypedNamespaceIterator(int nodeType) 649 { 650 super(); 651 _nodeType = nodeType; 652 } 653 654 659 public int next() 660 { 661 int node; 662 663 for (node = _currentNode; 664 node != END; 665 node = getNextNamespaceNode(_startNode, node, true)) { 666 if (getExpandedTypeID(node) == _nodeType 667 || getNodeType(node) == _nodeType 668 || getNamespaceType(node) == _nodeType) { 669 _currentNode = node; 670 671 return returnNode(node); 672 } 673 } 674 675 return (_currentNode =END); 676 } 677 } 679 683 public class RootIterator 684 extends InternalAxisIteratorBase 685 { 686 687 690 public RootIterator() 691 { 692 693 super(); 694 } 695 696 704 public DTMAxisIterator setStartNode(int node) 705 { 706 707 if (_isRestartable) 708 { 709 _startNode = getDocumentRoot(node); 710 _currentNode = NULL; 711 712 return resetPosition(); 713 } 714 715 return this; 716 } 717 718 723 public int next() 724 { 725 if(_startNode == _currentNode) 726 return NULL; 727 728 _currentNode = _startNode; 729 730 return returnNode(_startNode); 731 } 732 } 734 738 public class TypedRootIterator extends RootIterator 739 { 740 741 742 private final int _nodeType; 743 744 749 public TypedRootIterator(int nodeType) 750 { 751 super(); 752 _nodeType = nodeType; 753 } 754 755 760 public int next() 761 { 762 if(_startNode == _currentNode) 763 return NULL; 764 765 int nodeType = _nodeType; 766 int node = _startNode; 767 int expType = getExpandedTypeID(node); 768 769 _currentNode = node; 770 771 if (nodeType >= DTM.NTYPES) { 772 if (nodeType == expType) { 773 return returnNode(node); 774 } 775 } else { 776 if (expType < DTM.NTYPES) { 777 if (expType == nodeType) { 778 return returnNode(node); 779 } 780 } else { 781 if (m_expandedNameTable.getType(expType) == nodeType) { 782 return returnNode(node); 783 } 784 } 785 } 786 787 return END; 788 } 789 } 791 794 public final class NamespaceAttributeIterator 795 extends InternalAxisIteratorBase 796 { 797 798 799 private final int _nsType; 800 801 807 public NamespaceAttributeIterator(int nsType) 808 { 809 810 super(); 811 812 _nsType = nsType; 813 } 814 815 823 public DTMAxisIterator setStartNode(int node) 824 { 825 if (node == DTMDefaultBase.ROOTNODE) 827 node = getDocument(); 828 if (_isRestartable) 829 { 830 _startNode = node; 831 _currentNode = getFirstNamespaceNode(node, false); 832 833 return resetPosition(); 834 } 835 836 return this; 837 } 838 839 844 public int next() 845 { 846 847 int node = _currentNode; 848 849 if (DTM.NULL != node) 850 _currentNode = getNextNamespaceNode(_startNode, node, false); 851 852 return returnNode(node); 853 } 854 } 856 859 public class FollowingSiblingIterator extends InternalAxisIteratorBase 860 { 861 862 870 public DTMAxisIterator setStartNode(int node) 871 { 872 if (node == DTMDefaultBase.ROOTNODE) 874 node = getDocument(); 875 if (_isRestartable) 876 { 877 _startNode = node; 878 _currentNode = makeNodeIdentity(node); 879 880 return resetPosition(); 881 } 882 883 return this; 884 } 885 886 891 public int next() 892 { 893 _currentNode = (_currentNode == DTM.NULL) ? DTM.NULL 894 : _nextsib(_currentNode); 895 return returnNode(makeNodeHandle(_currentNode)); 896 } 897 } 899 902 public final class TypedFollowingSiblingIterator 903 extends FollowingSiblingIterator 904 { 905 906 907 private final int _nodeType; 908 909 915 public TypedFollowingSiblingIterator(int type) 916 { 917 _nodeType = type; 918 } 919 920 925 public int next() 926 { 927 if (_currentNode == DTM.NULL) { 928 return DTM.NULL; 929 } 930 931 int node = _currentNode; 932 int eType; 933 int nodeType = _nodeType; 934 935 if (nodeType >= DTM.NTYPES) { 936 do { 937 node = _nextsib(node); 938 } while (node != DTM.NULL && _exptype(node) != nodeType); 939 } else { 940 while ((node = _nextsib(node)) != DTM.NULL) { 941 eType = _exptype(node); 942 if (eType < DTM.NTYPES) { 943 if (eType == nodeType) { 944 break; 945 } 946 } else if (m_expandedNameTable.getType(eType) == nodeType) { 947 break; 948 } 949 } 950 } 951 952 _currentNode = node; 953 954 return (_currentNode == DTM.NULL) 955 ? DTM.NULL 956 : returnNode(makeNodeHandle(_currentNode)); 957 } 958 } 960 963 public final class AttributeIterator extends InternalAxisIteratorBase 964 { 965 966 968 976 public DTMAxisIterator setStartNode(int node) 977 { 978 if (node == DTMDefaultBase.ROOTNODE) 980 node = getDocument(); 981 if (_isRestartable) 982 { 983 _startNode = node; 984 _currentNode = getFirstAttributeIdentity(makeNodeIdentity(node)); 985 986 return resetPosition(); 987 } 988 989 return this; 990 } 991 992 997 public int next() 998 { 999 1000 final int node = _currentNode; 1001 1002 if (node != NULL) { 1003 _currentNode = getNextAttributeIdentity(node); 1004 return returnNode(makeNodeHandle(node)); 1005 } 1006 1007 return NULL; 1008 } 1009 } 1011 1014 public final class TypedAttributeIterator extends InternalAxisIteratorBase 1015 { 1016 1017 1018 private final int _nodeType; 1019 1020 1026 public TypedAttributeIterator(int nodeType) 1027 { 1028 _nodeType = nodeType; 1029 } 1030 1031 1033 1041 public DTMAxisIterator setStartNode(int node) 1042 { 1043 if (_isRestartable) 1044 { 1045 _startNode = node; 1046 1047 _currentNode = getTypedAttribute(node, _nodeType); 1048 1049 return resetPosition(); 1050 } 1051 1052 return this; 1053 } 1054 1055 1060 public int next() 1061 { 1062 1063 final int node = _currentNode; 1064 1065 _currentNode = NULL; 1068 1069 return returnNode(node); 1070 } 1071 } 1073 1076 public class PrecedingSiblingIterator extends InternalAxisIteratorBase 1077 { 1078 1079 1082 protected int _startNodeID; 1083 1084 1089 public boolean isReverse() 1090 { 1091 return true; 1092 } 1093 1094 1102 public DTMAxisIterator setStartNode(int node) 1103 { 1104 if (node == DTMDefaultBase.ROOTNODE) 1106 node = getDocument(); 1107 if (_isRestartable) 1108 { 1109 _startNode = node; 1110 node = _startNodeID = makeNodeIdentity(node); 1111 1112 if(node == NULL) 1113 { 1114 _currentNode = node; 1115 return resetPosition(); 1116 } 1117 1118 int type = m_expandedNameTable.getType(_exptype(node)); 1119 if(ExpandedNameTable.ATTRIBUTE == type 1120 || ExpandedNameTable.NAMESPACE == type ) 1121 { 1122 _currentNode = node; 1123 } 1124 else 1125 { 1126 _currentNode = _parent(node); 1128 if(NULL!=_currentNode) 1129 _currentNode = _firstch(_currentNode); 1130 else 1131 _currentNode = node; 1132 } 1133 1134 return resetPosition(); 1135 } 1136 1137 return this; 1138 } 1139 1140 1145 public int next() 1146 { 1147 1148 if (_currentNode == _startNodeID || _currentNode == DTM.NULL) 1149 { 1150 return NULL; 1151 } 1152 else 1153 { 1154 final int node = _currentNode; 1155 _currentNode = _nextsib(node); 1156 1157 return returnNode(makeNodeHandle(node)); 1158 } 1159 } 1160 } 1162 1166 public final class TypedPrecedingSiblingIterator 1167 extends PrecedingSiblingIterator 1168 { 1169 1170 1171 private final int _nodeType; 1172 1173 1179 public TypedPrecedingSiblingIterator(int type) 1180 { 1181 _nodeType = type; 1182 } 1183 1184 1189 public int next() 1190 { 1191 int node = _currentNode; 1192 int expType; 1193 1194 int nodeType = _nodeType; 1195 int startID = _startNodeID; 1196 1197 if (nodeType >= DTM.NTYPES) { 1198 while (node != NULL && node != startID && _exptype(node) != nodeType) { 1199 node = _nextsib(node); 1200 } 1201 } else { 1202 while (node != NULL && node != startID) { 1203 expType = _exptype(node); 1204 if (expType < DTM.NTYPES) { 1205 if (expType == nodeType) { 1206 break; 1207 } 1208 } else { 1209 if (m_expandedNameTable.getType(expType) == nodeType) { 1210 break; 1211 } 1212 } 1213 node = _nextsib(node); 1214 } 1215 } 1216 1217 if (node == DTM.NULL || node == _startNodeID) { 1218 _currentNode = NULL; 1219 return NULL; 1220 } else { 1221 _currentNode = _nextsib(node); 1222 return returnNode(makeNodeHandle(node)); 1223 } 1224 } 1225 } 1227 1232 public class PrecedingIterator extends InternalAxisIteratorBase 1233 { 1234 1235 1236 private final int _maxAncestors = 8; 1237 1238 1242 protected int[] _stack = new int[_maxAncestors]; 1243 1244 1245 protected int _sp, _oldsp; 1246 1247 protected int _markedsp, _markedNode, _markedDescendant; 1248 1249 1250 1251 1256 public boolean isReverse() 1257 { 1258 return true; 1259 } 1260 1261 1266 public DTMAxisIterator cloneIterator() 1267 { 1268 _isRestartable = false; 1269 1270 try 1271 { 1272 final PrecedingIterator clone = (PrecedingIterator) super.clone(); 1273 final int[] stackCopy = new int[_stack.length]; 1274 System.arraycopy(_stack, 0, stackCopy, 0, _stack.length); 1275 1276 clone._stack = stackCopy; 1277 1278 return clone; 1280 } 1281 catch (CloneNotSupportedException e) 1282 { 1283 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_ITERATOR_CLONE_NOT_SUPPORTED, null)); } 1285 } 1286 1287 1295 public DTMAxisIterator setStartNode(int node) 1296 { 1297 if (node == DTMDefaultBase.ROOTNODE) 1299 node = getDocument(); 1300 if (_isRestartable) 1301 { 1302 node = makeNodeIdentity(node); 1303 1304 int parent, index; 1306 1307 if (_type(node) == DTM.ATTRIBUTE_NODE) 1308 node = _parent(node); 1309 1310 _startNode = node; 1311 _stack[index = 0] = node; 1312 1313 1314 1315 parent=node; 1316 while ((parent = _parent(parent)) != NULL) 1317 { 1318 if (++index == _stack.length) 1319 { 1320 final int[] stack = new int[index + 4]; 1321 System.arraycopy(_stack, 0, stack, 0, index); 1322 _stack = stack; 1323 } 1324 _stack[index] = parent; 1325 } 1326 if(index>0) 1327 --index; 1329 _currentNode=_stack[index]; 1331 _oldsp = _sp = index; 1332 1333 return resetPosition(); 1334 } 1335 1336 return this; 1337 } 1338 1339 1344 public int next() 1345 { 1346 for(++_currentNode; 1350 _sp>=0; 1351 ++_currentNode) 1352 { 1353 if(_currentNode < _stack[_sp]) 1354 { 1355 if(_type(_currentNode) != ATTRIBUTE_NODE && 1356 _type(_currentNode) != NAMESPACE_NODE) 1357 return returnNode(makeNodeHandle(_currentNode)); 1358 } 1359 else 1360 --_sp; 1361 } 1362 return NULL; 1363 } 1364 1365 1367 1373 public DTMAxisIterator reset() 1374 { 1375 1376 _sp = _oldsp; 1377 1378 return resetPosition(); 1379 } 1380 1381 public void setMark() { 1382 _markedsp = _sp; 1383 _markedNode = _currentNode; 1384 _markedDescendant = _stack[0]; 1385 } 1386 1387 public void gotoMark() { 1388 _sp = _markedsp; 1389 _currentNode = _markedNode; 1390 } 1391 } 1393 1398 public final class TypedPrecedingIterator extends PrecedingIterator 1399 { 1400 1401 1402 private final int _nodeType; 1403 1404 1410 public TypedPrecedingIterator(int type) 1411 { 1412 _nodeType = type; 1413 } 1414 1415 1420 public int next() 1421 { 1422 int node = _currentNode; 1423 int nodeType = _nodeType; 1424 1425 if (nodeType >= DTM.NTYPES) { 1426 while (true) { 1427 node = node + 1; 1428 1429 if (_sp < 0) { 1430 node = NULL; 1431 break; 1432 } else if (node >= _stack[_sp]) { 1433 if (--_sp < 0) { 1434 node = NULL; 1435 break; 1436 } 1437 } else if (_exptype(node) == nodeType) { 1438 break; 1439 } 1440 } 1441 } else { 1442 int expType; 1443 1444 while (true) { 1445 node = node + 1; 1446 1447 if (_sp < 0) { 1448 node = NULL; 1449 break; 1450 } else if (node >= _stack[_sp]) { 1451 if (--_sp < 0) { 1452 node = NULL; 1453 break; 1454 } 1455 } else { 1456 expType = _exptype(node); 1457 if (expType < DTM.NTYPES) { 1458 if (expType == nodeType) { 1459 break; 1460 } 1461 } else { 1462 if (m_expandedNameTable.getType(expType) == nodeType) { 1463 break; 1464 } 1465 } 1466 } 1467 } 1468 } 1469 1470 _currentNode = node; 1471 1472 return (node == NULL) ? NULL : returnNode(makeNodeHandle(node)); 1473 } 1474 } 1476 1479 public class FollowingIterator extends InternalAxisIteratorBase 1480 { 1481 DTMAxisTraverser m_traverser; 1483 public FollowingIterator() 1484 { 1485 m_traverser = getAxisTraverser(Axis.FOLLOWING); 1486 } 1487 1488 1496 public DTMAxisIterator setStartNode(int node) 1497 { 1498 if (node == DTMDefaultBase.ROOTNODE) 1500 node = getDocument(); 1501 if (_isRestartable) 1502 { 1503 _startNode = node; 1504 1505 _currentNode = m_traverser.first(node); 1511 1512 return resetPosition(); 1514 } 1515 1516 return this; 1517 } 1518 1519 1524 public int next() 1525 { 1526 1527 int node = _currentNode; 1528 1529 _currentNode = m_traverser.next(_startNode, _currentNode); 1530 1531 return returnNode(node); 1532 } 1533 } 1535 1538 public final class TypedFollowingIterator extends FollowingIterator 1539 { 1540 1541 1542 private final int _nodeType; 1543 1544 1550 public TypedFollowingIterator(int type) 1551 { 1552 _nodeType = type; 1553 } 1554 1555 1560 public int next() 1561 { 1562 1563 int node; 1564 1565 do{ 1566 node = _currentNode; 1567 1568 _currentNode = m_traverser.next(_startNode, _currentNode); 1569 1570 } 1571 while (node != DTM.NULL 1572 && (getExpandedTypeID(node) != _nodeType && getNodeType(node) != _nodeType)); 1573 1574 return (node == DTM.NULL ? DTM.NULL :returnNode(node)); 1575 } 1576 } 1578 1582 public class AncestorIterator extends InternalAxisIteratorBase 1583 { 1584 org.apache.xml.utils.NodeVector m_ancestors = 1585 new org.apache.xml.utils.NodeVector(); 1586 1587 int m_ancestorsPos; 1588 1589 int m_markedPos; 1590 1591 1592 int m_realStartNode; 1593 1594 1600 public int getStartNode() 1601 { 1602 return m_realStartNode; 1603 } 1604 1605 1610 public final boolean isReverse() 1611 { 1612 return true; 1613 } 1614 1615 1620 public DTMAxisIterator cloneIterator() 1621 { 1622 _isRestartable = false; 1624 try 1625 { 1626 final AncestorIterator clone = (AncestorIterator) super.clone(); 1627 1628 clone._startNode = _startNode; 1629 1630 return clone; 1632 } 1633 catch (CloneNotSupportedException e) 1634 { 1635 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_ITERATOR_CLONE_NOT_SUPPORTED, null)); } 1637 } 1638 1639 1647 public DTMAxisIterator setStartNode(int node) 1648 { 1649 if (node == DTMDefaultBase.ROOTNODE) 1651 node = getDocument(); 1652 m_realStartNode = node; 1653 1654 if (_isRestartable) 1655 { 1656 int nodeID = makeNodeIdentity(node); 1657 1658 if (!_includeSelf && node != DTM.NULL) { 1659 nodeID = _parent(nodeID); 1660 node = makeNodeHandle(nodeID); 1661 } 1662 1663 _startNode = node; 1664 1665 while (nodeID != END) { 1666 m_ancestors.addElement(node); 1667 nodeID = _parent(nodeID); 1668 node = makeNodeHandle(nodeID); 1669 } 1670 m_ancestorsPos = m_ancestors.size()-1; 1671 1672 _currentNode = (m_ancestorsPos>=0) 1673 ? m_ancestors.elementAt(m_ancestorsPos) 1674 : DTM.NULL; 1675 1676 return resetPosition(); 1677 } 1678 1679 return this; 1680 } 1681 1682 1688 public DTMAxisIterator reset() 1689 { 1690 1691 m_ancestorsPos = m_ancestors.size()-1; 1692 1693 _currentNode = (m_ancestorsPos>=0) ? m_ancestors.elementAt(m_ancestorsPos) 1694 : DTM.NULL; 1695 1696 return resetPosition(); 1697 } 1698 1699 1704 public int next() 1705 { 1706 1707 int next = _currentNode; 1708 1709 int pos = --m_ancestorsPos; 1710 1711 _currentNode = (pos >= 0) ? m_ancestors.elementAt(m_ancestorsPos) 1712 : DTM.NULL; 1713 1714 return returnNode(next); 1715 } 1716 1717 public void setMark() { 1718 m_markedPos = m_ancestorsPos; 1719 } 1720 1721 public void gotoMark() { 1722 m_ancestorsPos = m_markedPos; 1723 _currentNode = m_ancestorsPos>=0 ? m_ancestors.elementAt(m_ancestorsPos) 1724 : DTM.NULL; 1725 } 1726 } 1728 1731 public final class TypedAncestorIterator extends AncestorIterator 1732 { 1733 1734 1735 private final int _nodeType; 1736 1737 1743 public TypedAncestorIterator(int type) 1744 { 1745 _nodeType = type; 1746 } 1747 1748 1756 public DTMAxisIterator setStartNode(int node) 1757 { 1758 if (node == DTMDefaultBase.ROOTNODE) 1760 node = getDocument(); 1761 m_realStartNode = node; 1762 1763 if (_isRestartable) 1764 { 1765 int nodeID = makeNodeIdentity(node); 1766 int nodeType = _nodeType; 1767 1768 if (!_includeSelf && node != DTM.NULL) { 1769 nodeID = _parent(nodeID); 1770 } 1771 1772 _startNode = node; 1773 1774 if (nodeType >= DTM.NTYPES) { 1775 while (nodeID != END) { 1776 int eType = _exptype(nodeID); 1777 1778 if (eType == nodeType) { 1779 m_ancestors.addElement(makeNodeHandle(nodeID)); 1780 } 1781 nodeID = _parent(nodeID); 1782 } 1783 } else { 1784 while (nodeID != END) { 1785 int eType = _exptype(nodeID); 1786 1787 if ((eType >= DTM.NTYPES 1788 && m_expandedNameTable.getType(eType) == nodeType) 1789 || (eType < DTM.NTYPES && eType == nodeType)) { 1790 m_ancestors.addElement(makeNodeHandle(nodeID)); 1791 } 1792 nodeID = _parent(nodeID); 1793 } 1794 } 1795 m_ancestorsPos = m_ancestors.size()-1; 1796 1797 _currentNode = (m_ancestorsPos>=0) 1798 ? m_ancestors.elementAt(m_ancestorsPos) 1799 : DTM.NULL; 1800 1801 return resetPosition(); 1802 } 1803 1804 return this; 1805 } 1806 } 1808 1811 public class DescendantIterator extends InternalAxisIteratorBase 1812 { 1813 1814 1822 public DTMAxisIterator setStartNode(int node) 1823 { 1824 if (node == DTMDefaultBase.ROOTNODE) 1826 node = getDocument(); 1827 if (_isRestartable) 1828 { 1829 node = makeNodeIdentity(node); 1830 _startNode = node; 1831 1832 if (_includeSelf) 1833 node--; 1834 1835 _currentNode = node; 1836 1837 return resetPosition(); 1838 } 1839 1840 return this; 1841 } 1842 1843 1857 protected boolean isDescendant(int identity) 1858 { 1859 return (_parent(identity) >= _startNode) || (_startNode == identity); 1860 } 1861 1862 1867 public int next() 1868 { 1869 if (_startNode == NULL) { 1870 return NULL; 1871 } 1872 1873 if (_includeSelf && (_currentNode + 1) == _startNode) 1874 return returnNode(makeNodeHandle(++_currentNode)); 1876 int node = _currentNode; 1877 int type; 1878 1879 do { 1880 node++; 1881 type = _type(node); 1882 1883 if (NULL == type ||!isDescendant(node)) { 1884 _currentNode = NULL; 1885 return END; 1886 } 1887 } while(ATTRIBUTE_NODE == type || TEXT_NODE == type 1888 || NAMESPACE_NODE == type); 1889 1890 _currentNode = node; 1891 return returnNode(makeNodeHandle(node)); } 1893 1894 1898 public DTMAxisIterator reset() 1899 { 1900 1901 final boolean temp = _isRestartable; 1902 1903 _isRestartable = true; 1904 1905 setStartNode(makeNodeHandle(_startNode)); 1906 1907 _isRestartable = temp; 1908 1909 return this; 1910 } 1911 1912 } 1914 1917 public final class TypedDescendantIterator extends DescendantIterator 1918 { 1919 1920 1921 private final int _nodeType; 1922 1923 1929 public TypedDescendantIterator(int nodeType) 1930 { 1931 _nodeType = nodeType; 1932 } 1933 1934 1939 public int next() 1940 { 1941 int node; 1942 int type; 1943 1944 if (_startNode == NULL) { 1945 return NULL; 1946 } 1947 1948 node = _currentNode; 1949 1950 do 1951 { 1952 node++; 1953 type = _type(node); 1954 1955 if (NULL == type ||!isDescendant(node)) { 1956 _currentNode = NULL; 1957 return END; 1958 } 1959 } 1960 while (type != _nodeType && _exptype(node) != _nodeType); 1961 1962 _currentNode = node; 1963 return returnNode(makeNodeHandle(node)); 1964 } 1965 } 1967 1971 public class NthDescendantIterator extends DescendantIterator 1972 { 1973 1974 1975 int _pos; 1976 1977 1983 public NthDescendantIterator(int pos) 1984 { 1985 _pos = pos; 1986 } 1987 1988 1993 public int next() 1994 { 1995 1996 int node; 1998 1999 while ((node = super.next()) != END) 2000 { 2001 node = makeNodeIdentity(node); 2002 2003 int parent = _parent(node); 2004 int child = _firstch(parent); 2005 int pos = 0; 2006 2007 do 2008 { 2009 int type = _type(child); 2010 2011 if (ELEMENT_NODE == type) 2012 pos++; 2013 } 2014 while ((pos < _pos) && (child = _nextsib(child)) != END); 2015 2016 if (node == child) 2017 return node; 2018 } 2019 2020 return (END); 2021 } 2022 } 2024 2027 public class SingletonIterator extends InternalAxisIteratorBase 2028 { 2029 2030 2031 private boolean _isConstant; 2032 2033 2037 public SingletonIterator() 2038 { 2039 this(Integer.MIN_VALUE, false); 2040 } 2041 2042 2048 public SingletonIterator(int node) 2049 { 2050 this(node, false); 2051 } 2052 2053 2060 public SingletonIterator(int node, boolean constant) 2061 { 2062 _currentNode = _startNode = node; 2063 _isConstant = constant; 2064 } 2065 2066 2074 public DTMAxisIterator setStartNode(int node) 2075 { 2076 if (node == DTMDefaultBase.ROOTNODE) 2078 node = getDocument(); 2079 if (_isConstant) 2080 { 2081 _currentNode = _startNode; 2082 2083 return resetPosition(); 2084 } 2085 else if (_isRestartable) 2086 { 2087 if (_currentNode == Integer.MIN_VALUE) 2088 { 2089 _currentNode = _startNode = node; 2090 } 2091 2092 return resetPosition(); 2093 } 2094 2095 return this; 2096 } 2097 2098 2104 public DTMAxisIterator reset() 2105 { 2106 2107 if (_isConstant) 2108 { 2109 _currentNode = _startNode; 2110 2111 return resetPosition(); 2112 } 2113 else 2114 { 2115 final boolean temp = _isRestartable; 2116 2117 _isRestartable = true; 2118 2119 setStartNode(_startNode); 2120 2121 _isRestartable = temp; 2122 } 2123 2124 return this; 2125 } 2126 2127 2132 public int next() 2133 { 2134 2135 final int result = _currentNode; 2136 2137 _currentNode = END; 2138 2139 return returnNode(result); 2140 } 2141 } 2143 2146 public final class TypedSingletonIterator extends SingletonIterator 2147 { 2148 2149 2150 private final int _nodeType; 2151 2152 2158 public TypedSingletonIterator(int nodeType) 2159 { 2160 _nodeType = nodeType; 2161 } 2162 2163 2168 public int next() 2169 { 2170 2171 final int result = _currentNode; 2173 int nodeType = _nodeType; 2174 2175 _currentNode = END; 2176 2177 if (nodeType >= DTM.NTYPES) { 2178 if (getExpandedTypeID(result) == nodeType) { 2179 return returnNode(result); 2180 } 2181 } else { 2182 if (getNodeType(result) == nodeType) { 2183 return returnNode(result); 2184 } 2185 } 2186 2187 return NULL; 2188 } 2189 } } 2191 | Popular Tags |