KickJava   Java API By Example, From Geeks To Geeks.

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


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/HasAttributeFilter.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/02/13 20:36:00 $
10
// $Revision: 1.5 $
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.Attribute;
32 import org.htmlparser.Node;
33 import org.htmlparser.NodeFilter;
34 import org.htmlparser.Tag;
35
36 /**
37  * This class accepts all tags that have a certain attribute, and optionally, with a certain value.
38  */

39 public class HasAttributeFilter implements NodeFilter
40 {
41     /**
42      * The attribute to check for.
43      */

44     protected String JavaDoc mAttribute;
45
46     /**
47      * The value to check for.
48      */

49     protected String JavaDoc mValue;
50
51     /**
52      * Creates a new instance of HasAttributeFilter.
53      * With no attribute name, this would always return <code>false</code>
54      * from {@link #accept}.
55      */

56     public HasAttributeFilter ()
57     {
58         this ("", null);
59     }
60
61     /**
62      * Creates a new instance of HasAttributeFilter that accepts tags with the given attribute.
63      * @param attribute The attribute to search for.
64      */

65     public HasAttributeFilter (String JavaDoc attribute)
66     {
67         this (attribute, null);
68     }
69
70     /**
71      * Creates a new instance of HasAttributeFilter that accepts tags with the given attribute.
72      * @param attribute The attribute to search for.
73      * @param value The value that must be matched, or null if any value will match.
74      */

75     public HasAttributeFilter (String JavaDoc attribute, String JavaDoc value)
76     {
77         mAttribute = attribute.toUpperCase (Locale.ENGLISH);
78         mValue = value;
79     }
80
81     /**
82      * Get the attribute name.
83      * @return Returns the name of the attribute that is acceptable.
84      */

85     public String JavaDoc getAttributeName ()
86     {
87         return (mAttribute);
88     }
89
90     /**
91      * Set the attribute name.
92      * @param name The name of the attribute to accept.
93      */

94     public void setAttributeName (String JavaDoc name)
95     {
96         mAttribute = name;
97     }
98
99     /**
100      * Get the attribute value.
101      * @return Returns the value of the attribute that is acceptable.
102      */

103     public String JavaDoc getAttributeValue ()
104     {
105         return (mValue);
106     }
107
108     /**
109      * Set the attribute value.
110      * @param value The value of the attribute to accept.
111      * If <code>null</code>, any tag with the attribute, no matter it's value is acceptable.
112      */

113     public void setAttributeValue (String JavaDoc value)
114     {
115         mValue = value;
116     }
117
118     /**
119      * Accept tags with a certain attribute.
120      * @param node The node to check.
121      */

122     public boolean accept (Node node)
123     {
124         Tag tag;
125         Attribute attribute;
126         boolean ret;
127
128         ret = false;
129         if (node instanceof Tag)
130         {
131             tag = (Tag)node;
132             attribute = tag.getAttributeEx (mAttribute);
133             ret = null != attribute;
134             if (ret && (null != mValue))
135                 ret = mValue.equals (attribute.getValue ());
136         }
137
138         return (ret);
139     }
140 }
141
Popular Tags