1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: StepIterator.java,v 1.15 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  21. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  22. import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  23. /**
  24. * A step iterator is used to evaluate expressions like "BOOK/TITLE".
  25. * A better name for this iterator would have been ParentIterator since
  26. * both "BOOK" and "TITLE" are steps in XPath lingo. Step iterators are
  27. * constructed from two other iterators which we are going to refer to
  28. * as "outer" and "inner". Every node from the outer iterator (the one
  29. * for BOOK in our example) is used to initialize the inner iterator.
  30. * After this initialization, every node from the inner iterator is
  31. * returned (in essence, implementing a "nested loop").
  32. * @author Jacek Ambroziak
  33. * @author Santiago Pericas-Geertsen
  34. * @author Erwin Bolwidt <ejb@klomp.org>
  35. * @author Morten Jorgensen
  36. */
  37. public class StepIterator extends DTMAxisIteratorBase {
  38. /**
  39. * A reference to the "outer" iterator.
  40. */
  41. protected DTMAxisIterator _source;
  42. /**
  43. * A reference to the "inner" iterator.
  44. */
  45. protected DTMAxisIterator _iterator;
  46. /**
  47. * Temp variable to store a marked position.
  48. */
  49. private int _pos = -1;
  50. public StepIterator(DTMAxisIterator source, DTMAxisIterator iterator) {
  51. _source = source;
  52. _iterator = iterator;
  53. // System.out.println("SI source = " + source + " this = " + this);
  54. // System.out.println("SI iterator = " + iterator + " this = " + this);
  55. }
  56. public void setRestartable(boolean isRestartable) {
  57. _isRestartable = isRestartable;
  58. _source.setRestartable(isRestartable);
  59. _iterator.setRestartable(true); // must be restartable
  60. }
  61. public DTMAxisIterator cloneIterator() {
  62. _isRestartable = false;
  63. try {
  64. final StepIterator clone = (StepIterator) super.clone();
  65. clone._source = _source.cloneIterator();
  66. clone._iterator = _iterator.cloneIterator();
  67. clone._iterator.setRestartable(true); // must be restartable
  68. clone._isRestartable = false;
  69. return clone.reset();
  70. }
  71. catch (CloneNotSupportedException e) {
  72. BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  73. e.toString());
  74. return null;
  75. }
  76. }
  77. public DTMAxisIterator setStartNode(int node) {
  78. if (_isRestartable) {
  79. // Set start node for left-hand iterator...
  80. _source.setStartNode(_startNode = node);
  81. // ... and get start node for right-hand iterator from left-hand,
  82. // with special case for //* path - see ParentLocationPath
  83. _iterator.setStartNode(_includeSelf ? _startNode : _source.next());
  84. return resetPosition();
  85. }
  86. return this;
  87. }
  88. public DTMAxisIterator reset() {
  89. _source.reset();
  90. // Special case for //* path - see ParentLocationPath
  91. _iterator.setStartNode(_includeSelf ? _startNode : _source.next());
  92. return resetPosition();
  93. }
  94. public int next() {
  95. for (int node;;) {
  96. // Try to get another node from the right-hand iterator
  97. if ((node = _iterator.next()) != END) {
  98. return returnNode(node);
  99. }
  100. // If not, get the next starting point from left-hand iterator...
  101. else if ((node = _source.next()) == END) {
  102. return END;
  103. }
  104. // ...and pass it on to the right-hand iterator
  105. else {
  106. _iterator.setStartNode(node);
  107. }
  108. }
  109. }
  110. public void setMark() {
  111. _source.setMark();
  112. _iterator.setMark();
  113. //_pos = _position;
  114. }
  115. public void gotoMark() {
  116. _source.gotoMark();
  117. _iterator.gotoMark();
  118. //_position = _pos;
  119. }
  120. }