org/objectweb/proactive/benchmarks/timit/util/charts/Utilities.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.benchmarks.timit.util.charts;
00032 
00033 import java.awt.Graphics2D;
00034 import java.awt.Rectangle;
00035 import java.io.File;
00036 import java.io.FileOutputStream;
00037 import java.io.IOException;
00038 import java.io.OutputStream;
00039 import java.io.OutputStreamWriter;
00040 import java.io.Writer;
00041 import java.lang.reflect.Constructor;
00042 import java.lang.reflect.Method;
00043 
00044 import org.jdom.Element;
00045 import org.jfree.chart.JFreeChart;
00046 import org.objectweb.proactive.benchmarks.timit.TimIt;
00047 import org.objectweb.proactive.benchmarks.timit.config.ConfigChart;
00048 import org.objectweb.proactive.benchmarks.timit.util.BenchmarkStatistics;
00049 import org.w3c.dom.DOMImplementation;
00050 
00056 public class Utilities {
00057 
00069     public static void generatingCharts(Element eTimitResult,
00070             BenchmarkStatistics bstats, ConfigChart[] charts) {
00071 
00072         if( charts == null ) return;
00073         TimIt.message(2, "Generating charts...");
00074         String className = null;
00075 
00076         for (ConfigChart cChart : charts) {
00077             TimIt.message(4, "Generating " + cChart.get("title") + " ["
00078                     + cChart.get("subtitle") + "]" + "...");
00079             try {
00080                 className = Utilities.class.getPackage().getName() + "."
00081                         + cChart.get("type");
00082                 Class chartClass = Class.forName(className);
00083                 Chart chart = (Chart) chartClass.newInstance();
00084                 chart.generateChart(eTimitResult, bstats, cChart);
00085 
00086             } catch (ClassNotFoundException e) {
00087                 System.err.println("  Fail: Unknown chart type: " + className);
00088             } catch (InstantiationException e) {
00089                 e.printStackTrace();
00090             } catch (IllegalAccessException e) {
00091                 e.printStackTrace();
00092             }
00093         }
00094     }
00095 
00108     public static void saveChartAsSVG(JFreeChart chart, Rectangle bounds,
00109             File svgFile) throws IOException {
00110         try {
00111             Class GDI = Class
00112                     .forName("org.apache.batik.dom.GenericDOMImplementation");
00113 
00114             // Get a DOMImplementation and create an XML document
00115             Method getDOMImplementation = GDI.getMethod("getDOMImplementation",
00116                     new Class[0]);
00117             DOMImplementation domImpl = (DOMImplementation) getDOMImplementation
00118                     .invoke(null, new Object[0]);
00119 
00120             org.w3c.dom.Document document = domImpl.createDocument(null, "svg",
00121                     null);
00122 
00123             // Create an instance of the SVG Generator
00124             Class SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D");
00125             Method streamMethod = SG2D.getMethod("stream", new Class[] {
00126                     Writer.class, boolean.class });
00127             Constructor SG2DConstr = SG2D
00128                     .getConstructor(new Class[] { org.w3c.dom.Document.class });
00129             Object svgGenerator = SG2DConstr.newInstance(document);
00130 
00131             // draw the chart in the SVG generator
00132             chart.draw((Graphics2D) svgGenerator, bounds);
00133 
00134             // Write svg file
00135             OutputStream outputStream = new FileOutputStream(svgFile);
00136             Writer out = new OutputStreamWriter(outputStream, "UTF-8");
00137             streamMethod.invoke(svgGenerator,
00138                     new Object[] { out, true /* use css */});
00139             outputStream.flush();
00140             outputStream.close();
00141             out.close();
00142 
00143         } catch (Exception e) {
00144             e.printStackTrace();
00145         }
00146     }
00147 }

Generated on Mon Jan 22 15:16:04 2007 for ProActive by  doxygen 1.5.1