📄 htmlwriter.java
字号:
/*
* $Id: HtmlWriter.java,v 1.35 2001/12/13 09:43:21 blowagie Exp $
* $Name: $
*
* Copyright 1999, 2000, 2001 by Bruno Lowagie.
*
* This library is free software; you can redistribute it and/or modify it
* 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.
*
* You should have received a copy of the GNU Library General Public License along
* with this library; if not, write to the Free Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*
* 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/
*
* ir-arch Bruno Lowagie,
* Adolf Baeyensstraat 121
* 9040 Sint-Amandsberg
* BELGIUM
* tel. +32 (0)9 228.10.97
* bruno@lowagie.com
*
*/
package com.lowagie.text.html;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import com.lowagie.text.*;
/**
* 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 implements DocListener {
// static membervariables (tags)
/** This is a possible HTML-tag. */
public static final byte[] BEGINCOMMENT = getISOBytes("\t<!-- ");
/** This is a possible HTML-tag. */
public static final byte[] ENDCOMMENT = getISOBytes(" -->\n");
/** This is a possible HTML-tag. */
public static final String NBSP = " ";
// membervariables
/** This is the current font of the HTML. */
protected Font font = new Font();
/** 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 static 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;
// constructor
/**
* Constructs a <CODE>HtmlWriter</CODE>.
*
* @param document 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);
os.write(NEWLINE);
}
catch(IOException 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.
* @throws DocumentException when a document isn't open yet, or has been closed
*/
public boolean newPage() throws DocumentException {
return false;
}
/**
* Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
*
* @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;
}
try {
if (element.type() == Element.CHUNK && !standardFont.isStandardFont()) {
Chunk chunk = new Chunk(((Chunk) element).content(), standardFont.difference(((Chunk) element).font()));
write(chunk, 2);
return true;
}
switch(element.type()) {
case Element.HEADER:
try {
Header h = (Header) element;
if (!HtmlTags.STYLESHEET.equals(h.name())) {
writeHeader(h);
}
else {
writeLink(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);
os.write(NEWLINE);
addTabs(3);
write(((Meta)element).content());
os.write(NEWLINE);
addTabs(2);
writeEnd(HtmlTags.TITLE);
return true;
case Element.CREATOR:
writeComment("Creator: " + ((Meta)element).content());
return true;
case Element.PRODUCER:
writeComment("Producer: " + ((Meta)element).content());
return true;
case Element.CREATIONDATE:
writeComment("Creationdate: " + ((Meta)element).content());
return true;
default:
write(element, 2);
return true;
}
}
catch(IOException ioe) {
throw new DocumentException(ioe.getMessage());
}
}
/**
* 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("Producer: iTextXML by lowagie.com");
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.backgroundColor() != null) {
write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(pageSize.backgroundColor()));
}
os.write(GT);
os.write(NEWLINE);
initHeader(); // line added by David Freels
}
catch(IOException 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);
writeEnd(HtmlTags.HTML);
super.close();
}
catch(IOException ioe) {
}
}
// some protected methods
/**
* Adds the header to the top of the </CODE>Document</CODE>
*
* @author David Freels
*/
protected void initHeader() {
if (header != null) {
try {
add(header.paragraph());
}
catch(Exception e) {
}
}
}
/**
* Adds the header to the top of the </CODE>Document</CODE>
*
* @author David Freels
*/
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(HtmlWriter.pageN + 1);
add(footer.paragraph());
}
catch(Exception e) {
}
}
}
/**
* Writes a Metatag in the header.
*
* @param element 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).name());
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, meta.content());
writeEnd();
}
/**
* Writes a link in the header.
*
* @param element the element that has to be written
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -