1 5 6 package org.w3c.tidy; 7 8 28 29 34 public class DOMNodeListByTagNameImpl implements org.w3c.dom.NodeList { 35 36 private Node first = null; 37 private String tagName = "*"; 38 private int currIndex = 0; 39 private int maxIndex = 0; 40 private Node currNode = null; 41 42 protected DOMNodeListByTagNameImpl(Node first, String tagName) 43 { 44 this.first = first; 45 this.tagName = tagName; 46 } 47 48 51 public org.w3c.dom.Node item(int index) 52 { 53 currIndex = 0; 54 maxIndex = index; 55 preTraverse(first); 56 57 if (currIndex > maxIndex && currNode != null) 58 return currNode.getAdapter(); 59 else 60 return null; 61 } 62 63 66 public int getLength() 67 { 68 currIndex = 0; 69 maxIndex = Integer.MAX_VALUE; 70 preTraverse(first); 71 return currIndex; 72 } 73 74 protected void preTraverse(Node node) 75 { 76 if (node == null) 77 return; 78 79 if (node.type == Node.StartTag || node.type == Node.StartEndTag) 80 { 81 if (currIndex <= maxIndex && 82 (tagName.equals("*") || tagName.equals(node.element))) 83 { 84 currIndex += 1; 85 currNode = node; 86 } 87 } 88 if (currIndex > maxIndex) 89 return; 90 91 node = node.content; 92 while (node != null) 93 { 94 preTraverse(node); 95 node = node.next; 96 } 97 } 98 99 } 100 | Popular Tags |