KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > DescendentHTMLCollection


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Dec 3, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import org.lobobrowser.js.*;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.html2.HTMLCollection;
29
30 public class DescendentHTMLCollection extends AbstractScriptableDelegate implements HTMLCollection {
31     //TODO: This collection is very inefficient for iteration.
32
private final NodeImpl rootNode;
33     private final NodeFilter nodeFilter;
34
35     /**
36      * @param node
37      * @param filter
38      */

39     public DescendentHTMLCollection(NodeImpl node, NodeFilter filter) {
40         super();
41         rootNode = node;
42         nodeFilter = filter;
43     }
44     
45     public int getLength() {
46         NodeCounter nc = new NodeCounter();
47         this.rootNode.visit(nc);
48         return nc.getCount();
49     }
50
51     public Node JavaDoc item(int index) {
52         NodeScanner ns = new NodeScanner(index);
53         try {
54             this.rootNode.visit(ns);
55         } catch(StopVisitorException sve) {
56             //ignore
57
}
58         return ns.getNode();
59     }
60
61     public Node JavaDoc namedItem(String JavaDoc name) {
62         org.w3c.dom.Document JavaDoc doc = this.rootNode.getOwnerDocument();
63         if(doc == null) {
64             return null;
65         }
66         //TODO: This might get elements that are not descendents.
67
Node JavaDoc node = (Node JavaDoc) doc.getElementById(name);
68         if(node != null && this.nodeFilter.accept(node)) {
69             return node;
70         }
71         return null;
72     }
73     
74     public int indexOf(Node JavaDoc node) {
75         NodeScanner2 ns = new NodeScanner2(node);
76         try {
77             this.rootNode.visit(ns);
78         } catch(StopVisitorException sve) {
79             //ignore
80
}
81         return ns.getIndex();
82     }
83
84     private final class NodeCounter implements NodeVisitor {
85         private int count = 0;
86         
87         public final void visit(Node JavaDoc node) {
88             if(nodeFilter.accept(node)) {
89                 this.count++;
90                 throw new SkipVisitorException();
91             }
92         }
93         
94         public int getCount() {
95             return this.count;
96         }
97     }
98
99     private final class NodeScanner implements NodeVisitor {
100         private int count = 0;
101         private Node JavaDoc foundNode = null;
102         private final int targetIndex;
103         
104         public NodeScanner(int idx) {
105             this.targetIndex = idx;
106         }
107         
108         public final void visit(Node JavaDoc node) {
109             if(nodeFilter.accept(node)) {
110                 if(this.count == this.targetIndex) {
111                     this.foundNode = node;
112                     throw new StopVisitorException();
113                 }
114                 this.count++;
115                 throw new SkipVisitorException();
116             }
117         }
118         
119         public Node JavaDoc getNode() {
120             return this.foundNode;
121         }
122     }
123
124     private final class NodeScanner2 implements NodeVisitor {
125         private int count = 0;
126         private int foundIndex = -1;
127         private final Node JavaDoc targetNode;
128         
129         public NodeScanner2(Node JavaDoc node) {
130             this.targetNode = node;
131         }
132         
133         public final void visit(Node JavaDoc node) {
134             if(nodeFilter.accept(node)) {
135                 if(node == this.targetNode) {
136                     this.foundIndex = this.count;
137                     throw new StopVisitorException();
138                 }
139                 this.count++;
140                 throw new SkipVisitorException();
141             }
142         }
143         
144         public int getIndex() {
145             return this.foundIndex;
146         }
147     }
148
149 }
150
Popular Tags