franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf.util; franta-hg@1: franta-hg@1: import java.awt.Color; franta-hg@1: import java.awt.Font; franta-hg@1: import java.awt.Graphics; franta-hg@1: import java.awt.image.BufferedImage; franta-hg@1: import java.io.IOException; franta-hg@1: import java.io.OutputStream; franta-hg@1: import java.io.PrintWriter; franta-hg@1: import java.io.StringWriter; franta-hg@1: import java.util.Enumeration; franta-hg@1: import java.util.StringTokenizer; franta-hg@1: franta-hg@1: import com.sun.image.codec.jpeg.JPEGCodec; franta-hg@1: import com.sun.image.codec.jpeg.JPEGEncodeParam; franta-hg@1: import com.sun.image.codec.jpeg.JPEGImageEncoder; franta-hg@1: franta-hg@1: /** franta-hg@1: * Some methods to render messages or exceptions into an image. franta-hg@1: * @author glaures franta-hg@1: */ franta-hg@1: public class RenderingHelper { franta-hg@1: franta-hg@1: private final static int PADDING_X = 5; franta-hg@1: franta-hg@1: public static String renderMessage(String msg, int width, int height, OutputStream out) throws IOException { franta-hg@1: BufferedImage image = ImageHelper.createImage(width, height); franta-hg@1: Graphics gr = image.getGraphics(); franta-hg@1: gr.setColor(Color.white); franta-hg@1: gr.fillRect(0, 0, width, height); franta-hg@1: gr.setColor(Color.black); franta-hg@1: gr.drawString(msg, PADDING_X, height/2 - 7); franta-hg@1: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); franta-hg@1: JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image); franta-hg@1: param.setQuality(1.0f, true); franta-hg@1: encoder.encode(image, param); franta-hg@1: return "image/jpeg"; franta-hg@1: } franta-hg@1: franta-hg@1: public static String renderException(Throwable ex, int width, int height, OutputStream out) throws IOException { franta-hg@1: BufferedImage image = ImageHelper.createImage(width, height); franta-hg@1: Graphics gr = image.getGraphics(); franta-hg@1: gr.setColor(Color.white); franta-hg@1: gr.fillRect(0, 0, width, height); franta-hg@1: gr.setColor(Color.red); franta-hg@1: gr.drawString(ex.getClass().getName() + " raised:", PADDING_X, 15); franta-hg@1: gr.drawString(String.valueOf(ex.getMessage()), PADDING_X, 30); franta-hg@1: gr.setColor(Color.black); franta-hg@1: Font stFont = gr.getFont().deriveFont(9f); franta-hg@1: gr.setFont(stFont); franta-hg@1: drawStackTrace(gr, PADDING_X, 50, ex); franta-hg@1: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); franta-hg@1: JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image); franta-hg@1: param.setQuality(1.0f, true); franta-hg@1: encoder.encode(image, param); franta-hg@1: return "image/jpeg"; franta-hg@1: } franta-hg@1: franta-hg@1: private static void drawStackTrace(Graphics gr, int x, int y, Throwable ex) { franta-hg@1: final int linePadding = 4; franta-hg@1: int lineHeight = gr.getFont().getSize() + linePadding; franta-hg@1: int currY = y; franta-hg@1: Enumeration lines = new StringTokenizer(getStackTrace(ex), "\n", false); franta-hg@1: while (lines.hasMoreElements()) { franta-hg@1: gr.drawString(((String)lines.nextElement()).trim(), x, currY); franta-hg@1: currY += lineHeight; franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: private static String getStackTrace(Throwable t) { franta-hg@1: StringWriter sw = new StringWriter(); franta-hg@1: PrintWriter pw = new PrintWriter(sw); franta-hg@1: t.printStackTrace(pw); franta-hg@1: pw.close(); franta-hg@1: return sw.getBuffer().toString(); franta-hg@1: } franta-hg@1: franta-hg@1: }