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: CurrentNodeListIterator.java,v 1.13 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.AbstractTranslet;
  21. import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  22. import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
  23. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  24. import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  25. /**
  26. * Iterators of this kind use a CurrentNodeListFilter to filter a subset of
  27. * nodes from a source iterator. For each node from the source, the boolean
  28. * method CurrentNodeListFilter.test() is called.
  29. *
  30. * All nodes from the source are read into an array upon calling setStartNode()
  31. * (this is needed to determine the value of last, a parameter to
  32. * CurrentNodeListFilter.test()). The method getLast() returns the last element
  33. * after applying the filter.
  34. * @author Jacek Ambroziak
  35. * @author Santiago Pericas-Geertsen
  36. * @author Morten Jorgensen
  37. */
  38. public final class CurrentNodeListIterator extends DTMAxisIteratorBase {
  39. /**
  40. * A flag indicating if nodes are returned in document order.
  41. */
  42. private boolean _docOrder;
  43. /**
  44. * The source for this iterator.
  45. */
  46. private DTMAxisIterator _source;
  47. /**
  48. * A reference to a filter object.
  49. */
  50. private final CurrentNodeListFilter _filter;
  51. /**
  52. * An integer array to store nodes from source iterator.
  53. */
  54. private IntegerArray _nodes = new IntegerArray();
  55. /**
  56. * Index in _nodes of the next node to filter.
  57. */
  58. private int _currentIndex;
  59. /**
  60. * The current node in the stylesheet at the time of evaluation.
  61. */
  62. private final int _currentNode;
  63. /**
  64. * A reference to the translet.
  65. */
  66. private AbstractTranslet _translet;
  67. public CurrentNodeListIterator(DTMAxisIterator source,
  68. CurrentNodeListFilter filter,
  69. int currentNode,
  70. AbstractTranslet translet)
  71. {
  72. this(source, !source.isReverse(), filter, currentNode, translet);
  73. }
  74. public CurrentNodeListIterator(DTMAxisIterator source, boolean docOrder,
  75. CurrentNodeListFilter filter,
  76. int currentNode,
  77. AbstractTranslet translet)
  78. {
  79. _source = source;
  80. _filter = filter;
  81. _translet = translet;
  82. _docOrder = docOrder;
  83. _currentNode = currentNode;
  84. }
  85. public DTMAxisIterator forceNaturalOrder() {
  86. _docOrder = true;
  87. return this;
  88. }
  89. public void setRestartable(boolean isRestartable) {
  90. _isRestartable = isRestartable;
  91. _source.setRestartable(isRestartable);
  92. }
  93. public boolean isReverse() {
  94. return !_docOrder;
  95. }
  96. public DTMAxisIterator cloneIterator() {
  97. try {
  98. final CurrentNodeListIterator clone =
  99. (CurrentNodeListIterator) super.clone();
  100. clone._nodes = (IntegerArray) _nodes.clone();
  101. clone._source = _source.cloneIterator();
  102. clone._isRestartable = false;
  103. return clone.reset();
  104. }
  105. catch (CloneNotSupportedException e) {
  106. BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  107. e.toString());
  108. return null;
  109. }
  110. }
  111. public DTMAxisIterator reset() {
  112. _currentIndex = 0;
  113. return resetPosition();
  114. }
  115. public int next() {
  116. final int last = _nodes.cardinality();
  117. final int currentNode = _currentNode;
  118. final AbstractTranslet translet = _translet;
  119. for (int index = _currentIndex; index < last; ) {
  120. final int position = _docOrder ? index + 1 : last - index;
  121. final int node = _nodes.at(index++); // note increment
  122. if (_filter.test(node, position, last, currentNode, translet,
  123. this)) {
  124. _currentIndex = index;
  125. return returnNode(node);
  126. }
  127. }
  128. return END;
  129. }
  130. public DTMAxisIterator setStartNode(int node) {
  131. if (_isRestartable) {
  132. _source.setStartNode(_startNode = node);
  133. _nodes.clear();
  134. while ((node = _source.next()) != END) {
  135. _nodes.add(node);
  136. }
  137. _currentIndex = 0;
  138. resetPosition();
  139. }
  140. return this;
  141. }
  142. public int getLast() {
  143. if (_last == -1) {
  144. _last = computePositionOfLast();
  145. }
  146. return _last;
  147. }
  148. public void setMark() {
  149. _markedNode = _currentIndex;
  150. }
  151. public void gotoMark() {
  152. _currentIndex = _markedNode;
  153. }
  154. private int computePositionOfLast() {
  155. final int last = _nodes.cardinality();
  156. final int currNode = _currentNode;
  157. final AbstractTranslet translet = _translet;
  158. int lastPosition = _position;
  159. for (int index = _currentIndex; index < last; ) {
  160. final int position = _docOrder ? index + 1 : last - index;
  161. int nodeIndex = _nodes.at(index++); // note increment
  162. if (_filter.test(nodeIndex, position, last, currNode, translet,
  163. this)) {
  164. lastPosition++;
  165. }
  166. }
  167. return lastPosition;
  168. }
  169. }