KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > DOMNodeList


1 package net.sf.saxon.dom;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.om.VirtualNode;
6 import net.sf.saxon.trans.DynamicError;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.value.SequenceExtent;
9 import org.w3c.dom.Node JavaDoc;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 /**
15 * This class wraps a list of nodes as a DOM NodeList
16 */

17
18 public final class DOMNodeList implements org.w3c.dom.NodeList JavaDoc {
19     private List JavaDoc sequence;
20
21     /**
22      * Construct an node list that wraps a supplied SequenceExtent. This constructor does
23      * not check that the items in the supplied SequenceExtent are indeed DOM Nodes.
24      */

25
26     public DOMNodeList(List JavaDoc extent) {
27         sequence = extent;
28     }
29
30     /**
31      * Construct an node list that wraps a supplied SequenceExtent, checking that all the
32      * items in the sequence are wrappers around DOM Nodes
33     */

34
35     public static DOMNodeList checkAndMake(SequenceExtent extent) throws XPathException {
36         SequenceIterator it = extent.iterate(null);
37         List JavaDoc list = new ArrayList JavaDoc(extent.getLength());
38         while (true) {
39             Item next = it.next();
40             if (next==null) break;
41             Object JavaDoc o = next;
42             if (!(o instanceof NodeInfo)) {
43                 throw new DynamicError("Supplied sequence contains an item that is not a Saxon NodeInfo");
44             }
45             if (o instanceof VirtualNode) {
46                 o = ((VirtualNode)o).getUnderlyingNode();
47                 if (!(o instanceof Node)) {
48                     throw new DynamicError("Supplied sequence contains an item that is not a wrapper around a DOM Node");
49                 }
50                 list.add(o);
51             }
52
53         }
54         return new DOMNodeList(list);
55     }
56
57     /**
58      * Return the sequence of nodes as a Saxon Value
59      */

60
61 // public Value getSequence() {
62
// return sequence;
63
// }
64

65     /**
66     * return the number of nodes in the list (DOM method)
67     */

68
69     public int getLength() {
70         return sequence.size();
71     }
72
73     /**
74     * Return the n'th item in the list (DOM method)
75     * @throws java.lang.ClassCastException if the item is not a DOM Node
76     */

77
78     public Node item(int index) {
79         return (Node)sequence.get(index);
80 // while (o instanceof VirtualNode) {
81
// o = ((VirtualNode)o).getUnderlyingNode();
82
// }
83
// if (o instanceof Node) {
84
// return (Node)o;
85
// } else if (o instanceof NodeInfo) {
86
// return NodeOverNodeInfo.wrap((NodeInfo)o);
87
// } else {
88
// throw new IllegalStateException(
89
// "Sequence cannot be used as a DOM NodeList, because it contains an item that is not a node");
90
// }
91
}
92
93 }
94
95 //
96
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
97
// you may not use this file except in compliance with the License. You may obtain a copy of the
98
// License at http://www.mozilla.org/MPL/
99
//
100
// Software distributed under the License is distributed on an "AS IS" basis,
101
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
102
// See the License for the specific language governing rights and limitations under the License.
103
//
104
// The Original Code is: all this file.
105
//
106
// The Initial Developer of the Original Code is Michael H. Kay.
107
//
108
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
109
//
110
// Contributor(s): none.
111
//
112

113
Popular Tags