1 | /* | |
2 | * $Id: DocWriter.java 3937 2009-05-27 12:56:48Z blowagie $ | |
3 | * | |
4 | * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
5 | * | |
6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
7 | * (the "License"); you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
9 | * | |
10 | * Software distributed under the License is distributed on an "AS IS" basis, | |
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 | * for the specific language governing rights and limitations under the License. | |
13 | * | |
14 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
17 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
18 | * All Rights Reserved. | |
19 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
20 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
21 | * | |
22 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | ||
50 | package com.lowagie.text; | |
51 | ||
52 | import java.io.BufferedOutputStream; | |
53 | import java.io.IOException; | |
54 | import java.io.OutputStream; | |
55 | import java.util.Iterator; | |
56 | import java.util.Properties; | |
57 | ||
58 | import com.lowagie.text.ExceptionConverter; | |
59 | import com.lowagie.text.pdf.OutputStreamCounter; | |
60 | ||
61 | ||
62 | /** | |
63 | * An abstract <CODE>Writer</CODE> class for documents. | |
64 | * <P> | |
65 | * <CODE>DocWriter</CODE> is the abstract class of several writers such | |
66 | * as <CODE>PdfWriter</CODE> and <CODE>HtmlWriter</CODE>. | |
67 | * A <CODE>DocWriter</CODE> can be added as a <CODE>DocListener</CODE> | |
68 | * to a certain <CODE>Document</CODE> by getting an instance (see method | |
69 | * <CODE>getInstance()</CODE> in the specific writer-classes). | |
70 | * Every <CODE>Element</CODE> added to the original <CODE>Document</CODE> | |
71 | * will be written to the <CODE>OutputStream</CODE> of the listening | |
72 | * <CODE>DocWriter</CODE>. | |
73 | * | |
74 | * @see Document | |
75 | * @see DocListener | |
76 | */ | |
77 | ||
78 | public abstract class DocWriter implements DocListener { | |
79 | ||
80 | /** This is some byte that is often used. */ | |
81 | public static final byte NEWLINE = (byte)'\n'; | |
82 | ||
83 | /** This is some byte that is often used. */ | |
84 | public static final byte TAB = (byte)'\t'; | |
85 | ||
86 | /** This is some byte that is often used. */ | |
87 | public static final byte LT = (byte)'<'; | |
88 | ||
89 | /** This is some byte that is often used. */ | |
90 | public static final byte SPACE = (byte)' '; | |
91 | ||
92 | /** This is some byte that is often used. */ | |
93 | public static final byte EQUALS = (byte)'='; | |
94 | ||
95 | /** This is some byte that is often used. */ | |
96 | public static final byte QUOTE = (byte)'\"'; | |
97 | ||
98 | /** This is some byte that is often used. */ | |
99 | public static final byte GT = (byte)'>'; | |
100 | ||
101 | /** This is some byte that is often used. */ | |
102 | public static final byte FORWARD = (byte)'/'; | |
103 | ||
104 | // membervariables | |
105 | ||
106 | /** The pageSize. */ | |
107 | protected Rectangle pageSize; | |
108 | ||
109 | /** This is the document that has to be written. */ | |
110 | protected Document document; | |
111 | ||
112 | /** The outputstream of this writer. */ | |
113 | protected OutputStreamCounter os; | |
114 | ||
115 | /** Is the writer open for writing? */ | |
116 | protected boolean open = false; | |
117 | ||
118 | /** Do we have to pause all writing actions? */ | |
119 | protected boolean pause = false; | |
120 | | |
121 | /** Closes the stream on document close */ | |
122 | protected boolean closeStream = true; | |
123 | ||
124 | // constructor | |
125 | | |
126 | protected DocWriter() { | |
127 | } | |
128 | ||
129 | /** | |
130 | * Constructs a <CODE>DocWriter</CODE>. | |
131 | * | |
132 | * @param document The <CODE>Document</CODE> that has to be written | |
133 | * @param os The <CODE>OutputStream</CODE> the writer has to write to. | |
134 | */ | |
135 | ||
136 | protected DocWriter(Document document, OutputStream os) { | |
137 | this.document = document; | |
138 | this.os = new OutputStreamCounter(new BufferedOutputStream(os)); | |
139 | } | |
140 | ||
141 | // implementation of the DocListener methods | |
142 | ||
143 | /** | |
144 | * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. | |
145 | * <P> | |
146 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
147 | * derived from this abstract class. | |
148 | * | |
149 | * @param element A high level object to add | |
150 | * @return <CODE>false</CODE> | |
151 | * @throws DocumentException when a document isn't open yet, or has been closed | |
152 | */ | |
153 | ||
154 | public boolean add(Element element) throws DocumentException { | |
155 | return false; | |
156 | } | |
157 | ||
158 | /** | |
159 | * Signals that the <CODE>Document</CODE> was opened. | |
160 | */ | |
161 | ||
162 | public void open() { | |
163 | open = true; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Sets the pagesize. | |
168 | * | |
169 | * @param pageSize the new pagesize | |
170 | * @return a <CODE>boolean</CODE> | |
171 | */ | |
172 | ||
173 | public boolean setPageSize(Rectangle pageSize) { | |
174 | this.pageSize = pageSize; | |
175 |
1
1. setPageSize : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return true; |
176 | } | |
177 | ||
178 | /** | |
179 | * Sets the margins. | |
180 | * <P> | |
181 | * This does nothing. Has to be overridden if needed. | |
182 | * | |
183 | * @param marginLeft the margin on the left | |
184 | * @param marginRight the margin on the right | |
185 | * @param marginTop the margin on the top | |
186 | * @param marginBottom the margin on the bottom | |
187 | * @return <CODE>false</CODE> | |
188 | */ | |
189 | ||
190 | public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) { | |
191 | return false; | |
192 | } | |
193 | ||
194 | /** | |
195 | * Signals that an new page has to be started. | |
196 | * <P> | |
197 | * This does nothing. Has to be overridden if needed. | |
198 | * | |
199 | * @return <CODE>true</CODE> if the page was added, <CODE>false</CODE> if not. | |
200 | */ | |
201 | ||
202 | public boolean newPage() { | |
203 | return open; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Changes the header of this document. | |
208 | * <P> | |
209 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
210 | * derived from this abstract class if they actually support the use of | |
211 | * headers. | |
212 | * | |
213 | * @param header the new header | |
214 | */ | |
215 | ||
216 | public void setHeader(HeaderFooter header) { | |
217 | } | |
218 | ||
219 | /** | |
220 | * Resets the header of this document. | |
221 | * <P> | |
222 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
223 | * derived from this abstract class if they actually support the use of | |
224 | * headers. | |
225 | */ | |
226 | ||
227 | public void resetHeader() { | |
228 | } | |
229 | ||
230 | /** | |
231 | * Changes the footer of this document. | |
232 | * <P> | |
233 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
234 | * derived from this abstract class if they actually support the use of | |
235 | * footers. | |
236 | * | |
237 | * @param footer the new footer | |
238 | */ | |
239 | ||
240 | public void setFooter(HeaderFooter footer) { | |
241 | } | |
242 | ||
243 | /** | |
244 | * Resets the footer of this document. | |
245 | * <P> | |
246 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
247 | * derived from this abstract class if they actually support the use of | |
248 | * footers. | |
249 | */ | |
250 | ||
251 | public void resetFooter() { | |
252 | } | |
253 | ||
254 | /** | |
255 | * Sets the page number to 0. | |
256 | * <P> | |
257 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
258 | * derived from this abstract class if they actually support the use of | |
259 | * pagenumbers. | |
260 | */ | |
261 | ||
262 | public void resetPageCount() { | |
263 | } | |
264 | ||
265 | /** | |
266 | * Sets the page number. | |
267 | * <P> | |
268 | * This method should be overridden in the specific <CODE>DocWriter<CODE> classes | |
269 | * derived from this abstract class if they actually support the use of | |
270 | * pagenumbers. | |
271 | * | |
272 | * @param pageN the new page number | |
273 | */ | |
274 | ||
275 | public void setPageCount(int pageN) { | |
276 | } | |
277 | ||
278 | /** | |
279 | * Signals that the <CODE>Document</CODE> was closed and that no other | |
280 | * <CODE>Elements</CODE> will be added. | |
281 | */ | |
282 | ||
283 | public void close() { | |
284 | open = false; | |
285 | try { | |
286 |
1
1. close : removed call to com/lowagie/text/pdf/OutputStreamCounter::flush → NO_COVERAGE |
os.flush(); |
287 |
1
1. close : negated conditional → NO_COVERAGE |
if (closeStream) |
288 |
1
1. close : removed call to com/lowagie/text/pdf/OutputStreamCounter::close → NO_COVERAGE |
os.close(); |
289 | } | |
290 | catch(IOException ioe) { | |
291 | throw new ExceptionConverter(ioe); | |
292 | } | |
293 | } | |
294 | ||
295 | // methods | |
296 | ||
297 | /** Converts a <CODE>String</CODE> into a <CODE>Byte</CODE> array | |
298 | * according to the ISO-8859-1 codepage. | |
299 | * @param text the text to be converted | |
300 | * @return the conversion result | |
301 | */ | |
302 | ||
303 | public static final byte[] getISOBytes(String text) | |
304 | { | |
305 |
1
1. getISOBytes : negated conditional → NO_COVERAGE |
if (text == null) |
306 |
1
1. getISOBytes : mutated return of Object value for com/lowagie/text/DocWriter::getISOBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
307 | int len = text.length(); | |
308 | byte[] b = new byte[len]; | |
309 |
3
1. getISOBytes : changed conditional boundary → NO_COVERAGE 2. getISOBytes : Changed increment from 1 to -1 → NO_COVERAGE 3. getISOBytes : negated conditional → NO_COVERAGE |
for (int k = 0; k < len; ++k) |
310 | b[k] = (byte)text.charAt(k); | |
311 |
1
1. getISOBytes : mutated return of Object value for com/lowagie/text/DocWriter::getISOBytes to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return b; |
312 | } | |
313 | ||
314 | /** | |
315 | * Let the writer know that all writing has to be paused. | |
316 | */ | |
317 | ||
318 | public void pause() { | |
319 | pause = true; | |
320 | } | |
321 | | |
322 | /** | |
323 | * Checks if writing is paused. | |
324 | * | |
325 | * @return <CODE>true</CODE> if writing temporarily has to be paused, <CODE>false</CODE> otherwise. | |
326 | */ | |
327 | | |
328 | public boolean isPaused() { | |
329 | return pause; | |
330 | } | |
331 | ||
332 | /** | |
333 | * Let the writer know that writing may be resumed. | |
334 | */ | |
335 | ||
336 | public void resume() { | |
337 | pause = false; | |
338 | } | |
339 | ||
340 | /** | |
341 | * Flushes the <CODE>BufferedOutputStream</CODE>. | |
342 | */ | |
343 | ||
344 | public void flush() { | |
345 | try { | |
346 |
1
1. flush : removed call to com/lowagie/text/pdf/OutputStreamCounter::flush → NO_COVERAGE |
os.flush(); |
347 | } | |
348 | catch(IOException ioe) { | |
349 | throw new ExceptionConverter(ioe); | |
350 | } | |
351 | } | |
352 | ||
353 | /** | |
354 | * Writes a <CODE>String</CODE> to the <CODE>OutputStream</CODE>. | |
355 | * | |
356 | * @param string the <CODE>String</CODE> to write | |
357 | * @throws IOException | |
358 | */ | |
359 | ||
360 | protected void write(String string) throws IOException { | |
361 |
1
1. write : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(getISOBytes(string)); |
362 | } | |
363 | ||
364 | /** | |
365 | * Writes a number of tabs. | |
366 | * | |
367 | * @param indent the number of tabs to add | |
368 | * @throws IOException | |
369 | */ | |
370 | ||
371 | protected void addTabs(int indent) throws IOException { | |
372 |
1
1. addTabs : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(NEWLINE); |
373 |
3
1. addTabs : changed conditional boundary → NO_COVERAGE 2. addTabs : Changed increment from 1 to -1 → NO_COVERAGE 3. addTabs : negated conditional → NO_COVERAGE |
for (int i = 0; i < indent; i++) { |
374 |
1
1. addTabs : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(TAB); |
375 | } | |
376 | } | |
377 | ||
378 | /** | |
379 | * Writes a key-value pair to the outputstream. | |
380 | * | |
381 | * @param key the name of an attribute | |
382 | * @param value the value of an attribute | |
383 | * @throws IOException | |
384 | */ | |
385 | ||
386 | protected void write(String key, String value) | |
387 | throws IOException { | |
388 |
1
1. write : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(SPACE); |
389 |
1
1. write : removed call to com/lowagie/text/DocWriter::write → NO_COVERAGE |
write(key); |
390 |
1
1. write : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(EQUALS); |
391 |
1
1. write : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(QUOTE); |
392 |
1
1. write : removed call to com/lowagie/text/DocWriter::write → NO_COVERAGE |
write(value); |
393 |
1
1. write : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(QUOTE); |
394 | } | |
395 | ||
396 | /** | |
397 | * Writes a starttag to the outputstream. | |
398 | * | |
399 | * @param tag the name of the tag | |
400 | * @throws IOException | |
401 | */ | |
402 | ||
403 | protected void writeStart(String tag) | |
404 | throws IOException { | |
405 |
1
1. writeStart : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(LT); |
406 |
1
1. writeStart : removed call to com/lowagie/text/DocWriter::write → NO_COVERAGE |
write(tag); |
407 | } | |
408 | ||
409 | /** | |
410 | * Writes an endtag to the outputstream. | |
411 | * | |
412 | * @param tag the name of the tag | |
413 | * @throws IOException | |
414 | */ | |
415 | ||
416 | protected void writeEnd(String tag) | |
417 | throws IOException { | |
418 |
1
1. writeEnd : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(LT); |
419 |
1
1. writeEnd : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(FORWARD); |
420 |
1
1. writeEnd : removed call to com/lowagie/text/DocWriter::write → NO_COVERAGE |
write(tag); |
421 |
1
1. writeEnd : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(GT); |
422 | } | |
423 | ||
424 | /** | |
425 | * Writes an endtag to the outputstream. | |
426 | * @throws IOException | |
427 | */ | |
428 | ||
429 | protected void writeEnd() | |
430 | throws IOException { | |
431 |
1
1. writeEnd : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(SPACE); |
432 |
1
1. writeEnd : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(FORWARD); |
433 |
1
1. writeEnd : removed call to com/lowagie/text/pdf/OutputStreamCounter::write → NO_COVERAGE |
os.write(GT); |
434 | } | |
435 | ||
436 | /** | |
437 | * Writes the markup attributes of the specified <CODE>MarkupAttributes</CODE> | |
438 | * object to the <CODE>OutputStream</CODE>. | |
439 | * @param markup a <CODE>Properties</CODE> collection to write. | |
440 | * @return true, if writing the markup attributes succeeded | |
441 | * @throws IOException | |
442 | */ | |
443 | protected boolean writeMarkupAttributes(Properties markup) | |
444 | throws IOException { | |
445 |
2
1. writeMarkupAttributes : negated conditional → NO_COVERAGE 2. writeMarkupAttributes : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
if (markup == null) return false; |
446 | Iterator attributeIterator = markup.keySet().iterator(); | |
447 | String name; | |
448 |
1
1. writeMarkupAttributes : negated conditional → NO_COVERAGE |
while (attributeIterator.hasNext()) { |
449 | name = String.valueOf(attributeIterator.next()); | |
450 |
1
1. writeMarkupAttributes : removed call to com/lowagie/text/DocWriter::write → NO_COVERAGE |
write(name, markup.getProperty(name)); |
451 | } | |
452 |
1
1. writeMarkupAttributes : removed call to java/util/Properties::clear → NO_COVERAGE |
markup.clear(); |
453 |
1
1. writeMarkupAttributes : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return true; |
454 | } | |
455 | ||
456 | /** Checks if the stream is to be closed on document close | |
457 | * @return true if the stream is closed on document close | |
458 | * | |
459 | */ | |
460 | public boolean isCloseStream() { | |
461 | return closeStream; | |
462 | } | |
463 | | |
464 | /** Sets the close state of the stream after document close | |
465 | * @param closeStream true if the stream is closed on document close | |
466 | * | |
467 | */ | |
468 | public void setCloseStream(boolean closeStream) { | |
469 | this.closeStream = closeStream; | |
470 | } | |
471 | | |
472 | /** | |
473 | * @see com.lowagie.text.DocListener#setMarginMirroring(boolean) | |
474 | */ | |
475 | public boolean setMarginMirroring(boolean MarginMirroring) { | |
476 | return false; | |
477 | } | |
478 | | |
479 | /** | |
480 | * @see com.lowagie.text.DocListener#setMarginMirroring(boolean) | |
481 | * @since 2.1.6 | |
482 | */ | |
483 | public boolean setMarginMirroringTopBottom(boolean MarginMirroring) { | |
484 | return false; | |
485 | } | |
486 | | |
487 | } | |
Mutations | ||
175 |
1.1 |
|
286 |
1.1 |
|
287 |
1.1 |
|
288 |
1.1 |
|
305 |
1.1 |
|
306 |
1.1 |
|
309 |
1.1 2.2 3.3 |
|
311 |
1.1 |
|
346 |
1.1 |
|
361 |
1.1 |
|
372 |
1.1 |
|
373 |
1.1 2.2 3.3 |
|
374 |
1.1 |
|
388 |
1.1 |
|
389 |
1.1 |
|
390 |
1.1 |
|
391 |
1.1 |
|
392 |
1.1 |
|
393 |
1.1 |
|
405 |
1.1 |
|
406 |
1.1 |
|
418 |
1.1 |
|
419 |
1.1 |
|
420 |
1.1 |
|
421 |
1.1 |
|
431 |
1.1 |
|
432 |
1.1 |
|
433 |
1.1 |
|
445 |
1.1 2.2 |
|
448 |
1.1 |
|
450 |
1.1 |
|
452 |
1.1 |
|
453 |
1.1 |