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