1 7 package org.xml.sax.helpers; 8 9 import org.xml.sax.Attributes ; 10 11 12 46 public class AttributesImpl implements Attributes 47 { 48 49 50 54 55 58 public AttributesImpl () 59 { 60 length = 0; 61 data = null; 62 } 63 64 65 73 public AttributesImpl (Attributes atts) 74 { 75 setAttributes(atts); 76 } 77 78 79 80 84 85 91 public int getLength () 92 { 93 return length; 94 } 95 96 97 105 public String getURI (int index) 106 { 107 if (index >= 0 && index < length) { 108 return data[index*5]; 109 } else { 110 return null; 111 } 112 } 113 114 115 123 public String getLocalName (int index) 124 { 125 if (index >= 0 && index < length) { 126 return data[index*5+1]; 127 } else { 128 return null; 129 } 130 } 131 132 133 141 public String getQName (int index) 142 { 143 if (index >= 0 && index < length) { 144 return data[index*5+2]; 145 } else { 146 return null; 147 } 148 } 149 150 151 159 public String getType (int index) 160 { 161 if (index >= 0 && index < length) { 162 return data[index*5+3]; 163 } else { 164 return null; 165 } 166 } 167 168 169 176 public String getValue (int index) 177 { 178 if (index >= 0 && index < length) { 179 return data[index*5+4]; 180 } else { 181 return null; 182 } 183 } 184 185 186 199 public int getIndex (String uri, String localName) 200 { 201 int max = length * 5; 202 for (int i = 0; i < max; i += 5) { 203 if (data[i].equals(uri) && data[i+1].equals(localName)) { 204 return i / 5; 205 } 206 } 207 return -1; 208 } 209 210 211 218 public int getIndex (String qName) 219 { 220 int max = length * 5; 221 for (int i = 0; i < max; i += 5) { 222 if (data[i+2].equals(qName)) { 223 return i / 5; 224 } 225 } 226 return -1; 227 } 228 229 230 240 public String getType (String uri, String localName) 241 { 242 int max = length * 5; 243 for (int i = 0; i < max; i += 5) { 244 if (data[i].equals(uri) && data[i+1].equals(localName)) { 245 return data[i+3]; 246 } 247 } 248 return null; 249 } 250 251 252 260 public String getType (String qName) 261 { 262 int max = length * 5; 263 for (int i = 0; i < max; i += 5) { 264 if (data[i+2].equals(qName)) { 265 return data[i+3]; 266 } 267 } 268 return null; 269 } 270 271 272 282 public String getValue (String uri, String localName) 283 { 284 int max = length * 5; 285 for (int i = 0; i < max; i += 5) { 286 if (data[i].equals(uri) && data[i+1].equals(localName)) { 287 return data[i+4]; 288 } 289 } 290 return null; 291 } 292 293 294 302 public String getValue (String qName) 303 { 304 int max = length * 5; 305 for (int i = 0; i < max; i += 5) { 306 if (data[i+2].equals(qName)) { 307 return data[i+4]; 308 } 309 } 310 return null; 311 } 312 313 314 315 319 320 327 public void clear () 328 { 329 if (data != null) { 330 for (int i = 0; i < (length * 5); i++) 331 data [i] = null; 332 } 333 length = 0; 334 } 335 336 337 345 public void setAttributes (Attributes atts) 346 { 347 clear(); 348 length = atts.getLength(); 349 if (length > 0) { 350 data = new String [length*5]; 351 for (int i = 0; i < length; i++) { 352 data[i*5] = atts.getURI(i); 353 data[i*5+1] = atts.getLocalName(i); 354 data[i*5+2] = atts.getQName(i); 355 data[i*5+3] = atts.getType(i); 356 data[i*5+4] = atts.getValue(i); 357 } 358 } 359 } 360 361 362 379 public void addAttribute (String uri, String localName, String qName, 380 String type, String value) 381 { 382 ensureCapacity(length+1); 383 data[length*5] = uri; 384 data[length*5+1] = localName; 385 data[length*5+2] = qName; 386 data[length*5+3] = type; 387 data[length*5+4] = value; 388 length++; 389 } 390 391 392 413 public void setAttribute (int index, String uri, String localName, 414 String qName, String type, String value) 415 { 416 if (index >= 0 && index < length) { 417 data[index*5] = uri; 418 data[index*5+1] = localName; 419 data[index*5+2] = qName; 420 data[index*5+3] = type; 421 data[index*5+4] = value; 422 } else { 423 badIndex(index); 424 } 425 } 426 427 428 436 public void removeAttribute (int index) 437 { 438 if (index >= 0 && index < length) { 439 if (index < length - 1) { 440 System.arraycopy(data, (index+1)*5, data, index*5, 441 (length-index-1)*5); 442 } 443 index = (length - 1) * 5; 444 data [index++] = null; 445 data [index++] = null; 446 data [index++] = null; 447 data [index++] = null; 448 data [index] = null; 449 length--; 450 } else { 451 badIndex(index); 452 } 453 } 454 455 456 466 public void setURI (int index, String uri) 467 { 468 if (index >= 0 && index < length) { 469 data[index*5] = uri; 470 } else { 471 badIndex(index); 472 } 473 } 474 475 476 486 public void setLocalName (int index, String localName) 487 { 488 if (index >= 0 && index < length) { 489 data[index*5+1] = localName; 490 } else { 491 badIndex(index); 492 } 493 } 494 495 496 506 public void setQName (int index, String qName) 507 { 508 if (index >= 0 && index < length) { 509 data[index*5+2] = qName; 510 } else { 511 badIndex(index); 512 } 513 } 514 515 516 525 public void setType (int index, String type) 526 { 527 if (index >= 0 && index < length) { 528 data[index*5+3] = type; 529 } else { 530 badIndex(index); 531 } 532 } 533 534 535 544 public void setValue (int index, String value) 545 { 546 if (index >= 0 && index < length) { 547 data[index*5+4] = value; 548 } else { 549 badIndex(index); 550 } 551 } 552 553 554 555 559 560 566 private void ensureCapacity (int n) { 567 if (n <= 0) { 568 return; 569 } 570 int max; 571 if (data == null || data.length == 0) { 572 max = 25; 573 } 574 else if (data.length >= n * 5) { 575 return; 576 } 577 else { 578 max = data.length; 579 } 580 while (max < n * 5) { 581 max *= 2; 582 } 583 584 String newData[] = new String [max]; 585 if (length > 0) { 586 System.arraycopy(data, 0, newData, 0, length*5); 587 } 588 data = newData; 589 } 590 591 592 598 private void badIndex (int index) 599 throws ArrayIndexOutOfBoundsException 600 { 601 String msg = 602 "Attempt to modify attribute at illegal index: " + index; 603 throw new ArrayIndexOutOfBoundsException (msg); 604 } 605 606 607 608 612 int length; 613 String data []; 614 615 } 616 617 619 | Popular Tags |