1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xalan.internal.xsltc.dom;
  23 
  24 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  25 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  26 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  27 
  28 /**
  29  * This iterator is a wrapper that always returns the position of
  30  * a node in document order. It is needed for the case where
  31  * a call to position() occurs in the context of an XSLT element
  32  * such as xsl:for-each, xsl:apply-templates, etc.
  33  *
  34  * The getPosition() methods in DTMAxisIterators defined
  35  * in DTMDefaultBaseIterators always return the position
  36  * in document order, which is backwards for XPath in the
  37  * case of the ancestor, ancestor-or-self, previous and
  38  * previous-sibling.
  39  *
  40  * XSLTC implements position() with the
  41  * BasisLibrary.positionF() method, and uses the
  42  * DTMAxisIterator.isReverse() method to determine
  43  * whether the result of getPosition() should be
  44  * interpreted as being equal to position().
  45  * But when the expression appears in apply-templates of
  46  * for-each, the position() function operates in document
  47  * order.
  48  *
  49  * The only effect of the ForwardPositionIterator is to force
  50  * the result of isReverse() to false, so that
  51  * BasisLibrary.positionF() calculates position() in a way
  52  * that's consistent with the context in which the
  53  * iterator is being used."
  54  *
  55  * (Apparently the correction of isReverse() occurs
  56  * implicitly, by inheritance. This class also appears
  57  * to maintain its own position counter, which seems
  58  * redundant.)
  59  *
  60  * @deprecated This class exists only for backwards compatibility with old
  61  *             translets.  New code should not reference it.
  62  */
  63 public final class ForwardPositionIterator extends DTMAxisIteratorBase {
  64 
  65     private DTMAxisIterator _source;
  66 
  67     public ForwardPositionIterator(DTMAxisIterator source) {
  68         _source = source;
  69     }
  70 
  71     public DTMAxisIterator cloneIterator() {
  72         try {
  73             final ForwardPositionIterator clone =
  74                 (ForwardPositionIterator) super.clone();
  75             clone._source = _source.cloneIterator();
  76             clone._isRestartable = false;
  77             return clone.reset();
  78         }
  79         catch (CloneNotSupportedException e) {
  80             BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
  81                                       e.toString());
  82             return null;
  83         }
  84     }
  85 
  86     public int next() {
  87         return returnNode(_source.next());
  88     }
  89 
  90     public DTMAxisIterator setStartNode(int node) {
  91         _source.setStartNode(node);
  92         return this;
  93     }
  94 
  95     public DTMAxisIterator reset() {
  96         _source.reset();
  97         return resetPosition();
  98     }
  99 
 100     public void setMark() {
 101         _source.setMark();
 102     }
 103 
 104     public void gotoMark() {
 105         _source.gotoMark();
 106     }
 107 }