KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > dom > NodeListImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.tax.dom;
22
23 import java.util.Iterator JavaDoc;
24 import org.w3c.dom.*;
25 import org.netbeans.tax.*;
26
27 /**
28  * NodeList taht filters out all unwrappable types.
29  *
30  *
31  * @author Petr Kuzel
32  */

33 class NodeListImpl implements NodeList {
34
35     public static final NodeList EMPTY = new NodeList() {
36         public int getLength() { return 0; }
37         public org.w3c.dom.Node JavaDoc item(int i) { return null; }
38         public String JavaDoc toString() { return "NodeListImpl.EMPTY"; }
39     };
40
41
42     private final TreeObjectList peer;
43
44     public NodeListImpl(TreeObjectList peer) {
45         this.peer = peer;
46     }
47     
48     /** The number of nodes in the list. The range of valid child node indices
49      * is 0 to <code>length-1</code> inclusive.
50      *
51      */

52     public int getLength() {
53         int i = 0;
54         Iterator JavaDoc it = peer.iterator();
55         while (it.hasNext()) {
56             TreeObject next = (TreeObject) it.next();
57             if (accept(next)) i++;
58         }
59         return i;
60     }
61     
62     /** Returns the <code>index</code>th item in the collection. If
63      * <code>index</code> is greater than or equal to the number of nodes in
64      * the list, this returns <code>null</code>.
65      * @param index Index into the collection.
66      * @return The node at the <code>index</code>th position in the
67      * <code>NodeList</code>, or <code>null</code> if that is not a valid
68      * index.
69      *
70      */

71     public Node item(int index) {
72         int i = 0;
73         Iterator JavaDoc it = peer.iterator();
74         while (it.hasNext()) {
75             TreeObject next = (TreeObject) it.next();
76             if (accept(next)) {
77                 if (i == index) {
78                     return Wrapper.wrap((TreeObject)peer.get(index));
79                 } else {
80                     i++;
81                 }
82             }
83         }
84         return null;
85     }
86
87     /**
88      * Supported types.
89      */

90     private boolean accept(TreeObject o) {
91         return o instanceof TreeText || o instanceof TreeElement;
92     }
93 }
94
Popular Tags