1 26 27 package it.stefanochizzolini.clown.files; 28 29 import it.stefanochizzolini.clown.objects.PdfDataObject; 30 import it.stefanochizzolini.clown.objects.PdfIndirectObject; 31 import it.stefanochizzolini.clown.tokens.XRefEntry; 32 import it.stefanochizzolini.clown.tokens.XRefEntryUsageEnum; 33 import it.stefanochizzolini.clown.util.NotImplementedException; 34 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.Hashtable ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.ListIterator ; 41 import java.util.NoSuchElementException ; 42 import java.util.TreeMap ; 43 44 61 public class IndirectObjects 62 implements List <PdfIndirectObject> 63 { 64 70 private File file; 71 72 80 private Hashtable <Integer ,PdfIndirectObject> importedObjects = new Hashtable <Integer ,PdfIndirectObject>(); 81 84 private TreeMap <Integer ,PdfIndirectObject> modifiedObjects = new TreeMap <Integer ,PdfIndirectObject>(); 85 90 private TreeMap <Integer ,PdfIndirectObject> wokenObjects = new TreeMap <Integer ,PdfIndirectObject>(); 91 92 95 private int lastObjectNumber = -1; 103 private XRefEntry[] xrefEntries; 104 105 private UpdateModeEnum updateMode = UpdateModeEnum.Manual; 106 108 IndirectObjects( 110 File file, 111 XRefEntry[] xrefEntries 112 ) 113 { 114 this.file = file; 115 this.xrefEntries = xrefEntries; 116 if(this.xrefEntries == null) 118 { 119 123 lastObjectNumber++; 125 modifiedObjects.put( 126 lastObjectNumber, 127 new PdfIndirectObject( 128 this.file, 129 null, 130 new XRefEntry( 131 lastObjectNumber, 132 65535, 133 0, 134 XRefEntryUsageEnum.Free 135 ) 136 ) 137 ); 138 } 139 else 140 { 141 lastObjectNumber = xrefEntries.length - 1; 143 } 144 } 145 147 public File getFile( 150 ) 151 {return file;} 152 153 163 public PdfIndirectObject add( 164 PdfDataObject object 165 ) 166 { 167 lastObjectNumber++; 169 PdfIndirectObject indirectObject = new PdfIndirectObject( 170 file, 171 object, 172 new XRefEntry( 173 lastObjectNumber, 174 0, 175 0, 176 XRefEntryUsageEnum.InUse 177 ) 178 ); 179 modifiedObjects.put(lastObjectNumber,indirectObject); 181 182 return indirectObject; 183 } 184 185 196 public PdfIndirectObject addExternal( 197 PdfIndirectObject object 198 ) 199 { 200 PdfIndirectObject indirectObject = importedObjects.get(object.hashCode()); 201 if(indirectObject == null) 203 { 204 indirectObject = add((PdfDataObject)object.getDataObject().clone(file)); 206 importedObjects.put(object.hashCode(),indirectObject); 208 } 209 210 return indirectObject; 211 } 212 213 public Collection <? extends PdfIndirectObject> addAllExternal( 214 Collection <? extends PdfIndirectObject> objects 215 ) 216 { 217 ArrayList <PdfIndirectObject> addedObjects = new ArrayList <PdfIndirectObject>(objects.size()); 218 for(PdfIndirectObject object : objects) 219 {addedObjects.add((PdfIndirectObject)addExternal(object));} 220 221 return addedObjects; 222 } 223 224 public void add( 226 int index, 227 PdfIndirectObject object 228 ) 229 {throw new UnsupportedOperationException ();} 230 231 public boolean addAll( 232 int index, 233 Collection <? extends PdfIndirectObject> objects 234 ) 235 {throw new UnsupportedOperationException ();} 236 237 242 public PdfIndirectObject get( 243 int index 244 ) 245 { 246 PdfIndirectObject object = modifiedObjects.get(index); 248 if(object == null) 250 { 251 object = wokenObjects.get(index); 253 if(object == null) 255 { 256 try 257 { 258 object = new PdfIndirectObject( 259 file, 260 null, 261 xrefEntries[index] 262 ); 263 } 264 catch(Exception e) 265 {throw new RuntimeException (e);} 266 267 272 wokenObjects.put(index,object); 273 274 if(updateMode == UpdateModeEnum.Automatic) 276 {update(object); } 277 } 278 } 279 280 return object; 281 } 282 283 public int indexOf( 284 Object object 285 ) 286 { 287 if(((PdfIndirectObject)object).getFile() != file) 289 return -1; 290 291 return ((PdfIndirectObject)object).getReference().getObjectNumber(); 292 } 293 294 public int lastIndexOf( 295 Object object 296 ) 297 { 298 302 return indexOf(object); 303 } 304 305 public ListIterator <PdfIndirectObject> listIterator( 306 ) 307 {throw new NotImplementedException();} 308 309 public ListIterator <PdfIndirectObject> listIterator( 310 int index 311 ) 312 {throw new NotImplementedException();} 313 314 public PdfIndirectObject remove( 315 int index 316 ) 317 { 318 325 return update( 326 new PdfIndirectObject( 327 file, 328 null, 329 new XRefEntry( 330 index, 331 65535, 0, 333 XRefEntryUsageEnum.Free 334 ) 335 ) 336 ); 337 } 338 339 368 public PdfIndirectObject set( 369 int index, 370 PdfIndirectObject object 371 ) 372 {throw new UnsupportedOperationException ();} 373 374 public List <PdfIndirectObject> subList( 375 int fromIndex, 376 int toIndex 377 ) 378 {throw new NotImplementedException();} 379 380 391 public boolean add( 392 PdfIndirectObject object 393 ) 394 { 395 boolean changed = (addExternal(object) != null); 396 397 return changed; 398 } 399 400 410 public boolean addAll( 411 Collection <? extends PdfIndirectObject> objects 412 ) 413 { 414 boolean changed = false; 415 for(PdfIndirectObject object : objects) 416 { 417 changed |= (addExternal(object) != null); 418 } 419 420 return changed; 421 } 422 423 public void clear( 424 ) 425 {throw new UnsupportedOperationException ();} 426 427 public boolean contains( 428 Object object 429 ) 430 {throw new NotImplementedException();} 431 432 public boolean containsAll( 433 Collection <?> objects 434 ) 435 {throw new NotImplementedException();} 436 437 public boolean equals( 438 Object object 439 ) 440 {throw new NotImplementedException();} 441 442 public int hashCode( 443 ) 444 {throw new NotImplementedException();} 445 446 public boolean isEmpty( 447 ) 448 { 449 453 for(PdfIndirectObject object : this) 454 { 455 if(object.isInUse()) 456 return false; 457 } 458 459 return true; 460 } 461 462 public boolean remove( 463 Object object 464 ) 465 {throw new NotImplementedException();} 466 467 public boolean removeAll( 468 Collection <?> objects 469 ) 470 {throw new NotImplementedException();} 471 472 public boolean retainAll( 473 Collection <?> objects 474 ) 475 {throw new UnsupportedOperationException ();} 476 477 482 public int size( 483 ) 484 {return (lastObjectNumber + 1);} 485 486 public PdfIndirectObject[] toArray( 487 ) 488 {throw new NotImplementedException();} 489 490 public <PdfIndirectObject> PdfIndirectObject[] toArray( 491 PdfIndirectObject[] objects 492 ) 493 {throw new NotImplementedException();} 494 495 public Iterator <PdfIndirectObject> iterator( 497 ) 498 { 499 return new Iterator <PdfIndirectObject>() 500 { 501 505 private int index = 0; 506 508 public boolean hasNext( 512 ) 513 {return (index < size());} 514 515 public PdfIndirectObject next( 516 ) 517 { 518 if(!hasNext()) 519 throw new NoSuchElementException (); 520 521 return get(index++); 522 } 523 524 public void remove( 525 ) 526 {throw new UnsupportedOperationException ();} 527 }; 533 } 534 539 544 public TreeMap <Integer ,PdfIndirectObject> getModifiedObjects( 545 ) 546 {return modifiedObjects;} 547 548 552 public PdfIndirectObject update( 553 PdfIndirectObject object 554 ) 555 { 556 int index = object.getReference().getObjectNumber(); 557 558 PdfIndirectObject old = get(index); 560 if(old != object) 561 {old.dropFile(); } 562 563 modifiedObjects.put(index,object); 565 wokenObjects.remove(index); 567 object.dropOriginal(); 569 570 return old; 571 } 572 } | Popular Tags |