1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.cos; 18 19 import org.apache.pdfbox.exceptions.COSVisitorException; 20 21 import java.io.IOException; 22 23 /** 24 * This class represents a PDF object. 25 * 26 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 27 * @version $Revision: 1.37 $ 28 */ 29 public class COSObject extends COSBase 30 { 31 private COSBase baseObject; 32 private COSInteger objectNumber; 33 private COSInteger generationNumber; 34 35 /** 36 * Constructor. 37 * 38 * @param object The object that this encapsulates. 39 * 40 * @throws IOException If there is an error with the object passed in. 41 */ 42 public COSObject( COSBase object ) throws IOException 43 { 44 setObject( object ); 45 } 46 47 /** 48 * This will get the dictionary object in this object that has the name key and 49 * if it is a pdfobjref then it will dereference that and return it. 50 * 51 * @param key The key to the value that we are searching for. 52 * 53 * @return The pdf object that matches the key. 54 */ 55 public COSBase getDictionaryObject( COSName key ) 56 { 57 COSBase retval =null; 58 if( baseObject instanceof COSDictionary ) 59 { 60 retval = ((COSDictionary)baseObject).getDictionaryObject( key ); 61 } 62 return retval; 63 } 64 65 /** 66 * This will get the dictionary object in this object that has the name key. 67 * 68 * @param key The key to the value that we are searching for. 69 * 70 * @return The pdf object that matches the key. 71 */ 72 public COSBase getItem( COSName key ) 73 { 74 COSBase retval =null; 75 if( baseObject instanceof COSDictionary ) 76 { 77 retval = ((COSDictionary)baseObject).getItem( key ); 78 } 79 return retval; 80 } 81 82 /** 83 * This will get the object that this object encapsulates. 84 * 85 * @return The encapsulated object. 86 */ 87 public COSBase getObject() 88 { 89 return baseObject; 90 } 91 92 /** 93 * This will set the object that this object encapsulates. 94 * 95 * @param object The new object to encapsulate. 96 * 97 * @throws IOException If there is an error setting the updated object. 98 */ 99 public void setObject( COSBase object ) throws IOException 100 { 101 baseObject = object; 102 /*if( baseObject == null ) 103 { 104 baseObject = object; 105 } 106 else 107 { 108 //This is for when an object appears twice in the 109 //pdf file we really want to replace it such that 110 //object references still work correctly. 111 //see owcp-as-received.pdf for an example 112 if( baseObject instanceof COSDictionary ) 113 { 114 COSDictionary dic = (COSDictionary)baseObject; 115 COSDictionary dicObject = (COSDictionary)object; 116 dic.clear(); 117 dic.addAll( dicObject ); 118 } 119 else if( baseObject instanceof COSArray ) 120 { 121 COSArray array = (COSArray)baseObject; 122 COSArray arrObject = (COSArray)object; 123 array.clear(); 124 for( int i=0; i<arrObject.size(); i++ ) 125 { 126 array.add( arrObject.get( i ) ); 127 } 128 } 129 else if( baseObject instanceof COSStream ) 130 { 131 COSStream oldStream = (COSStream)baseObject; 132 System.out.println( "object:" + object.getClass().getName() ); 133 COSStream newStream = (COSStream)object; 134 oldStream.replaceWithStream( newStream ); 135 } 136 else if( baseObject instanceof COSInteger ) 137 { 138 COSInteger oldInt = (COSInteger)baseObject; 139 COSInteger newInt = (COSInteger)object; 140 oldInt.setValue( newInt.longValue() ); 141 } 142 else if( baseObject == null ) 143 { 144 baseObject = object; 145 } 146 else 147 { 148 throw new IOException( "Unknown object substitution type:" + baseObject ); 149 } 150 }*/ 151 152 } 153 154 /** 155 * {@inheritDoc} 156 */ 157 public String toString() 158 { 159 return "COSObject{" + 160 (objectNumber == null ? "unknown" : "" + objectNumber.intValue() ) + ", " + 161 (generationNumber == null ? "unknown" : "" + generationNumber.intValue() ) + 162 "}"; 163 } 164 165 /** Getter for property objectNumber. 166 * @return Value of property objectNumber. 167 */ 168 public COSInteger getObjectNumber() 169 { 170 return objectNumber; 171 } 172 173 /** Setter for property objectNumber. 174 * @param objectNum New value of property objectNumber. 175 */ 176 public void setObjectNumber(COSInteger objectNum) 177 { 178 objectNumber = objectNum; 179 } 180 181 /** Getter for property generationNumber. 182 * @return Value of property generationNumber. 183 */ 184 public COSInteger getGenerationNumber() 185 { 186 return generationNumber; 187 } 188 189 /** Setter for property generationNumber. 190 * @param generationNumberValue New value of property generationNumber. 191 */ 192 public void setGenerationNumber(COSInteger generationNumberValue) 193 { 194 generationNumber = generationNumberValue; 195 } 196 197 /** 198 * visitor pattern double dispatch method. 199 * 200 * @param visitor The object to notify when visiting this object. 201 * @return any object, depending on the visitor implementation, or null 202 * @throws COSVisitorException If an error occurs while visiting this object. 203 */ 204 public Object accept( ICOSVisitor visitor ) throws COSVisitorException 205 { 206 return getObject() != null ? getObject().accept( visitor ) : COSNull.NULL.accept( visitor ); 207 } 208 }