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 30 public class DescendentHTMLCollection extends AbstractScriptableDelegate implements HTMLCollection { 31 private final NodeImpl rootNode; 33 private final NodeFilter nodeFilter; 34 35 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 item(int index) { 52 NodeScanner ns = new NodeScanner(index); 53 try { 54 this.rootNode.visit(ns); 55 } catch(StopVisitorException sve) { 56 } 58 return ns.getNode(); 59 } 60 61 public Node namedItem(String name) { 62 org.w3c.dom.Document doc = this.rootNode.getOwnerDocument(); 63 if(doc == null) { 64 return null; 65 } 66 Node node = (Node ) 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 node) { 75 NodeScanner2 ns = new NodeScanner2(node); 76 try { 77 this.rootNode.visit(ns); 78 } catch(StopVisitorException sve) { 79 } 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 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 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 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 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 targetNode; 128 129 public NodeScanner2(Node node) { 130 this.targetNode = node; 131 } 132 133 public final void visit(Node 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 |