KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > DocWriter


1 /*
2  * $Id: DocWriter.java 2563 2007-02-01 14:43:07Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text;
52
53 import java.io.BufferedOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.Properties JavaDoc;
58
59 import com.lowagie.text.pdf.OutputStreamCounter;
60
61 /**
62  * An abstract <CODE>Writer</CODE> class for documents.
63  * <P>
64  * <CODE>DocWriter</CODE> is the abstract class of several writers such
65  * as <CODE>PdfWriter</CODE> and <CODE>HtmlWriter</CODE>.
66  * A <CODE>DocWriter</CODE> can be added as a <CODE>DocListener</CODE>
67  * to a certain <CODE>Document</CODE> by getting an instance (see method
68  * <CODE>getInstance()</CODE> in the specific writer-classes).
69  * Every <CODE>Element</CODE> added to the original <CODE>Document</CODE>
70  * will be written to the <CODE>OutputStream</CODE> of the listening
71  * <CODE>DocWriter</CODE>.
72  *
73  * @see Document
74  * @see DocListener
75  */

76
77 public abstract class DocWriter implements DocListener {
78
79 /** This is some byte that is often used. */
80     public static final byte NEWLINE = (byte)'\n';
81
82 /** This is some byte that is often used. */
83     public static final byte TAB = (byte)'\t';
84
85 /** This is some byte that is often used. */
86     public static final byte LT = (byte)'<';
87
88 /** This is some byte that is often used. */
89     public static final byte SPACE = (byte)' ';
90
91 /** This is some byte that is often used. */
92     public static final byte EQUALS = (byte)'=';
93
94 /** This is some byte that is often used. */
95     public static final byte QUOTE = (byte)'\"';
96
97 /** This is some byte that is often used. */
98     public static final byte GT = (byte)'>';
99
100 /** This is some byte that is often used. */
101     public static final byte FORWARD = (byte)'/';
102
103     // membervariables
104

105 /** The pageSize. */
106     protected Rectangle pageSize;
107
108 /** This is the document that has to be written. */
109     protected Document document;
110
111 /** The outputstream of this writer. */
112     protected OutputStreamCounter os;
113
114 /** Is the writer open for writing? */
115     protected boolean open = false;
116
117 /** Do we have to pause all writing actions? */
118     protected boolean pause = false;
119     
120 /** Closes the stream on document close */
121     protected boolean closeStream = true;
122
123     // constructor
124

125     protected DocWriter() {
126     }
127
128 /**
129  * Constructs a <CODE>DocWriter</CODE>.
130  *
131  * @param document The <CODE>Document</CODE> that has to be written
132  * @param os The <CODE>OutputStream</CODE> the writer has to write to.
133  */

134
135     protected DocWriter(Document document, OutputStream JavaDoc os) {
136         this.document = document;
137         this.os = new OutputStreamCounter(new BufferedOutputStream JavaDoc(os));
138     }
139
140     // implementation of the DocListener methods
141

142 /**
143  * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
144  * <P>
145  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
146  * derived from this abstract class.
147  *
148  * @param element A high level object to add
149  * @return <CODE>false</CODE>
150  * @throws DocumentException when a document isn't open yet, or has been closed
151  */

152
153     public boolean add(Element element) throws DocumentException {
154         return false;
155     }
156
157 /**
158  * Signals that the <CODE>Document</CODE> was opened.
159  */

160
161     public void open() {
162         open = true;
163     }
164
165 /**
166  * Sets the pagesize.
167  *
168  * @param pageSize the new pagesize
169  * @return a <CODE>boolean</CODE>
170  */

171
172     public boolean setPageSize(Rectangle pageSize) {
173         this.pageSize = pageSize;
174         return true;
175     }
176
177 /**
178  * Sets the margins.
179  * <P>
180  * This does nothing. Has to be overridden if needed.
181  *
182  * @param marginLeft the margin on the left
183  * @param marginRight the margin on the right
184  * @param marginTop the margin on the top
185  * @param marginBottom the margin on the bottom
186  * @return <CODE>false</CODE>
187  */

188
189     public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) {
190         return false;
191     }
192
193 /**
194  * Signals that an new page has to be started.
195  * <P>
196  * This does nothing. Has to be overridden if needed.
197  *
198  * @return <CODE>true</CODE> if the page was added, <CODE>false</CODE> if not.
199  * @throws DocumentException when a document isn't open yet, or has been closed
200  */

201
202     public boolean newPage() {
203         if (!open) {
204             return false;
205         }
206         return true;
207     }
208
209 /**
210  * Changes the header of this document.
211  * <P>
212  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
213  * derived from this abstract class if they actually support the use of
214  * headers.
215  *
216  * @param header the new header
217  */

218
219     public void setHeader(HeaderFooter header) {
220     }
221
222 /**
223  * Resets the header of this document.
224  * <P>
225  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
226  * derived from this abstract class if they actually support the use of
227  * headers.
228  */

229
230     public void resetHeader() {
231     }
232
233 /**
234  * Changes the footer of this document.
235  * <P>
236  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
237  * derived from this abstract class if they actually support the use of
238  * footers.
239  *
240  * @param footer the new footer
241  */

242
243     public void setFooter(HeaderFooter footer) {
244     }
245
246 /**
247  * Resets the footer of this document.
248  * <P>
249  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
250  * derived from this abstract class if they actually support the use of
251  * footers.
252  */

253
254     public void resetFooter() {
255     }
256
257 /**
258  * Sets the page number to 0.
259  * <P>
260  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
261  * derived from this abstract class if they actually support the use of
262  * pagenumbers.
263  */

264
265     public void resetPageCount() {
266     }
267
268 /**
269  * Sets the page number.
270  * <P>
271  * This method should be overriden in the specific <CODE>DocWriter<CODE> classes
272  * derived from this abstract class if they actually support the use of
273  * pagenumbers.
274  *
275  * @param pageN the new page number
276  */

277
278     public void setPageCount(int pageN) {
279     }
280
281 /**
282  * Signals that the <CODE>Document</CODE> was closed and that no other
283  * <CODE>Elements</CODE> will be added.
284  */

285
286     public void close() {
287         open = false;
288         try {
289             os.flush();
290             if (closeStream)
291                 os.close();
292         }
293         catch(IOException JavaDoc ioe) {
294             throw new ExceptionConverter(ioe);
295         }
296     }
297
298     // methods
299

300 /** Converts a <CODE>String</CODE> into a <CODE>Byte</CODE> array
301  * according to the ISO-8859-1 codepage.
302  * @param text the text to be converted
303  * @return the conversion result
304  */

305
306     public static final byte[] getISOBytes(String JavaDoc text)
307     {
308         if (text == null)
309             return null;
310         int len = text.length();
311         byte b[] = new byte[len];
312         for (int k = 0; k < len; ++k)
313             b[k] = (byte)text.charAt(k);
314         return b;
315     }
316
317 /**
318  * Let the writer know that all writing has to be paused.
319  */

320
321     public void pause() {
322         pause = true;
323     }
324     
325     /**
326      * Checks if writing is paused.
327      *
328      * @return <CODE>true</CODE> if writing temporarely has to be paused, <CODE>false</CODE> otherwise.
329      */

330     
331     public boolean isPaused() {
332         return pause;
333     }
334
335 /**
336  * Let the writer know that writing may be resumed.
337  */

338
339     public void resume() {
340         pause = false;
341     }
342
343 /**
344  * Flushes the <CODE>BufferedOutputStream</CODE>.
345  */

346
347     public void flush() {
348         try {
349             os.flush();
350         }
351         catch(IOException JavaDoc ioe) {
352             throw new ExceptionConverter(ioe);
353         }
354     }
355
356 /**
357  * Writes a <CODE>String</CODE> to the <CODE>OutputStream</CODE>.
358  *
359  * @param string the <CODE>String</CODE> to write
360  * @throws IOException
361  */

362
363     protected void write(String JavaDoc string) throws IOException JavaDoc {
364         os.write(getISOBytes(string));
365     }
366
367 /**
368  * Writes a number of tabs.
369  *
370  * @param indent the number of tabs to add
371  * @throws IOException
372  */

373
374     protected void addTabs(int indent) throws IOException JavaDoc {
375         os.write(NEWLINE);
376         for (int i = 0; i < indent; i++) {
377             os.write(TAB);
378         }
379     }
380
381 /**
382  * Writes a key-value pair to the outputstream.
383  *
384  * @param key the name of an attribute
385  * @param value the value of an attribute
386  * @throws IOException
387  */

388
389     protected void write(String JavaDoc key, String JavaDoc value)
390     throws IOException JavaDoc {
391         os.write(SPACE);
392         write(key);
393         os.write(EQUALS);
394         os.write(QUOTE);
395         write(value);
396         os.write(QUOTE);
397     }
398
399 /**
400  * Writes a starttag to the outputstream.
401  *
402  * @param tag the name of the tag
403  * @throws IOException
404  */

405
406     protected void writeStart(String JavaDoc tag)
407     throws IOException JavaDoc {
408         os.write(LT);
409         write(tag);
410     }
411
412 /**
413  * Writes an endtag to the outputstream.
414  *
415  * @param tag the name of the tag
416  * @throws IOException
417  */

418
419     protected void writeEnd(String JavaDoc tag)
420     throws IOException JavaDoc {
421         os.write(LT);
422         os.write(FORWARD);
423         write(tag);
424         os.write(GT);
425     }
426
427 /**
428  * Writes an endtag to the outputstream.
429  * @throws IOException
430  */

431
432     protected void writeEnd()
433     throws IOException JavaDoc {
434         os.write(SPACE);
435         os.write(FORWARD);
436         os.write(GT);
437     }
438
439 /**
440  * Writes the markup attributes of the specified <CODE>MarkupAttributes</CODE>
441  * object to the <CODE>OutputStream</CODE>.
442  * @param markup a <CODE>Properties</CODE> collection to write.
443  * @return true, if writing the markup attributes succeeded
444  * @throws IOException
445  */

446     protected boolean writeMarkupAttributes(Properties JavaDoc markup)
447     throws IOException JavaDoc {
448         if (markup == null) return false;
449         Iterator JavaDoc attributeIterator = markup.keySet().iterator();
450         String JavaDoc name;
451         while (attributeIterator.hasNext()) {
452             name = String.valueOf(attributeIterator.next());
453             write(name, markup.getProperty(name));
454         }
455         markup.clear();
456         return true;
457     }
458
459     /** Checks if the stream is to be closed on document close
460      * @return true if the stream is closed on documnt close
461      *
462      */

463     public boolean isCloseStream() {
464         return closeStream;
465     }
466     
467     /** Sets the close state of the stream after document close
468      * @param closeStream true if the stream is closed on document close
469      *
470      */

471     public void setCloseStream(boolean closeStream) {
472         this.closeStream = closeStream;
473     }
474     
475     /**
476      * @see com.lowagie.text.DocListener#setMarginMirroring(boolean)
477      */

478     public boolean setMarginMirroring(boolean MarginMirroring) {
479         return false;
480     }
481     
482 }
483
Popular Tags