1 50 51 package com.lowagie.text.xml; 52 53 import java.io.OutputStream; 54 import java.io.IOException; 55 import java.util.Date; 56 import java.util.Iterator; 57 import java.util.TreeMap; 58 import java.util.HashMap; 59 60 import com.lowagie.text.*; 61 import com.lowagie.text.markup.MarkupTags; 62 63 90 91 public class XmlWriter extends DocWriter implements DocListener { 92 93 95 96 public static final byte[] PROLOG = getISOBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); 97 98 99 public static final byte[] DOCTYPE = getISOBytes("<!DOCTYPE ITEXT SYSTEM \""); 100 101 102 public final static byte[] DTD = getISOBytes("http://itext.sourceforge.net/itext.dtd"); 103 104 105 private static final String[] xmlCode = new String[256]; 106 107 static { 108 for (int i = 0; i < 10; i++) { 109 xmlCode[i] = "�" + i + ";"; 110 } 111 112 for (int i = 10; i < 32; i++) { 113 xmlCode[i] = "�" + i + ";"; 114 } 115 116 for (int i = 32; i < 128; i++) { 117 xmlCode[i] = String.valueOf((char)i); 118 } 119 120 xmlCode['\n'] = "<" + ElementTags.NEWLINE + " />\n"; 122 xmlCode['\"'] = """; xmlCode['\''] = "'"; xmlCode['&'] = "&"; xmlCode['<'] = "<"; xmlCode['>'] = ">"; 128 for (int i = 128; i < 256; i++) { 129 xmlCode[i] = "&#" + i + ";"; 130 } 131 } 132 134 135 private TreeMap itext = new TreeMap(new com.lowagie.text.StringCompare()); 136 137 139 145 146 protected XmlWriter(Document doc, OutputStream os) { 147 super(doc, os); 148 149 document.addDocListener(this); 150 try { 151 os.write(PROLOG); 152 os.write(DOCTYPE); 153 os.write(DTD); 154 os.write(QUOTE); 155 os.write(GT); 156 os.write(NEWLINE); 157 } 158 catch(IOException ioe) { 159 throw new ExceptionConverter(ioe); 160 } 161 } 162 163 170 171 protected XmlWriter(Document doc, OutputStream os, String dtd) { 172 super(doc, os); 173 174 document.addDocListener(this); 175 try { 176 os.write(PROLOG); 177 os.write(DOCTYPE); 178 os.write(getISOBytes(dtd)); 179 os.write(QUOTE); 180 os.write(GT); 181 os.write(NEWLINE); 182 } 183 catch(IOException ioe) { 184 throw new ExceptionConverter(ioe); 185 } 186 } 187 188 190 197 198 public static XmlWriter getInstance(Document document, OutputStream os) { 199 return new XmlWriter(document, os); 200 } 201 202 210 211 public static XmlWriter getInstance(Document document, OutputStream os, String dtd) { 212 return new XmlWriter(document, os, dtd); 213 } 214 215 217 224 225 public boolean add(Element element) throws DocumentException { 226 if (pause) { 227 return false; 228 } 229 try { 230 switch(element.type()) { 231 case Element.TITLE: 232 itext.put(ElementTags.TITLE, ((Meta)element).content()); 233 return true; 234 case Element.SUBJECT: 235 itext.put(ElementTags.SUBJECT, ((Meta)element).content()); 236 return true; 237 case Element.KEYWORDS: 238 itext.put(ElementTags.KEYWORDS, ((Meta)element).content()); 239 return true; 240 case Element.AUTHOR: 241 itext.put(ElementTags.AUTHOR, ((Meta)element).content()); 242 return true; 243 default: 244 write(element, 1); 245 return true; 246 } 247 } 248 catch(IOException ioe) { 249 return false; 250 } 251 } 252 253 257 258 public void open() { 259 super.open(); 260 try { 261 itext.put(ElementTags.PRODUCER, "iTextXML by lowagie.com"); 262 itext.put(ElementTags.CREATIONDATE, new Date().toString()); 263 writeStart(ElementTags.ITEXT); 264 String key; 265 for (java.util.Iterator i = itext.keySet().iterator(); i.hasNext(); ) { 266 key = (String) i.next(); 267 write(key, (String) itext.get(key)); 268 } 269 os.write(GT); 270 } 271 catch(IOException ioe) { 272 throw new ExceptionConverter(ioe); 273 } 274 } 275 276 282 283 public boolean newPage() throws DocumentException { 284 if (pause || !open) { 285 return false; 286 } 287 try { 288 writeStart(ElementTags.NEWPAGE); 289 writeEnd(); 290 return true; 291 } 292 catch(IOException ioe) { 293 return false; 294 } 295 } 296 297 301 302 public void close() { 303 try { 304 os.write(NEWLINE); 305 writeEnd(ElementTags.ITEXT); 306 super.close(); 307 } 308 catch(IOException ioe) { 309 throw new ExceptionConverter(ioe); 310 } 311 } 312 313 315 322 323 private void write(Element element, int indent) throws IOException { 324 switch(element.type()) { 325 case Element.CHUNK: 326 { 327 Chunk chunk = (Chunk) element; 328 329 try { 331 Image image = chunk.getImage(); 332 write(image, indent); 333 return; 334 } 335 catch(NullPointerException npe) { 336 } 338 339 addTabs(indent); 340 HashMap attributes = chunk.getAttributes(); 341 if (chunk.font().isStandardFont() && attributes == null && !(hasMarkupAttributes(chunk))) { 342 write(encode(chunk.content(), indent)); 343 return; 344 } 345 else { 346 if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) { 347 writeStart(ElementTags.NEWPAGE); 348 writeEnd(); 349 return; 350 } 351 writeStart(ElementTags.CHUNK); 352 if (! chunk.font().isStandardFont()) { 353 write(chunk.font()); 354 } 355 if (attributes != null) { 356 for (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) { 357 String key = (String) i.next(); 358 if (key.equals(Chunk.LOCALGOTO) 359 || key.equals(Chunk.LOCALDESTINATION) 360 || key.equals(Chunk.GENERICTAG)) { 361 String value = (String) attributes.get(key); 362 write(key.toLowerCase(), value); 363 } 364 if (key.equals(Chunk.SUBSUPSCRIPT)) { 365 write(key.toLowerCase(), String.valueOf((Float) attributes.get(key))); 366 } 367 } 368 } 369 if (hasMarkupAttributes(chunk)) { 370 writeMarkupAttributes((MarkupAttributes)chunk); 371 } 372 os.write(GT); 373 write(encode(chunk.content(), indent)); 374 writeEnd(ElementTags.CHUNK); 375 } 376 return; 377 } 378 case Element.PHRASE: 379 { 380 Phrase phrase = (Phrase) element; 381 382 addTabs(indent); 383 writeStart(ElementTags.PHRASE); 384 385 write(ElementTags.LEADING, String.valueOf(phrase.leading())); 386 write(phrase.font()); 387 if (hasMarkupAttributes(phrase)) { 388 writeMarkupAttributes((MarkupAttributes)phrase); 389 } 390 os.write(GT); 391 392 for (Iterator i = phrase.iterator(); i.hasNext(); ) { 393 write((Element) i.next(), indent + 1); 394 } 395 396 addTabs(indent); 397 writeEnd(ElementTags.PHRASE); 398 return; 399 } 400 case Element.ANCHOR: 401 { 402 Anchor anchor = (Anchor) element; 403 404 addTabs(indent); 405 writeStart(ElementTags.ANCHOR); 406 407 write(ElementTags.LEADING, String.valueOf(anchor.leading())); 408 write(anchor.font()); 409 if (anchor.name() != null) { 410 write(ElementTags.NAME, anchor.name()); 411 } 412 if (anchor.reference() != null) { 413 write(ElementTags.REFERENCE, anchor.reference()); 414 } 415 if (hasMarkupAttributes(anchor)) { 416 writeMarkupAttributes((MarkupAttributes)anchor); 417 } 418 os.write(GT); 419 for (Iterator i = anchor.iterator(); i.hasNext(); ) { 420 write((Element) i.next(), indent + 1); 421 } 422 addTabs(indent); 423 writeEnd(ElementTags.ANCHOR); 424 return; 425 } 426 case Element.PARAGRAPH: 427 { 428 Paragraph paragraph = (Paragraph) element; 429 430 addTabs(indent); 431 writeStart(ElementTags.PARAGRAPH); 432 433 write(ElementTags.LEADING, String.valueOf(paragraph.leading())); 434 write(paragraph.font()); 435 write(ElementTags.ALIGN, ElementTags.getAlignment(paragraph.alignment())); 436 if (paragraph.indentationLeft() != 0) { 437 write(ElementTags.INDENTATIONLEFT, String.valueOf(paragraph.indentationLeft())); 438 } 439 if (paragraph.indentationRight() != 0) { 440 write(ElementTags.INDENTATIONRIGHT, String.valueOf(paragraph.indentationRight())); 441 } 442 if (hasMarkupAttributes(paragraph)) { 443 writeMarkupAttributes((MarkupAttributes)paragraph); 444 } 445 os.write(GT); 446 for (Iterator i = paragraph.iterator(); i.hasNext(); ) { 447 write((Element) i.next(), indent + 1); 448 } 449 addTabs(indent); 450 writeEnd(ElementTags.PARAGRAPH); 451 return; 452 } 453 case Element.SECTION: 454 { 455 Section section = (Section) element; 456 457 addTabs(indent); 458 writeStart(ElementTags.SECTION); 459 writeSection(section, indent); 460 writeEnd(ElementTags.SECTION); 461 return; 462 } 463 case Element.CHAPTER: 464 { 465 Chapter chapter = (Chapter) element; 466 467 addTabs(indent); 468 writeStart(ElementTags.CHAPTER); 469 if (hasMarkupAttributes(chapter)) { 470 writeMarkupAttributes((MarkupAttributes)chapter); 471 } 472 writeSection(chapter, indent); 473 writeEnd(ElementTags.CHAPTER); 474 return; 475 476 } 477 case Element.LIST: 478 { 479 List list = (List) element; 480 481 addTabs(indent); 482 writeStart(ElementTags.LIST); 483 write(ElementTags.NUMBERED, String.valueOf(list.isNumbered())); 484 write(ElementTags.SYMBOLINDENT, String.valueOf(list.symbolIndent())); 485 if (list.first() != 1) { 486 write(ElementTags.FIRST, String.valueOf(list.first())); 487 } 488 if (list.indentationLeft() != 0) { 489 write(ElementTags.INDENTATIONLEFT, String.valueOf(list.indentationLeft())); 490 } 491 if (list.indentationRight() != 0) { 492 write(ElementTags.INDENTATIONRIGHT, String.valueOf(list.indentationRight())); 493 } 494 if (!list.isNumbered()) { 495 write(ElementTags.LISTSYMBOL, list.symbol().content()); 496 } 497 write(list.symbol().font()); 498 if (hasMarkupAttributes(list)) { 499 writeMarkupAttributes((MarkupAttributes)list); 500 } 501 os.write(GT); 502 for (Iterator i = list.getItems().iterator(); i.hasNext(); ) { 503 write((Element) i.next(), indent + 1); 504 } 505 addTabs(indent); 506 writeEnd(ElementTags.LIST); 507 return; 508 } 509 case Element.LISTITEM: 510 { 511 ListItem listItem = (ListItem) element; 512 513 addTabs(indent); 514 writeStart(ElementTags.LISTITEM); 515 write(ElementTags.LEADING, String.valueOf(listItem.leading())); 516 write(listItem.font()); 517 write(ElementTags.ALIGN, ElementTags.getAlignment(listItem.alignment())); 518 if (listItem.indentationLeft() != 0) { 519 write(ElementTags.INDENTATIONLEFT, String.valueOf(listItem.indentationLeft())); 520 } 521 if (listItem.indentationRight() != 0) { 522 write(ElementTags.INDENTATIONRIGHT, String.valueOf(listItem.indentationRight())); 523 } 524 if (hasMarkupAttributes(listItem)) { 525 writeMarkupAttributes((MarkupAttributes)listItem); 526 } 527 os.write(GT); 528 for (Iterator i = listItem.iterator(); i.hasNext(); ) { 529 write((Element) i.next(), indent + 1); 530 } 531 addTabs(indent); 532 writeEnd(ElementTags.LISTITEM); 533 return; 534 } 535 case Element.CELL: 536 { 537 Cell cell = (Cell) element; 538 539 addTabs(indent); 540 writeStart(ElementTags.CELL); 541 write((Rectangle) cell); 542 write(ElementTags.HORIZONTALALIGN, ElementTags.getAlignment(cell.horizontalAlignment())); 543 write(ElementTags.VERTICALALIGN, ElementTags.getAlignment(cell.verticalAlignment())); 544 if (cell.cellWidth() != null) { 545 write(ElementTags.WIDTH, cell.cellWidth()); 546 } 547 if (cell.colspan() != 1) { 548 write(ElementTags.COLSPAN, String.valueOf(cell.colspan())); 549 } 550 if (cell.rowspan() != 1) { 551 write(ElementTags.ROWSPAN, String.valueOf(cell.rowspan())); 552 } 553 if (cell.header()) { 554 write(ElementTags.HEADER, String.valueOf(true)); 555 } 556 if (cell.noWrap()) { 557 write(ElementTags.NOWRAP, String.valueOf(true)); 558 } 559 if (cell.leading() != -1) { 560 write(ElementTags.LEADING, String.valueOf(cell.leading())); 561 } 562 if (hasMarkupAttributes(cell)) { 563 writeMarkupAttributes((MarkupAttributes)cell); 564 } 565 os.write(GT); 566 for (Iterator i = cell.getElements(); i.hasNext(); ) { 567 write((Element) i.next(), indent + 1); 568 } 569 addTabs(indent); 570 writeEnd(ElementTags.CELL); 571 return; 572 } 573 case Element.ROW: 574 { 575 Row row = (Row) element; 576 577 addTabs(indent); 578 writeStart(ElementTags.ROW); 579 if (hasMarkupAttributes(row)){ 580 writeMarkupAttributes((MarkupAttributes)row); 581 } 582 os.write(GT); 583 Element cell; 584 for (int i = 0; i < row.columns(); i++) { 585 if ((cell = (Element)row.getCell(i)) != null) { 586 write(cell, indent + 1); 587 } 588 } 589 addTabs(indent); 590 writeEnd(ElementTags.ROW); 591 return; 592 } 593 case Element.TABLE: 594 { 595 Table table = (Table) element; 596 table.complete(); 597 addTabs(indent); 598 writeStart(ElementTags.TABLE); 599 write(ElementTags.COLUMNS, String.valueOf(table.columns())); 600 os.write(SPACE); 601 write(ElementTags.WIDTH); 602 os.write(EQUALS); 603 os.write(QUOTE); 604 if (! "".equals(table.absWidth())){ 605 write(table.absWidth()); 606 } 607 else{ 608 write(String.valueOf(table.widthPercentage())); 609 write("%"); 610 } 611 os.write(QUOTE); 612 write(ElementTags.ALIGN, ElementTags.getAlignment(table.alignment())); 613 write(ElementTags.CELLPADDING, String.valueOf(table.cellpadding())); 614 write(ElementTags.CELLSPACING, String.valueOf(table.cellspacing())); 615 os.write(SPACE); 616 write(ElementTags.WIDTHS); 617 os.write(EQUALS); 618 os.write(QUOTE); 619 float[] widths = table.getProportionalWidths(); 620 write(String.valueOf(widths[0])); 621 for (int i = 1; i < widths.length; i++) { 622 write(";"); 623 write(String.valueOf(widths[i])); 624 } 625 os.write(QUOTE); 626 write((Rectangle) table); 627 if (hasMarkupAttributes(table)) { 628 writeMarkupAttributes((MarkupAttributes)table); 629 } 630 os.write(GT); 631 Row row; 632 for (Iterator iterator = table.iterator(); iterator.hasNext(); ) { 633 row = (Row) iterator.next(); 634 write(row, indent + 1); 635 } 636 addTabs(indent); 637 writeEnd(ElementTags.TABLE); 638 return; 639 } 640 case Element.ANNOTATION: 641 { 642 Annotation annotation = (Annotation) element; 643 644 addTabs(indent); 645 writeStart(ElementTags.ANNOTATION); 646 if (annotation.title() != null) { 647 write(ElementTags.TITLE, annotation.title()); 648 } 649 if (annotation.content() != null) { 650 write(ElementTags.CONTENT, annotation.content()); 651 } 652 if (hasMarkupAttributes(annotation)) { 653 writeMarkupAttributes((MarkupAttributes)annotation); 654 } 655 writeEnd(); 656 return; 657 } 658 case Element.IMGRAW: 659 case Element.JPEG: 660 case Element.IMGTEMPLATE: 661 { 662 Image image = (Image) element; 663 if (image.url() == null) { 664 return; 665 } 666 667 addTabs(indent); 668 writeStart(ElementTags.IMAGE); 669 write(ElementTags.URL, image.url().toString()); 670 if ((image.alignment() & Image.LEFT) > 0) { 671 write(ElementTags.ALIGN, ElementTags.ALIGN_LEFT); 672 } 673 else if ((image.alignment() & Image.RIGHT) > 0) { 674 write(ElementTags.ALIGN, ElementTags.ALIGN_RIGHT); 675 } 676 else if ((image.alignment() & Image.MIDDLE) > 0) { 677 write(ElementTags.ALIGN, ElementTags.ALIGN_MIDDLE); 678 } 679 if ((image.alignment() & Image.UNDERLYING) > 0) { 680 write(ElementTags.UNDERLYING, String.valueOf(true)); 681 } 682 if ((image.alignment() & Image.TEXTWRAP) > 0) { 683 write(ElementTags.TEXTWRAP, String.valueOf(true)); 684 } 685 if (image.alt() != null) { 686 write(ElementTags.ALT, image.alt()); 687 } 688 if (image.hasAbsolutePosition()) { 689 write(ElementTags.ABSOLUTEX, String.valueOf(image.absoluteX())); 690 write(ElementTags.ABSOLUTEY, String.valueOf(image.absoluteY())); 691 } 692 write(ElementTags.PLAINWIDTH, String.valueOf(image.plainWidth())); 693 write(ElementTags.PLAINHEIGHT, String.valueOf(image.plainHeight())); 694 if (hasMarkupAttributes(image)) { 695 writeMarkupAttributes((MarkupAttributes)image); 696 } 697 writeEnd(); 698 return; 699 } 700 default: 701 return; 702 } 703 } 704 705 712 713 private void writeSection(Section section, int indent) throws IOException { 714 write(ElementTags.NUMBERDEPTH, String.valueOf(section.numberDepth())); 715 write(ElementTags.DEPTH, String.valueOf(section.depth())); 716 write(ElementTags.INDENT, String.valueOf(section.indentation())); 717 if (section.indentationLeft() != 0) { 718 write(ElementTags.INDENTATIONLEFT, String.valueOf(section.indentationLeft())); 719 } 720 if (section.indentationRight() != 0) { 721 write(ElementTags.INDENTATIONRIGHT, String.valueOf(section.indentationRight())); 722 } 723 os.write(GT); 724 725 if (section.title() != null) { 726 addTabs(indent + 1); 727 writeStart(ElementTags.TITLE); 728 write(ElementTags.LEADING, String.valueOf(section.title().leading())); 729 write(ElementTags.ALIGN, ElementTags.getAlignment(section.title().alignment())); 730 if (section.title().indentationLeft() != 0) { 731 write(ElementTags.INDENTATIONLEFT, String.valueOf(section.title().indentationLeft())); 732 } 733 if (section.title().indentationRight() != 0) { 734 write(ElementTags.INDENTATIONRIGHT, String.valueOf(section.title().indentationRight())); 735 } 736 write(section.title().font()); 737 os.write(GT); 738 Iterator i = section.title().iterator(); 739 if (section.depth() > 0) { 740 i.next(); 741 } 742 while (i.hasNext()) { 743 write((Element) i.next(), indent + 2); 744 } 745 addTabs(indent + 1); 746 writeEnd(ElementTags.TITLE); 747 } 748 for (Iterator i = section.iterator(); i.hasNext(); ) { 749 write((Element) i.next(), indent + 1); 750 } 751 addTabs(indent); 752 } 753 754 760 761 private void write(Rectangle rectangle) throws IOException { 762 if (rectangle.borderWidth() != Rectangle.UNDEFINED) { 763 write(ElementTags.BORDERWIDTH, String.valueOf(rectangle.borderWidth())); 764 if (rectangle.hasBorder(Rectangle.LEFT)) { 765 write(ElementTags.LEFT, String.valueOf(true)); 766 } 767 if (rectangle.hasBorder(Rectangle.RIGHT)) { 768 write(ElementTags.RIGHT, String.valueOf(true)); 769 } 770 if (rectangle.hasBorder(Rectangle.TOP)) { 771 write(ElementTags.TOP, String.valueOf(true)); 772 } 773 if (rectangle.hasBorder(Rectangle.BOTTOM)) { 774 write(ElementTags.BOTTOM, String.valueOf(true)); 775 } 776 } 777 if (rectangle.borderColor() != null) { 778 write(ElementTags.RED, String.valueOf(rectangle.borderColor().getRed())); 779 write(ElementTags.GREEN, String.valueOf(rectangle.borderColor().getGreen())); 780 write(ElementTags.BLUE, String.valueOf(rectangle.borderColor().getBlue())); 781 } 782 if (rectangle.backgroundColor() != null) { 783 write(ElementTags.BGRED, String.valueOf(rectangle.backgroundColor().getRed())); 784 write(ElementTags.BGGREEN, String.valueOf(rectangle.backgroundColor().getGreen())); 785 write(ElementTags.BGBLUE, String.valueOf(rectangle.backgroundColor().getBlue())); 786 } 787 if (rectangle.grayFill() > 0) { 788 write(ElementTags.GRAYFILL, String.valueOf(rectangle.grayFill())); 789 } 790 } 791 792 799 800 static final String encode(String string, int indent) { 801 int n = string.length(); 802 int pos = 0; 803 char character; 804 StringBuffer buf = new StringBuffer(); 805 for (int i = 0; i < n; i++) { 807 character = string.charAt(i); 808 switch(character) { 810 case ' ': 811 if ((i - pos) > 60) { 812 pos = i; 813 buf.append("\n"); 814 addTabs(buf, indent); 815 break; 816 } 817 default: 818 buf.append(xmlCode[(int) character]); 819 } 820 } 821 return buf.toString(); 822 } 823 824 830 831 static final void addTabs(StringBuffer buf, int indent) { 832 for (int i = 0; i < indent; i++) { 833 buf.append("\t"); 834 } 835 } 836 837 843 844 private void write(Font font) throws IOException { 845 write(ElementTags.FONT, font.getFamilyname()); 846 if (font.size() != Font.UNDEFINED) { 847 write(ElementTags.SIZE, String.valueOf(font.size())); 848 } 849 if (font.style() != Font.UNDEFINED) { 850 os.write(SPACE); 851 write(ElementTags.STYLE); 852 os.write(EQUALS); 853 os.write(QUOTE); 854 switch(font.style() & Font.BOLDITALIC) { 855 case Font.NORMAL: 856 write(MarkupTags.CSS_NORMAL); 857 break; 858 case Font.BOLD: 859 write(MarkupTags.CSS_BOLD); 860 break; 861 case Font.ITALIC: 862 write(MarkupTags.CSS_ITALIC); 863 break; 864 case Font.BOLDITALIC: 865 write(MarkupTags.CSS_BOLD); 866 write(", "); 867 write(MarkupTags.CSS_ITALIC); 868 break; 869 } 870 if ((font.style() & Font.UNDERLINE) > 0) { 871 write(", "); 872 write(MarkupTags.CSS_UNDERLINE); 873 } 874 if ((font.style() & Font.STRIKETHRU) > 0) { 875 write(", "); 876 write(MarkupTags.CSS_LINETHROUGH); 877 } 878 os.write(QUOTE); 879 } 880 if (font.color() != null) { 881 write(ElementTags.RED, String.valueOf(font.color().getRed())); 882 write(ElementTags.GREEN, String.valueOf(font.color().getGreen())); 883 write(ElementTags.BLUE, String.valueOf(font.color().getBlue())); 884 } 885 } 886 } | Popular Tags |