1 31 package org.pdfbox.pdmodel.common; 32 33 import org.pdfbox.cos.COSBase; 34 import org.pdfbox.cos.COSBoolean; 35 import org.pdfbox.cos.COSDictionary; 36 import org.pdfbox.cos.COSFloat; 37 import org.pdfbox.cos.COSInteger; 38 import org.pdfbox.cos.COSName; 39 import org.pdfbox.cos.COSString; 40 41 import java.io.IOException ; 42 43 import java.util.Collection ; 44 import java.util.HashMap ; 45 import java.util.Iterator ; 46 import java.util.Map ; 47 import java.util.Set ; 48 49 55 public class COSDictionaryMap implements Map 56 { 57 private COSDictionary map; 58 private Map actuals; 59 60 66 public COSDictionaryMap( Map actualsMap, COSDictionary dicMap ) 67 { 68 actuals = actualsMap; 69 map = dicMap; 70 } 71 72 73 76 public int size() 77 { 78 return map.size(); 79 } 80 81 84 public boolean isEmpty() 85 { 86 return size() == 0; 87 } 88 89 92 public boolean containsKey(Object key) 93 { 94 return map.keyList().contains( key ); 95 } 96 97 100 public boolean containsValue(Object value) 101 { 102 return actuals.containsValue( value ); 103 } 104 105 108 public Object get(Object key) 109 { 110 return actuals.get( key ); 111 } 112 113 116 public Object put(Object key, Object value) 117 { 118 COSObjectable object = (COSObjectable)value; 119 120 map.setItem( COSName.getPDFName( (String )key ), object.getCOSObject() ); 121 return actuals.put( key, value ); 122 } 123 124 127 public Object remove(Object key) 128 { 129 map.removeItem( COSName.getPDFName( (String )key ) ); 130 return actuals.remove( key ); 131 } 132 133 136 public void putAll(Map t) 137 { 138 throw new RuntimeException ( "Not yet implemented" ); 139 } 140 141 144 public void clear() 145 { 146 map.clear(); 147 actuals.clear(); 148 } 149 150 153 public Set keySet() 154 { 155 return actuals.keySet(); 156 } 157 158 161 public Collection values() 162 { 163 return actuals.values(); 164 } 165 166 169 public Set entrySet() 170 { 171 throw new RuntimeException ( "Not yet implemented" ); 172 } 173 174 177 public boolean equals(Object o) 178 { 179 boolean retval = false; 180 if( o instanceof COSDictionaryMap ) 181 { 182 COSDictionaryMap other = (COSDictionaryMap)o; 183 retval = other.map.equals( this.map ); 184 } 185 return retval; 186 } 187 188 191 public String toString() 192 { 193 return actuals.toString(); 194 } 195 196 199 public int hashCode() 200 { 201 return map.hashCode(); 202 } 203 204 212 public static COSDictionary convert( Map someMap ) 213 { 214 Iterator iter = someMap.keySet().iterator(); 215 COSDictionary dic = new COSDictionary(); 216 while( iter.hasNext() ) 217 { 218 String name = (String )iter.next(); 219 COSObjectable object = (COSObjectable)someMap.get( name ); 220 dic.setItem( COSName.getPDFName( name ), object.getCOSObject() ); 221 } 222 return dic; 223 } 224 225 233 public static COSDictionaryMap convertBasicTypesToMap( COSDictionary map ) throws IOException 234 { 235 COSDictionaryMap retval = null; 236 if( map != null ) 237 { 238 Map actualMap = new HashMap (); 239 Iterator keyIter = map.keyList().iterator(); 240 while( keyIter.hasNext() ) 241 { 242 COSName key = (COSName)keyIter.next(); 243 COSBase cosObj = map.getDictionaryObject( key ); 244 Object actualObject = null; 245 if( cosObj instanceof COSString ) 246 { 247 actualObject = ((COSString)cosObj).getString(); 248 } 249 else if( cosObj instanceof COSInteger ) 250 { 251 actualObject = new Integer ( ((COSInteger)cosObj).intValue() ); 252 } 253 else if( cosObj instanceof COSName ) 254 { 255 actualObject = ((COSName)cosObj).getName(); 256 } 257 else if( cosObj instanceof COSFloat ) 258 { 259 actualObject = new Float ( ((COSInteger)cosObj).floatValue() ); 260 } 261 else if( cosObj instanceof COSBoolean ) 262 { 263 actualObject = ((COSBoolean)cosObj).getValue() ? Boolean.TRUE : Boolean.FALSE; 264 } 265 else 266 { 267 throw new IOException ( "Error:unknown type of object to convert:" + cosObj ); 268 } 269 actualMap.put( key.getName(), actualObject ); 270 } 271 retval = new COSDictionaryMap( actualMap, map ); 272 } 273 274 return retval; 275 } 276 } | Popular Tags |