1 31 package org.pdfbox.pdmodel.common; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSDictionary; 36 import org.pdfbox.cos.COSInteger; 37 import org.pdfbox.cos.COSFloat; 38 import org.pdfbox.cos.COSString; 39 import org.pdfbox.cos.COSName; 40 import org.pdfbox.cos.COSNull; 41 import org.pdfbox.cos.COSNumber; 42 43 import java.util.ArrayList ; 44 import java.util.Collection ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 import java.util.ListIterator ; 48 49 55 public class COSArrayList implements List 56 { 57 private COSArray array; 58 private List actual; 59 60 private COSDictionary parentDict; 61 private String dictKey; 62 63 66 public COSArrayList() 67 { 68 array = new COSArray(); 69 actual = new ArrayList (); 70 } 71 72 78 public COSArrayList( List actualList, COSArray cosArray ) 79 { 80 actual = actualList; 81 array = cosArray; 82 } 83 84 99 public COSArrayList( Object actualObject, COSBase item, COSDictionary dictionary, String dictionaryKey ) 100 { 101 array = new COSArray(); 102 array.add( item ); 103 actual = new ArrayList (); 104 actual.add( actualObject ); 105 106 parentDict = dictionary; 107 dictKey = dictionaryKey; 108 } 109 110 113 public int size() 114 { 115 return actual.size(); 116 } 117 118 121 public boolean isEmpty() 122 { 123 return actual.isEmpty(); 124 } 125 126 129 public boolean contains(Object o) 130 { 131 return actual.contains(o); 132 } 133 134 137 public Iterator iterator() 138 { 139 return actual.iterator(); 140 } 141 142 145 public Object [] toArray() 146 { 147 return actual.toArray(); 148 } 149 150 153 public Object [] toArray(Object [] a) 154 { 155 return actual.toArray(a); 156 157 } 158 159 162 public boolean add(Object o) 163 { 164 if( parentDict != null ) 167 { 168 parentDict.setItem( dictKey, array ); 169 parentDict = null; 172 } 173 if( o instanceof String ) 175 { 176 array.add( new COSString( (String )o ) ); 177 } 178 else if( o instanceof DualCOSObjectable ) 179 { 180 DualCOSObjectable dual = (DualCOSObjectable)o; 181 array.add( dual.getFirstCOSObject() ); 182 array.add( dual.getSecondCOSObject() ); 183 } 184 else 185 { 186 array.add( ((COSObjectable)o).getCOSObject() ); 187 } 188 return actual.add(o); 189 } 190 191 194 public boolean remove(Object o) 195 { 196 boolean retval = true; 197 int index = actual.indexOf( o ); 198 if( index >= 0 ) 199 { 200 actual.remove( index ); 201 array.remove( index ); 202 } 203 else 204 { 205 retval = false; 206 } 207 return retval; 208 } 209 210 213 public boolean containsAll(Collection c) 214 { 215 return actual.containsAll( c ); 216 } 217 218 221 public boolean addAll(Collection c) 222 { 223 if( parentDict != null && c.size() > 0) 226 { 227 parentDict.setItem( dictKey, array ); 228 parentDict = null; 231 } 232 array.addAll( toCOSObjectList( c ) ); 233 return actual.addAll( c ); 234 } 235 236 239 public boolean addAll(int index, Collection c) 240 { 241 if( parentDict != null && c.size() > 0) 244 { 245 parentDict.setItem( dictKey, array ); 246 parentDict = null; 249 } 250 251 if( c.size() >0 && c.toArray()[0] instanceof DualCOSObjectable ) 252 { 253 array.addAll( index*2, toCOSObjectList( c ) ); 254 } 255 else 256 { 257 array.addAll( index, toCOSObjectList( c ) ); 258 } 259 return actual.addAll( index, c ); 260 } 261 262 270 public static List convertIntegerCOSArrayToList( COSArray intArray ) 271 { 272 List numbers = new ArrayList (); 273 for( int i=0; i<intArray.size(); i++ ) 274 { 275 numbers.add( new Integer ( ((COSNumber)intArray.get( i )).intValue() ) ); 276 } 277 return new COSArrayList( numbers, intArray ); 278 } 279 280 288 public static List convertFloatCOSArrayToList( COSArray floatArray ) 289 { 290 List retval = null; 291 if( floatArray != null ) 292 { 293 List numbers = new ArrayList (); 294 for( int i=0; i<floatArray.size(); i++ ) 295 { 296 numbers.add( new Float ( ((COSNumber)floatArray.get( i )).floatValue() ) ); 297 } 298 retval = new COSArrayList( numbers, floatArray ); 299 } 300 return retval; 301 } 302 303 311 public static List convertCOSNameCOSArrayToList( COSArray nameArray ) 312 { 313 List retval = null; 314 if( nameArray != null ) 315 { 316 List names = new ArrayList (); 317 for( int i=0; i<nameArray.size(); i++ ) 318 { 319 names.add( ((COSName)nameArray.getObject( i )).getName() ); 320 } 321 retval = new COSArrayList( names, nameArray ); 322 } 323 return retval; 324 } 325 326 334 public static List convertCOSStringCOSArrayToList( COSArray stringArray ) 335 { 336 List retval = null; 337 if( stringArray != null ) 338 { 339 List string = new ArrayList (); 340 for( int i=0; i<stringArray.size(); i++ ) 341 { 342 string.add( ((COSString)stringArray.getObject( i )).getString() ); 343 } 344 retval = new COSArrayList( string, stringArray ); 345 } 346 return retval; 347 } 348 349 357 public static COSArray convertStringListToCOSNameCOSArray( List strings ) 358 { 359 COSArray retval = new COSArray(); 360 for( int i=0; i<strings.size(); i++ ) 361 { 362 Object next = strings.get( i ); 363 if( next instanceof COSName ) 364 { 365 retval.add( (COSName)next ); 366 } 367 else 368 { 369 retval.add( COSName.getPDFName( (String )next ) ); 370 } 371 } 372 return retval; 373 } 374 375 383 public static COSArray convertStringListToCOSStringCOSArray( List strings ) 384 { 385 COSArray retval = new COSArray(); 386 for( int i=0; i<strings.size(); i++ ) 387 { 388 retval.add( new COSString( (String )strings.get( i ) ) ); 389 } 390 return retval; 391 } 392 393 401 public static COSArray converterToCOSArray( List cosObjectableList ) 402 { 403 COSArray array = null; 404 if( cosObjectableList != null ) 405 { 406 if( cosObjectableList instanceof COSArrayList ) 407 { 408 array = ((COSArrayList)cosObjectableList).array; 410 } 411 else 412 { 413 array = new COSArray(); 414 Iterator iter = cosObjectableList.iterator(); 415 while( iter.hasNext() ) 416 { 417 Object next = iter.next(); 418 if( next instanceof String ) 419 { 420 array.add( new COSString( (String )next ) ); 421 } 422 else if( next instanceof Integer || next instanceof Long ) 423 { 424 array.add( new COSInteger( ((Number )next).longValue() ) ); 425 } 426 else if( next instanceof Float || next instanceof Double ) 427 { 428 array.add( new COSFloat( ((Number )next).floatValue() ) ); 429 } 430 else if( next instanceof COSObjectable ) 431 { 432 COSObjectable object = (COSObjectable)next; 433 array.add( object.getCOSObject() ); 434 } 435 else if( next instanceof DualCOSObjectable ) 436 { 437 DualCOSObjectable object = (DualCOSObjectable)next; 438 array.add( object.getFirstCOSObject() ); 439 array.add( object.getSecondCOSObject() ); 440 } 441 else if( next == null ) 442 { 443 array.add( COSNull.NULL ); 444 } 445 else 446 { 447 throw new RuntimeException ( "Error: Don't know how to convert type to COSBase '" + 448 next.getClass().getName() + "'" ); 449 } 450 } 451 } 452 } 453 return array; 454 } 455 456 private List toCOSObjectList( Collection list ) 457 { 458 List cosObjects = new ArrayList (); 459 Iterator iter = list.iterator(); 460 while( iter.hasNext() ) 461 { 462 Object next = iter.next(); 463 if( next instanceof String ) 464 { 465 cosObjects.add( new COSString( (String )next ) ); 466 } 467 else if( next instanceof DualCOSObjectable ) 468 { 469 DualCOSObjectable object = (DualCOSObjectable)next; 470 array.add( object.getFirstCOSObject() ); 471 array.add( object.getSecondCOSObject() ); 472 } 473 else 474 { 475 COSObjectable cos = (COSObjectable)next; 476 cosObjects.add( cos.getCOSObject() ); 477 } 478 } 479 return cosObjects; 480 } 481 482 485 public boolean removeAll(Collection c) 486 { 487 array.removeAll( toCOSObjectList( c ) ); 488 return actual.removeAll( c ); 489 } 490 491 494 public boolean retainAll(Collection c) 495 { 496 array.retainAll( toCOSObjectList( c ) ); 497 return actual.retainAll( c ); 498 } 499 500 503 public void clear() 504 { 505 if( parentDict != null ) 508 { 509 parentDict.setItem( dictKey, (COSBase)null ); 510 } 511 actual.clear(); 512 array.clear(); 513 } 514 515 518 public boolean equals(Object o) 519 { 520 return actual.equals( o ); 521 } 522 523 526 public int hashCode() 527 { 528 return actual.hashCode(); 529 } 530 531 534 public Object get(int index) 535 { 536 return actual.get( index ); 537 538 } 539 540 543 public Object set(int index, Object element) 544 { 545 if( element instanceof String ) 546 { 547 COSString item = new COSString( (String )element ); 548 if( parentDict != null && index == 0 ) 549 { 550 parentDict.setItem( dictKey, item ); 551 } 552 array.set( index, item ); 553 } 554 else if( element instanceof DualCOSObjectable ) 555 { 556 DualCOSObjectable dual = (DualCOSObjectable)element; 557 array.set( index*2, dual.getFirstCOSObject() ); 558 array.set( index*2+1, dual.getSecondCOSObject() ); 559 } 560 else 561 { 562 if( parentDict != null && index == 0 ) 563 { 564 parentDict.setItem( dictKey, ((COSObjectable)element).getCOSObject() ); 565 } 566 array.set( index, ((COSObjectable)element).getCOSObject() ); 567 } 568 return actual.set( index, element ); 569 } 570 571 574 public void add(int index, Object element) 575 { 576 if( parentDict != null ) 579 { 580 parentDict.setItem( dictKey, array ); 581 parentDict = null; 584 } 585 actual.add( index, element ); 586 if( element instanceof String ) 587 { 588 array.add( index, new COSString( (String )element ) ); 589 } 590 else if( element instanceof DualCOSObjectable ) 591 { 592 DualCOSObjectable dual = (DualCOSObjectable)element; 593 array.add( index*2, dual.getFirstCOSObject() ); 594 array.add( index*2+1, dual.getSecondCOSObject() ); 595 } 596 else 597 { 598 array.add( index, ((COSObjectable)element).getCOSObject() ); 599 } 600 } 601 602 605 public Object remove(int index) 606 { 607 if( array.size() > index && array.get( index ) instanceof DualCOSObjectable ) 608 { 609 array.remove( index ); 611 array.remove( index ); 612 } 613 else 614 { 615 array.remove( index ); 616 } 617 return actual.remove( index ); 618 } 619 620 623 public int indexOf(Object o) 624 { 625 return actual.indexOf( o ); 626 } 627 628 631 public int lastIndexOf(Object o) 632 { 633 return actual.indexOf( o ); 634 635 } 636 637 640 public ListIterator listIterator() 641 { 642 return actual.listIterator(); 643 } 644 645 648 public ListIterator listIterator(int index) 649 { 650 return actual.listIterator( index ); 651 } 652 653 656 public List subList(int fromIndex, int toIndex) 657 { 658 return actual.subList( fromIndex, toIndex ); 659 } 660 } | Popular Tags |