KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sax > helpers > AttributesImpl


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package sax.helpers;
18                     
19 import org.xml.sax.Attributes JavaDoc;
20
21 /**
22  * An Attributes implementation that can perform more operations
23  * than the attribute list helper supplied with the standard SAX2
24  * distribution.
25  */

26 public class AttributesImpl
27     implements Attributes JavaDoc {
28
29     //
30
// Data
31
//
32

33     /** Head node. */
34     private ListNode head;
35
36     /** Tail node. */
37     private ListNode tail;
38
39     /** Length. */
40     private int length;
41
42     //
43
// Attributes methods
44
//
45

46     /** Returns the number of attributes. */
47     public int getLength() {
48         return length;
49     }
50
51     /** Returns the index of the specified attribute. */
52     public int getIndex(String JavaDoc raw) {
53         ListNode place = head;
54         int index = 0;
55         while (place != null) {
56             if (place.raw.equals(raw)) {
57                 return index;
58             }
59             index++;
60             place = place.next;
61         }
62         return -1;
63     }
64
65     /** Returns the index of the specified attribute. */
66     public int getIndex(String JavaDoc uri, String JavaDoc local) {
67         ListNode place = head;
68         int index = 0;
69         while (place != null) {
70             if (place.uri.equals(uri) && place.local.equals(local)) {
71                 return index;
72             }
73             index++;
74             place = place.next;
75         }
76         return -1;
77     }
78
79     /** Returns the attribute URI by index. */
80     public String JavaDoc getURI(int index) {
81
82         ListNode node = getListNodeAt(index);
83         return node != null ? node.uri : null;
84
85     } // getURI(int):String
86

87     /** Returns the attribute local name by index. */
88     public String JavaDoc getLocalName(int index) {
89
90         ListNode node = getListNodeAt(index);
91         return node != null ? node.local : null;
92
93     } // getLocalName(int):String
94

95     /** Returns the attribute raw name by index. */
96     public String JavaDoc getQName(int index) {
97
98         ListNode node = getListNodeAt(index);
99         return node != null ? node.raw : null;
100
101     } // getQName(int):String
102

103     /** Returns the attribute type by index. */
104     public String JavaDoc getType(int index) {
105
106         ListNode node = getListNodeAt(index);
107         return (node != null) ? node.type : null;
108
109     } // getType(int):String
110

111     /** Returns the attribute type by uri and local. */
112     public String JavaDoc getType(String JavaDoc uri, String JavaDoc local) {
113
114         ListNode node = getListNode(uri, local);
115         return (node != null) ? node.type : null;
116
117     } // getType(String,String):String
118

119     /** Returns the attribute type by raw name. */
120     public String JavaDoc getType(String JavaDoc raw) {
121
122         ListNode node = getListNode(raw);
123         return (node != null) ? node.type : null;
124
125     } // getType(String):String
126

127     /** Returns the attribute value by index. */
128     public String JavaDoc getValue(int index) {
129
130         ListNode node = getListNodeAt(index);
131         return (node != null) ? node.value : null;
132
133     } // getType(int):String
134

135     /** Returns the attribute value by uri and local. */
136     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc local) {
137
138         ListNode node = getListNode(uri, local);
139         return (node != null) ? node.value : null;
140
141     } // getType(String):String
142

143     /** Returns the attribute value by raw name. */
144     public String JavaDoc getValue(String JavaDoc raw) {
145
146         ListNode node = getListNode(raw);
147         return (node != null) ? node.value : null;
148
149     } // getType(String):String
150

151     //
152
// Public methods
153
//
154

155     /** Adds an attribute. */
156     public void addAttribute(String JavaDoc raw, String JavaDoc type, String JavaDoc value) {
157         addAttribute(null, null, raw, type, value);
158     }
159
160     /** Adds an attribute. */
161     public void addAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc raw,
162                              String JavaDoc type, String JavaDoc value) {
163
164         ListNode node = new ListNode(uri, local, raw, type, value);
165         if (length == 0) {
166             head = node;
167         }
168         else {
169             tail.next = node;
170         }
171         tail = node;
172         length++;
173
174     } // addAttribute(String,StringString,String,String)
175

176     /** Inserts an attribute. */
177     public void insertAttributeAt(int index,
178                                   String JavaDoc raw, String JavaDoc type, String JavaDoc value) {
179         insertAttributeAt(index, null, null, raw, type, value);
180     }
181
182     /** Inserts an attribute. */
183     public void insertAttributeAt(int index,
184                                   String JavaDoc uri, String JavaDoc local, String JavaDoc raw,
185                                   String JavaDoc type, String JavaDoc value) {
186
187         // if list is empty, add attribute
188
if (length == 0 || index >= length) {
189             addAttribute(uri, local, raw, type, value);
190             return;
191         }
192
193         // insert at beginning of list
194
ListNode node = new ListNode(uri, local, raw, type, value);
195         if (index < 1) {
196             node.next = head;
197             head = node;
198         }
199         else {
200             ListNode prev = getListNodeAt(index - 1);
201             node.next = prev.next;
202             prev.next = node;
203         }
204         length++;
205
206     } // insertAttributeAt(int,String,String,String,String,String)
207

208     /** Removes an attribute. */
209     public void removeAttributeAt(int index) {
210
211         if (length == 0) {
212             return;
213         }
214
215         if (index == 0) {
216             head = head.next;
217             if (head == null) {
218                 tail = null;
219             }
220             length--;
221         }
222         else {
223             ListNode prev = getListNodeAt(index - 1);
224             ListNode node = getListNodeAt(index);
225             if (node != null) {
226                 prev.next = node.next;
227                 if (node == tail) {
228                     tail = prev;
229                 }
230                 length--;
231             }
232         }
233
234     } // removeAttributeAt(int)
235

236     /** Removes the specified attribute. */
237     public void removeAttribute(String JavaDoc raw) {
238         removeAttributeAt(getIndex(raw));
239     }
240
241     /** Removes the specified attribute. */
242     public void removeAttribute(String JavaDoc uri, String JavaDoc local) {
243         removeAttributeAt(getIndex(uri, local));
244     }
245
246     //
247
// Private methods
248
//
249

250     /** Returns the node at the specified index. */
251     private ListNode getListNodeAt(int i) {
252
253         for (ListNode place = head; place != null; place = place.next) {
254             if (--i == -1) {
255                 return place;
256             }
257         }
258
259         return null;
260
261     } // getListNodeAt(int):ListNode
262

263     /** Returns the first node with the specified uri and local. */
264     public ListNode getListNode(String JavaDoc uri, String JavaDoc local) {
265
266         if (uri != null && local != null) {
267             ListNode place = head;
268             while (place != null) {
269                 if (place.uri != null && place.local != null &&
270                     place.uri.equals(uri) && place.local.equals(local)) {
271                     return place;
272                 }
273                 place = place.next;
274             }
275         }
276         return null;
277
278     } // getListNode(String,String):ListNode
279

280     /** Returns the first node with the specified raw name. */
281     private ListNode getListNode(String JavaDoc raw) {
282
283         if (raw != null) {
284             for (ListNode place = head; place != null; place = place.next) {
285                 if (place.raw != null && place.raw.equals(raw)) {
286                     return place;
287                 }
288             }
289         }
290
291         return null;
292
293     } // getListNode(String):ListNode
294

295     //
296
// Object methods
297
//
298

299     /** Returns a string representation of this object. */
300     public String JavaDoc toString() {
301         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
302
303         str.append('[');
304         str.append("len=");
305         str.append(length);
306         str.append(", {");
307         for (ListNode place = head; place != null; place = place.next) {
308             str.append(place.toString());
309             if (place.next != null) {
310                 str.append(", ");
311             }
312         }
313         str.append("}]");
314
315         return str.toString();
316
317     } // toString():String
318

319     //
320
// Classes
321
//
322

323     /**
324      * An attribute node.
325      */

326     static class ListNode {
327
328         //
329
// Data
330
//
331

332         /** Attribute uri. */
333         public String JavaDoc uri;
334
335         /** Attribute local. */
336         public String JavaDoc local;
337
338         /** Attribute raw. */
339         public String JavaDoc raw;
340
341         /** Attribute type. */
342         public String JavaDoc type;
343
344         /** Attribute value. */
345         public String JavaDoc value;
346
347         /** Next node. */
348         public ListNode next;
349
350         //
351
// Constructors
352
//
353

354         /** Constructs a list node. */
355         public ListNode(String JavaDoc uri, String JavaDoc local, String JavaDoc raw,
356                         String JavaDoc type, String JavaDoc value) {
357
358             this.uri = uri;
359             this.local = local;
360             this.raw = raw;
361             this.type = type;
362             this.value = value;
363
364         } // <init>(String,String,String,String,String)
365

366         //
367
// Object methods
368
//
369

370         /** Returns string representation of this object. */
371         public String JavaDoc toString() {
372             return raw != null ? raw : local;
373         }
374
375     } // class ListNode
376

377 } // class AttributesImpl
378
Popular Tags