01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.saaj;
20:
21: import org.w3c.dom.Node;
22: import org.w3c.dom.NodeList;
23:
24: import java.util.ArrayList;
25: import java.util.Collections;
26: import java.util.List;
27:
28: /**
29: * The <code>NodeList</code> interface provides the abstraction of an ordered collection of nodes,
30: * without defining or constraining how this collection is implemented. <code>NodeList</code>
31: * objects in the DOM are live. <p>The items in the <code>NodeList</code> are accessible via an
32: * integral index, starting from 0. <p>See also the <a href='http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113'>Document
33: * Object Model (DOM) Level 2 Core Specification</a>.
34: */
35: public class NodeListImpl implements NodeList {
36:
37: List mNodes;
38:
39: public static final NodeList EMPTY_NODELIST = new NodeListImpl(
40: Collections.EMPTY_LIST);
41:
42: /** Constructor and Setter is intensionally made package access only. */
43: NodeListImpl() {
44: mNodes = new ArrayList();
45: }
46:
47: NodeListImpl(List nodes) {
48: this ();
49: mNodes.addAll(nodes);
50: }
51:
52: void addNode(org.w3c.dom.Node node) {
53: mNodes.add(node);
54: }
55:
56: void addNodeList(org.w3c.dom.NodeList nodes) {
57: for (int i = 0; i < nodes.getLength(); i++) {
58: mNodes.add(nodes.item(i));
59: }
60: }
61:
62: /**
63: * Returns the <code>index</code>th item in the collection. If <code>index</code> is greater
64: * than or equal to the number of nodes in the list, this returns <code>null</code>.
65: *
66: * @param index Index into the collection.
67: * @return The node at the <code>index</code>th position in the <code>NodeList</code>, or
68: * <code>null</code> if that is not a valid index.
69: */
70: public Node item(int index) {
71: if (mNodes != null && mNodes.size() > index) {
72: return (Node) mNodes.get(index);
73: } else {
74: return null;
75: }
76: }
77:
78: /**
79: * The number of nodes in the list. The range of valid child node indices is 0 to
80: * <code>length-1</code> inclusive.
81: */
82: public int getLength() {
83: return mNodes.size();
84: }
85:
86: }
|