Commit fc16c962 authored by Fabien Triolet's avatar Fabien Triolet
Browse files

This serializer produces jpeg from SVG

parent 3aeaa627
/*
* $Id: SVGSerializer.java,v 1.1 2003-04-09 13:51:41 triolet Exp $
*
* Transmorpher
*
* Copyright (C) 2001-2002 Fluxmedia and INRIA Rhne-Alpes.
* Copyright (C) INRIA Rhne-Alpes, 2003.
*
* http://www.fluxmedia.fr - http://transmorpher.inrialpes.fr
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU 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 fr.fluxmedia.tmcontrib.serializer;
// Imported fr.fluxmedia.transmorpher
import fr.fluxmedia.transmorpher.engine.TSerializer;
import fr.fluxmedia.transmorpher.utils.Parameters;
import fr.fluxmedia.transmorpher.utils.StringParameters;
import fr.fluxmedia.transmorpher.utils.TMException;
import fr.fluxmedia.transmorpher.utils.TMRuntimeException;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.w3c.dom.*;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.transcoder.image.*;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.dom.DOMResult;
/**
* This class allows transmorpher to produce jpeg from svg file
*/
public final class SVGSerializer extends TSerializer {
protected SVGHandler handler = null;
protected SAXTransformerFactory tfactory = null;
public SVGSerializer(String[] pIn, Parameters pParam, StringParameters pStaticAttributes) throws TMRuntimeException {
super(pIn, pParam, pStaticAttributes);
handler = new SVGHandler(this);
}
public void setOutputStream() throws TMRuntimeException, TMException, SAXException {
super.setOutputStream();
}
public void notify(Document doc) throws SAXException{
try {
JPEGTranscoder t = new JPEGTranscoder();
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(.8));
/* t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH,
new Float(10)); */
trans.addTranscodingHint(ImageTranscoder.KEY_MEDIA, "print");
TranscoderInput input = new TranscoderInput(doc);
TranscoderOutput output = new TranscoderOutput(getOutputStream());
t.transcode(input,output);
}
catch (Exception ex) {
throw new SAXException("Exception writing image ", ex);
}
}
protected SAXTransformerFactory getTransformerFactory() {
if (tfactory == null) {
tfactory = (SAXTransformerFactory)TransformerFactory.newInstance();
}
return tfactory;
}
public ContentHandler getContentHandler() {
return (ContentHandler)handler;
}
}
class SVGHandler extends SAXSVGDocumentFactory{
private static final String SAX_PARSER
= "org.apache.xerces.parsers.SAXParser";
protected SVGSerializer owner=null;
protected SVGHandler(SVGSerializer owner) {
super(SAX_PARSER);
this.owner = owner;
}
public Document getDocument() {
return(this.document);
}
public void startDocument()
throws SAXException {
try {
// Create SVG Document
String namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;
this.document = implementation.createDocument(namespaceURI, "svg", null);
super.startDocument();
} catch (Exception ex){
ex.printStackTrace();
throw new SAXException("SVGBuilder: startDocument", ex);
}
}
public void endDocument ()
throws SAXException {
try {
super.endDocument();
// FIXME: Hack.
((org.apache.batik.dom.svg.SVGOMDocument)this.document).setURLObject(new java.net.URL("http://xml.apache.org"));
owner.notify(this.document);
} catch (Exception ex){
ex.printStackTrace();
throw new SAXException("SVGBuilder: endDocument", ex);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment