1 16 17 package sax.helpers; 18 19 import org.xml.sax.Attributes ; 20 21 26 public class AttributesImpl 27 implements Attributes { 28 29 33 34 private ListNode head; 35 36 37 private ListNode tail; 38 39 40 private int length; 41 42 46 47 public int getLength() { 48 return length; 49 } 50 51 52 public int getIndex(String 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 66 public int getIndex(String uri, String 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 80 public String getURI(int index) { 81 82 ListNode node = getListNodeAt(index); 83 return node != null ? node.uri : null; 84 85 } 87 88 public String getLocalName(int index) { 89 90 ListNode node = getListNodeAt(index); 91 return node != null ? node.local : null; 92 93 } 95 96 public String getQName(int index) { 97 98 ListNode node = getListNodeAt(index); 99 return node != null ? node.raw : null; 100 101 } 103 104 public String getType(int index) { 105 106 ListNode node = getListNodeAt(index); 107 return (node != null) ? node.type : null; 108 109 } 111 112 public String getType(String uri, String local) { 113 114 ListNode node = getListNode(uri, local); 115 return (node != null) ? node.type : null; 116 117 } 119 120 public String getType(String raw) { 121 122 ListNode node = getListNode(raw); 123 return (node != null) ? node.type : null; 124 125 } 127 128 public String getValue(int index) { 129 130 ListNode node = getListNodeAt(index); 131 return (node != null) ? node.value : null; 132 133 } 135 136 public String getValue(String uri, String local) { 137 138 ListNode node = getListNode(uri, local); 139 return (node != null) ? node.value : null; 140 141 } 143 144 public String getValue(String raw) { 145 146 ListNode node = getListNode(raw); 147 return (node != null) ? node.value : null; 148 149 } 151 155 156 public void addAttribute(String raw, String type, String value) { 157 addAttribute(null, null, raw, type, value); 158 } 159 160 161 public void addAttribute(String uri, String local, String raw, 162 String type, String 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 } 176 177 public void insertAttributeAt(int index, 178 String raw, String type, String value) { 179 insertAttributeAt(index, null, null, raw, type, value); 180 } 181 182 183 public void insertAttributeAt(int index, 184 String uri, String local, String raw, 185 String type, String value) { 186 187 if (length == 0 || index >= length) { 189 addAttribute(uri, local, raw, type, value); 190 return; 191 } 192 193 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 } 208 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 } 236 237 public void removeAttribute(String raw) { 238 removeAttributeAt(getIndex(raw)); 239 } 240 241 242 public void removeAttribute(String uri, String local) { 243 removeAttributeAt(getIndex(uri, local)); 244 } 245 246 250 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 } 263 264 public ListNode getListNode(String uri, String 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 } 280 281 private ListNode getListNode(String 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 } 295 299 300 public String toString() { 301 StringBuffer str = new StringBuffer (); 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 } 319 323 326 static class ListNode { 327 328 332 333 public String uri; 334 335 336 public String local; 337 338 339 public String raw; 340 341 342 public String type; 343 344 345 public String value; 346 347 348 public ListNode next; 349 350 354 355 public ListNode(String uri, String local, String raw, 356 String type, String value) { 357 358 this.uri = uri; 359 this.local = local; 360 this.raw = raw; 361 this.type = type; 362 this.value = value; 363 364 } 366 370 371 public String toString() { 372 return raw != null ? raw : local; 373 } 374 375 } 377 } | Popular Tags |