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: DTMChildIterNodeList.java 468653 2006-10-28 07:07:05Z minchau $
020     */
021    package org.apache.xml.dtm.ref;
022    
023    import org.apache.xml.dtm.DTM;
024    import org.w3c.dom.Node;
025    
026    /**
027     * <code>DTMNodeList</code> gives us an implementation of the DOM's
028     * NodeList interface wrapped around a DTM Iterator. The author
029     * considers this something of an abominations, since NodeList was not
030     * intended to be a general purpose "list of nodes" API and is
031     * generally considered by the DOM WG to have be a mistake... but I'm
032     * told that some of the XPath/XSLT folks say they must have this
033     * solution.
034     *
035     * Please note that this is not necessarily equivlaent to a DOM
036     * NodeList operating over the same document. In particular:
037     * <ul>
038     *
039     * <li>If there are several Text nodes in logical succession (ie,
040     * across CDATASection and EntityReference boundaries), we will return
041     * only the first; the caller is responsible for stepping through
042     * them.
043     * (%REVIEW% Provide a convenience routine here to assist, pending
044     * proposed DOM Level 3 getAdjacentText() operation?) </li>
045     *
046     * <li>Since the whole XPath/XSLT architecture assumes that the source
047     * document is not altered while we're working with it, we do not
048     * promise to implement the DOM NodeList's "live view" response to
049     * document mutation. </li>
050     *
051     * </ul>
052     *
053     * <p>State: In progress!!</p>
054     * */
055    public class DTMChildIterNodeList extends DTMNodeListBase {
056        private int m_firstChild;
057        private DTM m_parentDTM;
058    
059        //================================================================
060        // Methods unique to this class
061        private DTMChildIterNodeList() {
062        }
063    
064        /**
065         * Public constructor: Create a NodeList to support
066         * DTMNodeProxy.getChildren().
067         *
068         * Unfortunately AxisIterators and DTMIterators don't share an API,
069         * so I can't use the existing Axis.CHILD iterator. Rather than
070         * create Yet Another Class, let's set up a special case of this
071         * one.
072         *
073         * @param parentDTM The DTM containing this node
074         * @param parentHandle DTM node-handle integer
075         *
076         */
077        public DTMChildIterNodeList(DTM parentDTM,int parentHandle) {
078            m_parentDTM=parentDTM;
079            m_firstChild=parentDTM.getFirstChild(parentHandle);
080        }
081    
082    
083        //================================================================
084        // org.w3c.dom.NodeList API follows
085    
086        /**
087         * Returns the <code>index</code>th item in the collection. If 
088         * <code>index</code> is greater than or equal to the number of nodes in 
089         * the list, this returns <code>null</code>.
090         * @param index Index into the collection.
091         * @return The node at the <code>index</code>th position in the 
092         *   <code>NodeList</code>, or <code>null</code> if that is not a valid 
093         *   index.
094         */
095        public Node item(int index) {
096            int handle=m_firstChild;
097            while(--index>=0 && handle!=DTM.NULL) {
098                handle=m_parentDTM.getNextSibling(handle);
099            }
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    }