001: package net.sf.saxon.dom;
002:
003: import net.sf.saxon.om.Item;
004: import net.sf.saxon.om.NodeInfo;
005: import net.sf.saxon.om.SequenceIterator;
006: import net.sf.saxon.om.VirtualNode;
007: import net.sf.saxon.trans.DynamicError;
008: import net.sf.saxon.trans.XPathException;
009: import net.sf.saxon.value.SequenceExtent;
010: import org.w3c.dom.Node;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * This class wraps a list of nodes as a DOM NodeList
017: */
018:
019: public final class DOMNodeList implements org.w3c.dom.NodeList {
020: private List sequence;
021:
022: /**
023: * Construct an node list that wraps a supplied SequenceExtent. This constructor does
024: * not check that the items in the supplied SequenceExtent are indeed DOM Nodes.
025: */
026:
027: public DOMNodeList(List extent) {
028: sequence = extent;
029: }
030:
031: /**
032: * Construct an node list that wraps a supplied SequenceExtent, checking that all the
033: * items in the sequence are wrappers around DOM Nodes
034: */
035:
036: public static DOMNodeList checkAndMake(SequenceExtent extent)
037: throws XPathException {
038: SequenceIterator it = extent.iterate(null);
039: List list = new ArrayList(extent.getLength());
040: while (true) {
041: Item next = it.next();
042: if (next == null)
043: break;
044: Object o = next;
045: if (!(o instanceof NodeInfo)) {
046: throw new DynamicError(
047: "Supplied sequence contains an item that is not a Saxon NodeInfo");
048: }
049: if (o instanceof VirtualNode) {
050: o = ((VirtualNode) o).getUnderlyingNode();
051: if (!(o instanceof Node)) {
052: throw new DynamicError(
053: "Supplied sequence contains an item that is not a wrapper around a DOM Node");
054: }
055: list.add(o);
056: }
057:
058: }
059: return new DOMNodeList(list);
060: }
061:
062: /**
063: * Return the sequence of nodes as a Saxon Value
064: */
065:
066: // public Value getSequence() {
067: // return sequence;
068: // }
069: /**
070: * return the number of nodes in the list (DOM method)
071: */
072:
073: public int getLength() {
074: return sequence.size();
075: }
076:
077: /**
078: * Return the n'th item in the list (DOM method)
079: * @throws java.lang.ClassCastException if the item is not a DOM Node
080: */
081:
082: public Node item(int index) {
083: return (Node) sequence.get(index);
084: // while (o instanceof VirtualNode) {
085: // o = ((VirtualNode)o).getUnderlyingNode();
086: // }
087: // if (o instanceof Node) {
088: // return (Node)o;
089: // } else if (o instanceof NodeInfo) {
090: // return NodeOverNodeInfo.wrap((NodeInfo)o);
091: // } else {
092: // throw new IllegalStateException(
093: // "Sequence cannot be used as a DOM NodeList, because it contains an item that is not a node");
094: // }
095: }
096:
097: }
098:
099: //
100: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
101: // you may not use this file except in compliance with the License. You may obtain a copy of the
102: // License at http://www.mozilla.org/MPL/
103: //
104: // Software distributed under the License is distributed on an "AS IS" basis,
105: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
106: // See the License for the specific language governing rights and limitations under the License.
107: //
108: // The Original Code is: all this file.
109: //
110: // The Initial Developer of the Original Code is Michael H. Kay.
111: //
112: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
113: //
114: // Contributor(s): none.
115: //
|