KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > sax > Attributes


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Derrick Oswald
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/sax/Attributes.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/14 01:58:02 $
10
// $Revision: 1.1 $
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.sax;
28
29 import java.util.Vector JavaDoc;
30
31 import org.htmlparser.Attribute;
32 import org.htmlparser.Tag;
33 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
34
35 /**
36  * Provides access to the tag attributes.
37  */

38 public class Attributes
39     implements
40         org.xml.sax.Attributes JavaDoc
41 {
42     /**
43      * The tag from which attributes are exposed.
44      */

45     protected Tag mTag;
46     
47     /**
48      * The utility class that converts namespaces.
49      */

50     protected NamespaceSupport JavaDoc mSupport;
51
52     /**
53      * Elements of the qname.
54      * Allocated once for all uses of {@link #mSupport}.
55      */

56     protected String JavaDoc[] mParts;
57
58     /**
59      * Create an attibute access object.
60      * @param tag The tag to expose.
61      * @param support The namespace converter.
62      * @param parts The elements of the qualified name.
63      */

64     public Attributes (Tag tag, NamespaceSupport JavaDoc support, String JavaDoc[] parts)
65     {
66         mTag = tag;
67         mSupport = support;
68         mParts = parts;
69     }
70     
71
72     ////////////////////////////////////////////////////////////////////
73
// Indexed access.
74
////////////////////////////////////////////////////////////////////
75

76
77     /**
78      * Return the number of attributes in the list.
79      *
80      * <p>Once you know the number of attributes, you can iterate
81      * through the list.</p>
82      *
83      * @return The number of attributes in the list.
84      * @see #getURI(int)
85      * @see #getLocalName(int)
86      * @see #getQName(int)
87      * @see #getType(int)
88      * @see #getValue(int)
89      */

90     public int getLength ()
91     {
92         return (mTag.getAttributesEx ().size () - 1);
93     }
94
95
96     /**
97      * Look up an attribute's Namespace URI by index.
98      *
99      * @param index The attribute index (zero-based).
100      * @return The Namespace URI, or the empty string if none
101      * is available, or null if the index is out of
102      * range.
103      * @see #getLength
104      */

105     public String JavaDoc getURI (int index)
106     {
107         mSupport.processName (getQName (index), mParts, true);
108         return (mParts[0]);
109     }
110
111
112     /**
113      * Look up an attribute's local name by index.
114      *
115      * @param index The attribute index (zero-based).
116      * @return The local name, or the empty string if Namespace
117      * processing is not being performed, or null
118      * if the index is out of range.
119      * @see #getLength
120      */

121     public String JavaDoc getLocalName (int index)
122     {
123         mSupport.processName (getQName (index), mParts, true);
124         return (mParts[1]);
125     }
126
127
128     /**
129      * Look up an attribute's XML qualified (prefixed) name by index.
130      *
131      * @param index The attribute index (zero-based).
132      * @return The XML qualified name, or the empty string
133      * if none is available, or null if the index
134      * is out of range.
135      * @see #getLength
136      */

137     public String JavaDoc getQName (int index)
138     {
139         Attribute attribute;
140         String JavaDoc ret;
141         
142         attribute = (Attribute)(mTag.getAttributesEx ().elementAt (index + 1));
143         if (attribute.isWhitespace ())
144             ret = "#text";
145         else
146             ret = attribute.getName ();
147         
148         return (ret);
149     }
150
151     /**
152      * Look up an attribute's type by index.
153      *
154      * <p>The attribute type is one of the strings "CDATA", "ID",
155      * "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
156      * or "NOTATION" (always in upper case).</p>
157      *
158      * <p>If the parser has not read a declaration for the attribute,
159      * or if the parser does not report attribute types, then it must
160      * return the value "CDATA" as stated in the XML 1.0 Recommendation
161      * (clause 3.3.3, "Attribute-Value Normalization").</p>
162      *
163      * <p>For an enumerated attribute that is not a notation, the
164      * parser will report the type as "NMTOKEN".</p>
165      *
166      * @param index The attribute index (zero-based).
167      * @return The attribute's type as a string, or null if the
168      * index is out of range.
169      * @see #getLength
170      */

171     public String JavaDoc getType (int index)
172     {
173         return ("CDATA");
174     }
175
176
177     /**
178      * Look up an attribute's value by index.
179      *
180      * <p>If the attribute value is a list of tokens (IDREFS,
181      * ENTITIES, or NMTOKENS), the tokens will be concatenated
182      * into a single string with each token separated by a
183      * single space.</p>
184      *
185      * @param index The attribute index (zero-based).
186      * @return The attribute's value as a string, or null if the
187      * index is out of range.
188      * @see #getLength
189      */

190     public String JavaDoc getValue (int index)
191     {
192         Attribute attribute;
193         String JavaDoc ret;
194         
195         attribute = (Attribute)(mTag.getAttributesEx ().elementAt (index + 1));
196         ret = attribute.getValue ();
197         if (null == ret)
198             ret = "";
199
200         return (ret);
201     }
202
203
204     ////////////////////////////////////////////////////////////////////
205
// Name-based query.
206
////////////////////////////////////////////////////////////////////
207

208
209     /**
210      * Look up the index of an attribute by Namespace name.
211      *
212      * @param uri The Namespace URI, or the empty string if
213      * the name has no Namespace URI.
214      * @param localName The attribute's local name.
215      * @return The index of the attribute, or -1 if it does not
216      * appear in the list.
217      */

218     public int getIndex (String JavaDoc uri, String JavaDoc localName)
219     {
220         Vector JavaDoc attributes;
221         int size;
222         Attribute attribute;
223         String JavaDoc string;
224         int ret;
225
226         ret = -1;
227
228         attributes = mTag.getAttributesEx ();
229         if (null != attributes)
230         {
231             size = attributes.size ();
232             for (int i = 1; i < size; i++)
233             {
234                 attribute = (Attribute)attributes.elementAt (i);
235                 string = attribute.getName ();
236                 if (null != string) // not whitespace
237
{
238                     mSupport.processName (string, mParts, true);
239                     if ( uri.equals (mParts[0])
240                         & localName.equalsIgnoreCase (mParts[1]))
241                     {
242                         ret = i;
243                         i = size; // exit fast
244
}
245                 }
246             }
247         }
248
249         return (ret);
250     }
251
252
253     /**
254      * Look up the index of an attribute by XML qualified (prefixed) name.
255      *
256      * @param qName The qualified (prefixed) name.
257      * @return The index of the attribute, or -1 if it does not
258      * appear in the list.
259      */

260     public int getIndex (String JavaDoc qName)
261     {
262         mSupport.processName (qName, mParts, true);
263         return (getIndex (mParts[0], mParts[1]));
264     }
265
266
267     /**
268      * Look up an attribute's type by Namespace name.
269      *
270      * <p>See {@link #getType(int) getType(int)} for a description
271      * of the possible types.</p>
272      *
273      * @param uri The Namespace URI, or the empty String if the
274      * name has no Namespace URI.
275      * @param localName The local name of the attribute.
276      * @return The attribute type as a string, or null if the
277      * attribute is not in the list or if Namespace
278      * processing is not being performed.
279      */

280     public String JavaDoc getType (String JavaDoc uri, String JavaDoc localName)
281     {
282         return (null);
283     }
284
285
286     /**
287      * Look up an attribute's type by XML qualified (prefixed) name.
288      *
289      * <p>See {@link #getType(int) getType(int)} for a description
290      * of the possible types.</p>
291      *
292      * @param qName The XML qualified name.
293      * @return The attribute type as a string, or null if the
294      * attribute is not in the list or if qualified names
295      * are not available.
296      */

297     public String JavaDoc getType (String JavaDoc qName)
298     {
299         return (null);
300     }
301
302
303     /**
304      * Look up an attribute's value by Namespace name.
305      *
306      * <p>See {@link #getValue(int) getValue(int)} for a description
307      * of the possible values.</p>
308      *
309      * @param uri The Namespace URI, or the empty String if the
310      * name has no Namespace URI.
311      * @param localName The local name of the attribute.
312      * @return The attribute value as a string, or null if the
313      * attribute is not in the list.
314      */

315     public String JavaDoc getValue (String JavaDoc uri, String JavaDoc localName)
316     {
317         return (mTag.getAttribute (localName));
318     }
319
320
321     /**
322      * Look up an attribute's value by XML qualified (prefixed) name.
323      *
324      * <p>See {@link #getValue(int) getValue(int)} for a description
325      * of the possible values.</p>
326      *
327      * @param qName The XML qualified name.
328      * @return The attribute value as a string, or null if the
329      * attribute is not in the list or if qualified names
330      * are not available.
331      */

332     public String JavaDoc getValue (String JavaDoc qName)
333     {
334         mSupport.processName (qName, mParts, true);
335         return (getValue (mParts[0], mParts[1]));
336     }
337 }
338
Popular Tags