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: CachedNodeListIterator.java,v 1.2 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.xml.internal.dtm.DTMAxisIterator;
  21. import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  22. import com.sun.org.apache.xalan.internal.xsltc.util.IntegerArray;
  23. /**
  24. * CachedNodeListIterator is used for select expressions in a
  25. * variable or parameter. This iterator caches all nodes in an
  26. * IntegerArray. Its cloneIterator() method is overridden to
  27. * return an object of ClonedNodeListIterator.
  28. */
  29. public final class CachedNodeListIterator extends DTMAxisIteratorBase {
  30. /**
  31. * Source for this iterator.
  32. */
  33. private DTMAxisIterator _source;
  34. private IntegerArray _nodes = new IntegerArray();
  35. private int _numCachedNodes = 0;
  36. private int _index = 0;
  37. private boolean _isEnded = false;
  38. public CachedNodeListIterator(DTMAxisIterator source) {
  39. _source = source;
  40. }
  41. public void setRestartable(boolean isRestartable) {
  42. //_isRestartable = isRestartable;
  43. //_source.setRestartable(isRestartable);
  44. }
  45. public DTMAxisIterator setStartNode(int node) {
  46. if (_isRestartable) {
  47. _startNode = node;
  48. _source.setStartNode(node);
  49. resetPosition();
  50. _isRestartable = false;
  51. }
  52. return this;
  53. }
  54. public int next() {
  55. return getNode(_index++);
  56. }
  57. public int getPosition() {
  58. return _index == 0 ? 1 : _index;
  59. }
  60. public int getNodeByPosition(int pos) {
  61. return getNode(pos);
  62. }
  63. public int getNode(int index) {
  64. if (index < _numCachedNodes) {
  65. return _nodes.at(index);
  66. }
  67. else if (!_isEnded){
  68. int node = _source.next();
  69. if (node != END) {
  70. _nodes.add(node);
  71. _numCachedNodes++;
  72. }
  73. else {
  74. _isEnded = true;
  75. }
  76. return node;
  77. }
  78. else
  79. return END;
  80. }
  81. public DTMAxisIterator cloneIterator() {
  82. ClonedNodeListIterator clone = new ClonedNodeListIterator(this);
  83. return clone;
  84. }
  85. public DTMAxisIterator reset() {
  86. _index = 0;
  87. return this;
  88. }
  89. public void setMark() {
  90. _source.setMark();
  91. }
  92. public void gotoMark() {
  93. _source.gotoMark();
  94. }
  95. }