KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > compare > AttributesImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.compare;
12
13 import org.xml.sax.Attributes JavaDoc;
14
15 /**
16  * An Attributes implementation that can perform more operations
17  * than the attribute list helper supplied with the standard SAX2
18  * distribution.
19  */

20 public class AttributesImpl implements Attributes JavaDoc {
21
22     /** Head node. */
23     private ListNode fHead;
24
25     /** Tail node. */
26     private ListNode fTail;
27
28     /** Length. */
29     private int fLength;
30
31
32     /* Returns the number of attributes. */
33     public int getLength() {
34         return fLength;
35     }
36
37     /* Returns the index of the specified attribute. */
38     public int getIndex(String JavaDoc raw) {
39         ListNode place= fHead;
40         int index= 0;
41         while (place != null) {
42             if (place.raw.equals(raw)) {
43                 return index;
44             }
45             index++;
46             place= place.next;
47         }
48         return -1;
49     }
50
51     /* Returns the index of the specified attribute. */
52     public int getIndex(String JavaDoc uri, String JavaDoc local) {
53         ListNode place= fHead;
54         int index= 0;
55         while (place != null) {
56             if (place.uri.equals(uri) && place.local.equals(local)) {
57                 return index;
58             }
59             index++;
60             place= place.next;
61         }
62         return -1;
63     }
64
65     /* Returns the attribute URI by index. */
66     public String JavaDoc getURI(int index) {
67
68         ListNode node= getListNodeAt(index);
69         return node != null ? node.uri : null;
70     }
71
72     /* Returns the attribute local name by index. */
73     public String JavaDoc getLocalName(int index) {
74
75         ListNode node= getListNodeAt(index);
76         return node != null ? node.local : null;
77     }
78
79     /* Returns the attribute raw name by index. */
80     public String JavaDoc getQName(int index) {
81
82         ListNode node= getListNodeAt(index);
83         return node != null ? node.raw : null;
84
85     }
86
87     /* Returns the attribute type by index. */
88     public String JavaDoc getType(int index) {
89
90         ListNode node= getListNodeAt(index);
91         return (node != null) ? node.type : null;
92     }
93
94     /* Returns the attribute type by uri and local. */
95     public String JavaDoc getType(String JavaDoc uri, String JavaDoc local) {
96
97         ListNode node= getListNode(uri, local);
98         return (node != null) ? node.type : null;
99
100     }
101
102     /* Returns the attribute type by raw name. */
103     public String JavaDoc getType(String JavaDoc raw) {
104
105         ListNode node= getListNode(raw);
106         return (node != null) ? node.type : null;
107     }
108
109     /* Returns the attribute value by index. */
110     public String JavaDoc getValue(int index) {
111
112         ListNode node= getListNodeAt(index);
113         return (node != null) ? node.value : null;
114     }
115
116     /* Returns the attribute value by uri and local. */
117     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc local) {
118
119         ListNode node= getListNode(uri, local);
120         return (node != null) ? node.value : null;
121     }
122
123     /* Returns the attribute value by raw name. */
124     public String JavaDoc getValue(String JavaDoc raw) {
125
126         ListNode node= getListNode(raw);
127         return (node != null) ? node.value : null;
128     }
129
130     /* Adds an attribute. */
131     public void addAttribute(String JavaDoc raw, String JavaDoc type, String JavaDoc value) {
132         addAttribute(null, null, raw, type, value);
133     }
134
135     /* Adds an attribute. */
136     public void addAttribute(
137         String JavaDoc uri,
138         String JavaDoc local,
139         String JavaDoc raw,
140         String JavaDoc type,
141         String JavaDoc value) {
142
143         ListNode node= new ListNode(uri, local, raw, type, value);
144         if (fLength == 0) {
145             fHead= node;
146         } else {
147             fTail.next= node;
148         }
149         fTail= node;
150         fLength++;
151     }
152
153     /* Inserts an attribute. */
154     public void insertAttributeAt(
155         int index,
156         String JavaDoc raw,
157         String JavaDoc type,
158         String JavaDoc value) {
159         insertAttributeAt(index, null, null, raw, type, value);
160     }
161
162     /* Inserts an attribute. */
163     public void insertAttributeAt(
164         int index,
165         String JavaDoc uri,
166         String JavaDoc local,
167         String JavaDoc raw,
168         String JavaDoc type,
169         String JavaDoc value) {
170
171         // if list is empty, add attribute
172
if (fLength == 0 || index >= fLength) {
173             addAttribute(uri, local, raw, type, value);
174             return;
175         }
176
177         // insert at beginning of list
178
ListNode node= new ListNode(uri, local, raw, type, value);
179         if (index < 1) {
180             node.next= fHead;
181             fHead= node;
182         } else {
183             ListNode prev= getListNodeAt(index - 1);
184             node.next= prev.next;
185             prev.next= node;
186         }
187         fLength++;
188     }
189
190     /* Removes an attribute. */
191     public void removeAttributeAt(int index) {
192
193         if (fLength == 0)
194             return;
195
196         if (index == 0) {
197             fHead= fHead.next;
198             if (fHead == null) {
199                 fTail= null;
200             }
201             fLength--;
202         } else {
203             ListNode prev= getListNodeAt(index - 1);
204             ListNode node= getListNodeAt(index);
205             if (node != null) {
206                 prev.next= node.next;
207                 if (node == fTail) {
208                     fTail= prev;
209                 }
210                 fLength--;
211             }
212         }
213     }
214
215     /* Removes the specified attribute. */
216     public void removeAttribute(String JavaDoc raw) {
217         removeAttributeAt(getIndex(raw));
218     }
219
220     /* Removes the specified attribute. */
221     public void removeAttribute(String JavaDoc uri, String JavaDoc local) {
222         removeAttributeAt(getIndex(uri, local));
223     }
224
225     /* Returns the node at the specified index. */
226     private ListNode getListNodeAt(int i) {
227
228         for (ListNode place= fHead; place != null; place= place.next) {
229             if (--i == -1) {
230                 return place;
231             }
232         }
233         return null;
234     }
235
236     /* Returns the first node with the specified uri and local. */
237     public ListNode getListNode(String JavaDoc uri, String JavaDoc local) {
238
239         if (uri != null && local != null) {
240             ListNode place= fHead;
241             while (place != null) {
242                 if (place.uri != null
243                     && place.local != null
244                     && place.uri.equals(uri)
245                     && place.local.equals(local)) {
246                     return place;
247                 }
248                 place= place.next;
249             }
250         }
251         return null;
252     }
253
254     /* Returns the first node with the specified raw name. */
255     private ListNode getListNode(String JavaDoc raw) {
256
257         if (raw != null) {
258             for (ListNode place= fHead; place != null; place= place.next) {
259                 if (place.raw != null && place.raw.equals(raw)) {
260                     return place;
261                 }
262             }
263         }
264
265         return null;
266     }
267
268     /* Returns a string representation of this object. */
269     public String JavaDoc toString() {
270         StringBuffer JavaDoc str= new StringBuffer JavaDoc();
271
272         str.append('[');
273         str.append("len="); //$NON-NLS-1$
274
str.append(fLength);
275         str.append(", {"); //$NON-NLS-1$
276
for (ListNode place= fHead; place != null; place= place.next) {
277             str.append(place.toString());
278             if (place.next != null) {
279                 str.append(", "); //$NON-NLS-1$
280
}
281         }
282         str.append("}]"); //$NON-NLS-1$
283

284         return str.toString();
285     }
286
287     /*
288      * An attribute node.
289      */

290     static class ListNode {
291
292         /** Attribute uri. */
293         public String JavaDoc uri;
294
295         /** Attribute local. */
296         public String JavaDoc local;
297
298         /** Attribute raw. */
299         public String JavaDoc raw;
300
301         /** Attribute type. */
302         public String JavaDoc type;
303
304         /** Attribute value. */
305         public String JavaDoc value;
306
307         /** Next node. */
308         public ListNode next;
309
310         /* Constructs a list node. */
311         public ListNode(
312             String JavaDoc uri0,
313             String JavaDoc local0,
314             String JavaDoc raw0,
315             String JavaDoc type0,
316             String JavaDoc value0) {
317
318             this.uri= uri0;
319             this.local= local0;
320             this.raw= raw0;
321             this.type= type0;
322             this.value= value0;
323
324         }
325
326         /* Returns string representation of this object. */
327         public String JavaDoc toString() {
328             return raw != null ? raw : local;
329         }
330     }
331 }
332
Popular Tags