1 31 package org.pdfbox.pdmodel.common; 32 33 import java.io.IOException ; 34 import java.lang.reflect.Constructor ; 35 import java.util.ArrayList ; 36 import java.util.Collections ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 import org.pdfbox.cos.COSArray; 42 import org.pdfbox.cos.COSBase; 43 import org.pdfbox.cos.COSDictionary; 44 import org.pdfbox.cos.COSString; 45 46 53 public class PDNameTreeNode implements COSObjectable 54 { 55 private COSDictionary node; 56 private Class valueType = null; 57 58 63 public PDNameTreeNode( Class valueClass ) 64 { 65 node = new COSDictionary(); 66 valueType = valueClass; 67 } 68 69 75 public PDNameTreeNode( COSDictionary dict, Class valueClass ) 76 { 77 node = dict; 78 valueType = valueClass; 79 } 80 81 86 public COSBase getCOSObject() 87 { 88 return node; 89 } 90 91 96 public COSDictionary getCOSDictionary() 97 { 98 return node; 99 } 100 101 106 public List getKids() 107 { 108 109 List retval = null; 110 COSArray kids = (COSArray)node.getDictionaryObject( "Kids" ); 111 if( kids != null ) 112 { 113 List pdObjects = new ArrayList (); 114 for( int i=0; i<kids.size(); i++ ) 115 { 116 pdObjects.add( createChildNode( (COSDictionary)kids.getObject(i) ) ); 117 } 118 retval = new COSArrayList(pdObjects,kids); 119 } 120 121 return retval; 122 } 123 124 129 public void setKids( List kids ) 130 { 131 node.setItem( "Kids", COSArrayList.converterToCOSArray( kids ) ); 132 } 133 134 143 public Object getValue( String name ) throws IOException 144 { 145 Object retval = null; 146 Map names = getNames(); 147 if( names != null ) 148 { 149 retval = names.get( name ); 150 } 151 else 152 { 153 List kids = getKids(); 154 for( int i=0; i<kids.size() && retval == null; i++ ) 155 { 156 PDNameTreeNode childNode = (PDNameTreeNode)kids.get( i ); 157 if( childNode.getLowerLimit().compareTo( name ) <= 0 && 158 childNode.getUpperLimit().compareTo( name ) >= 0 ) 159 { 160 retval = childNode.getValue( name ); 161 } 162 } 163 } 164 return retval; 165 } 166 167 168 176 public Map getNames() throws IOException 177 { 178 Map names = null; 179 COSArray namesArray = (COSArray)node.getDictionaryObject( "Names" ); 180 if( namesArray != null ) 181 { 182 names = new HashMap (); 183 for( int i=0; i<namesArray.size(); i+=2 ) 184 { 185 COSString key = (COSString)namesArray.getObject(i); 186 COSBase cosValue = namesArray.getObject( i+1 ); 187 Object pdValue = convertCOSToPD( cosValue ); 188 189 names.put( key.getString(), pdValue ); 190 } 191 names = Collections.unmodifiableMap(names); 192 } 193 194 return names; 195 } 196 197 206 protected Object convertCOSToPD( COSBase base ) throws IOException 207 { 208 Object retval = null; 209 try 210 { 211 Constructor ctor = valueType.getConstructor( new Class [] { base.getClass() } ); 212 retval = ctor.newInstance( new Object [] { base } ); 213 } 214 catch( Throwable t ) 215 { 216 throw new IOException ( "Error while trying to create value in named tree:" + t.getMessage()); 217 218 } 219 return retval; 220 } 221 222 228 protected PDNameTreeNode createChildNode( COSDictionary dic ) 229 { 230 return new PDNameTreeNode(dic,valueType); 231 } 232 233 240 public void setNames( Map names ) 241 { 242 if( names == null ) 243 { 244 node.setItem( "Names", (COSObjectable)null ); 245 node.setItem( "Limits", (COSObjectable)null); 246 } 247 else 248 { 249 List keys = new ArrayList ( names.keySet() ); 250 Collections.sort( keys ); 251 COSArray array = new COSArray(); 252 for( int i=0; i<keys.size(); i++ ) 253 { 254 String key = (String )keys.get(i); 255 array.add( new COSString( key ) ); 256 COSObjectable obj = (COSObjectable)names.get( key ); 257 array.add( obj ); 258 } 259 String lower = null; 260 String upper = null; 261 if( keys.size() > 0 ) 262 { 263 lower = (String )keys.get( 0 ); 264 upper = (String )keys.get( keys.size()-1 ); 265 } 266 setUpperLimit( upper ); 267 setLowerLimit( lower ); 268 node.setItem( "Names", array ); 269 } 270 } 271 272 277 public String getUpperLimit() 278 { 279 String retval = null; 280 COSArray arr = (COSArray)node.getDictionaryObject( "Limits" ); 281 if( arr != null ) 282 { 283 retval = arr.getString( 1 ); 284 } 285 return retval; 286 } 287 288 293 private void setUpperLimit( String upper ) 294 { 295 COSArray arr = (COSArray)node.getDictionaryObject( "Limits" ); 296 if( arr == null ) 297 { 298 arr = new COSArray(); 299 arr.add( null ); 300 arr.add( null ); 301 } 302 arr.setString( 1, upper ); 303 } 304 305 310 public String getLowerLimit() 311 { 312 String retval = null; 313 COSArray arr = (COSArray)node.getDictionaryObject( "Limits" ); 314 if( arr != null ) 315 { 316 retval = arr.getString( 0 ); 317 } 318 return retval; 319 } 320 321 326 private void setLowerLimit( String lower ) 327 { 328 COSArray arr = (COSArray)node.getDictionaryObject( "Limits" ); 329 if( arr == null ) 330 { 331 arr = new COSArray(); 332 arr.add( null ); 333 arr.add( null ); 334 } 335 arr.setString( 0, lower ); 336 } 337 } | Popular Tags |