KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > dtm > ref > DTMChildIterNodeList


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DTMChildIterNodeList.java,v 1.3 2004/02/16 23:06:11 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.dtm.ref;
20
21 import com.sun.org.apache.xml.internal.dtm.DTM;
22 import org.w3c.dom.Node JavaDoc;
23
24 /**
25  * <code>DTMNodeList</code> gives us an implementation of the DOM's
26  * NodeList interface wrapped around a DTM Iterator. The author
27  * considers this something of an abominations, since NodeList was not
28  * intended to be a general purpose "list of nodes" API and is
29  * generally considered by the DOM WG to have be a mistake... but I'm
30  * told that some of the XPath/XSLT folks say they must have this
31  * solution.
32  *
33  * Please note that this is not necessarily equivlaent to a DOM
34  * NodeList operating over the same document. In particular:
35  * <ul>
36  *
37  * <li>If there are several Text nodes in logical succession (ie,
38  * across CDATASection and EntityReference boundaries), we will return
39  * only the first; the caller is responsible for stepping through
40  * them.
41  * (%REVIEW% Provide a convenience routine here to assist, pending
42  * proposed DOM Level 3 getAdjacentText() operation?) </li>
43  *
44  * <li>Since the whole XPath/XSLT architecture assumes that the source
45  * document is not altered while we're working with it, we do not
46  * promise to implement the DOM NodeList's "live view" response to
47  * document mutation. </li>
48  *
49  * </ul>
50  *
51  * <p>State: In progress!!</p>
52  * */

53 public class DTMChildIterNodeList extends DTMNodeListBase {
54     private int m_firstChild;
55     private DTM m_parentDTM;
56
57     //================================================================
58
// Methods unique to this class
59
private DTMChildIterNodeList() {
60     }
61
62     /**
63      * Public constructor: Create a NodeList to support
64      * DTMNodeProxy.getChildren().
65      *
66      * Unfortunately AxisIterators and DTMIterators don't share an API,
67      * so I can't use the existing Axis.CHILD iterator. Rather than
68      * create Yet Another Class, let's set up a special case of this
69      * one.
70      *
71      * @param parentDTM The DTM containing this node
72      * @param parentHandle DTM node-handle integer
73      *
74      */

75     public DTMChildIterNodeList(DTM parentDTM,int parentHandle) {
76         m_parentDTM=parentDTM;
77         m_firstChild=parentDTM.getFirstChild(parentHandle);
78     }
79
80
81     //================================================================
82
// org.w3c.dom.NodeList API follows
83

84     /**
85      * Returns the <code>index</code>th item in the collection. If
86      * <code>index</code> is greater than or equal to the number of nodes in
87      * the list, this returns <code>null</code>.
88      * @param indexIndex into the collection.
89      * @return The node at the <code>index</code>th position in the
90      * <code>NodeList</code>, or <code>null</code> if that is not a valid
91      * index.
92      */

93     public Node JavaDoc item(int index) {
94         int handle=m_firstChild;
95         while(--index>=0 && handle!=DTM.NULL) {
96             handle=m_parentDTM.getNextSibling(handle);
97         }
98         if (handle == DTM.NULL) {
99             return null;
100         }
101         return m_parentDTM.getNode(handle);
102     }
103
104     /**
105      * The number of nodes in the list. The range of valid child node indices
106      * is 0 to <code>length-1</code> inclusive.
107      */

108     public int getLength() {
109         int count=0;
110         for (int handle=m_firstChild;
111              handle!=DTM.NULL;
112              handle=m_parentDTM.getNextSibling(handle)) {
113             ++count;
114         }
115         return count;
116     }
117 }
118
Popular Tags