1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xalan.internal.xsltc.dom;
  22 
  23 import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  24 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  25 import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIteratorBase;
  26 
  27 /**
  28  * This iterator is a wrapper that always returns the position of
  29  * a node in document order. It is needed for the case where
  30  * a call to position() occurs in the context of an XSLT element
  31  * such as xsl:for-each, xsl:apply-templates, etc.
  32  *
  33  * The getPosition() methods in DTMAxisIterators defined
  34  * in DTMDefaultBaseIterators always return the position
  35  * in document order, which is backwards for XPath in the
  36  * case of the ancestor, ancestor-or-self, previous and
  37  * previous-sibling.
  38  *
  39  * XSLTC implements position() with the
  40  * BasisLibrary.positionF() method, and uses the
  41  * DTMAxisIterator.isReverse() method to determine
  42  * whether the result of getPosition() should be
  43  * interpreted as being equal to position().
  44  * But when the expression appears in apply-templates of
  45  * for-each, the position() function operates in document
  46  * order.
  47  *
  48  * The only effect of the ForwardPositionIterator is to force
  49  * the result of isReverse() to false, so that
  50  * BasisLibrary.positionF() calculates position() in a way
  51  * that's consistent with the context in which the
  52  * iterator is being used."
  53  *
  54  * (Apparently the correction of isReverse() occurs
  55  * implicitly, by inheritance. This class also appears
  56  * to maintain its own position counter, which seems
  57  * redundant.)
  58  *
  59  * @deprecated This class exists only for backwards compatibility with old
  60  *             translets.  New code should not reference it.
  61  */
  62 @Deprecated
  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 }