001: /*
002: * $Id: PdfObject.java 2366 2006-09-14 23:10:58Z xlv $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055:
056: /**
057: * <CODE>PdfObject</CODE> is the abstract superclass of all PDF objects.
058: * <P>
059: * PDF supports seven basic types of objects: Booleans, numbers, strings, names,
060: * arrays, dictionaries and streams. In addition, PDF provides a null object.
061: * Objects may be labeled so that they can be referred to by other objects.<BR>
062: * All these basic PDF objects are described in the 'Portable Document Format
063: * Reference Manual version 1.3' Chapter 4 (pages 37-54).
064: *
065: * @see PdfNull
066: * @see PdfBoolean
067: * @see PdfNumber
068: * @see PdfString
069: * @see PdfName
070: * @see PdfArray
071: * @see PdfDictionary
072: * @see PdfStream
073: * @see PdfIndirectReference
074: */
075:
076: public abstract class PdfObject {
077:
078: // static membervariables (all the possible types of a PdfObject)
079:
080: /** a possible type of <CODE>PdfObject</CODE> */
081: public static final int BOOLEAN = 1;
082:
083: /** a possible type of <CODE>PdfObject</CODE> */
084: public static final int NUMBER = 2;
085:
086: /** a possible type of <CODE>PdfObject</CODE> */
087: public static final int STRING = 3;
088:
089: /** a possible type of <CODE>PdfObject</CODE> */
090: public static final int NAME = 4;
091:
092: /** a possible type of <CODE>PdfObject</CODE> */
093: public static final int ARRAY = 5;
094:
095: /** a possible type of <CODE>PdfObject</CODE> */
096: public static final int DICTIONARY = 6;
097:
098: /** a possible type of <CODE>PdfObject</CODE> */
099: public static final int STREAM = 7;
100:
101: /** a possible type of <CODE>PdfObject</CODE> */
102: public static final int NULL = 8;
103:
104: /** a possible type of <CODE>PdfObject</CODE> */
105: public static final int INDIRECT = 10;
106:
107: /** This is an empty string used for the <CODE>PdfNull</CODE>-object and for an empty <CODE>PdfString</CODE>-object. */
108: public static final String NOTHING = "";
109:
110: /** This is the default encoding to be used for converting Strings into bytes and vice versa.
111: * The default encoding is PdfDocEncoding.
112: */
113: public static final String TEXT_PDFDOCENCODING = "PDF";
114:
115: /** This is the encoding to be used to output text in Unicode. */
116: public static final String TEXT_UNICODE = "UnicodeBig";
117:
118: // membervariables
119:
120: /** the content of this <CODE>PdfObject</CODE> */
121: protected byte[] bytes;
122:
123: /** the type of this <CODE>PdfObject</CODE> */
124: protected int type;
125:
126: /**
127: * Holds value of property indRef.
128: */
129: protected PRIndirectReference indRef;
130:
131: // constructors
132:
133: /**
134: * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> without any <VAR>content</VAR>.
135: *
136: * @param type type of the new <CODE>PdfObject</CODE>
137: */
138:
139: protected PdfObject(int type) {
140: this .type = type;
141: }
142:
143: /**
144: * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
145: *
146: * @param type type of the new <CODE>PdfObject</CODE>
147: * @param content content of the new <CODE>PdfObject</CODE> as a <CODE>String</CODE>.
148: */
149:
150: protected PdfObject(int type, String content) {
151: this .type = type;
152: bytes = PdfEncodings.convertToBytes(content, null);
153: }
154:
155: /**
156: * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
157: *
158: * @param type type of the new <CODE>PdfObject</CODE>
159: * @param bytes content of the new <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>.
160: */
161:
162: protected PdfObject(int type, byte[] bytes) {
163: this .bytes = bytes;
164: this .type = type;
165: }
166:
167: // methods dealing with the content of this object
168:
169: /**
170: * Writes the PDF representation of this <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>s to the writer.
171: * @param writer for backwards compatibility
172: * @param os the outputstream to write the bytes to.
173: * @throws IOException
174: */
175:
176: public void toPdf(PdfWriter writer, OutputStream os)
177: throws IOException {
178: if (bytes != null)
179: os.write(bytes);
180: }
181:
182: /**
183: * Gets the presentation of this object in a byte array
184: * @return a byte array
185: */
186: public byte[] getBytes() {
187: return bytes;
188: }
189:
190: /**
191: * Can this object be in an object stream?
192: * @return true if this object can be in an object stream.
193: */
194: public boolean canBeInObjStm() {
195: return (type >= 1 && type <= 6) || type == 8;
196: }
197:
198: /**
199: * Returns the length of the PDF representation of the <CODE>PdfObject</CODE>.
200: * <P>
201: * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
202: * this method differs from the method <CODE>length</CODE> because <CODE>length</CODE>
203: * returns the length of the actual content of the <CODE>PdfObject</CODE>.</P>
204: * <P>
205: * Remark: the actual content of an object is in most cases identical to its representation.
206: * The following statement is always true: length() >= pdfLength().</P>
207: *
208: * @return a length
209: */
210:
211: // public int pdfLength() {
212: // return toPdf(null).length;
213: // }
214: /**
215: * Returns the <CODE>String</CODE>-representation of this <CODE>PdfObject</CODE>.
216: *
217: * @return a <CODE>String</CODE>
218: */
219:
220: public String toString() {
221: if (bytes == null)
222: return super .toString();
223: else
224: return PdfEncodings.convertToString(bytes, null);
225: }
226:
227: /**
228: * Returns the length of the actual content of the <CODE>PdfObject</CODE>.
229: * <P>
230: * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
231: * this method differs from the method <CODE>pdfLength</CODE> because <CODE>pdfLength</CODE>
232: * returns the length of the PDF representation of the object, not of the actual content
233: * as does the method <CODE>length</CODE>.</P>
234: * <P>
235: * Remark: the actual content of an object is in some cases identical to its representation.
236: * The following statement is always true: length() >= pdfLength().</P>
237: *
238: * @return a length
239: */
240:
241: public int length() {
242: return toString().length();
243: }
244:
245: /**
246: * Changes the content of this <CODE>PdfObject</CODE>.
247: *
248: * @param content the new content of this <CODE>PdfObject</CODE>
249: */
250:
251: protected void setContent(String content) {
252: bytes = PdfEncodings.convertToBytes(content, null);
253: }
254:
255: // methods dealing with the type of this object
256:
257: /**
258: * Returns the type of this <CODE>PdfObject</CODE>.
259: *
260: * @return a type
261: */
262:
263: public int type() {
264: return type;
265: }
266:
267: /**
268: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNull</CODE>.
269: *
270: * @return <CODE>true</CODE> or <CODE>false</CODE>
271: */
272:
273: public boolean isNull() {
274: return (this .type == NULL);
275: }
276:
277: /**
278: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfBoolean</CODE>.
279: *
280: * @return <CODE>true</CODE> or <CODE>false</CODE>
281: */
282:
283: public boolean isBoolean() {
284: return (this .type == BOOLEAN);
285: }
286:
287: /**
288: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNumber</CODE>.
289: *
290: * @return <CODE>true</CODE> or <CODE>false</CODE>
291: */
292:
293: public boolean isNumber() {
294: return (this .type == NUMBER);
295: }
296:
297: /**
298: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfString</CODE>.
299: *
300: * @return <CODE>true</CODE> or <CODE>false</CODE>
301: */
302:
303: public boolean isString() {
304: return (this .type == STRING);
305: }
306:
307: /**
308: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfName</CODE>.
309: *
310: * @return <CODE>true</CODE> or <CODE>false</CODE>
311: */
312:
313: public boolean isName() {
314: return (this .type == NAME);
315: }
316:
317: /**
318: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfArray</CODE>.
319: *
320: * @return <CODE>true</CODE> or <CODE>false</CODE>
321: */
322:
323: public boolean isArray() {
324: return (this .type == ARRAY);
325: }
326:
327: /**
328: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfDictionary</CODE>.
329: *
330: * @return <CODE>true</CODE> or <CODE>false</CODE>
331: */
332:
333: public boolean isDictionary() {
334: return (this .type == DICTIONARY);
335: }
336:
337: /**
338: * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfStream</CODE>.
339: *
340: * @return <CODE>true</CODE> or <CODE>false</CODE>
341: */
342:
343: public boolean isStream() {
344: return (this .type == STREAM);
345: }
346:
347: /**
348: * Checks if this is an indirect object.
349: * @return true if this is an indirect object
350: */
351: public boolean isIndirect() {
352: return (this .type == INDIRECT);
353: }
354:
355: /**
356: * Getter for property indRef.
357: * @return Value of property indRef.
358: */
359: public PRIndirectReference getIndRef() {
360: return this .indRef;
361: }
362:
363: /**
364: * Setter for property indRef.
365: * @param indRef New value of property indRef.
366: */
367: public void setIndRef(PRIndirectReference indRef) {
368: this.indRef = indRef;
369: }
370: }
|