1 50 51 package com.lowagie.text; 52 53 import java.io.BufferedOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.util.Iterator ; 57 import java.util.Properties ; 58 59 import com.lowagie.text.pdf.OutputStreamCounter; 60 61 76 77 public abstract class DocWriter implements DocListener { 78 79 80 public static final byte NEWLINE = (byte)'\n'; 81 82 83 public static final byte TAB = (byte)'\t'; 84 85 86 public static final byte LT = (byte)'<'; 87 88 89 public static final byte SPACE = (byte)' '; 90 91 92 public static final byte EQUALS = (byte)'='; 93 94 95 public static final byte QUOTE = (byte)'\"'; 96 97 98 public static final byte GT = (byte)'>'; 99 100 101 public static final byte FORWARD = (byte)'/'; 102 103 105 106 protected Rectangle pageSize; 107 108 109 protected Document document; 110 111 112 protected OutputStreamCounter os; 113 114 115 protected boolean open = false; 116 117 118 protected boolean pause = false; 119 120 121 protected boolean closeStream = true; 122 123 125 protected DocWriter() { 126 } 127 128 134 135 protected DocWriter(Document document, OutputStream os) { 136 this.document = document; 137 this.os = new OutputStreamCounter(new BufferedOutputStream (os)); 138 } 139 140 142 152 153 public boolean add(Element element) throws DocumentException { 154 return false; 155 } 156 157 160 161 public void open() { 162 open = true; 163 } 164 165 171 172 public boolean setPageSize(Rectangle pageSize) { 173 this.pageSize = pageSize; 174 return true; 175 } 176 177 188 189 public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) { 190 return false; 191 } 192 193 201 202 public boolean newPage() { 203 if (!open) { 204 return false; 205 } 206 return true; 207 } 208 209 218 219 public void setHeader(HeaderFooter header) { 220 } 221 222 229 230 public void resetHeader() { 231 } 232 233 242 243 public void setFooter(HeaderFooter footer) { 244 } 245 246 253 254 public void resetFooter() { 255 } 256 257 264 265 public void resetPageCount() { 266 } 267 268 277 278 public void setPageCount(int pageN) { 279 } 280 281 285 286 public void close() { 287 open = false; 288 try { 289 os.flush(); 290 if (closeStream) 291 os.close(); 292 } 293 catch(IOException ioe) { 294 throw new ExceptionConverter(ioe); 295 } 296 } 297 298 300 305 306 public static final byte[] getISOBytes(String 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 320 321 public void pause() { 322 pause = true; 323 } 324 325 330 331 public boolean isPaused() { 332 return pause; 333 } 334 335 338 339 public void resume() { 340 pause = false; 341 } 342 343 346 347 public void flush() { 348 try { 349 os.flush(); 350 } 351 catch(IOException ioe) { 352 throw new ExceptionConverter(ioe); 353 } 354 } 355 356 362 363 protected void write(String string) throws IOException { 364 os.write(getISOBytes(string)); 365 } 366 367 373 374 protected void addTabs(int indent) throws IOException { 375 os.write(NEWLINE); 376 for (int i = 0; i < indent; i++) { 377 os.write(TAB); 378 } 379 } 380 381 388 389 protected void write(String key, String value) 390 throws IOException { 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 405 406 protected void writeStart(String tag) 407 throws IOException { 408 os.write(LT); 409 write(tag); 410 } 411 412 418 419 protected void writeEnd(String tag) 420 throws IOException { 421 os.write(LT); 422 os.write(FORWARD); 423 write(tag); 424 os.write(GT); 425 } 426 427 431 432 protected void writeEnd() 433 throws IOException { 434 os.write(SPACE); 435 os.write(FORWARD); 436 os.write(GT); 437 } 438 439 446 protected boolean writeMarkupAttributes(Properties markup) 447 throws IOException { 448 if (markup == null) return false; 449 Iterator attributeIterator = markup.keySet().iterator(); 450 String 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 463 public boolean isCloseStream() { 464 return closeStream; 465 } 466 467 471 public void setCloseStream(boolean closeStream) { 472 this.closeStream = closeStream; 473 } 474 475 478 public boolean setMarginMirroring(boolean MarginMirroring) { 479 return false; 480 } 481 482 } 483 | Popular Tags |