/ org.htmlparser / src / org / htmlparser / visitors / TagFindingVisitor.java
TagFindingVisitor.java
 1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
 2  // http://sourceforge.org/projects/htmlparser
 3  // Copyright (C) 2004 Joshua Kerievsky
 4  //
 5  // Revision Control Information
 6  //
 7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/visitors/TagFindingVisitor.java,v $
 8  // $Author: derrickoswald $
 9  // $Date: 2004/05/24 00:38:19 $
10  // $Revision: 1.42 $
11  //
12  // This library is free software; you can redistribute it and/or
13  // modify it under the terms of the GNU Lesser General Public
14  // License as published by the Free Software Foundation; either
15  // version 2.1 of the License, or (at your option) any later version.
16  //
17  // This library is distributed in the hope that it will be useful,
18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  // Lesser General Public License for more details.
21  //
22  // You should have received a copy of the GNU Lesser General Public
23  // License along with this library; if not, write to the Free Software
24  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  //
26  
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  }