KickJava   Java API By Example, From Geeks To Geeks.

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


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 Oct 8, 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 import java.util.*;
30 import org.lobobrowser.util.*;
31
32 public class FilteredHTMLCollectionImpl extends AbstractScriptableDelegate implements HTMLCollection {
33     // Note: Class must be public for reflection to work.
34
//OPTIMIZE: Indexers are inefficient in this class.
35
private final Map sourceMap;
36     private final NodeFilter filter;
37     private final Object JavaDoc lock;
38     private final NodeImpl rootNode;
39     
40     public FilteredHTMLCollectionImpl(NodeImpl rootNode, Map sourceMap, NodeFilter filter, Object JavaDoc lock) {
41         this.sourceMap = sourceMap;
42         this.filter = filter;
43         this.lock = lock;
44         this.rootNode = rootNode;
45     }
46
47     private class CounterNodeVisitor implements NodeVisitor {
48         private int counter = 0;
49         
50         public void visit(Node JavaDoc node) {
51             if(filter.accept(node)) {
52                 this.counter++;
53             }
54         }
55         
56         public int getCount() {
57             return this.counter;
58         }
59     }
60     
61     public int getLength() {
62         CounterNodeVisitor visitor = new CounterNodeVisitor();
63         synchronized(this.lock) {
64             this.rootNode.visitImpl(visitor);
65             return visitor.getCount();
66         }
67     }
68
69     public Node JavaDoc item(final int index) {
70         NodeVisitor visitor = new NodeVisitor() {
71             private int counter = 0;
72             
73             public void visit(Node JavaDoc node) {
74                 if(filter.accept(node)) {
75                     if(this.counter == index) {
76                         throw new StopVisitorException(node);
77                     }
78                     this.counter++;
79                 }
80             }
81         };
82         synchronized(this.lock) {
83             try {
84                 this.rootNode.visitImpl(visitor);
85                 return null;
86             } catch(StopVisitorException sve) {
87                 return (Node JavaDoc) sve.getTag();
88             }
89         }
90     }
91
92     public Node JavaDoc namedItem(String JavaDoc name) {
93         synchronized(this.lock) {
94             Node JavaDoc node = (Node JavaDoc) this.sourceMap.get(name);
95             if(node != null && this.filter.accept(node)) {
96                 return node;
97             }
98             return null;
99         }
100     }
101 }
102
Popular Tags