KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sax > helpers > AttributeListImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package sax.helpers;
59                     
60 import org.xml.sax.AttributeList JavaDoc;
61
62 /**
63  * An AttributeList implementation that can perform more operations
64  * than the attribute list helper supplied with the standard SAX
65  * distribution.
66  */

67 public class AttributeListImpl
68     implements AttributeList JavaDoc {
69
70     //
71
// Data
72
//
73

74     /** Head node. */
75     private ListNode head;
76
77     /** Tail node. */
78     private ListNode tail;
79
80     /** Length. */
81     private int length;
82
83     //
84
// AttributeList methods
85
//
86

87     /** Returns the number of attributes. */
88     public int getLength() {
89         return length;
90     }
91
92     /** Returns the attribute name by index. */
93     public String JavaDoc getName(int index) {
94
95         ListNode node = getNodeAt(index);
96         return (node != null) ? node.name : null;
97
98     } // getName(int):String
99

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

108     /** Returns the attribute value by index. */
109     public String JavaDoc getValue(int index) {
110
111         ListNode node = getNodeAt(index);
112         return (node != null) ? node.value : null;
113
114     } // getType(int):String
115

116     /** Returns the attribute type by name. */
117     public String JavaDoc getType(String JavaDoc name) {
118
119         ListNode node = getNodeAt(name);
120         return (node != null) ? node.type : null;
121
122     } // getType(int):String
123

124     /** Returns the attribute value by name. */
125     public String JavaDoc getValue(String JavaDoc name) {
126
127         ListNode node = getNodeAt(name);
128         return (node != null) ? node.value : null;
129
130     } // getType(int):String
131

132     //
133
// Public methods
134
//
135

136     /** Adds an attribute. */
137     public void addAttribute(String JavaDoc name, String JavaDoc type, String JavaDoc value) {
138
139         ListNode node = new ListNode(name, type, value);
140         if (length == 0) {
141             head = node;
142         }
143         else {
144             tail.next = node;
145         }
146         tail = node;
147         length++;
148
149     } // addAttribute(String,String,String)
150

151     /** Inserts an attribute. */
152     public void insertAttributeAt(int index,
153                                   String JavaDoc name, String JavaDoc type, String JavaDoc value) {
154
155         // if list is empty, add attribute
156
if (length == 0 || index >= length) {
157             addAttribute(name, type, value);
158             return;
159         }
160
161         // insert at beginning of list
162
ListNode node = new ListNode(name, type, value);
163         if (index < 1) {
164             node.next = head;
165             head = node;
166         }
167         else {
168             ListNode prev = getNodeAt(index - 1);
169             node.next = prev.next;
170             prev.next = node;
171         }
172         length++;
173
174     } // addAttribute(String,String,String)
175

176     /** Removes an attribute. */
177     public void removeAttributeAt(int index) {
178
179         if (length == 0) {
180             return;
181         }
182
183         if (index == 0) {
184             head = head.next;
185             if (head == null) {
186                 tail = null;
187             }
188             length--;
189         }
190         else {
191             ListNode prev = getNodeAt(index - 1);
192             ListNode node = getNodeAt(index);
193             if (node != null) {
194                 prev.next = node.next;
195                 if (node == tail) {
196                     tail = prev;
197                 }
198                 length--;
199             }
200         }
201
202     } // removeAttributeAt(int)
203

204     //
205
// Private methods
206
//
207

208     /** Returns the node at the specified index. */
209     private ListNode getNodeAt(int i) {
210
211         for (ListNode place = head; place != null; place = place.next) {
212             if (--i == -1) {
213                 return place;
214             }
215         }
216
217         return null;
218
219     } // getNodeAt(int):ListNode
220

221     /** Returns the first node with the specified name. */
222     private ListNode getNodeAt(String JavaDoc name) {
223
224         if (name != null) {
225             for (ListNode place = head; place != null; place = place.next) {
226                 if (place.name.equals(name)) {
227                     return place;
228                 }
229             }
230         }
231
232         return null;
233
234     } // getNodeAt(int):ListNode
235

236     //
237
// Object methods
238
//
239

240     /** Returns a string representation of this object. */
241     public String JavaDoc toString() {
242         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
243
244         str.append('[');
245         str.append("len=");
246         str.append(length);
247         str.append(", {");
248         for (ListNode place = head; place != null; place = place.next) {
249             str.append(place.name);
250             if (place.next != null) {
251                 str.append(", ");
252             }
253         }
254         str.append("}]");
255
256         return str.toString();
257
258     } // toString():String
259

260     //
261
// Classes
262
//
263

264     /**
265      * An attribute node.
266      */

267     static class ListNode {
268
269         //
270
// Data
271
//
272

273         /** Attribute name. */
274         public String JavaDoc name;
275
276         /** Attribute type. */
277         public String JavaDoc type;
278
279         /** Attribute value. */
280         public String JavaDoc value;
281
282         /** Next node. */
283         public ListNode next;
284
285         //
286
// Constructors
287
//
288

289         /** Default constructor. */
290         public ListNode(String JavaDoc name, String JavaDoc type, String JavaDoc value) {
291             this.name = name;
292             this.type = type;
293             this.value = value;
294         }
295
296     } // class ListNode
297

298 } // class AttributeListImpl
299
Popular Tags