001: /***********************************************************************************************
002: * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
003: *
004: * Redistribution and use of this software and associated documentation ("Software"), with or
005: * without modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain copyright statements and notices.
008: * Redistributions must also contain a copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other materials
012: * provided with the distribution.
013: *
014: * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
015: * products derived from this Software without prior written permission of Nathaniel G.
016: * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
017: *
018: * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
019: * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
020: * registered trademark of Nathaniel G. Auvil.
021: *
022: * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
023: *
024: * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
025: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
026: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
029: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
032: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
033: ************************************************************************************************/package org.krysalis.jcharts.encoders;
034:
035: import org.apache.batik.dom.GenericDOMImplementation;
036: import org.apache.batik.svggen.SVGGraphics2D;
037: import org.krysalis.jcharts.Chart;
038: import org.krysalis.jcharts.chartData.ChartDataException;
039: import org.krysalis.jcharts.imageMap.ImageMapNotSupportedException;
040: import org.krysalis.jcharts.properties.PropertyException;
041: import org.w3c.dom.DOMImplementation;
042: import org.w3c.dom.Document;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046: import java.io.OutputStreamWriter;
047: import java.io.Writer;
048:
049: /*************************************************************************************
050: * This class REQUIRES the Apache XML Batik libraries to run.
051: *
052: * @author Nathaniel Auvil
053: * @version $Id: SVGEncoder.java,v 1.2 2003/05/26 13:40:21 nathaniel_auvil Exp $
054: ************************************************************************************/
055: public class SVGEncoder {
056:
057: /******************************************************************************************
058: *
059: *
060: ******************************************************************************************/
061: private SVGEncoder() throws Exception {
062: throw new Exception(
063: "No need to create an instance of this class!");
064: }
065:
066: /******************************************************************************************
067: * Encodes the Chart to an OutputStream which can be a file or any other OutputStream
068: * implementation.
069: *
070: * @param chart
071: * @param outputStream
072: * @throws ChartDataException
073: * @throws PropertyException
074: * @throws IOException
075: *******************************************************************************************/
076: public static void encode(Chart chart, OutputStream outputStream)
077: throws ChartDataException, PropertyException, IOException {
078: //---hopefully eliminate support requests asking about this...
079: if (chart.getImageMap() != null) {
080: throw new ImageMapNotSupportedException(
081: "HTML client-side image maps are not supported by the SVG format.");
082: }
083:
084: //---Get a DOMImplementation
085: DOMImplementation domImpl = GenericDOMImplementation
086: .getDOMImplementation();
087:
088: //---Create an instance of org.w3c.dom.Document
089: Document document = domImpl.createDocument(null, "svg", null);
090:
091: //---Create an instance of the SVG Generator
092: SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
093:
094: chart.setGraphics2D(svgGenerator);
095: chart.render();
096:
097: Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
098: svgGenerator.stream(writer, false);
099:
100: writer.flush();
101: writer.close();
102: }
103: }
|