📄 htmlproducer.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner;
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* -----------------
* HtmlProducer.java
* -----------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: HtmlProducer.java,v 1.33.2.2 2003/08/24 14:18:14 taqua Exp $
*
* Changes
* -------
* 18-Jan-2003 : Initial version
*/
package com.jrefinery.report.targets.table.html;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import com.jrefinery.report.function.FunctionProcessingException;
import com.jrefinery.report.targets.table.TableCellBackground;
import com.jrefinery.report.targets.table.TableCellDataFactory;
import com.jrefinery.report.targets.table.TableGridLayout;
import com.jrefinery.report.targets.table.TableGridPosition;
import com.jrefinery.report.targets.table.TableProducer;
import com.jrefinery.report.util.CharacterEntityParser;
import org.jfree.io.IOUtils;
/**
* The TableProducer is responsible for creating the produced Table. After
* the writer has finished the band layout process, the layouted bands are
* forwarded into the TableProducer. The TableProducer coordinates the cell
* creation process and collects the generated TableCellData. The raw CellData
* objects are later transformed into a TableGridLayout.
* <p>
* The generated HTML code is cached and written after the last cell was created,
* to insert the StyleSheet into the html header.
*
* @author Thomas Morgner
*/
public class HtmlProducer extends TableProducer
{
/** the printwriter for the main html file. */
private PrintWriter pout;
/** the cell data factory used for creating the content cells. */
private HtmlCellDataFactory cellDataFactory;
/** the character entity parser converts Strings into the HTML format. */
private static CharacterEntityParser entityParser;
/** the style collection is used to create the style sheet and the cell styles. */
private HtmlStyleCollection styleCollection;
/** the Filesystem is used to store the main html file and any external content. */
private HtmlFilesystem filesystem;
public static final String GENERATE_XHTML = "GenerateXHTML";
/** the fileencoding for the main html file. */
public static final String ENCODING = "Encoding";
/** a default value for the fileencoding of the main html file. */
public static final String ENCODING_DEFAULT = "UTF-8";
/** the content cache for the main html file. */
private ByteArrayOutputStream content;
/** a flag indicating whether this producer is open. */
private boolean isOpen;
/** the standard XHTML document type declaration and header. */
private static final String[] XHTML_HEADER = {
"<!DOCTYPE html",
" PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"",
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
"<html xmlns=\"http://www.w3.org/1999/xhtml\">",
"<head>"};
/** the standard HTML4 document type declaration and header. */
private static final String[] HTML4_HEADER = {
"<!DOCTYPE HTML ",
" PUBLIC \"-//W3C//DTD HTML 4.01//EN\"",
" \"http://www.w3.org/TR/html4/strict.dtd\">",
"<html>",
"<head>"};
/**
* Creates a new HTMLProducer.
*
* @param filesystem the filesystem used to store the generated content.
* @param strict a flag whether to use the strict layout mode.
*/
public HtmlProducer(final HtmlFilesystem filesystem,
final boolean strict)
{
super(strict);
if (filesystem == null)
{
throw new NullPointerException();
}
this.filesystem = filesystem;
this.content = null;
this.pout = null;
this.styleCollection = new HtmlStyleCollection();
}
/**
* Gets the defined file encoding for the main html file.
*
* @return the encoding.
*/
public String getEncoding()
{
return String.valueOf(getProperty(ENCODING, ENCODING_DEFAULT));
}
/**
* Gets the character entity parser for HTML content. The CharacterEntity parser
* translates known characters into predefined entities.
*
* @return the character entity parser instance.
*/
public static CharacterEntityParser getEntityParser()
{
if (entityParser == null)
{
entityParser = CharacterEntityParser.createHTMLEntityParser();
}
return entityParser;
}
/**
* Starts the report writing and prepares the cached output stream.
*/
public void open()
{
this.content = new ByteArrayOutputStream();
final DeflaterOutputStream deflaterStream
= new DeflaterOutputStream(content, new Deflater(Deflater.BEST_COMPRESSION));
try
{
this.pout = new PrintWriter(new OutputStreamWriter
(deflaterStream, getEncoding()), false);
}
catch (IOException ioe)
{
throw new FunctionProcessingException("Failed to create the writer", ioe);
}
// the style sheet definition will be inserted right before the content is written ...
pout.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
pout.print(getEncoding());
if (isGenerateXHTML())
{
pout.println("\" />");
}
else
{
pout.println("\">");
}
pout.print("<title>");
pout.flush();
final String title = String.valueOf(getProperty(TITLE, "Untitled report"));
pout.print(getEntityParser().encodeEntities(title));
pout.println("</title></head>");
pout.println("<body>");
pout.flush();
isOpen = true;
}
/**
* Closes the target and writes all generated content into the root stream of the
* filesystem after generating the StyleSheet information.
*/
public void close()
{
try
{
pout.println("</body></html>");
//
// Creates the stylesheets and the StyleSheet reference.
//
final StringBuffer cssbuffer = new StringBuffer();
final Iterator styles = styleCollection.getDefinedStyles();
while (styles.hasNext())
{
final HtmlCellStyle style = (HtmlCellStyle) styles.next();
if (styleCollection.isRegistered(style))
{
final String name = styleCollection.lookupName(style);
cssbuffer.append(".");
cssbuffer.append(name);
cssbuffer.append(" { ");
cssbuffer.append(styleCollection.createStyleSheetDefinition(style));
cssbuffer.append(" } ");
cssbuffer.append(System.getProperty("line.separator", "\n"));
}
}
final HtmlReferenceData cssRef = filesystem.createCSSReference(cssbuffer.toString());
final PrintWriter writer = new PrintWriter(filesystem.getRootStream());
// write the standard headers
if (isGenerateXHTML())
{
writer.print("<?xml version=\"1.0\" encoding=\"");
writer.print(getEncoding());
writer.println("\"?>");
// now finish the style sheet definition
for (int i = 0; i < XHTML_HEADER.length; i++)
{
writer.println(XHTML_HEADER[i]);
}
}
else
{
// now finish the style sheet definition
for (int i = 0; i < HTML4_HEADER.length; i++)
{
writer.println(HTML4_HEADER[i]);
}
}
// write the generated stylesheet ...
// is a href type ...
if (cssRef.isExternal())
{
writer.print("<link rel=\"stylesheet\" type=\"text/css\" ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -