Writes out BufferedImages to some outputstream, like a file. Allows image writer parameters to be specified and - * thus controlled. Uses the java ImageIO libraries--see {@link javax.imageio.ImageIO} and related classes, - * especially {@link javax.imageio.ImageWriter}.
- *- * FSImageWriter writer = new FSImageWriter(); - * writer.write(img, new File("image.png")); - *- *
You can set the image format in the constructore ({@link com.openhtmltopdf.util.FSImageWriter#FSImageWriter(String)}, - * and can set compression settings using various setters; this lets you create writer to reuse across a number - * of images, all output at the same compression level. Note that not all image formats support compression. For - * those that do, you may need to set more than one compression setting, in combination, for it to work. For JPG, - * it might look like this
- *- * writer = new FSImageWriter("jpg"); - * writer.setWriteCompressionMode(ImageWriteParam.MODE_EXPLICIT); - * writer.setWriteCompressionType("JPEG"); - * writer.setWriteCompressionQuality(.75f); - *- *
The method {@link #newJpegWriter(float)} creates a writer for JPG images; you just need to specify the - * output quality. Note that for the JPG format, your image or BufferedImage shouldn't be ARGB.
- */ -public class FSImageWriter { - private String imageFormat; - private float writeCompressionQuality; - private int writeCompressionMode; - private String writeCompressionType; - public static final String DEFAULT_IMAGE_FORMAT = "png"; - - - /** - * New image writer for the PNG image format - */ - public FSImageWriter() { - this("png"); - } - - /** - * New writer for a given image format, using the informal format name. - * - * @param imageFormat Informal image format name, e.g. "jpg", "png", "bmp"; usually the part that appears - * as the file extension. - */ - public FSImageWriter(String imageFormat) { - this.imageFormat = imageFormat; - this.writeCompressionMode = ImageWriteParam.MODE_COPY_FROM_METADATA; - this.writeCompressionType = null; - this.writeCompressionQuality = 1.0f; - } - - /** - * Convenience method for initializing a writer for the JPEG image format. - * - * @param quality level of compression, between 0 and 1; 0 is lowest, 1 is highest quality. - * @return a writer for JPEG images - */ - public static FSImageWriter newJpegWriter(float quality) { - FSImageWriter writer = new FSImageWriter("jpg"); - writer.setWriteCompressionMode(ImageWriteParam.MODE_EXPLICIT); - writer.setWriteCompressionType("JPEG"); - writer.setWriteCompressionQuality(quality); - return writer; - } - - /** - * Writes the image out to the target file, creating the file if necessary, or overwriting if it already - * exists. - * - * @param bimg Image to write. - * @param filePath Path for file to write. The extension for the file name is not changed; it is up to the - * caller to make sure this corresponds to the image format. - * @throws IOException If the file could not be written. - */ - public void write(BufferedImage bimg, String filePath) throws IOException { - File file = new File(filePath); - if (file.exists()) { - if (!file.delete()) { - throw new IOException("File " + filePath + " exists already, and call to .delete() failed " + - "unexpectedly"); - } - } else { - if (!file.createNewFile()) { - throw new IOException("Unable to create file at path " + filePath + ", call to .createNewFile() " + - "failed unexpectedly."); - } - } - - OutputStream fos = new BufferedOutputStream(new FileOutputStream(file)); - try { - write(bimg, fos); - } finally { - try { - fos.close(); - } catch (IOException e) { - // ignore - } - } - } - - /** - * Writes the image out to the target file, creating the file if necessary, or overwriting if it already - * exists. - * - * @param bimg Image to write. - * @param os outputstream to write to - * @throws IOException If the file could not be written. - */ - public void write(BufferedImage bimg, OutputStream os) throws IOException { - ImageWriter writer = null; - ImageOutputStream ios = null; - try { - writer = lookupImageWriterForFormat(imageFormat); - ios = ImageIO.createImageOutputStream(os); - writer.setOutput(ios); - ImageWriteParam iwparam = getImageWriteParameters(writer); - - writer.write(null, new IIOImage(bimg, null, null), iwparam); - } finally { - if (ios != null) { - try { - ios.flush(); - } catch (IOException e) { - // ignore - } - try { - ios.close(); - } catch (IOException e) { - // ignore - } - } - if (writer != null) { - writer.dispose(); - } - } - } - - /** - * Returns the image output parameters to control the output image quality, compression, etc. By default - * this uses the compression values set in this class. Override this method to get full control over the - * ImageWriteParam used in image output. - * - * @param writer The ImageWriter we are going to use for image output. - * @return ImageWriteParam configured for image output. - */ - protected ImageWriteParam getImageWriteParameters(ImageWriter writer) { - ImageWriteParam param = writer.getDefaultWriteParam(); - if (param.canWriteCompressed()) { - if (writeCompressionMode != ImageWriteParam.MODE_COPY_FROM_METADATA) { - param.setCompressionMode(writeCompressionMode); - - // see docs for IWP--only allowed to set type and quality if mode is EXPLICIT - if (writeCompressionMode == ImageWriteParam.MODE_EXPLICIT) { - param.setCompressionType(writeCompressionType); - param.setCompressionQuality(writeCompressionQuality); - } - - } - } - - return param; - } - - /** - * Compression quality for images to be generated from this writer. See - * {@link javax.imageio.ImageWriteParam#setCompressionQuality(float)} for a description of what this means - * and valid range of values. - * - * @param q Compression quality for image output. - */ - public void setWriteCompressionQuality(float q) { - writeCompressionQuality = q; - } - - /** - * Compression mode for images to be generated from this writer. See - * {@link javax.imageio.ImageWriteParam#setCompressionMode(int)} for a description of what this means - * and valid range of values. - * - * @param mode Compression mode for image output. - */ - public void setWriteCompressionMode(int mode) { - this.writeCompressionMode = mode; - } - - /** - * Compression type for images to be generated from this writer. See - * {@link javax.imageio.ImageWriteParam#setCompressionType(String)} for a description of what this means - * and valid range of values. - * - * @param type Type of compression for image output. - */ - public void setWriteCompressionType(String type) { - this.writeCompressionType = type; - } - - /** - * Utility method to find an imagewriter. - * - * @param imageFormat String informal format name, "jpg" - * @return ImageWriter corresponding to that format, null if not found. - */ - private ImageWriter lookupImageWriterForFormat(String imageFormat) { - ImageWriter writer = null; - Iterator iter = ImageIO.getImageWritersByFormatName(imageFormat); - if (iter.hasNext()) { - writer = (ImageWriter) iter.next(); - } - return writer; - } -} diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/IOUtil.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/IOUtil.java deleted file mode 100644 index 1839c0de7..000000000 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/IOUtil.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.openhtmltopdf.util; - -import java.io.*; -import java.net.URL; -import java.net.URLConnection; - -/** - * @author patrick - */ -public class IOUtil { - public static File copyFile(File page, File outputDir) throws IOException { - InputStream in = null; - OutputStream out = null; - try { - in = new BufferedInputStream(new FileInputStream(page)); - File outputFile; - outputFile = new File(outputDir, page.getName()); - out = new BufferedOutputStream(new FileOutputStream(outputFile)); - - // Transfer bytes from in to out - byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - in.close(); - out.flush(); - out.close(); - return outputFile; - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - // swallow - } - } - if (out != null) { - try { - out.close(); - } catch (IOException e) { - // swallow - } - } - } - } - - public static void deleteAllFiles(final File dir) throws IOException { - File[] files = dir.listFiles(); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - if (!file.delete()) { - throw new IOException("Cleanup directory " + dir + ", can't delete file " + file); - } - } - } - - /** - * Attempts to open a connection, and a stream, to the URI provided. timeouts will be set for opening the connection - * and reading from it. will return the stream, or null if unable to open or read or a timeout occurred. Does not - * buffer the stream. - */ - @Deprecated - public static InputStream openStreamAtUrl(String uri) { - InputStream is = null; - try { - final URLConnection uc = new URL(uri).openConnection(); - - // If using Java 5+ you can set timeouts for the URL connection--useful if the remote - // server is down etc.; the default timeout is pretty long - // - //uc.setConnectTimeout(10 * 1000); - //uc.setReadTimeout(30 * 1000); - // - // TODO:CLEAN-JDK1.4 - // Since we target 1.4, we use a couple of system properties--note these are only supported - // in the Sun JDK implementation--see the Net properties guide in the JDK - // e.g. file:///usr/java/j2sdk1.4.2_17/docs/guide/net/properties.html - System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(10 * 1000)); - System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(30 * 1000)); - - uc.connect(); - - is = uc.getInputStream(); - } catch (java.net.MalformedURLException e) { - XRLog.exception("bad URL given: " + uri, e); - } catch (FileNotFoundException e) { - XRLog.exception("item at URI " + uri + " not found"); - } catch (IOException e) { - XRLog.exception("IO problem for " + uri, e); - } - - return is; - - } -} diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/JDKXRLogger.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/JDKXRLogger.java index e0d6c2eb4..760d1f774 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/JDKXRLogger.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/JDKXRLogger.java @@ -21,13 +21,9 @@ */ package com.openhtmltopdf.util; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; -import java.util.logging.LogManager; import java.util.logging.Logger; import java.util.logging.Handler; import java.util.logging.Formatter; diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/SystemPropertiesUtil.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/SystemPropertiesUtil.java deleted file mode 100644 index a0bd2c2bc..000000000 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/SystemPropertiesUtil.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * {{{ header & license - * Copyright (c) 2008 Patrick Wright - * - * This program 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 program 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 program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * }}} - */ -package com.openhtmltopdf.util; - -/** - * Utility methods for working with System properties. - */ -public class SystemPropertiesUtil { - /** - * Attempts to retrieve a system property; if the property is not found, or if a SecurityException is thrown (for - * example, in a sandbox environment) will return the default value. Will swallow stack traces and any - * SecurityExceptions, and will not log any output to the console. - * - * @param propertyName property to retrieve - * @param defaultVal value to use if not found, or not allowed to use the property - * @return - */ - public static String getPropertyOrDefaultSandbox(String propertyName, String defaultVal) { - String val = defaultVal; - try { - val = System.getProperty(propertyName); - } catch (SecurityException e) { - // can happen in sandbox - } - return val; - } -} diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XMLUtil.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XMLUtil.java deleted file mode 100644 index 21ca4a1ff..000000000 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/XMLUtil.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * {{{ header & license - * XMLUtil.java - * Copyright (c) 2004, 2005 Patrick Wright - * - * This program 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 program 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 program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * }}} - */ -package com.openhtmltopdf.util; - -import java.io.File; -import java.io.StringReader; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.xml.sax.InputSource; - -/** - * Booch utility class for XML processing using DOM - */ -public class XMLUtil { - - public static Document documentFromString(final String documentContents) - throws Exception { - - return createDocumentBuilder().parse(new InputSource(new StringReader(documentContents))); - } - - public static Document documentFromFile(final String filename) - throws Exception { - - return createDocumentBuilder().parse(new File(filename).toURI().toURL().openStream()); - } - - private static DocumentBuilder createDocumentBuilder() - throws ParserConfigurationException { - - DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = fact.newDocumentBuilder(); - - builder.setErrorHandler( null ); - - return builder; - } -} - -/* - * $Id$ - * - * $Log$ - * Revision 1.7 2007/05/23 00:12:18 peterbrant - * Code cleanup (patch from Sean Bright) - * - * Revision 1.6 2006/07/26 18:18:16 pdoubleya - * TODOs - * - * Revision 1.5 2006/05/08 20:55:08 pdoubleya - * Parse input source from string using a reader, to handle encoding. - * - * Revision 1.4 2005/01/29 20:18:38 pdoubleya - * Clean/reformat code. Removed commented blocks, checked copyright. - * - * Revision 1.3 2004/10/23 14:06:57 pdoubleya - * Re-formatted using JavaStyle tool. - * Cleaned imports to resolve wildcards except for common packages (java.io, java.util, etc). - * Added CVS log comments at bottom. - * - * - */ - diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/Zipper.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/Zipper.java deleted file mode 100644 index 7078bf19e..000000000 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/Zipper.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * {{{ header & license - * Copyright (c) 2006 Patrick Wright - * - * This program 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 program 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 program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * }}} - */ -package com.openhtmltopdf.util; - -import java.io.*; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -/** - * Create a ZIP-format file from the contents of some directory. All files - * in the directory are included. To use, instantiate with a reference to - * the directory to ZIP, and to the output file to create, then call - * {@link #zipDirectory()} to create the output file. - *