KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfObject


1 /*
2  * $Id: PdfObject.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52 import java.io.IOException JavaDoc;
53 import java.io.OutputStream JavaDoc;
54
55 /**
56  * <CODE>PdfObject</CODE> is the abstract superclass of all PDF objects.
57  * <P>
58  * PDF supports seven basic types of objects: Booleans, numbers, strings, names,
59  * arrays, dictionaries and streams. In addition, PDF provides a null object.
60  * Objects may be labeled so that they can be referred to by other objects.<BR>
61  * All these basic PDF objects are described in the 'Portable Document Format
62  * Reference Manual version 1.3' Chapter 4 (pages 37-54).
63  *
64  * @see PdfNull
65  * @see PdfBoolean
66  * @see PdfNumber
67  * @see PdfString
68  * @see PdfName
69  * @see PdfArray
70  * @see PdfDictionary
71  * @see PdfStream
72  * @see PdfIndirectReference
73  */

74
75 public abstract class PdfObject {
76     
77     // static membervariables (all the possible types of a PdfObject)
78

79 /** a possible type of <CODE>PdfObject</CODE> */
80     public static final int BOOLEAN = 1;
81     
82 /** a possible type of <CODE>PdfObject</CODE> */
83     public static final int NUMBER = 2;
84     
85 /** a possible type of <CODE>PdfObject</CODE> */
86     public static final int STRING = 3;
87     
88 /** a possible type of <CODE>PdfObject</CODE> */
89     public static final int NAME = 4;
90     
91 /** a possible type of <CODE>PdfObject</CODE> */
92     public static final int ARRAY = 5;
93     
94 /** a possible type of <CODE>PdfObject</CODE> */
95     public static final int DICTIONARY = 6;
96     
97 /** a possible type of <CODE>PdfObject</CODE> */
98     public static final int STREAM = 7;
99
100 /** a possible type of <CODE>PdfObject</CODE> */
101     public static final int NULL = 8;
102     
103     /** a possible type of <CODE>PdfObject</CODE> */
104     public static final int INDIRECT = 10;
105
106 /** This is an empty string used for the <CODE>PdfNull</CODE>-object and for an empty <CODE>PdfString</CODE>-object. */
107     public static final String JavaDoc NOTHING = "";
108     
109 /** This is the default encoding to be used for converting Strings into bytes and vice versa.
110  * The default encoding is PdfDocEncoding.
111  */

112     public static final String JavaDoc TEXT_PDFDOCENCODING = "PDF";
113     
114 /** This is the encoding to be used to output text in Unicode. */
115     public static final String JavaDoc TEXT_UNICODE = "UnicodeBig";
116     
117     // membervariables
118

119 /** the content of this <CODE>PdfObject</CODE> */
120     protected byte[] bytes;
121     
122 /** the type of this <CODE>PdfObject</CODE> */
123     protected int type;
124     
125     /**
126      * Holds value of property indRef.
127      */

128     protected PRIndirectReference indRef;
129     
130     // constructors
131

132 /**
133  * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> without any <VAR>content</VAR>.
134  *
135  * @param type type of the new <CODE>PdfObject</CODE>
136  */

137     
138     protected PdfObject(int type) {
139         this.type = type;
140     }
141     
142 /**
143  * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
144  *
145  * @param type type of the new <CODE>PdfObject</CODE>
146  * @param content content of the new <CODE>PdfObject</CODE> as a <CODE>String</CODE>.
147  */

148     
149     protected PdfObject(int type, String JavaDoc content) {
150         this.type = type;
151         bytes = PdfEncodings.convertToBytes(content, null);
152     }
153     
154 /**
155  * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
156  *
157  * @param type type of the new <CODE>PdfObject</CODE>
158  * @param bytes content of the new <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>.
159  */

160     
161     protected PdfObject(int type, byte[] bytes) {
162         this.bytes = bytes;
163         this.type = type;
164     }
165     
166     // methods dealing with the content of this object
167

168 /**
169  * Writes the PDF representation of this <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>s to the writer.
170  * @param writer for backwards compatibility
171  * @param os the outputstream to write the bytes to.
172  * @throws IOException
173  */

174     
175     public void toPdf(PdfWriter writer, OutputStream JavaDoc os) throws IOException JavaDoc {
176         if (bytes != null)
177             os.write(bytes);
178     }
179     
180     /**
181      * Gets the presentation of this object in a byte array
182      * @return a byte array
183      */

184     public byte[] getBytes() {
185         return bytes;
186     }
187
188     /**
189      * Can this object be in an object stream?
190      * @return true if this object can be in an object stream.
191      */

192     public boolean canBeInObjStm() {
193         return (type >= 1 && type <= 6) || type == 8;
194     }
195     
196 /**
197  * Returns the length of the PDF representation of the <CODE>PdfObject</CODE>.
198  * <P>
199  * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
200  * this method differs from the method <CODE>length</CODE> because <CODE>length</CODE>
201  * returns the length of the actual content of the <CODE>PdfObject</CODE>.</P>
202  * <P>
203  * Remark: the actual content of an object is in most cases identical to its representation.
204  * The following statement is always true: length() &gt;= pdfLength().</P>
205  *
206  * @return a length
207  */

208     
209 // public int pdfLength() {
210
// return toPdf(null).length;
211
// }
212

213 /**
214  * Returns the <CODE>String</CODE>-representation of this <CODE>PdfObject</CODE>.
215  *
216  * @return a <CODE>String</CODE>
217  */

218     
219     public String JavaDoc toString() {
220         if (bytes == null)
221             return super.toString();
222         else
223             return PdfEncodings.convertToString(bytes, null);
224     }
225     
226 /**
227  * Returns the length of the actual content of the <CODE>PdfObject</CODE>.
228  * <P>
229  * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
230  * this method differs from the method <CODE>pdfLength</CODE> because <CODE>pdfLength</CODE>
231  * returns the length of the PDF representation of the object, not of the actual content
232  * as does the method <CODE>length</CODE>.</P>
233  * <P>
234  * Remark: the actual content of an object is in some cases identical to its representation.
235  * The following statement is always true: length() &gt;= pdfLength().</P>
236  *
237  * @return a length
238  */

239     
240     public int length() {
241         return toString().length();
242     }
243     
244 /**
245  * Changes the content of this <CODE>PdfObject</CODE>.
246  *
247  * @param content the new content of this <CODE>PdfObject</CODE>
248  */

249     
250     protected void setContent(String JavaDoc content) {
251         bytes = PdfEncodings.convertToBytes(content, null);
252     }
253     
254     // methods dealing with the type of this object
255

256 /**
257  * Returns the type of this <CODE>PdfObject</CODE>.
258  *
259  * @return a type
260  */

261     
262     public int type() {
263         return type;
264     }
265     
266 /**
267  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNull</CODE>.
268  *
269  * @return <CODE>true</CODE> or <CODE>false</CODE>
270  */

271     
272     public boolean isNull() {
273         return (this.type == NULL);
274     }
275     
276 /**
277  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfBoolean</CODE>.
278  *
279  * @return <CODE>true</CODE> or <CODE>false</CODE>
280  */

281     
282     public boolean isBoolean() {
283         return (this.type == BOOLEAN);
284     }
285     
286 /**
287  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNumber</CODE>.
288  *
289  * @return <CODE>true</CODE> or <CODE>false</CODE>
290  */

291     
292     public boolean isNumber() {
293         return (this.type == NUMBER);
294     }
295     
296 /**
297  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfString</CODE>.
298  *
299  * @return <CODE>true</CODE> or <CODE>false</CODE>
300  */

301     
302     public boolean isString() {
303         return (this.type == STRING);
304     }
305     
306 /**
307  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfName</CODE>.
308  *
309  * @return <CODE>true</CODE> or <CODE>false</CODE>
310  */

311     
312     public boolean isName() {
313         return (this.type == NAME);
314     }
315     
316 /**
317  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfArray</CODE>.
318  *
319  * @return <CODE>true</CODE> or <CODE>false</CODE>
320  */

321     
322     public boolean isArray() {
323         return (this.type == ARRAY);
324     }
325     
326 /**
327  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfDictionary</CODE>.
328  *
329  * @return <CODE>true</CODE> or <CODE>false</CODE>
330  */

331     
332     public boolean isDictionary() {
333         return (this.type == DICTIONARY);
334     }
335     
336 /**
337  * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfStream</CODE>.
338  *
339  * @return <CODE>true</CODE> or <CODE>false</CODE>
340  */

341     
342     public boolean isStream() {
343         return (this.type == STREAM);
344     }
345
346     /**
347      * Checks if this is an indirect object.
348      * @return true if this is an indirect object
349      */

350     public boolean isIndirect() {
351         return (this.type == INDIRECT);
352     }
353     
354     /**
355      * Getter for property indRef.
356      * @return Value of property indRef.
357      */

358     public PRIndirectReference getIndRef() {
359         return this.indRef;
360     }
361     
362     /**
363      * Setter for property indRef.
364      * @param indRef New value of property indRef.
365      */

366     public void setIndRef(PRIndirectReference indRef) {
367         this.indRef = indRef;
368     }
369 }
370
Popular Tags