KickJava   Java API By Example, From Geeks To Geeks.

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


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

56 public class DTMAxisIterNodeList extends DTMNodeListBase {
57     private DTM m_dtm;
58     private DTMAxisIterator m_iter;
59     private IntVector m_cachedNodes;
60     private int m_last = -1;
61     //================================================================
62
// Methods unique to this class
63
private DTMAxisIterNodeList() {
64     }
65
66     /**
67      * Public constructor: Wrap a DTMNodeList around an existing
68      * and preconfigured DTMAxisIterator
69      */

70     public DTMAxisIterNodeList(DTM dtm, DTMAxisIterator dtmAxisIterator) {
71         if (dtmAxisIterator == null) {
72             m_last = 0;
73         } else {
74             m_cachedNodes = new IntVector();
75             m_dtm = dtm;
76         }
77         m_iter = dtmAxisIterator;
78     }
79
80     /**
81      * Access the wrapped DTMIterator. I'm not sure whether anyone will
82      * need this or not, but let's write it and think about it.
83      *
84      */

85     public DTMAxisIterator getDTMAxisIterator() {
86         return m_iter;
87     }
88   
89
90     //================================================================
91
// org.w3c.dom.NodeList API follows
92

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

102     public Node JavaDoc item(int index) {
103         if (m_iter != null) {
104             int node;
105             int count = m_cachedNodes.size();
106
107             if (count > index) {
108                 node = m_cachedNodes.elementAt(index);
109                 return m_dtm.getNode(node);
110             } else if (m_last == -1) {
111                 while (((node = m_iter.next()) != DTMAxisIterator.END)
112                            && count <= index) {
113                     m_cachedNodes.addElement(node);
114                     count++;
115                 }
116                 if (node == DTMAxisIterator.END) {
117                     m_last = count;
118                 } else {
119                     return m_dtm.getNode(node);
120                 }
121             }
122         }
123         return null;
124     }
125
126     /**
127      * The number of nodes in the list. The range of valid child node indices
128      * is 0 to <code>length-1</code> inclusive.
129      */

130     public int getLength() {
131         if (m_last == -1) {
132             int node;
133             while ((node = m_iter.next()) != DTMAxisIterator.END) {
134                 m_cachedNodes.addElement(node);
135             }
136             m_last = m_cachedNodes.size();
137         }
138         return m_last;
139     }
140 }
141
Popular Tags