1 27 package org.htmlparser.visitors; 28 29 import org.htmlparser.Node; 30 import org.htmlparser.Tag; 31 import org.htmlparser.util.NodeList; 32 33 public class TagFindingVisitor extends NodeVisitor { 34 private String [] tagsToBeFound; 35 private int count []; 36 private int endTagCount []; 37 private NodeList [] tags; 38 private NodeList [] endTags; 39 private boolean endTagCheck; 40 41 public TagFindingVisitor(String [] tagsToBeFound) { 42 this(tagsToBeFound,false); 43 } 44 45 public TagFindingVisitor(String [] tagsToBeFound, boolean endTagCheck) { 46 this.tagsToBeFound = tagsToBeFound; 47 this.tags = new NodeList[tagsToBeFound.length]; 48 if (endTagCheck) { 49 endTags = new NodeList[tagsToBeFound.length]; 50 endTagCount = new int[tagsToBeFound.length]; 51 } 52 for (int i=0;i<tagsToBeFound.length;i++) { 53 tags[i] = new NodeList(); 54 if (endTagCheck) 55 endTags[i] = new NodeList(); 56 } 57 this.count = new int[tagsToBeFound.length]; 58 this.endTagCheck = endTagCheck; 59 } 60 61 public int getTagCount(int index) { 62 return count[index]; 63 } 64 65 public void visitTag(Tag tag) 66 { 67 for (int i=0;i<tagsToBeFound.length;i++) 68 if (tag.getTagName().equalsIgnoreCase(tagsToBeFound[i])) { 69 count[i]++; 70 tags[i].add(tag); 71 } 72 } 73 74 public void visitEndTag(Tag tag) 75 { 76 if (!endTagCheck) return; 77 for (int i=0;i<tagsToBeFound.length;i++) 78 if (tag.getTagName().equalsIgnoreCase(tagsToBeFound[i])) 79 { 80 endTagCount[i]++; 81 endTags[i].add(tag); 82 } 83 } 84 85 public Node [] getTags(int index) { 86 return tags[index].toNodeArray(); 87 } 88 89 public int getEndTagCount(int index) { 90 return endTagCount[index]; 91 } 92 93 } 94 | Popular Tags |