1 21 24 package org.lobobrowser.html.domimpl; 25 26 import org.lobobrowser.js.*; 27 import org.w3c.dom.Node ; 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 private final Map sourceMap; 36 private final NodeFilter filter; 37 private final Object lock; 38 private final NodeImpl rootNode; 39 40 public FilteredHTMLCollectionImpl(NodeImpl rootNode, Map sourceMap, NodeFilter filter, Object 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 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 item(final int index) { 70 NodeVisitor visitor = new NodeVisitor() { 71 private int counter = 0; 72 73 public void visit(Node 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 ) sve.getTag(); 88 } 89 } 90 } 91 92 public Node namedItem(String name) { 93 synchronized(this.lock) { 94 Node node = (Node ) 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 |