1 31 package org.pdfbox.cos; 32 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 39 40 import org.pdfbox.exceptions.COSVisitorException; 41 import org.pdfbox.pdmodel.common.COSObjectable; 42 43 49 public class COSArray extends COSBase 50 { 51 private List objects = new ArrayList (); 52 53 56 public COSArray() 57 { 58 } 60 61 66 public void add( COSBase object ) 67 { 68 objects.add( object ); 69 } 70 71 76 public void add( COSObjectable object ) 77 { 78 objects.add( object.getCOSObject() ); 79 } 80 81 88 public void add( int i, COSBase object) 89 { 90 objects.add( i, object ); 91 } 92 93 96 public void clear() 97 { 98 objects.clear(); 99 } 100 101 106 public void removeAll( Collection objectsList ) 107 { 108 objects.removeAll( objectsList ); 109 } 110 111 116 public void retainAll( Collection objectsList ) 117 { 118 objects.retainAll( objectsList ); 119 } 120 121 126 public void addAll( Collection objectsList ) 127 { 128 objects.addAll( objectsList ); 129 } 130 131 136 public void addAll( COSArray objectList ) 137 { 138 if( objectList != null ) 139 { 140 objects.addAll( objectList.objects ); 141 } 142 } 143 144 151 public void addAll( int i, Collection objectList ) 152 { 153 objects.addAll( i, objectList ); 154 } 155 156 162 public void set( int index, COSBase object ) 163 { 164 objects.set( index, object ); 165 } 166 167 173 public void set( int index, int intVal ) 174 { 175 objects.set( index, new COSInteger( intVal ) ); 176 } 177 178 184 public void set( int index, COSObjectable object ) 185 { 186 COSBase base = null; 187 if( object != null ) 188 { 189 base = object.getCOSObject(); 190 } 191 objects.set( index, base ); 192 } 193 194 202 public COSBase getObject( int index ) 203 { 204 Object obj = objects.get( index ); 205 if( obj instanceof COSObject ) 206 { 207 obj = ((COSObject)obj).getObject(); 208 } 209 if( obj instanceof COSNull ) 210 { 211 obj = null; 212 } 213 return (COSBase)obj; 214 } 215 216 224 public COSBase get( int index ) 225 { 226 return (COSBase)objects.get( index ); 227 } 228 229 236 public int getInt( int index ) 237 { 238 return getInt( index, -1 ); 239 } 240 241 249 public int getInt( int index, int defaultValue ) 250 { 251 int retval = defaultValue; 252 if( defaultValue < size() ) 253 { 254 COSNumber number = (COSNumber)get( index ); 255 if( number != null ) 256 { 257 retval = number.intValue(); 258 } 259 } 260 return retval; 261 } 262 263 269 public void setInt( int index, int value ) 270 { 271 set( index, new COSInteger( value ) ); 272 } 273 274 279 public void setName( int index, String name ) 280 { 281 set( index, COSName.getPDFName( name ) ); 282 } 283 284 290 public String getName( int index ) 291 { 292 return getName( index, null ); 293 } 294 295 301 public String getName( int index, String defaultValue ) 302 { 303 String retval = defaultValue; 304 if( index < size() ) 305 { 306 COSName name = (COSName)get( index ); 307 if( name != null ) 308 { 309 retval = name.getName(); 310 } 311 } 312 return retval; 313 } 314 315 320 public void setString( int index, String string ) 321 { 322 set( index, new COSString( string ) ); 323 } 324 325 331 public String getString( int index ) 332 { 333 return getString( index, null ); 334 } 335 336 342 public String getString( int index, String defaultValue ) 343 { 344 String retval = defaultValue; 345 if( index < size() ) 346 { 347 COSString string = (COSString)get( index ); 348 if( string != null ) 349 { 350 retval = string.getString(); 351 } 352 } 353 return retval; 354 } 355 356 361 public int size() 362 { 363 return objects.size(); 364 } 365 366 373 public COSBase remove( int i ) 374 { 375 return (COSBase)objects.remove( i ); 376 } 377 378 385 public boolean remove( COSBase o ) 386 { 387 return objects.remove( o ); 388 } 389 390 393 public String toString() 394 { 395 return "COSArray{" + objects + "}"; 396 } 397 398 403 public Iterator iterator() 404 { 405 return objects.iterator(); 406 } 407 408 414 public int indexOf( COSBase object ) 415 { 416 int retval = -1; 417 for( int i=0; retval < 0 && i<size(); i++ ) 418 { 419 if( get( i ).equals( object ) ) 420 { 421 retval = i; 422 } 423 } 424 return retval; 425 } 426 427 434 public void growToSize( int size ) 435 { 436 growToSize( size, null ); 437 } 438 439 447 public void growToSize( int size, COSBase object ) 448 { 449 while( size() < size ) 450 { 451 add( object ); 452 } 453 } 454 455 462 public Object accept(ICOSVisitor visitor) throws COSVisitorException 463 { 464 return visitor.visitFromArray(this); 465 } 466 467 472 public float[] toFloatArray() 473 { 474 float[] retval = new float[size()]; 475 for( int i=0; i<size(); i++ ) 476 { 477 retval[i] = ((COSNumber)getObject( i )).floatValue(); 478 } 479 return retval; 480 } 481 482 487 public void setFloatArray( float[] value ) 488 { 489 this.clear(); 490 for( int i=0; i<value.length; i++ ) 491 { 492 add( new COSFloat( value[i] ) ); 493 } 494 } 495 } | Popular Tags |