1 /*
2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.org.apache.xpath.internal.jaxp;
27 
28 import java.util.Iterator;
29 import javax.xml.xpath.XPathException;
30 import javax.xml.xpath.XPathNodes;
31 import javax.xml.xpath.XPathEvaluationResult.XPathResultType;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 
35 /**
36  * This class implements XPathNodes that represents a set of nodes selected by
37  * evaluating an expression.
38  */
39 public class XPathNodesImpl implements XPathNodes {
40     Class<Node> elementType;
41     NodeList nodeList = null;
42 
XPathNodesImpl(NodeList nodeList, Class<Node> elementType)43     public XPathNodesImpl(NodeList nodeList, Class<Node> elementType) {
44         this.nodeList = nodeList;
45         this.elementType = elementType;
46     }
47 
48     @Override
iterator()49     public Iterator<Node> iterator() {
50         return new NodeSetIterator<>(elementType);
51     }
52 
53     class NodeSetIterator<E> implements Iterator<E> {
54         int currentIndex;
55         Class<E> elementType;
NodeSetIterator(Class<E> elementType)56         NodeSetIterator(Class<E> elementType) {
57             this.elementType = elementType;
58         }
hasNext()59         public boolean hasNext() {
60             if (nodeList != null) {
61                 return currentIndex < nodeList.getLength();
62             }
63 
64             return false;
65         }
66 
next()67         public E next() {
68             if (nodeList != null && nodeList.getLength() > 0) {
69                 return elementType.cast(nodeList.item(currentIndex++));
70             }
71             return null;
72         }
73     }
74 
75     @Override
size()76     public int size() {
77         if (nodeList != null) {
78             return nodeList.getLength();
79         }
80         return 0;
81     }
82 
83     @Override
get(int index)84     public Node get(int index) throws XPathException {
85         if (index <0 || index >= size()) {
86             throw new IndexOutOfBoundsException("Index " + index + " is out of bounds");
87         }
88         if (nodeList != null) {
89             return nodeList.item(index);
90         }
91         return null;
92     }
93 }
94