📄 ziphtmlfilesystem.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.
*
* ----------------------
* ZIPHtmlFilesystem.java
* ----------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: ZIPHtmlFilesystem.java,v 1.6 2003/11/07 18:33:56 taqua Exp $
*
* Changes
* -------
* 26-Jan-2003 : Initial version
*/
package org.jfree.report.modules.output.table.html;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.keypoint.PngEncoder;
import org.jfree.io.IOUtils;
import org.jfree.report.ImageReference;
import org.jfree.report.util.ImageComparator;
import org.jfree.report.util.NoCloseOutputStream;
import org.jfree.report.util.StringUtil;
import org.jfree.report.util.WaitingImageObserver;
/**
* Similiar to the DirectoryHtmlFilesystem, the generated Html-File and the supplementary
* data files (images and external Stylesheet definition) into a directory in a ZIP-File.
* The data files can be written into a separated data directory within the ZIP-File.
* <p>
* External referenced content can either be copied into the data directory or could
* be included as linked content. This behaviour is controled by the <code>copyExternalImages</code>
* flag.
*
* @author Thomas Morgner
*/
public class ZIPHtmlFilesystem implements HtmlFilesystem
{
/** The name of the data directory within the ZIP-file. */
private String dataDirectory;
/** the target zip output stream. */
private ZipOutputStream zipOut;
/** the buffer for caching the root stream, until all external data is generated. */
private ByteArrayOutputStream rootBase;
/** the root stream for writing the main html file. */
private OutputStream rootStream;
/** A collection of all used file names for generating external content. */
private HashMap usedNames;
/** A collection of all referenced external content. */
private HashMap usedURLs;
/** A collection of all previously encoded images. */
private HashMap encodedImages;
/** the image comparator used to compare generated images. */
private ImageComparator comparator;
/** A flag indicating whether to copy external references into the data directory. */
private boolean copyExternalImages;
/** A flag defining whether to use the digest image compare method. */
private boolean digestImageCompare;
/**
* Creates a new ZIPHtmlFilesystem. The given output stream is used to write
* a generated Zip-File. The given data directory must denote a relative directory
* within the ZIP file.
*
* @param out the target output stream.
* @param dataDirectory the data directory, relative within the ZIP file.
*
* @throws IOException if an IO error occurs.
*/
public ZIPHtmlFilesystem(final OutputStream out, String dataDirectory)
throws IOException
{
if (out == null)
{
throw new NullPointerException();
}
if (dataDirectory == null)
{
throw new NullPointerException();
}
this.zipOut = new ZipOutputStream(new NoCloseOutputStream(out));
//this.zipOut.setComment("Created with JFReeReport.");
this.rootBase = new ByteArrayOutputStream();
this.rootStream = new DeflaterOutputStream(rootBase, new Deflater(Deflater.BEST_COMPRESSION));
// dataDirectory creation ...
final File dataDir = new File(dataDirectory);
final File baseDir = new File("");
if (dataDir.isAbsolute() || IOUtils.getInstance().isSubDirectory(baseDir, dataDir) == false)
{
throw new IllegalArgumentException("The data directory is no relative directory in the "
+ "zip file");
}
dataDirectory = IOUtils.getInstance().createRelativeURL(dataDir.toURL(), baseDir.toURL());
if (dataDirectory.length() != 0)
{
if (dataDirectory.endsWith("/") == false)
{
this.dataDirectory = dataDirectory + "/";
}
else
{
this.dataDirectory = dataDirectory;
}
}
this.usedNames = new HashMap();
this.usedURLs = new HashMap();
this.comparator = new ImageComparator();
this.encodedImages = new HashMap();
}
/**
* The root stream is used to write the main HTML-File. Any external content is
* referenced from this file.
*
* @return the output stream of the main HTML file.
* @throws IOException if an IO error occured, while providing the root stream.
*/
public OutputStream getRootStream() throws IOException
{
return rootStream;
}
/**
* Returns true, if external content should be copied into the DataDirectory of
* the ZIPFile or false if the content should be included as linked content.
* <p>
* Linked content reduces the filesize, but the reader of the report will need access
* to the linked files. If you pan to use the report offline, then it is best to
* copy all referenced data into the zip file.
*
* @return true, if external referenced content should be copied into the ZIP file,
* false otherwise.
*/
public boolean isCopyExternalImages()
{
return copyExternalImages;
}
/**
* Defines, whether external content should be copied into the DataDirectory of
* the ZIPFile or should be included as linked content.
* <p>
* Linked content reduces the filesize, but the reader of the report will need access
* to the linked files. If you pan to use the report offline, then it is best to
* copy all referenced data into the zip file.
*
* @param copyExternalImages true, if external referenced content should be copied into the ZIP
* file, false otherwise.
*/
public void setCopyExternalImages(final boolean copyExternalImages)
{
this.copyExternalImages = copyExternalImages;
}
/**
* Tests, whether the given URL points to a supported file format for common
* browsers. Returns true if the URL references a JPEG, PNG or GIF image, false
* otherwise.
* <p>
* The checked filetypes are the ones recommended by the W3C.
*
* @param url the url that should be tested.
* @return true, if the content type is supported by the browsers, false otherwise.
*/
protected boolean isSupportedImageFormat(final URL url)
{
final String file = url.getFile();
if (StringUtil.endsWithIgnoreCase(file, ".jpg"))
{
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -