19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: DTMNodeList.java 468653 2006-10-28 07:07:05Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xml.dtm.ref;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMIterator;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.w3c.dom.Node;
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>DTMNodeList</code> gives us an implementation of the DOM's
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeList interface wrapped around a DTM Iterator. The author
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * considers this something of an abominations, since NodeList was not
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * intended to be a general purpose "list of nodes" API and is
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * generally considered by the DOM WG to have be a mistake... but I'm
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * told that some of the XPath/XSLT folks say they must have this
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * solution.
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Please note that this is not necessarily equivlaent to a DOM
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeList operating over the same document. In particular:
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <ul>
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>If there are several Text nodes in logical succession (ie,
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * across CDATASection and EntityReference boundaries), we will return
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * only the first; the caller is responsible for stepping through
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * them.
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (%REVIEW% Provide a convenience routine here to assist, pending
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * proposed DOM Level 3 getAdjacentText() operation?) </li>
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>Since the whole XPath/XSLT architecture assumes that the source
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * document is not altered while we're working with it, we do not
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * promise to implement the DOM NodeList's "live view" response to
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * document mutation. </li>
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </ul>
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p>State: In progress!!</p>
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class DTMNodeList extends DTMNodeListBase {
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    private DTMIterator m_iter;
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //================================================================
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Methods unique to this class
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    private DTMNodeList() {
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Public constructor: Wrap a DTMNodeList around an existing
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * and preconfigured DTMIterator
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * WARNING: THIS HAS THE SIDE EFFECT OF ISSUING setShouldCacheNodes(true)
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * AGAINST THE DTMIterator.
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMNodeList(DTMIterator dtmIterator) {
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (dtmIterator != null) {
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            int pos = dtmIterator.getCurrentPos();
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            try {
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                m_iter=(DTMIterator)dtmIterator.cloneWithReset();
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            } catch(CloneNotSupportedException cnse) {
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                m_iter = dtmIterator;
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            m_iter.setShouldCacheNodes(true);
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            m_iter.runTo(-1);
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            m_iter.setCurrentPos(pos);
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Access the wrapped DTMIterator. I'm not sure whether anyone will
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * need this or not, but let's write it and think about it.
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMIterator getDTMIterator() {
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return m_iter;
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //================================================================
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // org.w3c.dom.NodeList API follows
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Returns the <code>index</code>th item in the collection. If
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * <code>index</code> is greater than or equal to the number of nodes in
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * the list, this returns <code>null</code>.
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param index Index into the collection.
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @return The node at the <code>index</code>th position in the
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *   <code>NodeList</code>, or <code>null</code> if that is not a valid
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *   index.
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public Node item(int index)
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (m_iter != null) {
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            int handle=m_iter.item(index);
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (handle == DTM.NULL) {
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                return null;
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return m_iter.getDTM(handle).getNode(handle);
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } else {
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return null;
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * The number of nodes in the list. The range of valid child node indices
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * is 0 to <code>length-1</code> inclusive.
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public int getLength() {
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return (m_iter != null) ? m_iter.getLength() : 0;
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
128