1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: DTMChildIterNodeList.java 468653 2006-10-28 07:07:05Z minchau $
20 */
21package org.apache.xml.dtm.ref;
22
23import org.apache.xml.dtm.DTM;
24import org.w3c.dom.Node;
25
26/**
27 * <code>DTMNodeList</code> gives us an implementation of the DOM's
28 * NodeList interface wrapped around a DTM Iterator. The author
29 * considers this something of an abominations, since NodeList was not
30 * intended to be a general purpose "list of nodes" API and is
31 * generally considered by the DOM WG to have be a mistake... but I'm
32 * told that some of the XPath/XSLT folks say they must have this
33 * solution.
34 *
35 * Please note that this is not necessarily equivlaent to a DOM
36 * NodeList operating over the same document. In particular:
37 * <ul>
38 *
39 * <li>If there are several Text nodes in logical succession (ie,
40 * across CDATASection and EntityReference boundaries), we will return
41 * only the first; the caller is responsible for stepping through
42 * them.
43 * (%REVIEW% Provide a convenience routine here to assist, pending
44 * proposed DOM Level 3 getAdjacentText() operation?) </li>
45 *
46 * <li>Since the whole XPath/XSLT architecture assumes that the source
47 * document is not altered while we're working with it, we do not
48 * promise to implement the DOM NodeList's "live view" response to
49 * document mutation. </li>
50 *
51 * </ul>
52 *
53 * <p>State: In progress!!</p>
54 * */
55public class DTMChildIterNodeList extends DTMNodeListBase {
56    private int m_firstChild;
57    private DTM m_parentDTM;
58
59    //================================================================
60    // Methods unique to this class
61    private DTMChildIterNodeList() {
62    }
63
64    /**
65     * Public constructor: Create a NodeList to support
66     * DTMNodeProxy.getChildren().
67     *
68     * Unfortunately AxisIterators and DTMIterators don't share an API,
69     * so I can't use the existing Axis.CHILD iterator. Rather than
70     * create Yet Another Class, let's set up a special case of this
71     * one.
72     *
73     * @param parentDTM The DTM containing this node
74     * @param parentHandle DTM node-handle integer
75     *
76     */
77    public DTMChildIterNodeList(DTM parentDTM,int parentHandle) {
78        m_parentDTM=parentDTM;
79        m_firstChild=parentDTM.getFirstChild(parentHandle);
80    }
81
82
83    //================================================================
84    // org.w3c.dom.NodeList API follows
85
86    /**
87     * Returns the <code>index</code>th item in the collection. If
88     * <code>index</code> is greater than or equal to the number of nodes in
89     * the list, this returns <code>null</code>.
90     * @param index Index into the collection.
91     * @return The node at the <code>index</code>th position in the
92     *   <code>NodeList</code>, or <code>null</code> if that is not a valid
93     *   index.
94     */
95    public Node item(int index) {
96        int handle=m_firstChild;
97        while(--index>=0 && handle!=DTM.NULL) {
98            handle=m_parentDTM.getNextSibling(handle);
99        }
100        if (handle == DTM.NULL) {
101            return null;
102        }
103        return m_parentDTM.getNode(handle);
104    }
105
106    /**
107     * The number of nodes in the list. The range of valid child node indices
108     * is 0 to <code>length-1</code> inclusive.
109     */
110    public int getLength() {
111        int count=0;
112        for (int handle=m_firstChild;
113             handle!=DTM.NULL;
114             handle=m_parentDTM.getNextSibling(handle)) {
115            ++count;
116        }
117        return count;
118    }
119}
120