1 18 package org.apache.batik.dom.svg; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.apache.batik.parser.ParseException; 25 import org.w3c.dom.DOMException ; 26 import org.w3c.dom.svg.SVGException; 27 28 29 49 public abstract class AbstractSVGList { 50 51 54 protected boolean valid; 55 56 59 protected List itemList; 60 61 66 protected abstract String getItemSeparator(); 67 68 81 protected abstract SVGItem createSVGItem(Object newItem); 82 83 92 protected abstract void doParse(String value, ListHandler builder) 93 throws ParseException; 94 95 100 protected abstract void checkItemType(Object newItem) 101 throws SVGException; 102 103 104 110 protected abstract String getValueAsString(); 111 112 120 protected abstract void setAttributeValue(String value); 121 122 125 protected abstract DOMException createDOMException(short type, 126 String key, 127 Object [] args); 128 129 132 protected AbstractSVGList() { 133 } 134 135 140 public int getNumberOfItems( ){ 141 142 revalidate(); 143 144 if ( itemList != null ){ 145 return itemList.size(); 146 } 147 else{ 148 return 0; 149 } 150 } 151 152 160 public void clear() 161 throws DOMException { 162 revalidate(); 163 if ( itemList != null ){ 164 clear(itemList); 166 resetAttribute(); 168 } 169 } 170 171 188 protected SVGItem initializeImpl ( Object newItem ) 189 throws DOMException , SVGException { 190 191 checkItemType(newItem); 192 193 if ( itemList == null ) { 195 itemList = new ArrayList (1); 196 } 197 else{ 198 clear(itemList); 200 } 201 202 SVGItem item = removeIfNeeded(newItem); 203 204 itemList.add(item); 206 207 item.setParent(this); 209 210 resetAttribute(); 212 213 return( item ); 214 } 215 216 228 protected SVGItem getItemImpl ( int index ) 229 throws DOMException { 230 revalidate(); 231 232 if ( index < 0 || itemList == null || index >= itemList.size() ){ 233 throw createDOMException(DOMException.INDEX_SIZE_ERR, 234 "AbstractSVGList.getItem.OutOfBoundsException", 235 null); 236 } 237 238 return (SVGItem)itemList.get(index); 239 } 240 241 268 protected SVGItem insertItemBeforeImpl ( Object newItem, int index ) 269 throws DOMException , SVGException { 270 271 checkItemType(newItem); 272 273 revalidate(); 274 if ( index < 0 ) { 275 throw createDOMException(DOMException.INDEX_SIZE_ERR, 276 "AbstractSVGList.insertItemBefore.OutOfBoundsException", 277 null); 278 } 279 280 if ( index > itemList.size() ){ 281 index = itemList.size(); 282 } 283 284 SVGItem item = removeIfNeeded(newItem); 285 286 itemList.add(index,item); 288 289 item.setParent(this); 291 292 resetAttribute(); 293 294 return( item ); 295 } 296 297 298 321 protected SVGItem replaceItemImpl ( Object newItem, int index ) 322 throws DOMException , SVGException { 323 324 checkItemType(newItem); 325 326 revalidate(); 327 if ( index < 0 || index >= itemList.size() ){ 328 throw createDOMException(DOMException.INDEX_SIZE_ERR, 329 "AbstractSVGList.replaceItem.OutOfBoundsException", 330 null); 331 } 332 333 SVGItem item = removeIfNeeded(newItem); 334 335 itemList.set(index,item); 337 338 item.setParent(this); 340 341 resetAttribute(); 342 343 return( item ); 344 } 345 346 360 protected SVGItem removeItemImpl ( int index ) 361 throws DOMException { 362 363 revalidate(); 364 if ( index < 0 || index >= itemList.size() ){ 365 throw createDOMException(DOMException.INDEX_SIZE_ERR, 366 "AbstractSVGList.removeItem.OutOfBoundsException", 367 null); 368 } 369 370 SVGItem item = (SVGItem)itemList.remove(index); 371 372 item.setParent(null); 374 375 resetAttribute(); 376 377 return( item ); 378 } 379 380 396 protected SVGItem appendItemImpl ( Object newItem ) 397 throws DOMException , SVGException { 398 399 checkItemType(newItem); 400 401 revalidate(); 402 403 SVGItem item = removeIfNeeded(newItem); 404 405 itemList.add(item); 406 407 item.setParent(this); 409 410 if ( itemList.size() <= 1 ){ 411 resetAttribute(); 412 } 413 else{ 414 resetAttribute(item); 415 } 416 417 return( item ); 418 } 419 420 434 protected SVGItem removeIfNeeded(Object newItem){ 435 436 SVGItem item = null; 437 438 if ( newItem instanceof SVGItem ){ 439 item = (SVGItem)newItem; 442 if ( item.getParent() != null ){ 443 item.getParent().removeItem(item); 444 } 445 } 446 else{ 447 item = createSVGItem( newItem ); 448 } 449 450 return item; 451 } 452 453 456 protected void revalidate() { 457 if (valid) { 458 return; 459 } 460 461 try{ 462 ListBuilder builder = new ListBuilder(); 463 464 doParse(getValueAsString(),builder); 465 466 if ( builder.getList() != null ){ 467 clear(itemList); 468 } 469 itemList = builder.getList(); 470 } 471 catch(ParseException e){ 472 itemList = null; 473 } 474 valid = true; 475 } 476 477 483 protected void setValueAsString(List value) throws DOMException { 484 485 StringBuffer buf = null; 486 Iterator it = value.iterator(); 487 while( it.hasNext() ){ 488 SVGItem item = ( SVGItem )it.next(); 489 490 if ( buf == null ){ 491 buf = new StringBuffer (item.getValueAsString()); 492 } 493 else{ 494 buf.append(getItemSeparator()); 495 buf.append(item.getValueAsString()); 496 } 497 } 498 String finalValue = null; 499 if ( buf == null ){ 500 finalValue = null; 501 } 502 else{ 503 finalValue = buf.toString(); 504 } 505 setAttributeValue(finalValue); 506 507 valid = true; 508 } 509 510 512 public void itemChanged(){ 513 resetAttribute(); 514 } 515 516 519 protected void resetAttribute() { 520 setValueAsString(itemList); 521 } 522 523 528 protected void resetAttribute(SVGItem item) { 529 StringBuffer buf = new StringBuffer (getValueAsString()); 530 buf.append(getItemSeparator()); 531 buf.append(item.getValueAsString()); 532 setAttributeValue(buf.toString()); 533 valid = true; 534 } 535 536 539 public void invalidate() { 540 valid = false; 541 } 542 543 553 protected void removeItem(SVGItem item){ 554 if ( itemList.contains(item) ){ 555 itemList.remove(item); 556 item.setParent(null); 557 resetAttribute(); 558 } 559 } 560 561 567 protected void clear(List list){ 568 if ( list == null ){ 569 return; 570 } 571 572 Iterator it = list.iterator(); 573 574 while( it.hasNext() ){ 575 SVGItem item = (SVGItem)it.next(); 576 item.setParent(null); 577 } 578 579 list.clear(); 580 } 581 582 588 protected class ListBuilder implements ListHandler { 589 590 593 protected List list; 594 595 public ListBuilder(){ 597 } 598 599 604 public List getList(){ 605 return list; 606 } 607 608 public void startList(){ 609 if ( list == null ){ 610 list = new ArrayList (); 611 } 612 } 613 614 public void item(SVGItem item){ 615 item.setParent(AbstractSVGList.this); 616 list.add(item); 617 } 618 619 public void endList(){ 620 } 621 } 622 } 623 | Popular Tags |