package org.wikiwebserver.util.image;

import java.awt.Graphics2D;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

public class SVGHelper extends GraphicsHelper {
    
    private Graphics2D graphics;
    
    public SVGHelper(int width, int height, OutputStream out) {
        super(width, height, out);
    } 
    
    public Graphics2D createGraphics() {
        
        try {
            DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
            String svgNS = "http://www.w3.org/2000/svg";
            Document document = domImpl.createDocument(svgNS, "svg", null);
            graphics = new SVGGraphics2D(document);    

        } catch (NoClassDefFoundError ex) {
            ex.printStackTrace();
        }   

        return graphics;
    }    

    public void writeImageData() throws IOException {
        try {
            if (graphics instanceof SVGGraphics2D) {
                Writer writer = new OutputStreamWriter(getOutputStream(),"utf8");
                ((SVGGraphics2D)graphics).stream(writer, true);
            }
        } catch (NoClassDefFoundError ex) {
            ex.printStackTrace();
        }
    }
    
    public String getContentType() {
        return "image/svg+xml";
    }        
}

