KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > cos > COSObject


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.cos;
32
33 import org.pdfbox.exceptions.COSVisitorException;
34
35 import java.io.IOException JavaDoc;
36
37 /**
38  * This class represents a PDF object.
39  *
40  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
41  * @version $Revision: 1.37 $
42  */

43 public class COSObject extends COSBase
44 {
45     private COSBase baseObject;
46     private COSInteger objectNumber;
47     private COSInteger generationNumber;
48
49     /**
50      * Constructor.
51      *
52      * @param object The object that this encapsulates.
53      *
54      * @throws IOException If there is an error with the object passed in.
55      */

56     public COSObject( COSBase object ) throws IOException JavaDoc
57     {
58         setObject( object );
59     }
60
61     /**
62      * This will get the dictionary object in this object that has the name key and
63      * if it is a pdfobjref then it will dereference that and return it.
64      *
65      * @param key The key to the value that we are searching for.
66      *
67      * @return The pdf object that matches the key.
68      */

69     public COSBase getDictionaryObject( COSName key )
70     {
71         COSBase retval =null;
72         if( baseObject instanceof COSDictionary )
73         {
74             retval = ((COSDictionary)baseObject).getDictionaryObject( key );
75         }
76         return retval;
77     }
78
79     /**
80      * This will get the dictionary object in this object that has the name key.
81      *
82      * @param key The key to the value that we are searching for.
83      *
84      * @return The pdf object that matches the key.
85      */

86     public COSBase getItem( COSName key )
87     {
88         COSBase retval =null;
89         if( baseObject instanceof COSDictionary )
90         {
91             retval = ((COSDictionary)baseObject).getItem( key );
92         }
93         return retval;
94     }
95
96     /**
97      * This will get the object that this object encapsulates.
98      *
99      * @return The encapsulated object.
100      */

101     public COSBase getObject()
102     {
103         return baseObject;
104     }
105
106     /**
107      * This will set the object that this object encapsulates.
108      *
109      * @param object The new object to encapsulate.
110      *
111      * @throws IOException If there is an error setting the updated object.
112      */

113     public void setObject( COSBase object ) throws IOException JavaDoc
114     {
115         baseObject = object;
116         /*if( baseObject == null )
117         {
118             baseObject = object;
119         }
120         else
121         {
122             //This is for when an object appears twice in the
123             //pdf file we really want to replace it such that
124             //object references still work correctly.
125             //see owcp-as-received.pdf for an example
126             if( baseObject instanceof COSDictionary )
127             {
128                 COSDictionary dic = (COSDictionary)baseObject;
129                 COSDictionary dicObject = (COSDictionary)object;
130                 dic.clear();
131                 dic.addAll( dicObject );
132             }
133             else if( baseObject instanceof COSArray )
134             {
135                 COSArray array = (COSArray)baseObject;
136                 COSArray arrObject = (COSArray)object;
137                 array.clear();
138                 for( int i=0; i<arrObject.size(); i++ )
139                 {
140                     array.add( arrObject.get( i ) );
141                 }
142             }
143             else if( baseObject instanceof COSStream )
144             {
145                 COSStream oldStream = (COSStream)baseObject;
146                 System.out.println( "object:" + object.getClass().getName() );
147                 COSStream newStream = (COSStream)object;
148                 oldStream.replaceWithStream( newStream );
149             }
150             else if( baseObject instanceof COSInteger )
151             {
152                 COSInteger oldInt = (COSInteger)baseObject;
153                 COSInteger newInt = (COSInteger)object;
154                 oldInt.setValue( newInt.longValue() );
155             }
156             else if( baseObject == null )
157             {
158                 baseObject = object;
159             }
160             else
161             {
162                 throw new IOException( "Unknown object substitution type:" + baseObject );
163             }
164         }*/

165
166     }
167
168     /**
169      * {@inheritDoc}
170      */

171     public String JavaDoc toString()
172     {
173         return "COSObject{" +
174             (objectNumber == null ? "unknown" : "" + objectNumber.intValue() ) + ", " +
175             (generationNumber == null ? "unknown" : "" + generationNumber.intValue() ) +
176             "}";
177     }
178
179     /** Getter for property objectNumber.
180      * @return Value of property objectNumber.
181      */

182     public COSInteger getObjectNumber()
183     {
184         return objectNumber;
185     }
186
187     /** Setter for property objectNumber.
188      * @param objectNum New value of property objectNumber.
189      */

190     public void setObjectNumber(COSInteger objectNum)
191     {
192         objectNumber = objectNum;
193     }
194
195     /** Getter for property generationNumber.
196      * @return Value of property generationNumber.
197      */

198     public COSInteger getGenerationNumber()
199     {
200         return generationNumber;
201     }
202
203     /** Setter for property generationNumber.
204      * @param generationNumberValue New value of property generationNumber.
205      */

206     public void setGenerationNumber(COSInteger generationNumberValue)
207     {
208         generationNumber = generationNumberValue;
209     }
210
211     /**
212      * visitor pattern double dispatch method.
213      *
214      * @param visitor The object to notify when visiting this object.
215      * @return any object, depending on the visitor implementation, or null
216      * @throws COSVisitorException If an error occurs while visiting this object.
217      */

218     public Object JavaDoc accept( ICOSVisitor visitor ) throws COSVisitorException
219     {
220         return getObject() != null ? getObject().accept( visitor ) : COSNull.NULL.accept( visitor );
221     }
222 }
Popular Tags