001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: AbsoluteIterator.java 468651 2006-10-28 07:04:25Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.dom;
023    
024    import org.apache.xalan.xsltc.runtime.BasisLibrary;
025    import org.apache.xml.dtm.DTMAxisIterator;
026    import org.apache.xml.dtm.ref.DTMAxisIteratorBase;
027    import org.apache.xml.dtm.ref.DTMDefaultBase;
028    
029    /**
030     * Absolute iterators ignore the node that is passed to setStartNode(). 
031     * Instead, they always start from the root node. The node passed to 
032     * setStartNode() is not totally useless, though. It is needed to obtain the 
033     * DOM mask, i.e. the index into the MultiDOM table that corresponds to the 
034     * DOM "owning" the node. 
035     * 
036     * The DOM mask is cached, so successive calls to setStartNode() passing 
037     * nodes from other DOMs will have no effect (i.e. this iterator cannot 
038     * migrate between DOMs).
039     * @author Jacek Ambroziak
040     * @author Santiago Pericas-Geertsen
041     */
042    public final class AbsoluteIterator extends DTMAxisIteratorBase {
043    
044        /**
045         * Source for this iterator.
046         */
047        private DTMAxisIterator _source;
048    
049        public AbsoluteIterator(DTMAxisIterator source) {
050            _source = source;
051    // System.out.println("AI source = " + source + " this = " + this);
052        }
053    
054        public void setRestartable(boolean isRestartable) {
055            _isRestartable = isRestartable;
056            _source.setRestartable(isRestartable);
057        }
058    
059        public DTMAxisIterator setStartNode(int node) {
060            _startNode = DTMDefaultBase.ROOTNODE;
061            if (_isRestartable) {
062                _source.setStartNode(_startNode);
063                resetPosition();
064            }
065            return this;
066        }
067    
068        public int next() {
069            return returnNode(_source.next());
070        }
071    
072        public DTMAxisIterator cloneIterator() {
073            try {
074                final AbsoluteIterator clone = (AbsoluteIterator) super.clone();
075                clone._source = _source.cloneIterator();    // resets source
076                clone.resetPosition();
077                clone._isRestartable = false;
078                return clone;
079            }
080            catch (CloneNotSupportedException e) {
081                BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,
082                                          e.toString());
083                return null;
084            }
085        }
086    
087        public DTMAxisIterator reset() {
088            _source.reset();
089            return resetPosition();
090        }
091        
092        public void setMark() {
093            _source.setMark();
094        }
095    
096        public void gotoMark() {
097            _source.gotoMark();
098        }
099    }