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: DTMNodeList.java,v 1.2.4.1 2005/09/15 08:15:04 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 com.sun.org.apache.xml.internal.dtm.DTMIterator;
27 import org.w3c.dom.Node;
28
29 /**
30 * <code>DTMNodeList</code> gives us an implementation of the DOM's
31 * NodeList interface wrapped around a DTM Iterator. The author
32 * considers this something of an abominations, since NodeList was not
33 * intended to be a general purpose "list of nodes" API and is
34 * generally considered by the DOM WG to have be a mistake... but I'm
35 * told that some of the XPath/XSLT folks say they must have this
36 * solution.
37 *
38 * Please note that this is not necessarily equivlaent to a DOM
39 * NodeList operating over the same document. In particular:
40 * <ul>
41 *
42 * <li>If there are several Text nodes in logical succession (ie,
43 * across CDATASection and EntityReference boundaries), we will return
44 * only the first; the caller is responsible for stepping through
45 * them.
46 * (%REVIEW% Provide a convenience routine here to assist, pending
47 * proposed DOM Level 3 getAdjacentText() operation?) </li>
48 *
49 * <li>Since the whole XPath/XSLT architecture assumes that the source
50 * document is not altered while we're working with it, we do not
51 * promise to implement the DOM NodeList's "live view" response to
52 * document mutation. </li>
53 *
54 * </ul>
55 *
56 * <p>State: In progress!!</p>
57 * */
58 public class DTMNodeList extends DTMNodeListBase {
59 private DTMIterator m_iter;
60
61 //================================================================
62 // Methods unique to this class
63 private DTMNodeList() {
64 }
65
66 /**
67 * Public constructor: Wrap a DTMNodeList around an existing
68 * and preconfigured DTMIterator
69 *
70 * WARNING: THIS HAS THE SIDE EFFECT OF ISSUING setShouldCacheNodes(true)
71 * AGAINST THE DTMIterator.
72 *
73 */
74 public DTMNodeList(DTMIterator dtmIterator) {
75 if (dtmIterator != null) {
76 int pos = dtmIterator.getCurrentPos();
77 try {
78 m_iter=(DTMIterator)dtmIterator.cloneWithReset();
79 } catch(CloneNotSupportedException cnse) {
80 m_iter = dtmIterator;
81 }
82 m_iter.setShouldCacheNodes(true);
83 m_iter.runTo(-1);
84 m_iter.setCurrentPos(pos);
85 }
86 }
87
88 /**
89 * Access the wrapped DTMIterator. I'm not sure whether anyone will
90 * need this or not, but let's write it and think about it.
91 *
92 */
93 public DTMIterator getDTMIterator() {
94 return m_iter;
95 }
96
97 //================================================================
98 // org.w3c.dom.NodeList API follows
99
100 /**
101 * Returns the <code>index</code>th item in the collection. If
102 * <code>index</code> is greater than or equal to the number of nodes in
103 * the list, this returns <code>null</code>.
104 * @param index Index into the collection.
105 * @return The node at the <code>index</code>th position in the
106 * <code>NodeList</code>, or <code>null</code> if that is not a valid
107 * index.
108 */
109 public Node item(int index)
110 {
111 if (m_iter != null) {
112 int handle=m_iter.item(index);
113 if (handle == DTM.NULL) {
114 return null;
115 }
116 return m_iter.getDTM(handle).getNode(handle);
117 } else {
118 return null;
119 }
120 }
121
122 /**
123 * The number of nodes in the list. The range of valid child node indices
124 * is 0 to <code>length-1</code> inclusive.
125 */
126 public int getLength() {
127 return (m_iter != null) ? m_iter.getLength() : 0;
128 }
129 }