001: /*
002: * $Id: RtfField.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2004 by Mark Hall
006: * Uses code Copyright 2002
007: * <a href="http://www.smb-tec.com">SMB</a>
008: * Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
009: *
010: * The contents of this file are subject to the Mozilla Public License Version 1.1
011: * (the "License"); you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
013: *
014: * Software distributed under the License is distributed on an "AS IS" basis,
015: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
016: * for the specific language governing rights and limitations under the License.
017: *
018: * The Original Code is 'iText, a free JAVA-PDF library'.
019: *
020: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
021: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
022: * All Rights Reserved.
023: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
024: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
025: *
026: * Contributor(s): all the names of the contributors are added in the source code
027: * where applicable.
028: *
029: * Alternatively, the contents of this file may be used under the terms of the
030: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
031: * provisions of LGPL are applicable instead of those above. If you wish to
032: * allow use of your version of this file only under the terms of the LGPL
033: * License and not to allow others to use your version of this file under
034: * the MPL, indicate your decision by deleting the provisions above and
035: * replace them with the notice and other provisions required by the LGPL.
036: * If you do not delete the provisions above, a recipient may use your version
037: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
038: *
039: * This library is free software; you can redistribute it and/or modify it
040: * under the terms of the MPL as stated above or under the terms of the GNU
041: * Library General Public License as published by the Free Software Foundation;
042: * either version 2 of the License, or any later version.
043: *
044: * This library is distributed in the hope that it will be useful, but WITHOUT
045: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
046: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
047: * details.
048: *
049: * If you didn't download this code from the following link, you should check if
050: * you aren't using an obsolete version:
051: * http://www.lowagie.com/iText/
052: */
053:
054: package com.lowagie.text.rtf.field;
055:
056: import java.io.ByteArrayOutputStream;
057: import java.io.IOException;
058: import java.io.OutputStream;
059:
060: import com.lowagie.text.Chunk;
061: import com.lowagie.text.Font;
062: import com.lowagie.text.rtf.RtfBasicElement;
063: import com.lowagie.text.rtf.document.RtfDocument;
064: import com.lowagie.text.rtf.style.RtfFont;
065:
066: /**
067: * The RtfField class is an abstract base class for all rtf field functionality.
068: * Subclasses only need to implement the two abstract methods writeFieldInstContent
069: * and writeFieldResultContent. All other field functionality is handled by the
070: * RtfField class.
071: *
072: * @version $Id: RtfField.java 2776 2007-05-23 20:01:40Z hallm $
073: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
074: * @author Dirk Weigenand (Dirk.Weigenand@smb-tec.com)
075: * @author Thomas Bickel (tmb99@inode.at)
076: */
077: public abstract class RtfField extends Chunk implements RtfBasicElement {
078:
079: /**
080: * Constant for a rtf field
081: */
082: private static final byte[] FIELD = "\\field".getBytes();
083: /**
084: * Constant for a dirty field
085: */
086: private static final byte[] FIELD_DIRTY = "\\flddirty".getBytes();
087: /**
088: * Constant for a private field
089: */
090: private static final byte[] FIELD_PRIVATE = "\\fldpriv".getBytes();
091: /**
092: * Constant for a locked field
093: */
094: private static final byte[] FIELD_LOCKED = "\\fldlock".getBytes();
095: /**
096: * Constant for a edited field
097: */
098: private static final byte[] FIELD_EDIT = "\\fldedit".getBytes();
099: /**
100: * Constant for an alt field
101: */
102: private static final byte[] FIELD_ALT = "\\fldalt".getBytes();
103: /**
104: * Constant for the field instructions
105: */
106: private static final byte[] FIELD_INSTRUCTIONS = "\\*\\fldinst"
107: .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 {
193: ByteArrayOutputStream result = new ByteArrayOutputStream();
194: writeFieldBegin(result);
195: return result.toByteArray();
196: }
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 result)
205: throws IOException {
206: result.write(OPEN_GROUP);
207: result.write(FIELD);
208: if (fieldDirty)
209: result.write(FIELD_DIRTY);
210: if (fieldEdit)
211: result.write(FIELD_EDIT);
212: if (fieldLocked)
213: result.write(FIELD_LOCKED);
214: if (fieldPrivate)
215: result.write(FIELD_PRIVATE);
216: }
217:
218: /**
219: * Writes the beginning of the field instruction area.
220: *
221: * @return The beginning of the field instruction area
222: * @deprecated replaced by {@link #writeFieldInstBegin(OutputStream)}
223: * @throws IOException
224: */
225: private byte[] writeFieldInstBegin() throws IOException {
226: ByteArrayOutputStream result = new ByteArrayOutputStream();
227: writeFieldInstBegin(result);
228: return result.toByteArray();
229: }
230:
231: /**
232: * Writes the beginning of the field instruction area.
233: *
234: * @return The beginning of the field instruction area
235: * @throws IOException
236: */
237: private void writeFieldInstBegin(OutputStream result)
238: throws IOException {
239: result.write(OPEN_GROUP);
240: result.write(FIELD_INSTRUCTIONS);
241: result.write(DELIMITER);
242: }
243:
244: /**
245: * Writes the content of the field instruction area. Override this
246: * method in your subclasses.
247: *
248: * @return The content of the field instruction area
249: * @deprecated replaced by {@link #writeFieldInstContent(OutputStream)}
250: * @throws IOException If an error occurs.
251: */
252: protected abstract byte[] writeFieldInstContent()
253: throws IOException;
254:
255: /**
256: * Writes the content of the field instruction area. Override this
257: * method in your subclasses.
258: */
259: protected void writeFieldInstContent(OutputStream out)
260: throws IOException {
261: byte[] b = writeFieldInstContent();
262: if (b != null)
263: out.write(b);
264: }
265:
266: /**
267: * Writes the end of the field instruction area.
268: *
269: * @return A byte array containing the end of the field instruction area
270: * @deprecated replaced by {@link #writeFieldInstEnd(OutputStream)}
271: * @throws IOException
272: */
273: private byte[] writeFieldInstEnd() throws IOException {
274: ByteArrayOutputStream result = new ByteArrayOutputStream();
275: writeFieldInstEnd(result);
276: return result.toByteArray();
277: }
278:
279: /**
280: * Writes the end of the field instruction area.
281: */
282: private void writeFieldInstEnd(OutputStream result)
283: throws IOException {
284: if (fieldAlt) {
285: result.write(DELIMITER);
286: result.write(FIELD_ALT);
287: }
288: result.write(CLOSE_GROUP);
289: }
290:
291: /**
292: * Writes the beginning of the field result area
293: *
294: * @return A byte array containing the beginning of the field result area
295: * @deprecated replaced by {@link #writeFieldResultBegin(OutputStream)}
296: * @throws IOException
297: */
298: private byte[] writeFieldResultBegin() throws IOException {
299: ByteArrayOutputStream result = new ByteArrayOutputStream();
300: writeFieldResultBegin(result);
301: return result.toByteArray();
302: }
303:
304: /**
305: * Writes the beginning of the field result area
306: */
307: private void writeFieldResultBegin(final OutputStream result)
308: throws IOException {
309: result.write(OPEN_GROUP);
310: result.write(FIELD_RESULT);
311: result.write(DELIMITER);
312: }
313:
314: /**
315: * Writes the content of the pre-calculated field result. Override this
316: * method in your subclasses.
317: *
318: * @return A byte array containing the field result
319: * @deprecated replaced by {@link #writeFieldResultContent(OutputStream)}
320: * @throws IOException If an error occurs
321: */
322: protected abstract byte[] writeFieldResultContent()
323: throws IOException;
324:
325: /**
326: * Writes the content of the pre-calculated field result. Override this
327: * method in your subclasses.
328: */
329: protected void writeFieldResultContent(OutputStream out)
330: throws IOException {
331: byte[] b = writeFieldResultContent();
332: if (b != null)
333: out.write(b);
334: }
335:
336: /**
337: * Writes the end of the field result area
338: *
339: * @return A byte array containing the end of the field result area
340: * @deprecated replaced by {@link #writeFieldResultEnd(OutputStream)}
341: * @throws IOException
342: */
343: private byte[] writeFieldResultEnd() throws IOException {
344: ByteArrayOutputStream result = new ByteArrayOutputStream();
345: writeFieldResultEnd(result);
346: return result.toByteArray();
347: }
348:
349: /**
350: * Writes the end of the field result area
351: */
352: private void writeFieldResultEnd(final OutputStream result)
353: throws IOException {
354: result.write(DELIMITER);
355: result.write(CLOSE_GROUP);
356: }
357:
358: /**
359: * Writes the end of the field
360: *
361: * @return A byte array containing the end of the field
362: * @deprecated replaced by {@link #writeFieldEnd(OutputStream)}
363: * @throws IOException
364: */
365: private byte[] writeFieldEnd() throws IOException {
366: ByteArrayOutputStream result = new ByteArrayOutputStream();
367: writeFieldEnd(result);
368: return result.toByteArray();
369: }
370:
371: /**
372: * Writes the end of the field
373: */
374: private void writeFieldEnd(OutputStream result) throws IOException {
375: result.write(CLOSE_GROUP);
376: }
377:
378: /**
379: * Write the content of this RtfField.
380: *
381: * @return A byte array containing the content of this RtfField
382: * @deprecated replaced by {@link #writeContent(OutputStream)}
383: */
384: public byte[] write() {
385: ByteArrayOutputStream result = new ByteArrayOutputStream();
386: try {
387: writeContent(result);
388: } catch (IOException ioe) {
389: ioe.printStackTrace();
390: }
391: return result.toByteArray();
392: }
393:
394: /**
395: * Writes the element content to the given output stream.
396: */
397: public void writeContent(final OutputStream result)
398: throws IOException {
399: result.write(this .font.writeBegin());
400:
401: // result.write(writeFieldBegin());
402: // result.write(writeFieldInstBegin());
403: // result.write(writeFieldInstContent());
404: // result.write(writeFieldInstEnd());
405: // result.write(writeFieldResultBegin());
406: // result.write(writeFieldResultContent());
407: // result.write(writeFieldResultEnd());
408: // result.write(writeFieldEnd());
409: writeFieldBegin(result);
410: writeFieldInstBegin(result);
411: writeFieldInstContent(result);
412: writeFieldInstEnd(result);
413: writeFieldResultBegin(result);
414: writeFieldResultContent(result);
415: writeFieldResultEnd(result);
416: writeFieldEnd(result);
417:
418: result.write(this .font.writeEnd());
419: }
420:
421: /**
422: * Get whether this field is an alt field
423: *
424: * @return Returns whether this field is an alt field
425: */
426: public boolean isFieldAlt() {
427: return fieldAlt;
428: }
429:
430: /**
431: * Set whether this field is an alt field
432: *
433: * @param fieldAlt The value to use
434: */
435: public void setFieldAlt(boolean fieldAlt) {
436: this .fieldAlt = fieldAlt;
437: }
438:
439: /**
440: * Get whether this field is dirty
441: *
442: * @return Returns whether this field is dirty
443: */
444: public boolean isFieldDirty() {
445: return fieldDirty;
446: }
447:
448: /**
449: * Set whether this field is dirty
450: *
451: * @param fieldDirty The value to use
452: */
453: public void setFieldDirty(boolean fieldDirty) {
454: this .fieldDirty = fieldDirty;
455: }
456:
457: /**
458: * Get whether this field is edited
459: *
460: * @return Returns whether this field is edited
461: */
462: public boolean isFieldEdit() {
463: return fieldEdit;
464: }
465:
466: /**
467: * Set whether this field is edited.
468: *
469: * @param fieldEdit The value to use
470: */
471: public void setFieldEdit(boolean fieldEdit) {
472: this .fieldEdit = fieldEdit;
473: }
474:
475: /**
476: * Get whether this field is locked
477: *
478: * @return Returns the fieldLocked.
479: */
480: public boolean isFieldLocked() {
481: return fieldLocked;
482: }
483:
484: /**
485: * Set whether this field is locked
486: * @param fieldLocked The value to use
487: */
488: public void setFieldLocked(boolean fieldLocked) {
489: this .fieldLocked = fieldLocked;
490: }
491:
492: /**
493: * Get whether this field is private
494: *
495: * @return Returns the fieldPrivate.
496: */
497: public boolean isFieldPrivate() {
498: return fieldPrivate;
499: }
500:
501: /**
502: * Set whether this field is private
503: *
504: * @param fieldPrivate The value to use
505: */
506: public void setFieldPrivate(boolean fieldPrivate) {
507: this .fieldPrivate = fieldPrivate;
508: }
509:
510: /**
511: * Sets whether this RtfField is in a table
512: *
513: * @param inTable <code>True</code> if this RtfField is in a table, <code>false</code> otherwise
514: */
515: public void setInTable(boolean inTable) {
516: this .inTable = inTable;
517: }
518:
519: /**
520: * Sets whether this RtfField is in a header
521: *
522: * @param inHeader <code>True</code> if this RtfField is in a header, <code>false</code> otherwise
523: */
524: public void setInHeader(boolean inHeader) {
525: this .inHeader = inHeader;
526: }
527:
528: /**
529: * An RtfField is never empty.
530: */
531: public boolean isEmpty() {
532: return false;
533: }
534:
535: /**
536: * Override setFont to perform the correct font handling.
537: */
538: public void setFont(Font font) {
539: super .setFont(font);
540: this .font = new RtfFont(this.document, font);
541: }
542: }
|