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: ForwardPositionIterator.java,v 1.9 2004/02/27 01:59:31 zongaro 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. * This iterator is a wrapper that always returns the position of
  25. * a node in document order. It is needed for the case where
  26. * a call to position() occurs in the context of an XSLT element
  27. * such as xsl:for-each, xsl:apply-templates, etc.
  28. *
  29. * The getPosition() methods in DTMAxisIterators defined
  30. * in DTMDefaultBaseIterators always return the position
  31. * in document order, which is backwards for XPath in the
  32. * case of the ancestor, ancestor-or-self, previous and
  33. * previous-sibling.
  34. *
  35. * XSLTC implements position() with the
  36. * BasisLibrary.positionF() method, and uses the
  37. * DTMAxisIterator.isReverse() method to determine
  38. * whether the result of getPosition() should be
  39. * interpreted as being equal to position().
  40. * But when the expression appears in apply-templates of
  41. * for-each, the position() function operates in document
  42. * order.
  43. *
  44. * The only effect of the ForwardPositionIterator is to force
  45. * the result of isReverse() to false, so that
  46. * BasisLibrary.positionF() calculates position() in a way
  47. * that's consistent with the context in which the
  48. * iterator is being used."
  49. *
  50. * (Apparently the correction of isReverse() occurs
  51. * implicitly, by inheritance. This class also appears
  52. * to maintain its own position counter, which seems
  53. * redundant.)
  54. *
  55. * @deprecated This class exists only for backwards compatibility with old
  56. * translets. New code should not reference it.
  57. */
  58. public final class ForwardPositionIterator extends DTMAxisIteratorBase {
  59. private DTMAxisIterator _source;
  60. public ForwardPositionIterator(DTMAxisIterator source) {
  61. _source = source;
  62. }
  63. public DTMAxisIterator cloneIterator() {
  64. try {
  65. final ForwardPositionIterator clone =
  66. (ForwardPositionIterator) super.clone();
  67. clone._source = _source.cloneIterator();
  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 int next() {
  78. return returnNode(_source.next());
  79. }
  80. public DTMAxisIterator setStartNode(int node) {
  81. _source.setStartNode(node);
  82. return this;
  83. }
  84. public DTMAxisIterator reset() {
  85. _source.reset();
  86. return resetPosition();
  87. }
  88. public void setMark() {
  89. _source.setMark();
  90. }
  91. public void gotoMark() {
  92. _source.gotoMark();
  93. }
  94. }