1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
|
/*
* $Id: HtmlWriter.java 3583 2008-08-12 00:00:09Z xlv $
*
* Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text.html;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.EmptyStackException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Stack;
import com.lowagie.text.Anchor;
import com.lowagie.text.Annotation;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocWriter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Font;
import com.lowagie.text.Header;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.MarkedObject;
import com.lowagie.text.MarkedSection;
import com.lowagie.text.Meta;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Row;
import com.lowagie.text.Section;
import com.lowagie.text.SimpleTable;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
/**
* A <CODE>DocWriter</CODE> class for HTML.
* <P>
* An <CODE>HtmlWriter</CODE> can be added as a <CODE>DocListener</CODE>
* to a certain <CODE>Document</CODE> by getting an instance.
* Every <CODE>Element</CODE> added to the original <CODE>Document</CODE>
* will be written to the <CODE>OutputStream</CODE> of this <CODE>HtmlWriter</CODE>.
* <P>
* Example:
* <BLOCKQUOTE><PRE>
* // creation of the document with a certain size and certain margins
* Document document = new Document(PageSize.A4, 50, 50, 50, 50);
* try {
* // this will write HTML to the Standard OutputStream
* <STRONG>HtmlWriter.getInstance(document, System.out);</STRONG>
* // this will write HTML to a file called text.html
* <STRONG>HtmlWriter.getInstance(document, new FileOutputStream("text.html"));</STRONG>
* // this will write HTML to for instance the OutputStream of a HttpServletResponse-object
* <STRONG>HtmlWriter.getInstance(document, response.getOutputStream());</STRONG>
* }
* catch(DocumentException de) {
* System.err.println(de.getMessage());
* }
* // this will close the document and all the OutputStreams listening to it
* <STRONG>document.close();</CODE>
* </PRE></BLOCKQUOTE>
*/
public class HtmlWriter extends DocWriter {
// static membervariables (tags)
/** This is a possible HTML-tag. */
public static final byte[] BEGINCOMMENT = getISOBytes("<!-- ");
/** This is a possible HTML-tag. */
public static final byte[] ENDCOMMENT = getISOBytes(" -->");
/** This is a possible HTML-tag. */
public static final String NBSP = " ";
// membervariables
/** This is the current font of the HTML. */
protected Stack currentfont = new Stack();
/** This is the standard font of the HTML. */
protected Font standardfont = new Font();
/** This is a path for images. */
protected String imagepath = null;
/** Stores the page number. */
protected int pageN = 0;
/** This is the textual part of a header */
protected HeaderFooter header = null;
/** This is the textual part of the footer */
protected HeaderFooter footer = null;
/** Store the markup properties of a MarkedObject. */
protected Properties markup = new Properties();
// constructor
/**
* Constructs a <CODE>HtmlWriter</CODE>.
*
* @param doc The <CODE>Document</CODE> that has to be written as HTML
* @param os The <CODE>OutputStream</CODE> the writer has to write to.
*/
protected HtmlWriter(Document doc, OutputStream os) {
super(doc, os);
document.addDocListener(this);
this.pageN = document.getPageNumber();
try {
os.write(LT);
os.write(getISOBytes(HtmlTags.HTML));
os.write(GT);
os.write(NEWLINE);
os.write(TAB);
os.write(LT);
os.write(getISOBytes(HtmlTags.HEAD));
os.write(GT);
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
// get an instance of the HtmlWriter
/**
* Gets an instance of the <CODE>HtmlWriter</CODE>.
*
* @param document The <CODE>Document</CODE> that has to be written
* @param os The <CODE>OutputStream</CODE> the writer has to write to.
* @return a new <CODE>HtmlWriter</CODE>
*/
public static HtmlWriter getInstance(Document document, OutputStream os) {
return new HtmlWriter(document, os);
}
// implementation of the DocListener methods
/**
* Signals that an new page has to be started.
*
* @return <CODE>true</CODE> if this action succeeded, <CODE>false</CODE> if not.
*/
public boolean newPage() {
try {
writeStart(HtmlTags.DIV);
write(" ");
write(HtmlTags.STYLE);
write("=\"");
writeCssProperty(Markup.CSS_KEY_PAGE_BREAK_BEFORE, Markup.CSS_VALUE_ALWAYS);
write("\" /");
os.write(GT);
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
return true;
}
/**
* Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
*
* @param element a high level object that has to be translated to HTML
* @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
public boolean add(Element element) throws DocumentException {
if (pause) {
return false;
}
if (open && !element.isContent()) {
throw new DocumentException(
"The document is open; you can only add Elements with content.");
}
try {
switch(element.type()) {
case Element.HEADER:
try {
Header h = (Header) element;
if (HtmlTags.STYLESHEET.equals(h.getName())) {
writeLink(h);
}
else if (HtmlTags.JAVASCRIPT.equals(h.getName())) {
writeJavaScript(h);
}
else {
writeHeader(h);
}
}
catch(ClassCastException cce) {
}
return true;
case Element.SUBJECT:
case Element.KEYWORDS:
case Element.AUTHOR:
Meta meta = (Meta) element;
writeHeader(meta);
return true;
case Element.TITLE:
addTabs(2);
writeStart(HtmlTags.TITLE);
os.write(GT);
addTabs(3);
write(HtmlEncoder.encode(((Meta)element).getContent()));
addTabs(2);
writeEnd(HtmlTags.TITLE);
return true;
case Element.CREATOR:
writeComment("Creator: " + HtmlEncoder.encode(((Meta)element).getContent()));
return true;
case Element.PRODUCER:
writeComment("Producer: " + HtmlEncoder.encode(((Meta)element).getContent()));
return true;
case Element.CREATIONDATE:
writeComment("Creationdate: " + HtmlEncoder.encode(((Meta)element).getContent()));
return true;
case Element.MARKED:
if (element instanceof MarkedSection) {
MarkedSection ms = (MarkedSection)element;
addTabs(1);
writeStart(HtmlTags.DIV);
writeMarkupAttributes(ms.getMarkupAttributes());
os.write(GT);
MarkedObject mo = ((MarkedSection)element).getTitle();
if (mo != null) {
markup = mo.getMarkupAttributes();
mo.process(this);
}
ms.process(this);
writeEnd(HtmlTags.DIV);
return true;
}
else {
MarkedObject mo = (MarkedObject) element;
markup = mo.getMarkupAttributes();
return mo.process(this);
}
default:
write(element, 2);
return true;
}
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
/**
* Signals that the <CODE>Document</CODE> has been opened and that
* <CODE>Elements</CODE> can be added.
* <P>
* The <CODE>HEAD</CODE>-section of the HTML-document is written.
*/
public void open() {
super.open();
try {
writeComment(Document.getVersion());
writeComment("CreationDate: " + new Date().toString());
addTabs(1);
writeEnd(HtmlTags.HEAD);
addTabs(1);
writeStart(HtmlTags.BODY);
if (document.leftMargin() > 0) {
write(HtmlTags.LEFTMARGIN, String.valueOf(document.leftMargin()));
}
if (document.rightMargin() > 0) {
write(HtmlTags.RIGHTMARGIN, String.valueOf(document.rightMargin()));
}
if (document.topMargin() > 0) {
write(HtmlTags.TOPMARGIN, String.valueOf(document.topMargin()));
}
if (document.bottomMargin() > 0) {
write(HtmlTags.BOTTOMMARGIN, String.valueOf(document.bottomMargin()));
}
if (pageSize.getBackgroundColor() != null) {
write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(pageSize.getBackgroundColor()));
}
if (document.getJavaScript_onLoad() != null) {
write(HtmlTags.JAVASCRIPT_ONLOAD, HtmlEncoder.encode(document.getJavaScript_onLoad()));
}
if (document.getJavaScript_onUnLoad() != null) {
write(HtmlTags.JAVASCRIPT_ONUNLOAD, HtmlEncoder.encode(document.getJavaScript_onUnLoad()));
}
if (document.getHtmlStyleClass() != null) {
write(Markup.HTML_ATTR_CSS_CLASS, document.getHtmlStyleClass());
}
os.write(GT);
initHeader(); // line added by David Freels
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
/**
* Signals that the <CODE>Document</CODE> was closed and that no other
* <CODE>Elements</CODE> will be added.
*/
public void close() {
try {
initFooter(); // line added by David Freels
addTabs(1);
writeEnd(HtmlTags.BODY);
os.write(NEWLINE);
writeEnd(HtmlTags.HTML);
super.close();
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
// some protected methods
/**
* Adds the header to the top of the </CODE>Document</CODE>
*/
protected void initHeader() {
if (header != null) {
try {
add(header.paragraph());
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
}
/**
* Adds the header to the top of the </CODE>Document</CODE>
*/
protected void initFooter() {
if (footer != null) {
try {
// Set the page number. HTML has no notion of a page, so it should always
// add up to 1
footer.setPageNumber(pageN + 1);
add(footer.paragraph());
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}
}
/**
* Writes a Metatag in the header.
*
* @param meta the element that has to be written
* @throws IOException
*/
protected void writeHeader(Meta meta) throws IOException {
addTabs(2);
writeStart(HtmlTags.META);
switch(meta.type()) {
case Element.HEADER:
write(HtmlTags.NAME, ((Header) meta).getName());
break;
case Element.SUBJECT:
write(HtmlTags.NAME, HtmlTags.SUBJECT);
break;
case Element.KEYWORDS:
write(HtmlTags.NAME, HtmlTags.KEYWORDS);
break;
case Element.AUTHOR:
write(HtmlTags.NAME, HtmlTags.AUTHOR);
break;
}
write(HtmlTags.CONTENT, HtmlEncoder.encode(meta.getContent()));
writeEnd();
}
/**
* Writes a link in the header.
*
* @param header the element that has to be written
* @throws IOException
*/
protected void writeLink(Header header) throws IOException {
addTabs(2);
writeStart(HtmlTags.LINK);
write(HtmlTags.REL, header.getName());
write(HtmlTags.TYPE, HtmlTags.TEXT_CSS);
write(HtmlTags.REFERENCE, header.getContent());
writeEnd();
}
/**
* Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header.
*
* @param header the element that has to be written
* @throws IOException
*/
protected void writeJavaScript(Header header) throws IOException {
addTabs(2);
writeStart(HtmlTags.SCRIPT);
write(HtmlTags.LANGUAGE, HtmlTags.JAVASCRIPT);
if (markup.size() > 0) {
/* JavaScript reference example:
*
* <script language="JavaScript" src="/myPath/MyFunctions.js"/>
*/
writeMarkupAttributes(markup);
os.write(GT);
writeEnd(HtmlTags.SCRIPT);
}
else {
/* JavaScript coding convention:
*
* <script language="JavaScript" type="text/javascript">
* <!--
* // ... JavaScript methods ...
* //-->
* </script>
*/
write(HtmlTags.TYPE, Markup.HTML_VALUE_JAVASCRIPT);
os.write(GT);
addTabs(2);
write(new String(BEGINCOMMENT) + "\n");
write(header.getContent());
addTabs(2);
write("//" + new String(ENDCOMMENT));
addTabs(2);
writeEnd(HtmlTags.SCRIPT);
}
}
/**
* Writes some comment.
* <P>
* This method writes some comment.
*
* @param comment the comment that has to be written
* @throws IOException
*/
protected void writeComment(String comment) throws IOException {
addTabs(2);
os.write(BEGINCOMMENT);
write(comment);
os.write(ENDCOMMENT);
}
// public methods
/**
* Changes the standardfont.
*
* @param standardfont The font
*/
public void setStandardFont(Font standardfont) {
this.standardfont = standardfont;
}
/**
* Checks if a given font is the same as the font that was last used.
*
* @param font the font of an object
* @return true if the font differs
*/
public boolean isOtherFont(Font font) {
try {
Font cFont = (Font) currentfont.peek();
if (cFont.compareTo(font) == 0) return false;
return true;
}
catch(EmptyStackException ese) {
if (standardfont.compareTo(font) == 0) return false;
return true;
}
}
/**
* Sets the basepath for images.
* <P>
* This is especially useful if you add images using a file,
* rather than an URL. In PDF there is no problem, since
* the images are added inline, but in HTML it is sometimes
* necessary to use a relative path or a special path to some
* images directory.
*
* @param imagepath the new imagepath
*/
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
/**
* Resets the imagepath.
*/
public void resetImagepath() {
imagepath = null;
}
/**
* Changes the header of this document.
*
* @param header the new header
*/
public void setHeader(HeaderFooter header) {
this.header = header;
}
/**
* Changes the footer of this document.
*
* @param footer the new footer
*/
public void setFooter(HeaderFooter footer) {
this.footer = footer;
}
/**
* Signals that a <CODE>String</CODE> was added to the <CODE>Document</CODE>.
*
* @param string a String to add to the HTML
* @return <CODE>true</CODE> if the string was added, <CODE>false</CODE> if not.
*/
public boolean add(String string) {
if (pause) {
return false;
}
try
{
write(string);
return true;
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
/**
* Writes the HTML representation of an element.
*
* @param element the element
* @param indent the indentation
* @throws IOException
*/
protected void write(Element element, int indent) throws IOException {
Properties styleAttributes = null;
switch(element.type()) {
case Element.MARKED: {
try {
add(element);
} catch (DocumentException e) {
e.printStackTrace();
}
return;
}
case Element.CHUNK:
{
Chunk chunk = (Chunk) element;
// if the chunk contains an image, return the image representation
Image image = chunk.getImage();
if (image != null) {
write(image, indent);
return;
}
if (chunk.isEmpty()) return;
HashMap attributes = chunk.getAttributes();
if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) {
return;
}
boolean tag = isOtherFont(chunk.getFont()) || markup.size() > 0;
if (tag) {
// start span tag
addTabs(indent);
writeStart(HtmlTags.SPAN);
if (isOtherFont(chunk.getFont())) {
write(chunk.getFont(), null);
}
writeMarkupAttributes(markup);
os.write(GT);
}
if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) {
// start sup or sub tag
if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) {
writeStart(HtmlTags.SUP);
}
else {
writeStart(HtmlTags.SUB);
}
os.write(GT);
}
// contents
write(HtmlEncoder.encode(chunk.getContent()));
if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) {
// end sup or sub tag
os.write(LT);
os.write(FORWARD);
if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) {
write(HtmlTags.SUP);
}
else {
write(HtmlTags.SUB);
}
os.write(GT);
}
if (tag) {
// end tag
writeEnd(Markup.HTML_TAG_SPAN);
}
return;
}
case Element.PHRASE:
{
Phrase phrase = (Phrase) element;
styleAttributes = new Properties();
if (phrase.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, phrase.getLeading() + "pt");
// start tag
addTabs(indent);
writeStart(Markup.HTML_TAG_SPAN);
writeMarkupAttributes(markup);
write(phrase.getFont(), styleAttributes);
os.write(GT);
currentfont.push(phrase.getFont());
// contents
for (Iterator i = phrase.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(Markup.HTML_TAG_SPAN);
currentfont.pop();
return;
}
case Element.ANCHOR:
{
Anchor anchor = (Anchor) element;
styleAttributes = new Properties();
if (anchor.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, anchor.getLeading() + "pt");
// start tag
addTabs(indent);
writeStart(HtmlTags.ANCHOR);
if (anchor.getName() != null) {
write(HtmlTags.NAME, anchor.getName());
}
if (anchor.getReference() != null) {
write(HtmlTags.REFERENCE, anchor.getReference());
}
writeMarkupAttributes(markup);
write(anchor.getFont(), styleAttributes);
os.write(GT);
currentfont.push(anchor.getFont());
// contents
for (Iterator i = anchor.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.ANCHOR);
currentfont.pop();
return;
}
case Element.PARAGRAPH:
{
Paragraph paragraph = (Paragraph) element;
styleAttributes = new Properties();
if (paragraph.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, paragraph.getTotalLeading() + "pt");
// start tag
addTabs(indent);
writeStart(HtmlTags.DIV);
writeMarkupAttributes(markup);
String alignment = HtmlEncoder.getAlignment(paragraph.getAlignment());
if (!"".equals(alignment)) {
write(HtmlTags.ALIGN, alignment);
}
write(paragraph.getFont(), styleAttributes);
os.write(GT);
currentfont.push(paragraph.getFont());
// contents
for (Iterator i = paragraph.iterator(); i.hasNext(); ) {
write((Element)i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.DIV);
currentfont.pop();
return;
}
case Element.SECTION:
case Element.CHAPTER:
{
// part of the start tag + contents
writeSection((Section) element, indent);
return;
}
case Element.LIST:
{
List list = (List) element;
// start tag
addTabs(indent);
if (list.isNumbered()) {
writeStart(HtmlTags.ORDEREDLIST);
}
else {
writeStart(HtmlTags.UNORDEREDLIST);
}
writeMarkupAttributes(markup);
os.write(GT);
// contents
for (Iterator i = list.getItems().iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
// end tag
addTabs(indent);
if (list.isNumbered()) {
writeEnd(HtmlTags.ORDEREDLIST);
}
else {
writeEnd(HtmlTags.UNORDEREDLIST);
}
return;
}
case Element.LISTITEM:
{
ListItem listItem = (ListItem) element;
styleAttributes = new Properties();
if (listItem.hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, listItem.getTotalLeading() + "pt");
// start tag
addTabs(indent);
writeStart(HtmlTags.LISTITEM);
writeMarkupAttributes(markup);
write(listItem.getFont(), styleAttributes);
os.write(GT);
currentfont.push(listItem.getFont());
// contents
for (Iterator i = listItem.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.LISTITEM);
currentfont.pop();
return;
}
case Element.CELL:
{
Cell cell = (Cell) element;
// start tag
addTabs(indent);
if (cell.isHeader()) {
writeStart(HtmlTags.HEADERCELL);
}
else {
writeStart(HtmlTags.CELL);
}
writeMarkupAttributes(markup);
if (cell.getBorderWidth() != Rectangle.UNDEFINED) {
write(HtmlTags.BORDERWIDTH, String.valueOf(cell.getBorderWidth()));
}
if (cell.getBorderColor() != null) {
write(HtmlTags.BORDERCOLOR, HtmlEncoder.encode(cell.getBorderColor()));
}
if (cell.getBackgroundColor() != null) {
write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(cell.getBackgroundColor()));
}
String alignment = HtmlEncoder.getAlignment(cell.getHorizontalAlignment());
if (!"".equals(alignment)) {
write(HtmlTags.HORIZONTALALIGN, alignment);
}
alignment = HtmlEncoder.getAlignment(cell.getVerticalAlignment());
if (!"".equals(alignment)) {
write(HtmlTags.VERTICALALIGN, alignment);
}
if (cell.getWidthAsString() != null) {
write(HtmlTags.WIDTH, cell.getWidthAsString());
}
if (cell.getColspan() != 1) {
write(HtmlTags.COLSPAN, String.valueOf(cell.getColspan()));
}
if (cell.getRowspan() != 1) {
write(HtmlTags.ROWSPAN, String.valueOf(cell.getRowspan()));
}
if (cell.getMaxLines() == 1) {
write(HtmlTags.STYLE, "white-space: nowrap;");
}
os.write(GT);
// contents
if (cell.isEmpty()) {
write(NBSP);
} else {
for (Iterator i = cell.getElements(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
}
// end tag
addTabs(indent);
if (cell.isHeader()) {
writeEnd(HtmlTags.HEADERCELL);
}
else {
writeEnd(HtmlTags.CELL);
}
return;
}
case Element.ROW:
{
Row row = (Row) element;
// start tag
addTabs(indent);
writeStart(HtmlTags.ROW);
writeMarkupAttributes(markup);
os.write(GT);
// contents
Element cell;
for (int i = 0; i < row.getColumns(); i++) {
if ((cell = (Element)row.getCell(i)) != null) {
write(cell, indent + 1);
}
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.ROW);
return;
}
case Element.TABLE:
{
Table table;
try {
table = (Table) element;
}
catch(ClassCastException cce) {
try {
table = ((SimpleTable)element).createTable();
} catch (BadElementException e) {
throw new ExceptionConverter(e);
}
}
table.complete();
// start tag
addTabs(indent);
writeStart(HtmlTags.TABLE);
writeMarkupAttributes(markup);
os.write(SPACE);
write(HtmlTags.WIDTH);
os.write(EQUALS);
os.write(QUOTE);
write(String.valueOf(table.getWidth()));
if (!table.isLocked()){
write("%");
}
os.write(QUOTE);
String alignment = HtmlEncoder.getAlignment(table.getAlignment());
if (!"".equals(alignment)) {
write(HtmlTags.ALIGN, alignment);
}
write(HtmlTags.CELLPADDING, String.valueOf(table.getPadding()));
write(HtmlTags.CELLSPACING, String.valueOf(table.getSpacing()));
if (table.getBorderWidth() != Rectangle.UNDEFINED) {
write(HtmlTags.BORDERWIDTH, String.valueOf(table.getBorderWidth()));
}
if (table.getBorderColor() != null) {
write(HtmlTags.BORDERCOLOR, HtmlEncoder.encode(table.getBorderColor()));
}
if (table.getBackgroundColor() != null) {
write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(table.getBackgroundColor()));
}
os.write(GT);
// contents
Row row;
for (Iterator iterator = table.iterator(); iterator.hasNext(); ) {
row = (Row) iterator.next();
write(row, indent + 1);
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.TABLE);
return;
}
case Element.ANNOTATION:
{
Annotation annotation = (Annotation) element;
writeComment(annotation.title() + ": " + annotation.content());
return;
}
case Element.IMGRAW:
case Element.JPEG:
case Element.JPEG2000:
case Element.IMGTEMPLATE:
{
Image image = (Image) element;
if (image.getUrl() == null) {
return;
}
// start tag
addTabs(indent);
writeStart(HtmlTags.IMAGE);
String path = image.getUrl().toString();
if (imagepath != null) {
if (path.indexOf('/') > 0) {
path = imagepath + path.substring(path.lastIndexOf('/') + 1);
}
else {
path = imagepath + path;
}
}
write(HtmlTags.URL, path);
if ((image.getAlignment() & Image.RIGHT) > 0) {
write(HtmlTags.ALIGN, HtmlTags.ALIGN_RIGHT);
}
else if ((image.getAlignment() & Image.MIDDLE) > 0) {
write(HtmlTags.ALIGN, HtmlTags.ALIGN_MIDDLE);
}
else {
write(HtmlTags.ALIGN, HtmlTags.ALIGN_LEFT);
}
if (image.getAlt() != null) {
write(HtmlTags.ALT, image.getAlt());
}
write(HtmlTags.PLAINWIDTH, String.valueOf(image.getScaledWidth()));
write(HtmlTags.PLAINHEIGHT, String.valueOf(image.getScaledHeight()));
writeMarkupAttributes(markup);
writeEnd();
return;
}
default:
return;
}
}
/**
* Writes the HTML representation of a section.
*
* @param section the section to write
* @param indent the indentation
* @throws IOException
*/
protected void writeSection(Section section, int indent) throws IOException {
if (section.getTitle() != null) {
int depth = section.getDepth() - 1;
if (depth > 5) {
depth = 5;
}
Properties styleAttributes = new Properties();
if (section.getTitle().hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, section.getTitle().getTotalLeading() + "pt");
// start tag
addTabs(indent);
writeStart(HtmlTags.H[depth]);
write(section.getTitle().getFont(), styleAttributes);
String alignment = HtmlEncoder.getAlignment(section.getTitle().getAlignment());
if (!"".equals(alignment)) {
write(HtmlTags.ALIGN, alignment);
}
writeMarkupAttributes(markup);
os.write(GT);
currentfont.push(section.getTitle().getFont());
// contents
for (Iterator i = section.getTitle().iterator(); i.hasNext(); ) {
write((Element)i.next(), indent + 1);
}
// end tag
addTabs(indent);
writeEnd(HtmlTags.H[depth]);
currentfont.pop();
}
for (Iterator i = section.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent);
}
}
/**
* Writes the representation of a <CODE>Font</CODE>.
*
* @param font a <CODE>Font</CODE>
* @param styleAttributes the style of the font
* @throws IOException
*/
protected void write(Font font, Properties styleAttributes) throws IOException {
if (font == null || !isOtherFont(font) /* || styleAttributes == null*/) return;
write(" ");
write(HtmlTags.STYLE);
write("=\"");
if (styleAttributes != null) {
String key;
for (Enumeration e = styleAttributes.propertyNames(); e.hasMoreElements(); ) {
key = (String)e.nextElement();
writeCssProperty(key, styleAttributes.getProperty(key));
}
}
if (isOtherFont(font)) {
writeCssProperty(Markup.CSS_KEY_FONTFAMILY, font.getFamilyname());
if (font.getSize() != Font.UNDEFINED) {
writeCssProperty(Markup.CSS_KEY_FONTSIZE, font.getSize() + "pt");
}
if (font.getColor() != null) {
writeCssProperty(Markup.CSS_KEY_COLOR, HtmlEncoder.encode(font.getColor()));
}
int fontstyle = font.getStyle();
BaseFont bf = font.getBaseFont();
if (bf != null) {
String ps = bf.getPostscriptFontName().toLowerCase();
if (ps.indexOf("bold") >= 0) {
if (fontstyle == Font.UNDEFINED)
fontstyle = 0;
fontstyle |= Font.BOLD;
}
if (ps.indexOf("italic") >= 0 || ps.indexOf("oblique") >= 0) {
if (fontstyle == Font.UNDEFINED)
fontstyle = 0;
fontstyle |= Font.ITALIC;
}
}
if (fontstyle != Font.UNDEFINED && fontstyle != Font.NORMAL) {
switch (fontstyle & Font.BOLDITALIC) {
case Font.BOLD:
writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
break;
case Font.ITALIC:
writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
break;
case Font.BOLDITALIC:
writeCssProperty(Markup.CSS_KEY_FONTWEIGHT, Markup.CSS_VALUE_BOLD);
writeCssProperty(Markup.CSS_KEY_FONTSTYLE, Markup.CSS_VALUE_ITALIC);
break;
}
// CSS only supports one decoration tag so if both are specified
// only one of the two will display
if ((fontstyle & Font.UNDERLINE) > 0) {
writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_UNDERLINE);
}
if ((fontstyle & Font.STRIKETHRU) > 0) {
writeCssProperty(Markup.CSS_KEY_TEXTDECORATION, Markup.CSS_VALUE_LINETHROUGH);
}
}
}
write("\"");
}
/**
* Writes out a CSS property.
* @param prop a CSS property
* @param value the value of the CSS property
* @throws IOException
*/
protected void writeCssProperty(String prop, String value) throws IOException {
write(new StringBuffer(prop).append(": ").append(value).append("; ").toString());
}
}
|