~ubuntu-branches/ubuntu/karmic/libitext-java/karmic

« back to all changes in this revision

Viewing changes to com/lowagie/text/rtf/field/RtfField.java

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2008-11-23 12:26:51 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20081123122651-ab7juwjz41q1123k
Tags: 2.1.4-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: RtfField.java,v 1.12 2006/09/12 13:12:56 blowagie Exp $
3
 
 * $Name:  $
4
 
 *
5
 
 * Copyright 2004 by Mark Hall
6
 
 * Uses code Copyright 2002
7
 
 *   <a href="http://www.smb-tec.com">SMB</a> 
8
 
 *   Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
9
 
 *
10
 
 * The contents of this file are subject to the Mozilla Public License Version 1.1
11
 
 * (the "License"); you may not use this file except in compliance with the License.
12
 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
13
 
 *
14
 
 * Software distributed under the License is distributed on an "AS IS" basis,
15
 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16
 
 * for the specific language governing rights and limitations under the License.
17
 
 *
18
 
 * The Original Code is 'iText, a free JAVA-PDF library'.
19
 
 *
20
 
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
21
 
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
22
 
 * All Rights Reserved.
23
 
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
24
 
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
25
 
 *
26
 
 * Contributor(s): all the names of the contributors are added in the source code
27
 
 * where applicable.
28
 
 *
29
 
 * Alternatively, the contents of this file may be used under the terms of the
30
 
 * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
31
 
 * provisions of LGPL are applicable instead of those above.  If you wish to
32
 
 * allow use of your version of this file only under the terms of the LGPL
33
 
 * License and not to allow others to use your version of this file under
34
 
 * the MPL, indicate your decision by deleting the provisions above and
35
 
 * replace them with the notice and other provisions required by the LGPL.
36
 
 * If you do not delete the provisions above, a recipient may use your version
37
 
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
38
 
 *
39
 
 * This library is free software; you can redistribute it and/or modify it
40
 
 * under the terms of the MPL as stated above or under the terms of the GNU
41
 
 * Library General Public License as published by the Free Software Foundation;
42
 
 * either version 2 of the License, or any later version.
43
 
 *
44
 
 * This library is distributed in the hope that it will be useful, but WITHOUT
45
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
46
 
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
47
 
 * details.
48
 
 *
49
 
 * If you didn't download this code from the following link, you should check if
50
 
 * you aren't using an obsolete version:
51
 
 * http://www.lowagie.com/iText/
52
 
 */
53
 
 
54
 
package com.lowagie.text.rtf.field;
55
 
 
56
 
import java.io.ByteArrayOutputStream;
57
 
import java.io.IOException;
58
 
 
59
 
import com.lowagie.text.Chunk;
60
 
import com.lowagie.text.Font;
61
 
import com.lowagie.text.rtf.RtfBasicElement;
62
 
import com.lowagie.text.rtf.document.RtfDocument;
63
 
import com.lowagie.text.rtf.style.RtfFont;
64
 
 
65
 
 
66
 
/**
67
 
 * The RtfField class is an abstract base class for all rtf field functionality.
68
 
 * Subclasses only need to implement the two abstract methods writeFieldInstContent
69
 
 * and writeFieldResultContent. All other field functionality is handled by the
70
 
 * RtfField class.
71
 
 * 
72
 
 * @version $Version:$
73
 
 * @author Mark Hall (mhall@edu.uni-klu.ac.at)
74
 
 * @author Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
75
 
 */
76
 
public abstract class RtfField extends Chunk implements RtfBasicElement {
77
 
 
78
 
    /**
79
 
     * Constant for a rtf field
80
 
     */
81
 
    private static final byte[] FIELD = "\\field".getBytes();
82
 
    /**
83
 
     * Constant for a dirty field
84
 
     */
85
 
    private static final byte[] FIELD_DIRTY = "\\flddirty".getBytes();
86
 
    /**
87
 
     * Constant for a private field
88
 
     */
89
 
    private static final byte[] FIELD_PRIVATE = "\\fldpriv".getBytes();
90
 
    /**
91
 
     * Constant for a locked field
92
 
     */
93
 
    private static final byte[] FIELD_LOCKED = "\\fldlock".getBytes();
94
 
    /**
95
 
     * Constant for a edited field
96
 
     */
97
 
    private static final byte[] FIELD_EDIT = "\\fldedit".getBytes();
98
 
    /**
99
 
     * Constant for an alt field
100
 
     */
101
 
    private static final byte[] FIELD_ALT = "\\fldalt".getBytes();
102
 
    /**
103
 
     * Constant for the field instructions
104
 
     */
105
 
    private static final byte[] FIELD_INSTRUCTIONS = "\\*\\fldinst".getBytes();
106
 
    /**
107
 
     * Constant for the field result
108
 
     */
109
 
    private static final byte[] FIELD_RESULT = "\\fldrslt".getBytes();
110
 
 
111
 
    /**
112
 
     * Is the field dirty
113
 
     */
114
 
    private boolean fieldDirty = false;
115
 
    /**
116
 
     * Is the field edited
117
 
     */
118
 
    private boolean fieldEdit = false;
119
 
    /**
120
 
     * Is the field locked
121
 
     */
122
 
    private boolean fieldLocked = false;
123
 
    /**
124
 
     * Is the field private
125
 
     */
126
 
    private boolean fieldPrivate = false;
127
 
    /**
128
 
     * Is it an alt field
129
 
     */
130
 
    private boolean fieldAlt = false;
131
 
    /**
132
 
     * Whether this RtfField is in a table
133
 
     */
134
 
    private boolean inTable = false;
135
 
    /**
136
 
     * Whether this RtfElement is in a header
137
 
     */
138
 
    private boolean inHeader = false;
139
 
    /**
140
 
     * The RtfDocument this RtfField belongs to 
141
 
     */
142
 
    protected RtfDocument document = null;
143
 
    /**
144
 
     * The RtfFont of this RtfField
145
 
     */
146
 
    private RtfFont font = null;
147
 
 
148
 
    /**
149
 
     * Constructs a RtfField for a RtfDocument. This is not very usefull,
150
 
     * since the RtfField by itself does not do anything. Use one of the
151
 
     * subclasses instead.
152
 
     * 
153
 
     * @param doc The RtfDocument this RtfField belongs to.
154
 
     */
155
 
    protected RtfField(RtfDocument doc) {
156
 
        this(doc, new Font());
157
 
    }
158
 
    
159
 
    /**
160
 
     * Constructs a RtfField for a RtfDocument. This is not very usefull,
161
 
     * since the RtfField by itself does not do anything. Use one of the
162
 
     * subclasses instead.
163
 
     * 
164
 
     * @param doc The RtfDocument this RtfField belongs to.
165
 
     * @param font The Font this RtfField should use
166
 
     */
167
 
    protected RtfField(RtfDocument doc, Font font) {
168
 
        super("", font);
169
 
        this.document = doc;
170
 
        this.font = new RtfFont(this.document, font);
171
 
    }
172
 
    
173
 
    /**
174
 
     * Sets the RtfDocument this RtfElement belongs to
175
 
     * 
176
 
     * @param doc The RtfDocument to use
177
 
     */
178
 
    public void setRtfDocument(RtfDocument doc) {
179
 
        this.document = doc;
180
 
        this.font.setRtfDocument(this.document);
181
 
    }
182
 
    
183
 
    /**
184
 
     * Writes the field beginning. Also writes field properties.
185
 
     * 
186
 
     * @return A byte array with the field beginning.
187
 
     * @throws IOException
188
 
     */
189
 
    private byte[] writeFieldBegin() throws IOException {
190
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
191
 
 
192
 
        result.write(OPEN_GROUP);
193
 
        result.write(FIELD);
194
 
 
195
 
        if (fieldDirty) {
196
 
            result.write(FIELD_DIRTY);
197
 
        }
198
 
        if (fieldEdit) {
199
 
            result.write(FIELD_EDIT);
200
 
        }
201
 
        if (fieldLocked) {
202
 
            result.write(FIELD_LOCKED);
203
 
        }
204
 
        if (fieldPrivate) {
205
 
            result.write(FIELD_PRIVATE);
206
 
        }
207
 
 
208
 
        return result.toByteArray();
209
 
    }
210
 
    
211
 
    /**
212
 
     * Writes the beginning of the field instruction area.
213
 
     * 
214
 
     * @return The beginning of the field instruction area
215
 
     * @throws IOException
216
 
     */
217
 
    private byte[] writeFieldInstBegin() throws IOException {
218
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
219
 
        
220
 
        result.write(OPEN_GROUP);        
221
 
        result.write(FIELD_INSTRUCTIONS);
222
 
        result.write(DELIMITER);
223
 
        
224
 
        return result.toByteArray();
225
 
    }
226
 
    
227
 
    /**
228
 
     * Writes the content of the field instruction area. Override this
229
 
     * method in your subclasses.
230
 
     * 
231
 
     * @return The content of the field instruction area
232
 
     * @throws IOException If an error occurs.
233
 
     */
234
 
    protected abstract byte[] writeFieldInstContent() throws IOException;
235
 
    
236
 
    /**
237
 
     * Writes the end of the field instruction area.
238
 
     * 
239
 
     * @return A byte array containing the end of the field instruction area
240
 
     * @throws IOException
241
 
     */
242
 
    private byte[] writeFieldInstEnd() throws IOException {
243
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
244
 
 
245
 
        if (fieldAlt) {
246
 
            result.write(DELIMITER);
247
 
            result.write(FIELD_ALT);
248
 
        }
249
 
        result.write(CLOSE_GROUP);
250
 
        
251
 
        return result.toByteArray();
252
 
    }
253
 
    
254
 
    /**
255
 
     * Writes the beginning of the field result area
256
 
     * 
257
 
     * @return A byte array containing the beginning of the field result area
258
 
     * @throws IOException
259
 
     */
260
 
    private byte[] writeFieldResultBegin() throws IOException {
261
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
262
 
        
263
 
        result.write(OPEN_GROUP);
264
 
        result.write(FIELD_RESULT);
265
 
        result.write(DELIMITER);
266
 
        
267
 
        return result.toByteArray();
268
 
    }
269
 
    
270
 
    /**
271
 
     * Writes the content of the pre-calculated field result. Override this
272
 
     * method in your subclasses.
273
 
     * 
274
 
     * @return A byte array containing the field result
275
 
     * @throws IOException If an error occurs
276
 
     */
277
 
    protected abstract byte[] writeFieldResultContent() throws IOException;
278
 
    
279
 
    /**
280
 
     * Writes the end of the field result area
281
 
     * 
282
 
     * @return A byte array containing the end of the field result area
283
 
     * @throws IOException
284
 
     */
285
 
    private byte[] writeFieldResultEnd() throws IOException {
286
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
287
 
        
288
 
        result.write(DELIMITER);
289
 
        result.write(CLOSE_GROUP);
290
 
        
291
 
        return result.toByteArray();
292
 
    }
293
 
 
294
 
    /**
295
 
     * Writes the end of the field
296
 
     * 
297
 
     * @return A byte array containing the end of the field
298
 
     * @throws IOException
299
 
     */
300
 
    private byte[] writeFieldEnd() throws IOException {
301
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
302
 
        
303
 
        result.write(CLOSE_GROUP);
304
 
        
305
 
        return result.toByteArray();
306
 
    }
307
 
    
308
 
    /**
309
 
     * Write the content of this RtfField.
310
 
     * 
311
 
     * @return A byte array containing the content of this RtfField
312
 
     */
313
 
    public byte[] write() {
314
 
        ByteArrayOutputStream result = new ByteArrayOutputStream();
315
 
        try {
316
 
            result.write(this.font.writeBegin());
317
 
            result.write(writeFieldBegin());
318
 
            result.write(writeFieldInstBegin());
319
 
            result.write(writeFieldInstContent());
320
 
            result.write(writeFieldInstEnd());
321
 
            result.write(writeFieldResultBegin());
322
 
            result.write(writeFieldResultContent());
323
 
            result.write(writeFieldResultEnd());
324
 
            result.write(writeFieldEnd());
325
 
            result.write(this.font.writeEnd());
326
 
        } catch(IOException ioe) {
327
 
            ioe.printStackTrace();
328
 
        }
329
 
        return result.toByteArray();
330
 
    }
331
 
        
332
 
    /**
333
 
     * Get whether this field is an alt field
334
 
     * 
335
 
     * @return Returns whether this field is an alt field
336
 
     */
337
 
    public boolean isFieldAlt() {
338
 
        return fieldAlt;
339
 
    }
340
 
    
341
 
    /**
342
 
     * Set whether this field is an alt field
343
 
     * 
344
 
     * @param fieldAlt The value to use
345
 
     */
346
 
    public void setFieldAlt(boolean fieldAlt) {
347
 
        this.fieldAlt = fieldAlt;
348
 
    }
349
 
    
350
 
    /**
351
 
     * Get whether this field is dirty
352
 
     * 
353
 
     * @return Returns whether this field is dirty
354
 
     */
355
 
    public boolean isFieldDirty() {
356
 
        return fieldDirty;
357
 
    }
358
 
    
359
 
    /**
360
 
     * Set whether this field is dirty
361
 
     * 
362
 
     * @param fieldDirty The value to use
363
 
     */
364
 
    public void setFieldDirty(boolean fieldDirty) {
365
 
        this.fieldDirty = fieldDirty;
366
 
    }
367
 
    
368
 
    /**
369
 
     * Get whether this field is edited
370
 
     * 
371
 
     * @return Returns whether this field is edited
372
 
     */
373
 
    public boolean isFieldEdit() {
374
 
        return fieldEdit;
375
 
    }
376
 
    
377
 
    /**
378
 
     * Set whether this field is edited.
379
 
     * 
380
 
     * @param fieldEdit The value to use
381
 
     */
382
 
    public void setFieldEdit(boolean fieldEdit) {
383
 
        this.fieldEdit = fieldEdit;
384
 
    }
385
 
    
386
 
    /**
387
 
     * Get whether this field is locked
388
 
     * 
389
 
     * @return Returns the fieldLocked.
390
 
     */
391
 
    public boolean isFieldLocked() {
392
 
        return fieldLocked;
393
 
    }
394
 
    
395
 
    /**
396
 
     * Set whether this field is locked
397
 
     * @param fieldLocked The value to use
398
 
     */
399
 
    public void setFieldLocked(boolean fieldLocked) {
400
 
        this.fieldLocked = fieldLocked;
401
 
    }
402
 
    
403
 
    /**
404
 
     * Get whether this field is private
405
 
     * 
406
 
     * @return Returns the fieldPrivate.
407
 
     */
408
 
    public boolean isFieldPrivate() {
409
 
        return fieldPrivate;
410
 
    }
411
 
    
412
 
    /**
413
 
     * Set whether this field is private
414
 
     * 
415
 
     * @param fieldPrivate The value to use
416
 
     */
417
 
    public void setFieldPrivate(boolean fieldPrivate) {
418
 
        this.fieldPrivate = fieldPrivate;
419
 
    }
420
 
 
421
 
    /**
422
 
     * Sets whether this RtfField is in a table
423
 
     * 
424
 
     * @param inTable <code>True</code> if this RtfField is in a table, <code>false</code> otherwise
425
 
     */
426
 
    public void setInTable(boolean inTable) {
427
 
        this.inTable = inTable;
428
 
    }
429
 
    
430
 
    /**
431
 
     * Sets whether this RtfField is in a header
432
 
     * 
433
 
     * @param inHeader <code>True</code> if this RtfField is in a header, <code>false</code> otherwise
434
 
     */
435
 
    public void setInHeader(boolean inHeader) {
436
 
        this.inHeader = inHeader;
437
 
    }
438
 
    
439
 
    /**
440
 
     * An RtfField is never empty.
441
 
     */
442
 
    public boolean isEmpty() {
443
 
        return false;
444
 
    }
445
 
}