2
* $Id: RtfField.java,v 1.12 2006/09/12 13:12:56 blowagie Exp $
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)
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/
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.
18
* The Original Code is 'iText, a free JAVA-PDF library'.
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.
26
* Contributor(s): all the names of the contributors are added in the source code
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.
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.
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
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/
54
package com.lowagie.text.rtf.field;
56
import java.io.ByteArrayOutputStream;
57
import java.io.IOException;
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;
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
73
* @author Mark Hall (mhall@edu.uni-klu.ac.at)
74
* @author Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
76
public abstract class RtfField extends Chunk implements RtfBasicElement {
79
* Constant for a rtf field
81
private static final byte[] FIELD = "\\field".getBytes();
83
* Constant for a dirty field
85
private static final byte[] FIELD_DIRTY = "\\flddirty".getBytes();
87
* Constant for a private field
89
private static final byte[] FIELD_PRIVATE = "\\fldpriv".getBytes();
91
* Constant for a locked field
93
private static final byte[] FIELD_LOCKED = "\\fldlock".getBytes();
95
* Constant for a edited field
97
private static final byte[] FIELD_EDIT = "\\fldedit".getBytes();
99
* Constant for an alt field
101
private static final byte[] FIELD_ALT = "\\fldalt".getBytes();
103
* Constant for the field instructions
105
private static final byte[] FIELD_INSTRUCTIONS = "\\*\\fldinst".getBytes();
107
* Constant for the field result
109
private static final byte[] FIELD_RESULT = "\\fldrslt".getBytes();
114
private boolean fieldDirty = false;
116
* Is the field edited
118
private boolean fieldEdit = false;
120
* Is the field locked
122
private boolean fieldLocked = false;
124
* Is the field private
126
private boolean fieldPrivate = false;
130
private boolean fieldAlt = false;
132
* Whether this RtfField is in a table
134
private boolean inTable = false;
136
* Whether this RtfElement is in a header
138
private boolean inHeader = false;
140
* The RtfDocument this RtfField belongs to
142
protected RtfDocument document = null;
144
* The RtfFont of this RtfField
146
private RtfFont font = null;
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.
153
* @param doc The RtfDocument this RtfField belongs to.
155
protected RtfField(RtfDocument doc) {
156
this(doc, new Font());
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.
164
* @param doc The RtfDocument this RtfField belongs to.
165
* @param font The Font this RtfField should use
167
protected RtfField(RtfDocument doc, Font font) {
170
this.font = new RtfFont(this.document, font);
174
* Sets the RtfDocument this RtfElement belongs to
176
* @param doc The RtfDocument to use
178
public void setRtfDocument(RtfDocument doc) {
180
this.font.setRtfDocument(this.document);
184
* Writes the field beginning. Also writes field properties.
186
* @return A byte array with the field beginning.
187
* @throws IOException
189
private byte[] writeFieldBegin() throws IOException {
190
ByteArrayOutputStream result = new ByteArrayOutputStream();
192
result.write(OPEN_GROUP);
196
result.write(FIELD_DIRTY);
199
result.write(FIELD_EDIT);
202
result.write(FIELD_LOCKED);
205
result.write(FIELD_PRIVATE);
208
return result.toByteArray();
212
* Writes the beginning of the field instruction area.
214
* @return The beginning of the field instruction area
215
* @throws IOException
217
private byte[] writeFieldInstBegin() throws IOException {
218
ByteArrayOutputStream result = new ByteArrayOutputStream();
220
result.write(OPEN_GROUP);
221
result.write(FIELD_INSTRUCTIONS);
222
result.write(DELIMITER);
224
return result.toByteArray();
228
* Writes the content of the field instruction area. Override this
229
* method in your subclasses.
231
* @return The content of the field instruction area
232
* @throws IOException If an error occurs.
234
protected abstract byte[] writeFieldInstContent() throws IOException;
237
* Writes the end of the field instruction area.
239
* @return A byte array containing the end of the field instruction area
240
* @throws IOException
242
private byte[] writeFieldInstEnd() throws IOException {
243
ByteArrayOutputStream result = new ByteArrayOutputStream();
246
result.write(DELIMITER);
247
result.write(FIELD_ALT);
249
result.write(CLOSE_GROUP);
251
return result.toByteArray();
255
* Writes the beginning of the field result area
257
* @return A byte array containing the beginning of the field result area
258
* @throws IOException
260
private byte[] writeFieldResultBegin() throws IOException {
261
ByteArrayOutputStream result = new ByteArrayOutputStream();
263
result.write(OPEN_GROUP);
264
result.write(FIELD_RESULT);
265
result.write(DELIMITER);
267
return result.toByteArray();
271
* Writes the content of the pre-calculated field result. Override this
272
* method in your subclasses.
274
* @return A byte array containing the field result
275
* @throws IOException If an error occurs
277
protected abstract byte[] writeFieldResultContent() throws IOException;
280
* Writes the end of the field result area
282
* @return A byte array containing the end of the field result area
283
* @throws IOException
285
private byte[] writeFieldResultEnd() throws IOException {
286
ByteArrayOutputStream result = new ByteArrayOutputStream();
288
result.write(DELIMITER);
289
result.write(CLOSE_GROUP);
291
return result.toByteArray();
295
* Writes the end of the field
297
* @return A byte array containing the end of the field
298
* @throws IOException
300
private byte[] writeFieldEnd() throws IOException {
301
ByteArrayOutputStream result = new ByteArrayOutputStream();
303
result.write(CLOSE_GROUP);
305
return result.toByteArray();
309
* Write the content of this RtfField.
311
* @return A byte array containing the content of this RtfField
313
public byte[] write() {
314
ByteArrayOutputStream result = new ByteArrayOutputStream();
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();
329
return result.toByteArray();
333
* Get whether this field is an alt field
335
* @return Returns whether this field is an alt field
337
public boolean isFieldAlt() {
342
* Set whether this field is an alt field
344
* @param fieldAlt The value to use
346
public void setFieldAlt(boolean fieldAlt) {
347
this.fieldAlt = fieldAlt;
351
* Get whether this field is dirty
353
* @return Returns whether this field is dirty
355
public boolean isFieldDirty() {
360
* Set whether this field is dirty
362
* @param fieldDirty The value to use
364
public void setFieldDirty(boolean fieldDirty) {
365
this.fieldDirty = fieldDirty;
369
* Get whether this field is edited
371
* @return Returns whether this field is edited
373
public boolean isFieldEdit() {
378
* Set whether this field is edited.
380
* @param fieldEdit The value to use
382
public void setFieldEdit(boolean fieldEdit) {
383
this.fieldEdit = fieldEdit;
387
* Get whether this field is locked
389
* @return Returns the fieldLocked.
391
public boolean isFieldLocked() {
396
* Set whether this field is locked
397
* @param fieldLocked The value to use
399
public void setFieldLocked(boolean fieldLocked) {
400
this.fieldLocked = fieldLocked;
404
* Get whether this field is private
406
* @return Returns the fieldPrivate.
408
public boolean isFieldPrivate() {
413
* Set whether this field is private
415
* @param fieldPrivate The value to use
417
public void setFieldPrivate(boolean fieldPrivate) {
418
this.fieldPrivate = fieldPrivate;
422
* Sets whether this RtfField is in a table
424
* @param inTable <code>True</code> if this RtfField is in a table, <code>false</code> otherwise
426
public void setInTable(boolean inTable) {
427
this.inTable = inTable;
431
* Sets whether this RtfField is in a header
433
* @param inHeader <code>True</code> if this RtfField is in a header, <code>false</code> otherwise
435
public void setInHeader(boolean inHeader) {
436
this.inHeader = inHeader;
440
* An RtfField is never empty.
442
public boolean isEmpty() {