~ubuntu-branches/ubuntu/utopic/libitext-java/utopic

« back to all changes in this revision

Viewing changes to core/com/lowagie/text/pdf/PdfObject.java

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2009-05-02 08:23:58 UTC
  • mfrom: (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090502082358-exbqmmq3xcm735gb
Tags: 2.1.5-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Disable building of libitext-rups-java; we don't need another PDF
    renderer, they're subject to many security holes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: PdfObject.java 3373 2008-05-12 16:21:24Z xlv $
 
2
 * $Id: PdfObject.java 3760 2009-03-06 16:07:16Z blowagie $
3
3
 *
4
4
 * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
5
5
 *
70
70
 * @see         PdfStream
71
71
 * @see         PdfIndirectReference
72
72
 */
73
 
 
74
73
public abstract class PdfObject {
75
 
    
76
 
    // static membervariables (all the possible types of a PdfObject)
77
 
    
78
 
/** a possible type of <CODE>PdfObject</CODE> */
 
74
 
 
75
    // CONSTANTS
 
76
 
 
77
    /** A possible type of <CODE>PdfObject</CODE> */
79
78
    public static final int BOOLEAN = 1;
80
 
    
81
 
/** a possible type of <CODE>PdfObject</CODE> */
 
79
 
 
80
    /** A possible type of <CODE>PdfObject</CODE> */
82
81
    public static final int NUMBER = 2;
83
 
    
84
 
/** a possible type of <CODE>PdfObject</CODE> */
 
82
 
 
83
    /** A possible type of <CODE>PdfObject</CODE> */
85
84
    public static final int STRING = 3;
86
 
    
87
 
/** a possible type of <CODE>PdfObject</CODE> */
 
85
 
 
86
    /** A possible type of <CODE>PdfObject</CODE> */
88
87
    public static final int NAME = 4;
89
 
    
90
 
/** a possible type of <CODE>PdfObject</CODE> */
 
88
 
 
89
    /** A possible type of <CODE>PdfObject</CODE> */
91
90
    public static final int ARRAY = 5;
92
 
    
93
 
/** a possible type of <CODE>PdfObject</CODE> */
 
91
 
 
92
    /** A possible type of <CODE>PdfObject</CODE> */
94
93
    public static final int DICTIONARY = 6;
95
 
    
96
 
/** a possible type of <CODE>PdfObject</CODE> */
 
94
 
 
95
    /** A possible type of <CODE>PdfObject</CODE> */
97
96
    public static final int STREAM = 7;
98
97
 
99
 
/** a possible type of <CODE>PdfObject</CODE> */
 
98
    /** A possible type of <CODE>PdfObject</CODE> */
100
99
    public static final int NULL = 8;
101
 
    
102
 
    /** a possible type of <CODE>PdfObject</CODE> */
103
 
    public static final int INDIRECT = 10;    
104
 
 
105
 
/** This is an empty string used for the <CODE>PdfNull</CODE>-object and for an empty <CODE>PdfString</CODE>-object. */
 
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. */
106
105
    public static final String NOTHING = "";
107
 
    
108
 
/** This is the default encoding to be used for converting Strings into bytes and vice versa.
109
 
 * The default encoding is PdfDocEncoding.
110
 
 */
 
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
111
    public static final String TEXT_PDFDOCENCODING = "PDF";
112
 
    
113
 
/** This is the encoding to be used to output text in Unicode. */
 
112
 
 
113
    /** This is the encoding to be used to output text in Unicode. */
114
114
    public static final String TEXT_UNICODE = "UnicodeBig";
115
 
    
116
 
    // membervariables
117
 
    
118
 
/** the content of this <CODE>PdfObject</CODE> */
 
115
 
 
116
    // CLASS VARIABLES
 
117
 
 
118
    /** The content of this <CODE>PdfObject</CODE> */
119
119
    protected byte[] bytes;
120
 
    
121
 
/** the type of this <CODE>PdfObject</CODE> */
 
120
 
 
121
    /** The type of this <CODE>PdfObject</CODE> */
122
122
    protected int type;
123
 
    
 
123
 
 
124
    /** Holds the indirect reference. */
 
125
    protected PdfIndirectReference indRef;
 
126
 
 
127
    // CONSTRUCTORS
 
128
 
124
129
    /**
125
 
     * Holds value of property indRef.
 
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>
126
134
     */
127
 
    protected PRIndirectReference indRef;
128
 
    
129
 
    // constructors
130
 
    
131
 
/**
132
 
 * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> without any <VAR>content</VAR>.
133
 
 *
134
 
 * @param               type                    type of the new <CODE>PdfObject</CODE>
135
 
 */
136
 
    
137
135
    protected PdfObject(int type) {
138
136
        this.type = type;
139
137
    }
140
 
    
141
 
/**
142
 
 * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> with a certain <VAR>content</VAR>.
143
 
 *
144
 
 * @param               type                    type of the new <CODE>PdfObject</CODE>
145
 
 * @param               content                 content of the new <CODE>PdfObject</CODE> as a <CODE>String</CODE>.
146
 
 */
147
 
    
 
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
     */
148
147
    protected PdfObject(int type, String content) {
149
148
        this.type = type;
150
149
        bytes = PdfEncodings.convertToBytes(content, null);
151
150
    }
152
 
    
153
 
/**
154
 
 * Constructs a <CODE>PdfObject</CODE> of a certain <VAR>type</VAR> 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 <CODE>byte</CODE>.
158
 
 */
159
 
    
 
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
160
    protected PdfObject(int type, byte[] bytes) {
161
161
        this.bytes = bytes;
162
162
        this.type = type;
163
163
    }
164
 
    
 
164
 
165
165
    // methods dealing with the content of this object
166
 
    
167
 
/**
168
 
 * Writes the PDF representation of this <CODE>PdfObject</CODE> as an array of <CODE>byte</CODE>s to the writer.
169
 
 * @param writer for backwards compatibility
170
 
 * @param os the outputstream to write the bytes to.
171
 
 * @throws IOException
172
 
 */
173
 
    
 
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
     */
174
175
    public void toPdf(PdfWriter writer, OutputStream os) throws IOException {
175
176
        if (bytes != null)
176
177
            os.write(bytes);
177
178
    }
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
 
179
192
    /**
180
193
     * Gets the presentation of this object in a byte array
 
194
     * 
181
195
     * @return a byte array
182
196
     */
183
197
    public byte[] getBytes() {
185
199
    }
186
200
 
187
201
    /**
188
 
     * Can this object be in an object stream?
189
 
     * @return true if this object can be in an object stream.
 
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>
190
209
     */
191
210
    public boolean canBeInObjStm() {
192
 
        return (type >= 1 && type <= 6) || type == 8;
 
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
                return true;
 
220
            case STREAM:
 
221
            case INDIRECT:
 
222
            default:
 
223
                return false;
 
224
        }
193
225
    }
194
 
    
195
 
/**
196
 
 * Returns the length of the PDF representation of the <CODE>PdfObject</CODE>.
197
 
 * <P>
198
 
 * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
199
 
 * this method differs from the method <CODE>length</CODE> because <CODE>length</CODE>
200
 
 * returns the length of the actual content of the <CODE>PdfObject</CODE>.</P>
201
 
 * <P>
202
 
 * Remark: the actual content of an object is in most cases identical to its representation.
203
 
 * The following statement is always true: length() &gt;= pdfLength().</P>
204
 
 *
205
 
 * @return              a length
206
 
 */
207
 
    
 
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() &gt;= pdfLength().</P>
 
236
     *
 
237
     * @return          a length
 
238
     */
208
239
//    public int pdfLength() {
209
240
//        return toPdf(null).length;
210
241
//    }
211
 
    
212
 
/**
213
 
 * Returns the <CODE>String</CODE>-representation of this <CODE>PdfObject</CODE>.
214
 
 *
215
 
 * @return              a <CODE>String</CODE>
216
 
 */
217
 
    
218
 
    public String toString() {
219
 
        if (bytes == null)
220
 
            return super.toString();
221
 
        else
222
 
            return PdfEncodings.convertToString(bytes, null);
223
 
    }
224
 
    
225
 
/**
226
 
 * Returns the length of the actual content of the <CODE>PdfObject</CODE>.
227
 
 * <P>
228
 
 * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
229
 
 * this method differs from the method <CODE>pdfLength</CODE> because <CODE>pdfLength</CODE>
230
 
 * returns the length of the PDF representation of the object, not of the actual content
231
 
 * as does the method <CODE>length</CODE>.</P>
232
 
 * <P>
233
 
 * Remark: the actual content of an object is in some cases identical to its representation.
234
 
 * The following statement is always true: length() &gt;= pdfLength().</P>
235
 
 *
236
 
 * @return              a length
237
 
 */
238
 
    
 
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() &gt;= pdfLength().</P>
 
253
     *
 
254
     * @return The length as <CODE>int</CODE>
 
255
     */
239
256
    public int length() {
240
257
        return toString().length();
241
258
    }
242
 
    
243
 
/**
244
 
 * Changes the content of this <CODE>PdfObject</CODE>.
245
 
 *
246
 
 * @param               content                 the new content of this <CODE>PdfObject</CODE>
247
 
 */
248
 
    
 
259
 
 
260
    /**
 
261
     * Changes the content of this <CODE>PdfObject</CODE>.
 
262
     *
 
263
     * @param content    the new content of this <CODE>PdfObject</CODE>
 
264
     */
249
265
    protected void setContent(String content) {
250
266
        bytes = PdfEncodings.convertToBytes(content, null);
251
267
    }
252
 
    
 
268
 
253
269
    // methods dealing with the type of this object
254
 
    
255
 
/**
256
 
 * Returns the type of this <CODE>PdfObject</CODE>.
257
 
 *
258
 
 * @return              a type
259
 
 */
260
 
    
 
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
     */
261
287
    public int type() {
262
288
        return type;
263
289
    }
264
 
    
265
 
/**
266
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNull</CODE>.
267
 
 *
268
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
269
 
 */
270
 
    
 
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
     */
271
297
    public boolean isNull() {
272
 
        return (this.type == NULL);
 
298
        return (type == NULL);
273
299
    }
274
 
    
275
 
/**
276
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfBoolean</CODE>.
277
 
 *
278
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
279
 
 */
280
 
    
 
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
     */
281
307
    public boolean isBoolean() {
282
 
        return (this.type == BOOLEAN);
 
308
        return (type == BOOLEAN);
283
309
    }
284
 
    
285
 
/**
286
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfNumber</CODE>.
287
 
 *
288
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
289
 
 */
290
 
    
 
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
     */
291
317
    public boolean isNumber() {
292
 
        return (this.type == NUMBER);
 
318
        return (type == NUMBER);
293
319
    }
294
 
    
295
 
/**
296
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfString</CODE>.
297
 
 *
298
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
299
 
 */
300
 
    
 
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
     */
301
327
    public boolean isString() {
302
 
        return (this.type == STRING);
 
328
        return (type == STRING);
303
329
    }
304
 
    
305
 
/**
306
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfName</CODE>.
307
 
 *
308
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
309
 
 */
310
 
    
 
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
     */
311
337
    public boolean isName() {
312
 
        return (this.type == NAME);
 
338
        return (type == NAME);
313
339
    }
314
 
    
315
 
/**
316
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfArray</CODE>.
317
 
 *
318
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
319
 
 */
320
 
    
 
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
     */
321
347
    public boolean isArray() {
322
 
        return (this.type == ARRAY);
 
348
        return (type == ARRAY);
323
349
    }
324
 
    
325
 
/**
326
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfDictionary</CODE>.
327
 
 *
328
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
329
 
 */
330
 
    
 
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
     */
331
357
    public boolean isDictionary() {
332
 
        return (this.type == DICTIONARY);
 
358
        return (type == DICTIONARY);
333
359
    }
334
 
    
335
 
/**
336
 
 * Checks if this <CODE>PdfObject</CODE> is of the type <CODE>PdfStream</CODE>.
337
 
 *
338
 
 * @return              <CODE>true</CODE> or <CODE>false</CODE>
339
 
 */
340
 
    
 
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
     */
341
367
    public boolean isStream() {
342
 
        return (this.type == STREAM);
 
368
        return (type == STREAM);
343
369
    }
344
370
 
345
371
    /**
346
 
     * Checks if this is an indirect object.
347
 
     * @return true if this is an indirect object
 
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>
348
377
     */
349
378
    public boolean isIndirect() {
350
 
        return (this.type == INDIRECT);
351
 
    }
352
 
    
353
 
    /**
354
 
     * Getter for property indRef.
355
 
     * @return Value of property indRef.
356
 
     */
357
 
    public PRIndirectReference getIndRef() {
358
 
        return this.indRef;
359
 
    }
360
 
    
361
 
    /**
362
 
     * Setter for property indRef.
363
 
     * @param indRef New value of property indRef.
364
 
     */
365
 
    public void setIndRef(PRIndirectReference indRef) {
 
379
        return (type == INDIRECT);
 
380
    }
 
381
 
 
382
    /**
 
383
     * Get the indirect reference
 
384
     * 
 
385
     * @return A <CODE>PdfIndirectReference</CODE>
 
386
     */
 
387
    public PdfIndirectReference 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(PdfIndirectReference indRef) {
366
397
        this.indRef = indRef;
 
398
        indRef.setDirectObject(this);
 
399
    }
 
400
 
 
401
    /**
 
402
     * Simply here to be overridden by indirect references.
 
403
     * 
 
404
     * @return this
 
405
     * @since 2.1.5
 
406
     */
 
407
    public PdfObject getDirectObject() {
 
408
        return this;
367
409
    }
368
410
}