14c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/*
24c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
34c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * or more contributor license agreements. See the NOTICE file
44c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * distributed with this work for additional information
54c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * regarding copyright ownership. The ASF licenses this file
64c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
74c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * you may not use this file except in compliance with the License.
84c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * You may obtain a copy of the License at
94c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
104c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
114c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
124c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Unless required by applicable law or agreed to in writing, software
134c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
144c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
154c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * See the License for the specific language governing permissions and
164c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * limitations under the License.
174c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */
184c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/*
194c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * $Id: DTMChildIterNodeList.java 468653 2006-10-28 07:07:05Z minchau $
204c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */
214c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonpackage org.apache.xml.dtm.ref;
224c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
234c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xml.dtm.DTM;
244c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.w3c.dom.Node;
254c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
264c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/**
274c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * <code>DTMNodeList</code> gives us an implementation of the DOM's
284c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * NodeList interface wrapped around a DTM Iterator. The author
294c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * considers this something of an abominations, since NodeList was not
304c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * intended to be a general purpose "list of nodes" API and is
314c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * generally considered by the DOM WG to have be a mistake... but I'm
324c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * told that some of the XPath/XSLT folks say they must have this
334c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * solution.
344c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
354c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Please note that this is not necessarily equivlaent to a DOM
364c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * NodeList operating over the same document. In particular:
374c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * <ul>
384c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
394c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * <li>If there are several Text nodes in logical succession (ie,
404c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * across CDATASection and EntityReference boundaries), we will return
414c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * only the first; the caller is responsible for stepping through
424c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * them.
434c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * (%REVIEW% Provide a convenience routine here to assist, pending
444c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * proposed DOM Level 3 getAdjacentText() operation?) </li>
454c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
464c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * <li>Since the whole XPath/XSLT architecture assumes that the source
474c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * document is not altered while we're working with it, we do not
484c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * promise to implement the DOM NodeList's "live view" response to
494c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * document mutation. </li>
504c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
514c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * </ul>
524c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson *
534c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * <p>State: In progress!!</p>
544c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * */
554c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonpublic class DTMChildIterNodeList extends DTMNodeListBase {
564c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    private int m_firstChild;
574c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    private DTM m_parentDTM;
584c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
594c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    //================================================================
604c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    // Methods unique to this class
614c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    private DTMChildIterNodeList() {
624c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    }
634c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
644c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    /**
654c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * Public constructor: Create a NodeList to support
664c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * DTMNodeProxy.getChildren().
674c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     *
684c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * Unfortunately AxisIterators and DTMIterators don't share an API,
694c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * so I can't use the existing Axis.CHILD iterator. Rather than
704c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * create Yet Another Class, let's set up a special case of this
714c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * one.
724c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     *
734c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * @param parentDTM The DTM containing this node
744c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * @param parentHandle DTM node-handle integer
754c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     *
764c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     */
774c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    public DTMChildIterNodeList(DTM parentDTM,int parentHandle) {
784c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        m_parentDTM=parentDTM;
794c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        m_firstChild=parentDTM.getFirstChild(parentHandle);
804c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    }
814c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
824c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
834c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    //================================================================
844c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    // org.w3c.dom.NodeList API follows
854c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
864c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    /**
874c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * Returns the <code>index</code>th item in the collection. If
884c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * <code>index</code> is greater than or equal to the number of nodes in
894c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * the list, this returns <code>null</code>.
904c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * @param index Index into the collection.
914c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * @return The node at the <code>index</code>th position in the
924c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     *   <code>NodeList</code>, or <code>null</code> if that is not a valid
934c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     *   index.
944c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     */
954c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    public Node item(int index) {
964c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        int handle=m_firstChild;
974c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        while(--index>=0 && handle!=DTM.NULL) {
984c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson            handle=m_parentDTM.getNextSibling(handle);
994c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        }
1004c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        if (handle == DTM.NULL) {
1014c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson            return null;
1024c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        }
1034c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        return m_parentDTM.getNode(handle);
1044c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    }
1054c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson
1064c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    /**
1074c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * The number of nodes in the list. The range of valid child node indices
1084c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     * is 0 to <code>length-1</code> inclusive.
1094c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson     */
1104c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    public int getLength() {
1114c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        int count=0;
1124c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        for (int handle=m_firstChild;
1134c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson             handle!=DTM.NULL;
1144c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson             handle=m_parentDTM.getNextSibling(handle)) {
1154c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson            ++count;
1164c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        }
1174c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson        return count;
1184c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson    }
1194c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson}
120