KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > field > RtfField


1 /*
2  * $Id: RtfField.java 2776 2007-05-23 20:01:40Z hallm $
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 JavaDoc;
57 import java.io.IOException JavaDoc;
58 import java.io.OutputStream JavaDoc;
59
60 import com.lowagie.text.Chunk;
61 import com.lowagie.text.Font;
62 import com.lowagie.text.rtf.RtfBasicElement;
63 import com.lowagie.text.rtf.document.RtfDocument;
64 import com.lowagie.text.rtf.style.RtfFont;
65
66
67 /**
68  * The RtfField class is an abstract base class for all rtf field functionality.
69  * Subclasses only need to implement the two abstract methods writeFieldInstContent
70  * and writeFieldResultContent. All other field functionality is handled by the
71  * RtfField class.
72  *
73  * @version $Id: RtfField.java 2776 2007-05-23 20:01:40Z hallm $
74  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
75  * @author Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
76  * @author Thomas Bickel (tmb99@inode.at)
77  */

78 public abstract class RtfField extends Chunk implements RtfBasicElement {
79
80     /**
81      * Constant for a rtf field
82      */

83     private static final byte[] FIELD = "\\field".getBytes();
84     /**
85      * Constant for a dirty field
86      */

87     private static final byte[] FIELD_DIRTY = "\\flddirty".getBytes();
88     /**
89      * Constant for a private field
90      */

91     private static final byte[] FIELD_PRIVATE = "\\fldpriv".getBytes();
92     /**
93      * Constant for a locked field
94      */

95     private static final byte[] FIELD_LOCKED = "\\fldlock".getBytes();
96     /**
97      * Constant for a edited field
98      */

99     private static final byte[] FIELD_EDIT = "\\fldedit".getBytes();
100     /**
101      * Constant for an alt field
102      */

103     private static final byte[] FIELD_ALT = "\\fldalt".getBytes();
104     /**
105      * Constant for the field instructions
106      */

107     private static final byte[] FIELD_INSTRUCTIONS = "\\*\\fldinst".getBytes();
108     /**
109      * Constant for the field result
110      */

111     private static final byte[] FIELD_RESULT = "\\fldrslt".getBytes();
112
113     /**
114      * Is the field dirty
115      */

116     private boolean fieldDirty = false;
117     /**
118      * Is the field edited
119      */

120     private boolean fieldEdit = false;
121     /**
122      * Is the field locked
123      */

124     private boolean fieldLocked = false;
125     /**
126      * Is the field private
127      */

128     private boolean fieldPrivate = false;
129     /**
130      * Is it an alt field
131      */

132     private boolean fieldAlt = false;
133     /**
134      * Whether this RtfField is in a table
135      */

136     private boolean inTable = false;
137     /**
138      * Whether this RtfElement is in a header
139      */

140     private boolean inHeader = false;
141     /**
142      * The RtfDocument this RtfField belongs to
143      */

144     protected RtfDocument document = null;
145     /**
146      * The RtfFont of this RtfField
147      */

148     private RtfFont font = null;
149
150     /**
151      * Constructs a RtfField for a RtfDocument. This is not very useful,
152      * since the RtfField by itself does not do anything. Use one of the
153      * subclasses instead.
154      *
155      * @param doc The RtfDocument this RtfField belongs to.
156      */

157     protected RtfField(RtfDocument doc) {
158         this(doc, new Font());
159     }
160     
161     /**
162      * Constructs a RtfField for a RtfDocument. This is not very useful,
163      * since the RtfField by itself does not do anything. Use one of the
164      * subclasses instead.
165      *
166      * @param doc The RtfDocument this RtfField belongs to.
167      * @param font The Font this RtfField should use
168      */

169     protected RtfField(RtfDocument doc, Font font) {
170         super("", font);
171         this.document = doc;
172         this.font = new RtfFont(this.document, font);
173     }
174     
175     /**
176      * Sets the RtfDocument this RtfElement belongs to
177      *
178      * @param doc The RtfDocument to use
179      */

180     public void setRtfDocument(RtfDocument doc) {
181         this.document = doc;
182         this.font.setRtfDocument(this.document);
183     }
184     
185     /**
186      * Writes the field beginning. Also writes field properties.
187      *
188      * @return A byte array with the field beginning.
189      * @deprecated replaced by {@link #writeFieldBegin(OutputStream)}
190      * @throws IOException
191      */

192     private byte[] writeFieldBegin() throws IOException JavaDoc
193     {
194         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
195         writeFieldBegin(result);
196         return result.toByteArray();
197     }
198     /**
199      * Writes the field beginning. Also writes field properties.
200      *
201      * @return A byte array with the field beginning.
202      * @throws IOException
203      */

204     private void writeFieldBegin(OutputStream JavaDoc result) throws IOException JavaDoc
205     {
206         result.write(OPEN_GROUP);
207         result.write(FIELD);
208         if(fieldDirty) result.write(FIELD_DIRTY);
209         if(fieldEdit) result.write(FIELD_EDIT);
210         if(fieldLocked) result.write(FIELD_LOCKED);
211         if(fieldPrivate) result.write(FIELD_PRIVATE);
212     }
213     
214     /**
215      * Writes the beginning of the field instruction area.
216      *
217      * @return The beginning of the field instruction area
218      * @deprecated replaced by {@link #writeFieldInstBegin(OutputStream)}
219      * @throws IOException
220      */

221     private byte[] writeFieldInstBegin() throws IOException JavaDoc
222     {
223         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
224         writeFieldInstBegin(result);
225         return result.toByteArray();
226     }
227     /**
228      * Writes the beginning of the field instruction area.
229      *
230      * @return The beginning of the field instruction area
231      * @throws IOException
232      */

233     private void writeFieldInstBegin(OutputStream JavaDoc result) throws IOException JavaDoc
234     {
235         result.write(OPEN_GROUP);
236         result.write(FIELD_INSTRUCTIONS);
237         result.write(DELIMITER);
238     }
239     
240     /**
241      * Writes the content of the field instruction area. Override this
242      * method in your subclasses.
243      *
244      * @return The content of the field instruction area
245      * @deprecated replaced by {@link #writeFieldInstContent(OutputStream)}
246      * @throws IOException If an error occurs.
247      */

248     protected abstract byte[] writeFieldInstContent() throws IOException JavaDoc;
249
250     /**
251      * Writes the content of the field instruction area. Override this
252      * method in your subclasses.
253      */

254     protected void writeFieldInstContent(OutputStream JavaDoc out) throws IOException JavaDoc
255     {
256         byte[] b = writeFieldInstContent();
257         if(b != null) out.write(b);
258     }
259     
260     /**
261      * Writes the end of the field instruction area.
262      *
263      * @return A byte array containing the end of the field instruction area
264      * @deprecated replaced by {@link #writeFieldInstEnd(OutputStream)}
265      * @throws IOException
266      */

267     private byte[] writeFieldInstEnd() throws IOException JavaDoc
268     {
269         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
270         writeFieldInstEnd(result);
271         return result.toByteArray();
272     }
273     /**
274      * Writes the end of the field instruction area.
275      */

276     private void writeFieldInstEnd(OutputStream JavaDoc result) throws IOException JavaDoc
277     {
278         if(fieldAlt) {
279             result.write(DELIMITER);
280             result.write(FIELD_ALT);
281         }
282         result.write(CLOSE_GROUP);
283     }
284     
285     /**
286      * Writes the beginning of the field result area
287      *
288      * @return A byte array containing the beginning of the field result area
289      * @deprecated replaced by {@link #writeFieldResultBegin(OutputStream)}
290      * @throws IOException
291      */

292     private byte[] writeFieldResultBegin() throws IOException JavaDoc
293     {
294         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
295         writeFieldResultBegin(result);
296         return result.toByteArray();
297     }
298     /**
299      * Writes the beginning of the field result area
300      */

301     private void writeFieldResultBegin(final OutputStream JavaDoc result) throws IOException JavaDoc
302     {
303         result.write(OPEN_GROUP);
304         result.write(FIELD_RESULT);
305         result.write(DELIMITER);
306     }
307     
308     /**
309      * Writes the content of the pre-calculated field result. Override this
310      * method in your subclasses.
311      *
312      * @return A byte array containing the field result
313      * @deprecated replaced by {@link #writeFieldResultContent(OutputStream)}
314      * @throws IOException If an error occurs
315      */

316     protected abstract byte[] writeFieldResultContent() throws IOException JavaDoc;
317     /**
318      * Writes the content of the pre-calculated field result. Override this
319      * method in your subclasses.
320      */

321     protected void writeFieldResultContent(OutputStream JavaDoc out) throws IOException JavaDoc
322     {
323         byte[] b = writeFieldResultContent();
324         if(b != null) out.write(b);
325     }
326     
327     /**
328      * Writes the end of the field result area
329      *
330      * @return A byte array containing the end of the field result area
331      * @deprecated replaced by {@link #writeFieldResultEnd(OutputStream)}
332      * @throws IOException
333      */

334     private byte[] writeFieldResultEnd() throws IOException JavaDoc
335     {
336         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
337         writeFieldResultEnd(result);
338         return result.toByteArray();
339     }
340     /**
341      * Writes the end of the field result area
342      */

343     private void writeFieldResultEnd(final OutputStream JavaDoc result) throws IOException JavaDoc
344     {
345         result.write(DELIMITER);
346         result.write(CLOSE_GROUP);
347     }
348     
349     /**
350      * Writes the end of the field
351      *
352      * @return A byte array containing the end of the field
353      * @deprecated replaced by {@link #writeFieldEnd(OutputStream)}
354      * @throws IOException
355      */

356     private byte[] writeFieldEnd() throws IOException JavaDoc
357     {
358         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
359         writeFieldEnd(result);
360         return result.toByteArray();
361     }
362     /**
363      * Writes the end of the field
364      */

365     private void writeFieldEnd(OutputStream JavaDoc result) throws IOException JavaDoc
366     {
367         result.write(CLOSE_GROUP);
368     }
369     
370     
371     /**
372      * Write the content of this RtfField.
373      *
374      * @return A byte array containing the content of this RtfField
375      * @deprecated replaced by {@link #writeContent(OutputStream)}
376      */

377     public byte[] write()
378     {
379         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
380         try {
381             writeContent(result);
382         } catch(IOException JavaDoc ioe) {
383             ioe.printStackTrace();
384         }
385         return result.toByteArray();
386     }
387     /**
388      * Writes the element content to the given output stream.
389      */

390     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
391     {
392         result.write(this.font.writeBegin());
393         
394 // result.write(writeFieldBegin());
395
// result.write(writeFieldInstBegin());
396
// result.write(writeFieldInstContent());
397
// result.write(writeFieldInstEnd());
398
// result.write(writeFieldResultBegin());
399
// result.write(writeFieldResultContent());
400
// result.write(writeFieldResultEnd());
401
// result.write(writeFieldEnd());
402
writeFieldBegin(result);
403         writeFieldInstBegin(result);
404         writeFieldInstContent(result);
405         writeFieldInstEnd(result);
406         writeFieldResultBegin(result);
407         writeFieldResultContent(result);
408         writeFieldResultEnd(result);
409         writeFieldEnd(result);
410         
411         result.write(this.font.writeEnd());
412     }
413         
414     /**
415      * Get whether this field is an alt field
416      *
417      * @return Returns whether this field is an alt field
418      */

419     public boolean isFieldAlt() {
420         return fieldAlt;
421     }
422     
423     /**
424      * Set whether this field is an alt field
425      *
426      * @param fieldAlt The value to use
427      */

428     public void setFieldAlt(boolean fieldAlt) {
429         this.fieldAlt = fieldAlt;
430     }
431     
432     /**
433      * Get whether this field is dirty
434      *
435      * @return Returns whether this field is dirty
436      */

437     public boolean isFieldDirty() {
438         return fieldDirty;
439     }
440     
441     /**
442      * Set whether this field is dirty
443      *
444      * @param fieldDirty The value to use
445      */

446     public void setFieldDirty(boolean fieldDirty) {
447         this.fieldDirty = fieldDirty;
448     }
449     
450     /**
451      * Get whether this field is edited
452      *
453      * @return Returns whether this field is edited
454      */

455     public boolean isFieldEdit() {
456         return fieldEdit;
457     }
458     
459     /**
460      * Set whether this field is edited.
461      *
462      * @param fieldEdit The value to use
463      */

464     public void setFieldEdit(boolean fieldEdit) {
465         this.fieldEdit = fieldEdit;
466     }
467     
468     /**
469      * Get whether this field is locked
470      *
471      * @return Returns the fieldLocked.
472      */

473     public boolean isFieldLocked() {
474         return fieldLocked;
475     }
476     
477     /**
478      * Set whether this field is locked
479      * @param fieldLocked The value to use
480      */

481     public void setFieldLocked(boolean fieldLocked) {
482         this.fieldLocked = fieldLocked;
483     }
484     
485     /**
486      * Get whether this field is private
487      *
488      * @return Returns the fieldPrivate.
489      */

490     public boolean isFieldPrivate() {
491         return fieldPrivate;
492     }
493     
494     /**
495      * Set whether this field is private
496      *
497      * @param fieldPrivate The value to use
498      */

499     public void setFieldPrivate(boolean fieldPrivate) {
500         this.fieldPrivate = fieldPrivate;
501     }
502
503     /**
504      * Sets whether this RtfField is in a table
505      *
506      * @param inTable <code>True</code> if this RtfField is in a table, <code>false</code> otherwise
507      */

508     public void setInTable(boolean inTable) {
509         this.inTable = inTable;
510     }
511     
512     /**
513      * Sets whether this RtfField is in a header
514      *
515      * @param inHeader <code>True</code> if this RtfField is in a header, <code>false</code> otherwise
516      */

517     public void setInHeader(boolean inHeader) {
518         this.inHeader = inHeader;
519     }
520     
521     /**
522      * An RtfField is never empty.
523      */

524     public boolean isEmpty() {
525         return false;
526     }
527     
528     /**
529      * Override setFont to perform the correct font handling.
530      */

531     public void setFont(Font font) {
532         super.setFont(font);
533         this.font = new RtfFont(this.document, font);
534     }
535 }
536
Popular Tags