KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > filters > TagNameFilter


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2003 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/filters/TagNameFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.4 $
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.filters;
28
29 import java.util.Locale JavaDoc;
30
31 import org.htmlparser.Node;
32 import org.htmlparser.NodeFilter;
33 import org.htmlparser.Tag;
34
35 /**
36  * This class accepts all tags matching the tag name.
37  */

38 public class TagNameFilter
39     implements
40         NodeFilter
41 {
42     /**
43      * The tag name to match.
44      */

45     protected String JavaDoc mName;
46
47     /**
48      * Creates a new instance of TagNameFilter.
49      * With no name, this would always return <code>false</code>
50      * from {@link #accept}.
51      */

52     public TagNameFilter ()
53     {
54         this ("");
55     }
56
57     /**
58      * Creates a new instance of TagNameFilter that accepts tags with the given name.
59      * @param name The tag name to match.
60      */

61     public TagNameFilter (String JavaDoc name)
62     {
63         mName = name.toUpperCase (Locale.ENGLISH);
64     }
65
66     /**
67      * Get the tag name.
68      * @return Returns the name of acceptable tags.
69      */

70     public String JavaDoc getName ()
71     {
72         return (mName);
73     }
74
75     /**
76      * Set the tag name.
77      * @param name The name of the tag to accept.
78      */

79     public void setName (String JavaDoc name)
80     {
81         mName = name;
82     }
83
84     /**
85      * Accept nodes that are tags and have a matching tag name.
86      * This discards non-tag nodes and end tags.
87      * The end tags are available on the enclosing non-end tag.
88      * @param node The node to check.
89      */

90     public boolean accept (Node node)
91     {
92         return ((node instanceof Tag) &&
93                 !((Tag)node).isEndTag () &&
94                 ((Tag)node).getTagName ().equals (mName));
95     }
96 }
97
Popular Tags