1 56 57 package org.jdom; 58 59 import java.util.*; 60 61 77 class AttributeList extends AbstractList 78 implements List, java.io.Serializable { 79 80 private static final String CVS_ID = 81 "@(#) $RCSfile: AttributeList.java,v $ $Revision: 1.23 $ $Date: 2004/02/28 03:30:27 $ $Name: $"; 82 83 private static final int INITIAL_ARRAY_SIZE = 5; 84 85 86 private Attribute elementData[]; 87 private int size; 88 89 90 private Element parent; 91 92 93 private AttributeList() {} 94 95 101 AttributeList(Element parent) { 102 this.parent = parent; 103 } 104 105 111 final void uncheckedAddAttribute(Attribute a) { 112 a.parent = parent; 113 ensureCapacity(size + 1); 114 elementData[size++] = a; 115 modCount++; 116 } 117 118 126 public boolean add(Object obj) { 127 if (obj instanceof Attribute) { 128 Attribute attribute = (Attribute) obj; 129 int duplicate = indexOfDuplicate(attribute); 130 if (duplicate < 0) { 131 add(size(), attribute); 132 } 133 else { 134 set(duplicate, attribute); 135 } 136 } 137 else if (obj == null) { 138 throw new IllegalAddException("Cannot add null attribute"); 139 } 140 else { 141 throw new IllegalAddException("Class " + 142 obj.getClass().getName() + 143 " is not an attribute"); 144 } 145 return true; 146 } 147 148 157 public void add(int index, Object obj) { 158 if (obj instanceof Attribute) { 159 Attribute attribute = (Attribute) obj; 160 int duplicate = indexOfDuplicate(attribute); 161 if (duplicate >= 0) { 162 throw new IllegalAddException("Cannot add duplicate attribute"); 163 } 164 add(index, attribute); 165 } 166 else if (obj == null) { 167 throw new IllegalAddException("Cannot add null attribute"); 168 } 169 else { 170 throw new IllegalAddException("Class " + 171 obj.getClass().getName() + 172 " is not an attribute"); 173 } 174 modCount++; 175 } 176 177 185 void add(int index, Attribute attribute) { 186 if (attribute.getParent() != null) { 187 throw new IllegalAddException( 188 "The attribute already has an existing parent \"" + 189 attribute.getParent().getQualifiedName() + "\""); 190 } 191 192 String reason = Verifier.checkNamespaceCollision(attribute, parent); 193 if (reason != null) { 194 throw new IllegalAddException(parent, attribute, reason); 195 } 196 197 if (index<0 || index>size) { 198 throw new IndexOutOfBoundsException ("Index: " + index + 199 " Size: " + size()); 200 } 201 202 attribute.setParent(parent); 203 204 ensureCapacity(size+1); 205 if( index==size ) { 206 elementData[size++] = attribute; 207 } else { 208 System.arraycopy(elementData, index, elementData, index + 1, size - index); 209 elementData[index] = attribute; 210 size++; 211 } 212 modCount++; 213 } 214 215 222 public boolean addAll(Collection collection) { 223 return addAll(size(), collection); 224 } 225 226 237 public boolean addAll(int index, Collection collection) { 238 if (index<0 || index>size) { 239 throw new IndexOutOfBoundsException ("Index: " + index + 240 " Size: " + size()); 241 } 242 243 if ((collection == null) || (collection.size() == 0)) { 244 return false; 245 } 246 ensureCapacity(size() + collection.size()); 247 248 int count = 0; 249 250 try { 251 Iterator i = collection.iterator(); 252 while (i.hasNext()) { 253 Object obj = i.next(); 254 add(index + count, obj); 255 count++; 256 } 257 } 258 catch (RuntimeException exception) { 259 for (int i = 0; i < count; i++) { 260 remove(index); 261 } 262 throw exception; 263 } 264 265 return true; 266 } 267 268 271 public void clear() { 272 if (elementData != null) { 273 for (int i = 0; i < size; i++) { 274 Attribute attribute = elementData[i]; 275 attribute.setParent(null); 276 } 277 elementData = null; 278 size = 0; 279 } 280 modCount++; 281 } 282 283 290 void clearAndSet(Collection collection) { 291 Attribute[] old = elementData; 292 int oldSize = size; 293 294 elementData = null; 295 size = 0; 296 297 if ((collection != null) && (collection.size() != 0)) { 298 ensureCapacity(collection.size()); 299 try { 300 addAll(0, collection); 301 } 302 catch (RuntimeException exception) { 303 elementData = old; 304 size = oldSize; 305 throw exception; 306 } 307 } 308 309 if (old != null) { 310 for (int i = 0; i < oldSize; i++) { 311 Attribute attribute = old[i]; 312 attribute.setParent(null); 313 } 314 } 315 modCount++; 316 } 317 318 325 private void ensureCapacity(int minCapacity) { 326 if (elementData == null) { 327 elementData = new Attribute[Math.max(minCapacity, INITIAL_ARRAY_SIZE)]; 328 } 329 else { 330 int oldCapacity = elementData.length; 331 if (minCapacity > oldCapacity) { 332 Attribute oldData[] = elementData; 333 int newCapacity = (oldCapacity * 3)/2 + 1; 334 if (newCapacity < minCapacity) 335 newCapacity = minCapacity; 336 elementData = new Attribute[newCapacity]; 337 System.arraycopy(oldData, 0, elementData, 0, size); 338 } 339 } 340 } 341 342 348 public Object get(int index) { 349 if (index<0 || index>=size) { 350 throw new IndexOutOfBoundsException ("Index: " + index + 351 " Size: " + size()); 352 } 353 354 return elementData[index]; 355 } 356 357 365 Object get(String name, Namespace namespace) { 366 int index = indexOf(name, namespace); 367 if (index < 0) { 368 return null; 369 } 370 return elementData[index]; 371 } 372 373 377 int indexOf(String name, Namespace namespace) { 378 String uri = namespace.getURI(); 379 if (elementData != null) { 380 for (int i = 0; i < size; i++) { 381 Attribute old = elementData[i]; 382 String oldURI = old.getNamespaceURI(); 383 String oldName = old.getName(); 384 if (oldURI.equals(uri) && oldName.equals(name)) { 385 return i; 386 } 387 } 388 } 389 return -1; 390 } 391 392 398 public Object remove(int index) { 399 if (index<0 || index>=size) 400 throw new IndexOutOfBoundsException ("Index: " + index + 401 " Size: " + size()); 402 403 Attribute old = elementData[index]; 404 old.setParent(null); 405 int numMoved = size - index - 1; 406 if (numMoved > 0) 407 System.arraycopy(elementData, index+1, elementData, index,numMoved); 408 elementData[--size] = null; modCount++; 410 return old; 411 } 412 413 421 boolean remove(String name, Namespace namespace) { 422 int index = indexOf(name, namespace); 423 if (index < 0) { 424 return false; 425 } 426 remove(index); 427 return true; 428 } 429 430 439 public Object set(int index, Object obj) { 440 if (obj instanceof Attribute) { 441 Attribute attribute = (Attribute) obj; 442 int duplicate = indexOfDuplicate(attribute); 443 if ((duplicate >= 0) && (duplicate != index)) { 444 throw new IllegalAddException("Cannot set duplicate attribute"); 445 } 446 return set(index, attribute); 447 } 448 else if (obj == null) { 449 throw new IllegalAddException("Cannot add null attribute"); 450 } 451 else { 452 throw new IllegalAddException("Class " + 453 obj.getClass().getName() + 454 " is not an attribute"); 455 } 456 } 457 458 467 Object set(int index, Attribute attribute) { 468 if (index < 0 || index >= size) 469 throw new IndexOutOfBoundsException ("Index: " + index + 470 " Size: " + size()); 471 472 if (attribute.getParent() != null) { 473 throw new IllegalAddException( 474 "The attribute already has an existing parent \"" + 475 attribute.getParent().getQualifiedName() + "\""); 476 } 477 478 String reason = Verifier.checkNamespaceCollision(attribute, parent); 479 if (reason != null) { 480 throw new IllegalAddException(parent, attribute, reason); 481 } 482 483 Attribute old = (Attribute) elementData[index]; 484 old.setParent(null); 485 486 elementData[index] = attribute; 487 attribute.setParent(parent); 488 return old; 489 } 490 491 495 private int indexOfDuplicate(Attribute attribute) { 496 int duplicate = -1; 497 String name = attribute.getName(); 498 Namespace namespace = attribute.getNamespace(); 499 duplicate = indexOf(name, namespace); 500 return duplicate; 501 } 502 503 508 public int size() { 509 return size; 510 } 511 512 515 public String toString() { 516 return super.toString(); 517 } 518 } 519 | Popular Tags |