1 | /* | |
2 | * $Id: PdfObject.java 3912 2009-04-26 08:38:15Z blowagie $ | |
3 | * | |
4 | * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie | |
5 | * | |
6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
7 | * (the "License"); you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
9 | * | |
10 | * Software distributed under the License is distributed on an "AS IS" basis, | |
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 | * for the specific language governing rights and limitations under the License. | |
13 | * | |
14 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
17 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
18 | * All Rights Reserved. | |
19 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
20 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
21 | * | |
22 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | ||
50 | package com.lowagie.text.pdf; | |
51 | import java.io.IOException; | |
52 | import java.io.OutputStream; | |
53 | ||
54 | /** | |
55 | * <CODE>PdfObject</CODE> is the abstract superclass of all PDF objects. | |
56 | * <P> | |
57 | * PDF supports seven basic types of objects: Booleans, numbers, strings, names, | |
58 | * arrays, dictionaries and streams. In addition, PDF provides a null object. | |
59 | * Objects may be labeled so that they can be referred to by other objects.<BR> | |
60 | * All these basic PDF objects are described in the 'Portable Document Format | |
61 | * Reference Manual version 1.3' Chapter 4 (pages 37-54). | |
62 | * | |
63 | * @see PdfNull | |
64 | * @see PdfBoolean | |
65 | * @see PdfNumber | |
66 | * @see PdfString | |
67 | * @see PdfName | |
68 | * @see PdfArray | |
69 | * @see PdfDictionary | |
70 | * @see PdfStream | |
71 | * @see PdfIndirectReference | |
72 | */ | |
73 | public abstract class PdfObject { | |
74 | ||
75 | // CONSTANTS | |
76 | ||
77 | /** A possible type of <CODE>PdfObject</CODE> */ | |
78 | public static final int BOOLEAN = 1; | |
79 | ||
80 | /** A possible type of <CODE>PdfObject</CODE> */ | |
81 | public static final int NUMBER = 2; | |
82 | ||
83 | /** A possible type of <CODE>PdfObject</CODE> */ | |
84 | public static final int STRING = 3; | |
85 | ||
86 | /** A possible type of <CODE>PdfObject</CODE> */ | |
87 | public static final int NAME = 4; | |
88 | ||
89 | /** A possible type of <CODE>PdfObject</CODE> */ | |
90 | public static final int ARRAY = 5; | |
91 | ||
92 | /** A possible type of <CODE>PdfObject</CODE> */ | |
93 | public static final int DICTIONARY = 6; | |
94 | ||
95 | /** A possible type of <CODE>PdfObject</CODE> */ | |
96 | public static final int STREAM = 7; | |
97 | ||
98 | /** A possible type of <CODE>PdfObject</CODE> */ | |
99 | public static final int NULL = 8; | |
100 | ||
101 | /** A possible type of <CODE>PdfObject</CODE> */ | |
102 | public static final int INDIRECT = 10; | |
103 | ||
104 | /** An empty string used for the <CODE>PdfNull</CODE>-object and for an empty <CODE>PdfString</CODE>-object. */ | |
105 | public static final String NOTHING = ""; | |
106 | ||
107 | /** | |
108 | * This is the default encoding to be used for converting Strings into | |
109 | * bytes and vice versa. The default encoding is PdfDocEncoding. | |
110 | */ | |
111 | public static final String TEXT_PDFDOCENCODING = "PDF"; | |
112 | ||
113 | /** This is the encoding to be used to output text in Unicode. */ | |
114 | public static final String TEXT_UNICODE = "UnicodeBig"; | |
115 | ||
116 | // CLASS VARIABLES | |
117 | ||
118 | /** The content of this <CODE>PdfObject</CODE> */ | |
119 | protected byte[] bytes; | |
120 | ||
121 | /** The type of this <CODE>PdfObject</CODE> */ | |
122 | protected int type; | |
123 | ||
124 | /** Holds the indirect reference. */ | |
125 | protected PRIndirectReference indRef; | |
126 | ||
127 | // CONSTRUCTORS | |
128 | ||
129 | /** | |
130 | * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> | |
131 | * without any <VAR>content</VAR>. | |
132 | * | |
133 | * @param type type of the new <CODE>PdfObject</CODE> | |
134 | */ | |
135 | protected PdfObject(int type) { | |
136 | this.type = type; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> | |
141 | * with a certain <VAR>content</VAR>. | |
142 | * | |
143 | * @param type type of the new <CODE>PdfObject</CODE> | |
144 | * @param content content of the new <CODE>PdfObject</CODE> as a | |
145 | * <CODE>String</CODE>. | |
146 | */ | |
147 | protected PdfObject(int type, String content) { | |
148 | this.type = type; | |
149 | bytes = PdfEncodings.convertToBytes(content, null); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> | |
154 | * with a certain <VAR>content</VAR>. | |
155 | * | |
156 | * @param type type of the new <CODE>PdfObject</CODE> | |
157 | * @param bytes content of the new <CODE>PdfObject</CODE> as an array of | |
158 | * <CODE>byte</CODE>. | |
159 | */ | |
160 | protected PdfObject(int type, byte[] bytes) { | |
161 | this.bytes = bytes; | |
162 | this.type = type; | |
163 | } | |
164 | ||
165 | // methods dealing with the content of this object | |
166 | ||
167 | /** | |
168 | * Writes the PDF representation of this <CODE>PdfObject</CODE> as an | |
169 | * array of <CODE>byte</CODE>s to the writer. | |
170 | * | |
171 | * @param writer for backwards compatibility | |
172 | * @param os The <CODE>OutputStream</CODE> to write the bytes to. | |
173 | * @throws IOException | |
174 | */ | |
175 | public void toPdf(PdfWriter writer, OutputStream os) throws IOException { | |
176 |
1
1. toPdf : negated conditional → NO_COVERAGE |
if (bytes != null) |
177 |
1
1. toPdf : removed call to java/io/OutputStream::write → NO_COVERAGE |
os.write(bytes); |
178 | } | |
179 | ||
180 | /** | |
181 | * Returns the <CODE>String</CODE>-representation of this | |
182 | * <CODE>PdfObject</CODE>. | |
183 | * | |
184 | * @return a <CODE>String</CODE> | |
185 | */ | |
186 | public String toString() { | |
187 | if (bytes == null) | |
188 | return super.toString(); | |
189 | return PdfEncodings.convertToString(bytes, null); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Gets the presentation of this object in a byte array | |
194 | * | |
195 | * @return a byte array | |
196 | */ | |
197 | public byte[] getBytes() { | |
198 | return bytes; | |
199 | } | |
200 | ||
201 | /** | |
202 | * Whether this object can be contained in an object stream. | |
203 | * | |
204 | * PdfObjects of type STREAM OR INDIRECT can not be contained in an | |
205 | * object stream. | |
206 | * | |
207 | * @return <CODE>true</CODE> if this object can be in an object stream. | |
208 | * Otherwise <CODE>false</CODE> | |
209 | */ | |
210 | public boolean canBeInObjStm() { | |
211 | switch (type) { | |
212 | case NULL: | |
213 | case BOOLEAN: | |
214 | case NUMBER: | |
215 | case STRING: | |
216 | case NAME: | |
217 | case ARRAY: | |
218 | case DICTIONARY: | |
219 |
1
1. canBeInObjStm : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return true; |
220 | case STREAM: | |
221 | case INDIRECT: | |
222 | default: | |
223 |
1
1. canBeInObjStm : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return false; |
224 | } | |
225 | } | |
226 | ||
227 | /** | |
228 | * Returns the length of the PDF representation 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>length</CODE> because <CODE>length</CODE> | |
232 | * returns the length of the actual content of the <CODE>PdfObject</CODE>.</P> | |
233 | * <P> | |
234 | * Remark: the actual content of an object is in most cases identical to its representation. | |
235 | * The following statement is always true: length() >= pdfLength().</P> | |
236 | * | |
237 | * @return a length | |
238 | */ | |
239 | // public int pdfLength() { | |
240 | // return toPdf(null).length; | |
241 | // } | |
242 | ||
243 | /** | |
244 | * Returns the length of the actual content of the <CODE>PdfObject</CODE>. | |
245 | * <P> | |
246 | * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>, | |
247 | * this method differs from the method <CODE>pdfLength</CODE> because <CODE>pdfLength</CODE> | |
248 | * returns the length of the PDF representation of the object, not of the actual content | |
249 | * as does the method <CODE>length</CODE>.</P> | |
250 | * <P> | |
251 | * Remark: the actual content of an object is in some cases identical to its representation. | |
252 | * The following statement is always true: length() >= pdfLength().</P> | |
253 | * | |
254 | * @return The length as <CODE>int</CODE> | |
255 | */ | |
256 | public int length() { | |
257 |
1
1. length : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return toString().length(); |
258 | } | |
259 | ||
260 | /** | |
261 | * Changes the content of this <CODE>PdfObject</CODE>. | |
262 | * | |
263 | * @param content the new content of this <CODE>PdfObject</CODE> | |
264 | */ | |
265 | protected void setContent(String content) { | |
266 | bytes = PdfEncodings.convertToBytes(content, null); | |
267 | } | |
268 | ||
269 | // methods dealing with the type of this object | |
270 | ||
271 | /** | |
272 | * Returns the type of this <CODE>PdfObject</CODE>. | |
273 | * | |
274 | * May be either of: | |
275 | * - <VAR>NULL</VAR>: A <CODE>PdfNull</CODE> | |
276 | * - <VAR>BOOLEAN</VAR>: A <CODE>PdfBoolean</CODE> | |
277 | * - <VAR>NUMBER</VAR>: A <CODE>PdfNumber</CODE> | |
278 | * - <VAR>STRING</VAR>: A <CODE>PdfString</CODE> | |
279 | * - <VAR>NAME</VAR>: A <CODE>PdfName</CODE> | |
280 | * - <VAR>ARRAY</VAR>: A <CODE>PdfArray</CODE> | |
281 | * - <VAR>DICTIONARY</VAR>: A <CODE>PdfDictionary</CODE> | |
282 | * - <VAR>STREAM</VAR>: A <CODE>PdfStream</CODE> | |
283 | * - <VAR>INDIRECT</VAR>: ><CODE>PdfIndirectObject</CODE> | |
284 | * | |
285 | * @return The type | |
286 | */ | |
287 | public int type() { | |
288 | return type; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
293 | * <CODE>PdfNull</CODE>. | |
294 | * | |
295 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
296 | */ | |
297 | public boolean isNull() { | |
298 |
2
1. isNull : negated conditional → NO_COVERAGE 2. isNull : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == NULL); |
299 | } | |
300 | ||
301 | /** | |
302 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
303 | * <CODE>PdfBoolean</CODE>. | |
304 | * | |
305 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
306 | */ | |
307 | public boolean isBoolean() { | |
308 |
2
1. isBoolean : negated conditional → NO_COVERAGE 2. isBoolean : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == BOOLEAN); |
309 | } | |
310 | ||
311 | /** | |
312 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
313 | * <CODE>PdfNumber</CODE>. | |
314 | * | |
315 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
316 | */ | |
317 | public boolean isNumber() { | |
318 |
2
1. isNumber : negated conditional → NO_COVERAGE 2. isNumber : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == NUMBER); |
319 | } | |
320 | ||
321 | /** | |
322 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
323 | * <CODE>PdfString</CODE>. | |
324 | * | |
325 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
326 | */ | |
327 | public boolean isString() { | |
328 |
2
1. isString : negated conditional → NO_COVERAGE 2. isString : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == STRING); |
329 | } | |
330 | ||
331 | /** | |
332 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
333 | * <CODE>PdfName</CODE>. | |
334 | * | |
335 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
336 | */ | |
337 | public boolean isName() { | |
338 |
2
1. isName : negated conditional → NO_COVERAGE 2. isName : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == NAME); |
339 | } | |
340 | ||
341 | /** | |
342 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
343 | * <CODE>PdfArray</CODE>. | |
344 | * | |
345 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
346 | */ | |
347 | public boolean isArray() { | |
348 |
2
1. isArray : negated conditional → NO_COVERAGE 2. isArray : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == ARRAY); |
349 | } | |
350 | ||
351 | /** | |
352 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
353 | * <CODE>PdfDictionary</CODE>. | |
354 | * | |
355 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
356 | */ | |
357 | public boolean isDictionary() { | |
358 |
2
1. isDictionary : negated conditional → NO_COVERAGE 2. isDictionary : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == DICTIONARY); |
359 | } | |
360 | ||
361 | /** | |
362 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
363 | * <CODE>PdfStream</CODE>. | |
364 | * | |
365 | * @return <CODE>true</CODE> or <CODE>false</CODE> | |
366 | */ | |
367 | public boolean isStream() { | |
368 |
2
1. isStream : negated conditional → NO_COVERAGE 2. isStream : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == STREAM); |
369 | } | |
370 | ||
371 | /** | |
372 | * Checks if this <CODE>PdfObject</CODE> is of the type | |
373 | * <CODE>PdfIndirectObject</CODE>. | |
374 | * | |
375 | * @return <CODE>true</CODE> if this is an indirect object, | |
376 | * otherwise <CODE>false</CODE> | |
377 | */ | |
378 | public boolean isIndirect() { | |
379 |
2
1. isIndirect : negated conditional → NO_COVERAGE 2. isIndirect : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return (type == INDIRECT); |
380 | } | |
381 | ||
382 | /** | |
383 | * Get the indirect reference | |
384 | * | |
385 | * @return A <CODE>PdfIndirectReference</CODE> | |
386 | */ | |
387 | public PRIndirectReference getIndRef() { | |
388 | return indRef; | |
389 | } | |
390 | ||
391 | /** | |
392 | * Set the indirect reference | |
393 | * | |
394 | * @param indRef New value as a <CODE>PdfIndirectReference</CODE> | |
395 | */ | |
396 | public void setIndRef(PRIndirectReference indRef) { | |
397 | this.indRef = indRef; | |
398 | } | |
399 | } | |
Mutations | ||
176 |
1.1 |
|
177 |
1.1 |
|
219 |
1.1 |
|
223 |
1.1 |
|
257 |
1.1 |
|
298 |
1.1 2.2 |
|
308 |
1.1 2.2 |
|
318 |
1.1 2.2 |
|
328 |
1.1 2.2 |
|
338 |
1.1 2.2 |
|
348 |
1.1 2.2 |
|
358 |
1.1 2.2 |
|
368 |
1.1 2.2 |
|
379 |
1.1 2.2 |