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 return retval; 936 } 937 938 946 public int getEmbeddedInt( String embeddedDictionary, String key ) 947 { 948 return getEmbeddedInt( embeddedDictionary, COSName.getPDFName( key ) ); 949 } 950 951 959 public int getEmbeddedInt( String embeddedDictionary, COSName key ) 960 { 961 return getEmbeddedInt( embeddedDictionary, key, -1 ); 962 } 963 964 973 public int getEmbeddedInt( String embeddedDictionary, String key, int defaultValue ) 974 { 975 return getEmbeddedInt( embeddedDictionary, COSName.getPDFName( key ), defaultValue ); 976 } 977 978 979 988 public int getEmbeddedInt( String embeddedDictionary, COSName key, int defaultValue ) 989 { 990 int retval = defaultValue; 991 COSDictionary embedded = (COSDictionary)getDictionaryObject( embeddedDictionary ); 992 if( embedded != null ) 993 { 994 retval = embedded.getInt( key, defaultValue ); 995 } 996 return retval; 997 } 998 999 1006 public int getInt( String key ) 1007 { 1008 return getInt( COSName.getPDFName( key ) ); 1009 } 1010 1011 1018 public int getInt( COSName key ) 1019 { 1020 return getInt( key, -1 ); 1021 } 1022 1023 1032 public int getInt( String [] keyList, int defaultValue ) 1033 { 1034 int retval = defaultValue; 1035 COSNumber obj = (COSNumber)getDictionaryObject( keyList ); 1036 if( obj != null ) 1037 { 1038 retval = obj.intValue(); 1039 } 1040 return retval; 1041 } 1042 1043 1052 public int getInt( String key, int defaultValue ) 1053 { 1054 return getInt( new String []{ key }, defaultValue ); 1055 } 1056 1057 1066 public int getInt( COSName key, int defaultValue ) 1067 { 1068 return getInt(key.getName(), defaultValue ); 1069 } 1070 1071 1079 public long getLong( String key ) 1080 { 1081 return getLong( COSName.getPDFName( key ) ); 1082 } 1083 1084 1091 public long getLong( COSName key ) 1092 { 1093 return getLong( key, -1L ); 1094 } 1095 1096 1105 public long getLong( String [] keyList, long defaultValue ) 1106 { 1107 long retval = defaultValue; 1108 COSNumber obj = (COSNumber)getDictionaryObject( keyList ); 1109 if( obj != null ) 1110 { 1111 retval = obj.longValue(); 1112 } 1113 return retval; 1114 } 1115 1116 1125 public long getLong( String key, long defaultValue ) 1126 { 1127 return getLong( new String []{ key }, defaultValue ); 1128 } 1129 1130 1139 public long getLong( COSName key, long defaultValue ) 1140 { 1141 return getLong(key.getName(), defaultValue ); 1142 } 1143 1144 1151 public float getFloat( String key ) 1152 { 1153 return getFloat( COSName.getPDFName( key ) ); 1154 } 1155 1156 1163 public float getFloat( COSName key ) 1164 { 1165 return getFloat( key, -1 ); 1166 } 1167 1168 1177 public float getFloat( String key, float defaultValue ) 1178 { 1179 return getFloat( COSName.getPDFName( key ), defaultValue ); 1180 } 1181 1182 1191 public float getFloat( COSName key, float defaultValue ) 1192 { 1193 float retval = defaultValue; 1194 COSNumber obj = (COSNumber)getDictionaryObject( key ); 1195 if( obj != null ) 1196 { 1197 retval = obj.floatValue(); 1198 } 1199 return retval; 1200 } 1201 1202 1208 public void removeItem( COSName key ) 1209 { 1210 keys.remove( key ); 1211 items.remove( key ); 1212 } 1213 1214 1221 public COSBase getItem( COSName key ) 1222 { 1223 return (COSBase)items.get( key ); 1224 } 1225 1226 1227 1228 1229 1230 1237 public List keyList() 1238 { 1239 return keys; 1240 } 1241 1242 1247 public Collection getValues() 1248 { 1249 return items.values(); 1250 } 1251 1252 1260 public Object accept(ICOSVisitor visitor) throws COSVisitorException 1261 { 1262 return visitor.visitFromDictionary(this); 1263 } 1264 1265 1270 public void addAll( COSDictionary dic ) 1271 { 1272 Iterator dicKeys = dic.keyList().iterator(); 1273 while( dicKeys.hasNext() ) 1274 { 1275 COSName key = (COSName)dicKeys.next(); 1276 COSBase value = dic.getItem( key ); 1277 setItem( key, value ); 1278 } 1279 } 1280 1281 1288 public void mergeInto( COSDictionary dic ) 1289 { 1290 Iterator dicKeys = dic.keyList().iterator(); 1291 while( dicKeys.hasNext() ) 1292 { 1293 COSName key = (COSName)dicKeys.next(); 1294 COSBase value = dic.getItem( key ); 1295 if( getItem( key ) == null ) 1296 { 1297 setItem( key, value ); 1298 } 1299 } 1300 } 1301 1302 1310 public COSBase getObjectFromPath(String objPath) 1311 { 1312 COSBase retval = null; 1313 String [] path = objPath.split(PATH_SEPARATOR); 1314 retval = this; 1315 1316 for (int i = 0; i < path.length; i++) 1317 { 1318 if(retval instanceof COSArray) 1319 { 1320 int idx = new Integer (path[i].replaceAll("\\[","").replaceAll("\\]","")).intValue(); 1321 retval = ((COSArray)retval).getObject(idx); 1322 } 1323 else if (retval instanceof COSDictionary) 1324 { 1325 retval = ((COSDictionary)retval).getDictionaryObject( path[i] ); 1326 } 1327 } 1328 return retval; 1329 } 1330 1331} | Popular Tags |