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: NthIterator.java,v 1.14 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. * @author Jacek Ambroziak
  25. * @author Morten Jorgensen
  26. */
  27. public final class NthIterator extends DTMAxisIteratorBase {
  28. // ...[N]
  29. private DTMAxisIterator _source;
  30. private final int _position;
  31. private boolean _ready;
  32. public NthIterator(DTMAxisIterator source, int n) {
  33. _source = source;
  34. _position = n;
  35. }
  36. public void setRestartable(boolean isRestartable) {
  37. _isRestartable = isRestartable;
  38. _source.setRestartable(isRestartable);
  39. }
  40. public DTMAxisIterator cloneIterator() {
  41. try {
  42. final NthIterator clone = (NthIterator) super.clone();
  43. clone._source = _source.cloneIterator(); // resets source
  44. clone._isRestartable = false;
  45. return clone;
  46. }
  47. catch (CloneNotSupportedException e) {
  48. BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  49. e.toString());
  50. return null;
  51. }
  52. }
  53. public int next() {
  54. if (_ready) {
  55. _ready = false;
  56. return _source.getNodeByPosition(_position);
  57. }
  58. return DTMAxisIterator.END;
  59. /*
  60. if (_ready && _position > 0) {
  61. final int pos = _source.isReverse()
  62. ? _source.getLast() - _position + 1
  63. : _position;
  64. _ready = false;
  65. int node;
  66. while ((node = _source.next()) != DTMAxisIterator.END) {
  67. if (pos == _source.getPosition()) {
  68. return node;
  69. }
  70. }
  71. }
  72. return DTMAxisIterator.END;
  73. */
  74. }
  75. public DTMAxisIterator setStartNode(final int node) {
  76. if (_isRestartable) {
  77. _source.setStartNode(node);
  78. _ready = true;
  79. }
  80. return this;
  81. }
  82. public DTMAxisIterator reset() {
  83. _source.reset();
  84. _ready = true;
  85. return this;
  86. }
  87. public int getLast() {
  88. return 1;
  89. }
  90. public int getPosition() {
  91. return 1;
  92. }
  93. public void setMark() {
  94. _source.setMark();
  95. }
  96. public void gotoMark() {
  97. _source.gotoMark();
  98. }
  99. }