com.lowagie.text.html.HtmlWriter Maven / Gradle / Ivy
Show all versions of rtf-gen Show documentation
/*
* $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:
* https://github.com/osobolev/rtf-gen
*/
package com.lowagie.text.html;
import com.lowagie.text.List;
import com.lowagie.text.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
/**
* A DocWriter
class for HTML.
*
* An HtmlWriter
can be added as a DocListener
* to a certain Document
by getting an instance.
* Every Element
added to the original Document
* will be written to the OutputStream
of this HtmlWriter
.
*
* Example:
*
* // 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
* HtmlWriter.getInstance(document, System.out);
* // this will write HTML to a file called text.html
* HtmlWriter.getInstance(document, new FileOutputStream("text.html"));
* // this will write HTML to for instance the OutputStream of a HttpServletResponse-object
* HtmlWriter.getInstance(document, response.getOutputStream());
* }
* catch(DocumentException de) {
* System.err.println(de.getMessage());
* }
* // this will close the document and all the OutputStreams listening to it
* document.close();
*
*/
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 String NBSP = " ";
// membervariables
/**
* This is the current font of the HTML.
*/
protected final ArrayDeque currentfont = new ArrayDeque<>();
/**
* 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;
/**
* 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 HtmlWriter
.
*
* @param doc The Document
that has to be written as HTML
* @param os The OutputStream
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 HtmlWriter
.
*
* @param document The Document
that has to be written
* @param os The OutputStream
the writer has to write to.
* @return a new HtmlWriter
*/
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 true
if this action succeeded, false
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 Element
was added to the Document
.
*
* @param element a high level object that has to be translated to HTML
* @return true
if the element was added, false
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) {
// ignore
}
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 Document
has been opened and that
* Elements
can be added.
*
* The HEAD
-section of the HTML-document is written.
*/
public void open() {
super.open();
try {
writeComment(Document.getVersion());
writeComment("CreationDate: " + new Date());
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 Document
was closed and that no other
* Elements
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 Document
*/
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 Document
*/
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, 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.isEmpty()) {
/* JavaScript reference example:
*
*
*/
writeMarkupAttributes(markup);
os.write(GT);
writeEnd(HtmlTags.SCRIPT);
} else {
/* JavaScript coding convention:
*
*
*/
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.
*
* 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) { if (currentfont.isEmpty()) { return !standardfont.same(font); } else { Font cFont = currentfont.peek(); return !cFont.same(font); } } /** * Sets the basepath for images. *
* 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 String
was added to the Document
.
*
* @param string a String to add to the HTML
* @return true
if the string was added, false
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;
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;
MapFont
.
*
* @param font a Font
* @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) {
for (String key : styleAttributes.stringPropertyNames()) {
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();
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(prop + ": " + value + "; ");
}
}