KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > xpath > NodeListImpl


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.xpath;
18
19 import org.w3c.dom.DocumentFragment JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23
24 /**
25  * Implementation of the <code>NodeList</code> interface.<P>
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $Id: NodeListImpl.java,v 1.4 2004/02/28 11:47:15 cziegeler Exp $
29 */

30 public class NodeListImpl implements NodeList JavaDoc {
31
32     private Node JavaDoc[] nodelist;
33
34     /**
35      * Construct a NodeList by copying.
36      */

37     public NodeListImpl(NodeList JavaDoc list) {
38       if (list == null || list.getLength() == 0) {
39         nodelist = null;
40       } else {
41         nodelist = new Node JavaDoc[list.getLength()];
42         for(int i = 0; i < list.getLength(); i++) {
43           nodelist[i] = list.item(i).cloneNode(true);
44         }
45       }
46     }
47
48     /**
49      * Constructor
50      */

51     public NodeListImpl(Node JavaDoc[] nodes) {
52         this.nodelist = nodes;
53     }
54
55     /**
56      * Constructor
57      */

58     public NodeListImpl() {}
59
60     /**
61      * Construct a NodeList by copying.
62      */

63     public NodeListImpl(DocumentFragment JavaDoc fragment, String JavaDoc rootName) {
64       if (fragment != null) {
65         Element JavaDoc root = fragment.getOwnerDocument().createElementNS(null, rootName);
66         Node JavaDoc current;
67         while (fragment.hasChildNodes() == true) {
68           current = fragment.getFirstChild();
69           fragment.removeChild(current);
70           root.appendChild(current);
71         }
72         nodelist = new Node JavaDoc[1];
73         nodelist[0] = root;
74       }
75     }
76
77     /**
78      * Add a node to list
79      */

80     public void addNode(Node JavaDoc node) {
81         if (this.nodelist == null) {
82             this.nodelist = new Node JavaDoc[1];
83             this.nodelist[0] = node;
84         } else {
85             Node JavaDoc[] copy = new Node JavaDoc[this.nodelist.length+1];
86             System.arraycopy(this.nodelist, 0, copy, 0, this.nodelist.length);
87             copy[copy.length-1] = node;
88             this.nodelist = copy;
89         }
90     }
91
92     /**
93      * Returns the <code>index</code> th item in the collection. If
94      * <code>index</code> is greater than or equal to the number of nodes in
95      * the list, this returns <code>null</code> .
96      * @param index Index into the collection.
97      * @return The node at the <code>index</code> th position in the
98      * <code>NodeList</code> , or <code>null</code> if that is not a valid
99      * index.
100      */

101     public Node JavaDoc item(int index) {
102       if (nodelist == null || index >= nodelist.length) {
103         return null;
104       } else {
105         return nodelist[index];
106       }
107     }
108
109     /**
110      * The number of nodes in the list. The range of valid child node indices
111      * is 0 to <code>length-1</code> inclusive.
112      */

113     public int getLength() {
114       return (nodelist == null ? 0 : nodelist.length);
115     }
116
117 }
118
Popular Tags