| 1 31 package org.pdfbox.cos; 32 33 import java.io.IOException ; 34 import java.util.ArrayList ; 35 import java.util.Calendar ; 36 import java.util.Collection ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 import java.util.Iterator ; 42 43 import org.pdfbox.exceptions.COSVisitorException; 44 45 import org.pdfbox.pdmodel.common.COSObjectable; 46 import org.pdfbox.util.DateConverter; 47 48 54 public class COSDictionary extends COSBase 55 { 56 private static final String PATH_SEPARATOR = "/"; 57 58 61 private Map items = new HashMap (); 62 63 66 private List keys = new ArrayList (); 67 68 71 public COSDictionary() 72 { 73 } 75 76 81 public COSDictionary( COSDictionary dict ) 82 { 83 items = new HashMap ( dict.items ); 84 keys = new ArrayList ( dict.keys ); 85 } 86 87 94 public boolean containsValue( Object value ) 95 { 96 boolean contains = items.containsValue( value ); 97 if( !contains && value instanceof COSObject ) 98 { 99 contains = items.containsValue( ((COSObject)value).getObject()); 100 } 101 return contains; 102 } 103 104 111 public COSName getKeyForValue( Object value ) 112 { 113 COSName key = null; 114 Iterator iter = items.entrySet().iterator(); 115 while( key == null && iter.hasNext() ) 116 { 117 Map.Entry next = (Map.Entry )iter.next(); 118 Object nextValue = next.getValue(); 119 if( nextValue.equals( value ) || 120 (nextValue instanceof COSObject && 121 ((COSObject)nextValue).getObject().equals( value)) 122 ) 123 { 124 key = (COSName)next.getKey(); 125 } 126 } 127 128 return key; 129 } 130 131 136 public int size() 137 { 138 return keys.size(); 139 } 140 141 144 public void clear() 145 { 146 items.clear(); 147 keys.clear(); 148 } 149 150 159 public COSBase getDictionaryObject( String key ) 160 { 161 return getDictionaryObject( COSName.getPDFName( key ) ); 162 } 163 164 177 public COSBase getDictionaryObject( String firstKey, String secondKey ) 178 { 179 COSBase retval = getDictionaryObject( COSName.getPDFName( firstKey ) ); 180 if( retval == null ) 181 { 182 retval = getDictionaryObject( COSName.getPDFName( secondKey ) ); 183 } 184 return retval; 185 } 186 187 199 public COSBase getDictionaryObject( String [] keyList ) 200 { 201 COSBase retval = null; 202 for( int i=0; i<keyList.length && retval == null; i++ ) 203 { 204 retval = getDictionaryObject( COSName.getPDFName( keyList[i] ) ); 205 } 206 return retval; 207 } 208 209 218 public COSBase getDictionaryObject( COSName key ) 219 { 220 COSBase retval = (COSBase)items.get( key ); 221 if( retval instanceof COSObject ) 222 { 223 retval = ((COSObject)retval).getObject(); 224 } 225 if( retval instanceof COSNull ) 226 { 227 retval = null; 228 } 229 return retval; 230 } 231 232 239 public void setItem( COSName key, COSBase value ) 240 { 241 if( value == null ) 242 { 243 removeItem( key ); 244 } 245 else 246 { 247 if (!items.containsKey(key)) 248 { 249 keys.add(key); 251 } 252 items.put( key, value ); 253 } 254 } 255 256 263 public void setItem( COSName key, COSObjectable value ) 264 { 265 COSBase base = null; 266 if( value != null ) 267 { 268 base = value.getCOSObject(); 269 } 270 setItem( key, base ); 271 } 272 273 280 public void setItem( String key, COSObjectable value ) 281 { 282 setItem( COSName.getPDFName( key ), value ); 283 } 284 285 291 public void setBoolean( String key, boolean value ) 292 { 293 setItem( COSName.getPDFName( key ), COSBoolean.getBoolean( value ) ); 294 } 295 296 302 public void setBoolean( COSName key, boolean value ) 303 { 304 setItem( key , COSBoolean.getBoolean( value ) ); 305 } 306 307 314 public void setItem( String key, COSBase value ) 315 { 316 setItem( COSName.getPDFName( key ), value ); 317 } 318 319 326 public void setName( String key, String value ) 327 { 328 setName( COSName.getPDFName( key ), value ); 329 } 330 331 338 public void setName( COSName key, String value ) 339 { 340 COSName name = null; 341 if( value != null ) 342 { 343 name = COSName.getPDFName( value ); 344 } 345 setItem( key, name ); 346 } 347 348 354 public void setDate( String key, Calendar date ) 355 { 356 setDate( COSName.getPDFName( key ), date ); 357 } 358 359 365 public void setDate( COSName key, Calendar date ) 366 { 367 setString( key, DateConverter.toString( date ) ); 368 } 369 370 377 public void setEmbeddedDate( String embedded, String key, Calendar date ) 378 { 379 setEmbeddedDate( embedded, COSName.getPDFName( key ), date ); 380 } 381 382 389 public void setEmbeddedDate( String embedded, COSName key, Calendar date ) 390 { 391 COSDictionary dic = (COSDictionary)getDictionaryObject( embedded ); 392 if( dic == null && date != null ) 393 { 394 dic = new COSDictionary(); 395 setItem( embedded, dic ); 396 } 397 if( dic != null ) 398 { 399 dic.setDate( key, date ); 400 } 401 } 402 403 410 public void setString( String key, String value ) 411 { 412 setString( COSName.getPDFName( key ), value ); 413 } 414 415 422 public void setString( COSName key, String value ) 423 { 424 COSString name = null; 425 if( value != null ) 426 { 427 name = new COSString( value ); 428 } 429 setItem( key, name ); 430 } 431 432 440 public void setEmbeddedString( String embedded, String key, String value ) 441 { 442 setEmbeddedString( embedded, COSName.getPDFName( key ), value ); 443 } 444 445 453 public void setEmbeddedString( String embedded, COSName key, String value ) 454 { 455 COSDictionary dic = (COSDictionary)getDictionaryObject( embedded ); 456 if( dic == null && value != null ) 457 { 458 dic = new COSDictionary(); 459 setItem( embedded, dic ); 460 } 461 if( dic != null ) 462 { 463 dic.setString( key, value ); 464 } 465 } 466 467 474 public void setInt( String key, int value ) 475 { 476 setInt( COSName.getPDFName( key ), value ); 477 } 478 479 486 public void setInt( COSName key, int value ) 487 { 488 COSInteger intVal = null; 489 intVal = new COSInteger(value); 490 setItem( key, intVal ); 491 } 492 493 500 public void setLong( String key, long value ) 501 { 502 setLong( COSName.getPDFName( key ), value ); 503 } 504 505 512 public void setLong( COSName key, long value ) 513 { 514 COSInteger intVal = null; 515 intVal = new COSInteger(value); 516 setItem( key, intVal ); 517 } 518 519 527 public void setEmbeddedInt( String embeddedDictionary, String key, int value ) 528 { 529 setEmbeddedInt( embeddedDictionary, COSName.getPDFName( key ), value ); 530 } 531 532 540 public void setEmbeddedInt( String embeddedDictionary, COSName key, int value ) 541 { 542 COSDictionary embedded = (COSDictionary)getDictionaryObject( embeddedDictionary ); 543 if( embedded == null ) 544 { 545 embedded = new COSDictionary(); 546 setItem( embeddedDictionary, embedded ); 547 } 548 embedded.setInt( key, value ); 549 } 550 551 558 public void setFloat( String key, float value ) 559 { 560 setFloat( COSName.getPDFName( key ), value ); 561 } 562 563 570 public void setFloat( COSName key, float value ) 571 { 572 COSFloat fltVal = new COSFloat( value ); 573 setItem( key, fltVal ); 574 } 575 576 584 public String getNameAsString( String key ) 585 { 586 return getNameAsString( COSName.getPDFName( key ) ); 587 } 588 589 597 public String getNameAsString( COSName key ) 598 { 599 String retval = null; 600 COSName name = (COSName)getDictionaryObject( key ); 601 if( name != null ) 602 { 603 retval = name.getName(); 604 } 605 return retval; 606 } 607 608 617 public String getNameAsString( String key, String defaultValue ) 618 { 619 return getNameAsString( COSName.getPDFName( key ), defaultValue ); 620 } 621 622 631 public String getNameAsString( COSName key, String defaultValue ) 632 { 633 String retval = getNameAsString( key ); 634 if( retval == null ) 635 { 636 retval = defaultValue; 637 } 638 return retval; 639 } 640 641 649 public String getString( String key ) 650 { 651 return getString( COSName.getPDFName( key ) ); 652 } 653 654 662 public String getString( COSName key ) 663 { 664 String retval = null; 665 COSString name = (COSString)getDictionaryObject( key ); 666 if( name != null ) 667 { 668 retval = name.getString(); 669 } 670 return retval; 671 } 672 673 682 public String getString( String key, String defaultValue ) 683 { 684 return getString( COSName.getPDFName( key ), defaultValue ); 685 } 686 687 696 public String getString( COSName key, String defaultValue ) 697 { 698 String retval = getString( key ); 699 if( retval == null ) 700 { 701 retval = defaultValue; 702 } 703 return retval; 704 } 705 706 715 public String getEmbeddedString( String embedded, String key ) 716 { 717 return getEmbeddedString( embedded, COSName.getPDFName( key ), null ); 718 } 719 720 729 public String getEmbeddedString( String embedded, COSName key ) 730 { 731 return getEmbeddedString( embedded, key, null ); 732 } 733 734 744 public String getEmbeddedString( String embedded, String key, String defaultValue ) 745 { 746 return getEmbeddedString( embedded, COSName.getPDFName( key ), defaultValue ); 747 } 748 749 759 public String getEmbeddedString( String embedded, COSName key, String defaultValue ) 760 { 761 String retval = defaultValue; 762 COSDictionary dic = (COSDictionary)getDictionaryObject( embedded ); 763 if( dic != null ) 764 { 765 retval = dic.getString( key, defaultValue ); 766 } 767 return retval; 768 } 769 770 779 public Calendar getDate( String key ) throws IOException  780 { 781 return getDate( COSName.getPDFName( key ) ); 782 } 783 784 794 public Calendar getDate( COSName key ) throws IOException  795 { 796 COSString date = (COSString)getDictionaryObject( key ); 797 return DateConverter.toCalendar( date ); 798 } 799 800 810 public Calendar getDate( String key, Calendar defaultValue ) throws IOException  811 { 812 return getDate( COSName.getPDFName( key ), defaultValue ); 813 } 814 815 825 public Calendar getDate( COSName key, Calendar defaultValue ) throws IOException  826 { 827 Calendar retval = getDate( key ); 828 if( retval == null ) 829 { 830 retval = defaultValue; 831 } 832 return retval; 833 } 834 835 845 public Calendar getEmbeddedDate( String embedded, String key ) throws IOException  846 { 847 return getEmbeddedDate( embedded, COSName.getPDFName( key ), null ); 848 } 849 850 861 public Calendar getEmbeddedDate( String embedded, COSName key ) throws IOException  862 { 863 return getEmbeddedDate( embedded, key, null ); 864 } 865 866 877 public Calendar getEmbeddedDate( String embedded, String key, Calendar defaultValue ) throws IOException  878 { 879 return getEmbeddedDate( embedded, COSName.getPDFName( key ), defaultValue ); 880 } 881 882 893 public Calendar getEmbeddedDate( String embedded, COSName key, Calendar defaultValue ) throws IOException  894 { 895 Calendar retval = defaultValue; 896 COSDictionary eDic = (COSDictionary)getDictionaryObject( embedded ); 897 if( eDic != null ) 898 { 899 retval = eDic.getDate( key, defaultValue ); 900 } 901 return retval; 902 } 903 904 913 public boolean getBoolean( String key, boolean defaultValue ) 914 { 915 return getBoolean( COSName.getPDFName( key ), defaultValue ); 916 } 917 918 927 public boolean getBoolean( COSName key, boolean defaultValue ) 928 { 929 boolean retval = defaultValue; 930 COSBoolean bool = (COSBoolean)getDictionaryObject( key ); 931 if( bool != null ) 932 { 933 retval = bool.getValue(); 934 } 935
|