KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > text > sax > AttributeList


1 package com.quadcap.text.sax;
2
3 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 /**
42  * AttributeList implementation; uses arrays of Strings internally.
43  *
44  * @author Stan Bailes
45  */

46 public class AttributeList implements org.xml.sax.AttributeList JavaDoc {
47     int length = 0;
48     String JavaDoc[] name = new String JavaDoc[8];
49     String JavaDoc[] value = new String JavaDoc[8];
50     String JavaDoc[] type = new String JavaDoc[8];
51
52     /**
53      * Return the number of attributes in this list.
54      *
55      * <p>The SAX parser may provide attributes in any
56      * arbitrary order, regardless of the order in which they were
57      * declared or specified. The number of attributes may be
58      * zero.</p>
59      *
60      * @return The number of attributes in the list.
61      */

62     public final int getLength() {
63         return length;
64     }
65
66
67     /**
68      * Return the name of an attribute in this list (by position).
69      *
70      * <p>The names must be unique: the SAX parser shall not include the
71      * same attribute twice. Attributes without values (those declared
72      * #IMPLIED without a value specified in the start tag) will be
73      * omitted from the list.</p>
74      *
75      * <p>If the attribute name has a namespace prefix, the prefix
76      * will still be attached.</p>
77      *
78      * @param i The index of the attribute in the list (starting at 0).
79      * @return The name of the indexed attribute, or null
80      * if the index is out of range.
81      * @see #getLength
82      */

83     public final String JavaDoc getName(int i) {
84         try {
85             return name[i];
86         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
87             return null;
88         }
89     }
90
91
92     /**
93      * Return the type of an attribute in the list (by position).
94      *
95      * <p>The attribute type is one of the strings "CDATA", "ID",
96      * "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
97      * or "NOTATION" (always in upper case).</p>
98      *
99      * <p>If the parser has not read a declaration for the attribute,
100      * or if the parser does not report attribute types, then it must
101      * return the value "CDATA" as stated in the XML 1.0 Recommentation
102      * (clause 3.3.3, "Attribute-Value Normalization").</p>
103      *
104      * <p>For an enumerated attribute that is not a notation, the
105      * parser will report the type as "NMTOKEN".</p>
106      *
107      * @param i The index of the attribute in the list (starting at 0).
108      * @return The attribute type as a string, or
109      * null if the index is out of range.
110      * @see #getLength
111      * @see #getType(java.lang.String)
112      */

113     public final String JavaDoc getType(int i) {
114         try {
115             return type[i];
116         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
117             return null;
118         }
119     }
120
121     /**
122      * Return the value of an attribute in the list (by position).
123      *
124      * <p>If the attribute value is a list of tokens (IDREFS,
125      * ENTITIES, or NMTOKENS), the tokens will be concatenated
126      * into a single string separated by whitespace.</p>
127      *
128      * @param i The index of the attribute in the list (starting at 0).
129      * @return The attribute value as a string, or
130      * null if the index is out of range.
131      * @see #getLength
132      * @see #getValue(java.lang.String)
133      */

134     public final String JavaDoc getValue (int i) {
135         try {
136             return value[i];
137         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
138             return null;
139         }
140     }
141
142
143
144     /**
145      * Return the type of an attribute in the list (by name).
146      *
147      * <p>The return value is the same as the return value for
148      * getType(int).</p>
149      *
150      * <p>If the attribute name has a namespace prefix in the document,
151      * the application must include the prefix here.</p>
152      *
153      * @param name The name of the attribute.
154      * @return The attribute type as a string, or null if no
155      * such attribute exists.
156      * @see #getType(int)
157      */

158     public final String JavaDoc getType(String JavaDoc name) {
159         return getType(getAttribute(name));
160     }
161
162     /**
163      * Return the value of an attribute in the list (by name).
164      *
165      * <p>The return value is the same as the return value for
166      * getValue(int).</p>
167      *
168      * <p>If the attribute name has a namespace prefix in the document,
169      * the application must include the prefix here.</p>
170      *
171      * @param i The index of the attribute in the list.
172      * @return The attribute value as a string, or null if
173      * no such attribute exists.
174      * @see #getValue(int)
175      */

176     public final String JavaDoc getValue(String JavaDoc n) {
177         return getValue(getAttribute(n));
178     }
179
180     final int getAttribute(String JavaDoc n) {
181         for (int i = 0; i < length; i++) {
182             if (name[i].equals(n)) return i;
183         }
184         return -1;
185     }
186
187     final String JavaDoc[] resize(String JavaDoc[] v, int len) {
188         String JavaDoc[] n = new String JavaDoc[len];
189         System.arraycopy(v, 0, n, 0, v.length);
190         return n;
191     }
192
193     final void addAttribute(String JavaDoc n, String JavaDoc t, String JavaDoc v) {
194         if (length >= name.length) {
195             name = resize(name, length + (length >> 2) + 4);
196             type = resize(type, length + (length >> 2) + 4);
197             value = resize(value, length + (length >> 2) + 4);
198         }
199         name[length] = n;
200         type[length] = t;
201         value[length] = v;
202         length++;
203     }
204
205     final void clear() {
206         length = 0;
207     }
208
209     public static AttributeList copy(org.xml.sax.AttributeList JavaDoc a) {
210         AttributeList c = new AttributeList();
211         for (int i = 0; i < a.getLength(); i++) {
212             c.addAttribute(a.getName(i), a.getType(i), a.getValue(i));
213         }
214         return c;
215     }
216
217     public static String JavaDoc toString(org.xml.sax.AttributeList JavaDoc attributes) {
218     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
219     if (attributes != null) {
220             for (int i = 0; i < attributes.getLength(); i++) {
221                 sb.append(' ');
222                 sb.append(attributes.getName(i));
223                 sb.append("=\"");
224                 sb.append(attributes.getValue(i));
225                 sb.append("\"");
226             }
227         }
228         return sb.toString();
229     }
230
231     public String JavaDoc toString() {
232         return toString(this);
233     }
234 }
235
Popular Tags