KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > dtm > ref > DTMNodeList


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: DTMNodeList.java,v 1.11 2004/02/16 23:06:11 minchau Exp $
18  */

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

54 public class DTMNodeList extends DTMNodeListBase {
55     private DTMIterator m_iter;
56
57     //================================================================
58
// Methods unique to this class
59
private DTMNodeList() {
60     }
61
62     /**
63      * Public constructor: Wrap a DTMNodeList around an existing
64      * and preconfigured DTMIterator
65      *
66      * WARNING: THIS HAS THE SIDE EFFECT OF ISSUING setShouldCacheNodes(true)
67      * AGAINST THE DTMIterator.
68      *
69      */

70     public DTMNodeList(DTMIterator dtmIterator) {
71         if (dtmIterator != null) {
72             int pos = dtmIterator.getCurrentPos();
73             try {
74                 m_iter=(DTMIterator)dtmIterator.cloneWithReset();
75             } catch(CloneNotSupportedException JavaDoc cnse) {
76                 m_iter = dtmIterator;
77             }
78             m_iter.setShouldCacheNodes(true);
79             m_iter.runTo(-1);
80             m_iter.setCurrentPos(pos);
81         }
82     }
83
84     /**
85      * Access the wrapped DTMIterator. I'm not sure whether anyone will
86      * need this or not, but let's write it and think about it.
87      *
88      */

89     public DTMIterator getDTMIterator() {
90         return m_iter;
91     }
92
93     //================================================================
94
// org.w3c.dom.NodeList API follows
95

96     /**
97      * Returns the <code>index</code>th item in the collection. If
98      * <code>index</code> is greater than or equal to the number of nodes in
99      * the list, this returns <code>null</code>.
100      * @param indexIndex into the collection.
101      * @return The node at the <code>index</code>th position in the
102      * <code>NodeList</code>, or <code>null</code> if that is not a valid
103      * index.
104      */

105     public Node JavaDoc item(int index)
106     {
107         if (m_iter != null) {
108             int handle=m_iter.item(index);
109             if (handle == DTM.NULL) {
110                 return null;
111             }
112             return m_iter.getDTM(handle).getNode(handle);
113         } else {
114             return null;
115         }
116     }
117
118     /**
119      * The number of nodes in the list. The range of valid child node indices
120      * is 0 to <code>length-1</code> inclusive.
121      */

122     public int getLength() {
123         return (m_iter != null) ? m_iter.getLength() : 0;
124     }
125 }
126
Popular Tags