Java tutorial
/* =========================================================== * JFreeChart : a free chart library for the Java(tm) platform * =========================================================== * * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors. * * Project Info: http://www.jfree.org/jfreechart/index.html * * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners.] * * --------------------- * ServletUtilities.java * --------------------- * (C) Copyright 2002-2008, by Richard Atkinson and Contributors. * * Original Author: Richard Atkinson; * Contributor(s): J?rgen Hoffman; * David Gilbert (for Object Refinery Limited); * Douglas Clayton; * * Changes * ------- * 19-Aug-2002 : Version 1; * 20-Apr-2003 : Added additional sendTempFile method to allow MIME type * specification and modified original sendTempFile method to * automatically set MIME type for JPEG and PNG files * 23-Jun-2003 : Added additional sendTempFile method at the request of * J?rgen Hoffman; * 07-Jul-2003 : Added more header information to streamed images; * 19-Aug-2003 : Forced images to be stored in the temporary directory defined * by System property java.io.tmpdir, rather than default (RA); * 24-Mar-2004 : Added temp filename prefix attribute (DG); * 09-Mar-2005 : Added "one time" file option (DG); * ------------- JFREECHART 1.0.x RELEASED ------------------------------------ * 10-Jan-2006 : Updated API docs and reformatted (DG); * 13-Sep-2006 : Format date in response header in English, not locale default * (see bug 1557141) (DG); * */ package tools; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import javax.servlet.http.HttpSession; import org.jfree.chart.JFreeChart; import org.sourceforge.jlibeps.epsgraphics.EpsGraphics2D; /** * Utility class used for servlet related JFreeChart operations. */ public class EpsServletUtilities { /** The filename prefix. */ private static String tempFilePrefix = "eps-"; /** A prefix for "one time" charts. */ private static String tempOneTimeFilePrefix = "eps-onetime-"; /** * Returns the prefix for the temporary file names generated by this class. * * @return The prefix (never <code>null</code>). */ public static String getTempFilePrefix() { return EpsServletUtilities.tempFilePrefix; } /** * Sets the prefix for the temporary file names generated by this class. * * @param prefix the prefix (<code>null</code> not permitted). */ public static void setTempFilePrefix(String prefix) { if (prefix == null) { throw new IllegalArgumentException("Null 'prefix' argument."); } EpsServletUtilities.tempFilePrefix = prefix; } /** * Returns the prefix for "one time" temporary file names generated by * this class. * * @return The prefix. */ public static String getTempOneTimeFilePrefix() { return EpsServletUtilities.tempOneTimeFilePrefix; } /** * Sets the prefix for the "one time" temporary file names generated by * this class. * * @param prefix the prefix (<code>null</code> not permitted). */ public static void setTempOneTimeFilePrefix(String prefix) { if (prefix == null) { throw new IllegalArgumentException("Null 'prefix' argument."); } EpsServletUtilities.tempOneTimeFilePrefix = prefix; } public static String saveChartAsEPS(JFreeChart chart, int width, int height, HttpSession session) throws IOException { if (chart == null) { throw new IllegalArgumentException("Null 'chart' argument."); } EpsServletUtilities.createTempDir(); String prefix = EpsServletUtilities.tempFilePrefix; if (session == null) { prefix = EpsServletUtilities.tempOneTimeFilePrefix; } File tempFile = File.createTempFile(prefix, ".eps", new File(System.getProperty("java.io.tmpdir"))); EpsGraphics2D graphic2d = new EpsGraphics2D(); chart.draw(graphic2d, new Rectangle2D.Double(0, 0, width, height)); try { FileWriter fos = new FileWriter(tempFile); fos.write(graphic2d.toString()); fos.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } if (session != null) { EpsServletUtilities.registerChartForDeletion(tempFile, session); } return tempFile.getPath(); } protected static void createTempDir() { String tempDirName = System.getProperty("java.io.tmpdir"); if (tempDirName == null) { throw new RuntimeException("Temporary directory system property " + "(java.io.tmpdir) is null."); } // create the temporary directory if it doesn't exist File tempDir = new File(tempDirName); if (!tempDir.exists()) { tempDir.mkdirs(); } } protected static void registerChartForDeletion(File tempFile, HttpSession session) { // Add chart to deletion list in session if (session != null) { EpsDeleter chartDeleter = (EpsDeleter) session.getAttribute("EpsChart_Deleter"); if (chartDeleter == null) { chartDeleter = new EpsDeleter(); session.setAttribute("EpsChart_Deleter", chartDeleter); } chartDeleter.addChart(tempFile.getName()); System.out.println("add a chart to delete: " + tempFile.getName()); } else { System.out.println("Session is null - chart will not be deleted"); } } }