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: DTMChildIterNodeList.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.w3c.dom.Node;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>DTMNodeList</code> gives us an implementation of the DOM's
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeList interface wrapped around a DTM Iterator. The author
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * considers this something of an abominations, since NodeList was not
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * intended to be a general purpose "list of nodes" API and is
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * generally considered by the DOM WG to have be a mistake... but I'm
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * told that some of the XPath/XSLT folks say they must have this
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * solution.
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Please note that this is not necessarily equivlaent to a DOM
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeList operating over the same document. In particular:
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <ul>
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>If there are several Text nodes in logical succession (ie,
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * across CDATASection and EntityReference boundaries), we will return
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * only the first; the caller is responsible for stepping through
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * them.
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (%REVIEW% Provide a convenience routine here to assist, pending
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * proposed DOM Level 3 getAdjacentText() operation?) </li>
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li>Since the whole XPath/XSLT architecture assumes that the source
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * document is not altered while we're working with it, we do not
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * promise to implement the DOM NodeList's "live view" response to
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * document mutation. </li>
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </ul>
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p>State: In progress!!</p>
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * */
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class DTMChildIterNodeList extends DTMNodeListBase {
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    private int m_firstChild;
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    private DTM m_parentDTM;
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //================================================================
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Methods unique to this class
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    private DTMChildIterNodeList() {
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Public constructor: Create a NodeList to support
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * DTMNodeProxy.getChildren().
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Unfortunately AxisIterators and DTMIterators don't share an API,
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * so I can't use the existing Axis.CHILD iterator. Rather than
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * create Yet Another Class, let's set up a special case of this
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * one.
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param parentDTM The DTM containing this node
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param parentHandle DTM node-handle integer
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public DTMChildIterNodeList(DTM parentDTM,int parentHandle) {
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        m_parentDTM=parentDTM;
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        m_firstChild=parentDTM.getFirstChild(parentHandle);
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //================================================================
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // org.w3c.dom.NodeList API follows
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Returns the <code>index</code>th item in the collection. If
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * <code>index</code> is greater than or equal to the number of nodes in
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * the list, this returns <code>null</code>.
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @param index Index into the collection.
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * @return The node at the <code>index</code>th position in the
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *   <code>NodeList</code>, or <code>null</code> if that is not a valid
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     *   index.
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public Node item(int index) {
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        int handle=m_firstChild;
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        while(--index>=0 && handle!=DTM.NULL) {
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            handle=m_parentDTM.getNextSibling(handle);
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (handle == DTM.NULL) {
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return null;
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return m_parentDTM.getNode(handle);
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * The number of nodes in the list. The range of valid child node indices
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * is 0 to <code>length-1</code> inclusive.
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    public int getLength() {
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        int count=0;
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        for (int handle=m_firstChild;
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson             handle!=DTM.NULL;
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson             handle=m_parentDTM.getNextSibling(handle)) {
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            ++count;
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return count;
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
120