package org.deegree.rendering.r2d.context;

import static org.apache.commons.io.IOUtils.closeQuietly;

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 SvgRenderContext extends Java2DRenderContext {
        
    private SvgRenderContext ( RenderingInfo info, Document document, OutputStream outputStream ) {
        super( info, new SVGGraphics2D( document ), outputStream );
    }
    
    public static RenderContext createInstance( RenderingInfo info, OutputStream outputStream ) {
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        Document document = domImpl.createDocument( "http://www.w3.org/2000/svg", "svg", null );
        
        return new SvgRenderContext( info, document, outputStream );
    }
    
    @Override
    public boolean close () throws IOException {
        try {
            if ( outputStream != null ) {
                final Writer writer = new OutputStreamWriter( outputStream, "UTF-8" );
                ((SVGGraphics2D)graphics).stream( writer, true );
            }
            graphics.dispose();
            return outputStream != null;
        } finally {
            closeQuietly( outputStream );
        }
    }

    @Override
    public void applyOptions (final MapOptions options) {
        // TODO: apply options.
    }
}
@@ -96,24 +96,52 @@
 Quietly;%0D%0A%0D%0A
+import java.awt.Dimension;%0D%0A
 import java.
@@ -153,24 +153,24 @@
 Exception;%0D%0A
-
 import java.
@@ -544,33 +544,38 @@
 o info, 
-Document document
+SVGGraphics2D graphics
 , Output
@@ -624,37 +624,16 @@
 fo, 
-new SVGGraphics2D( document )
+graphics
 , ou
@@ -947,24 +947,187 @@
 );%0D%0A        
+        
+%0D%0A        SVGGraphics2D graphics = new SVGGraphics2D( document );%0D%0A        graphics.setSVGCanvasSize( new Dimension( info.getWidth(), info.getHeight() ) );
 %0D%0A        re
@@ -1159,24 +1159,24 @@
 ( info, 
-document
+graphics
 , output
@@ -1427,17 +1427,19 @@
        (
-(
+ ( 
 SVGGraph
@@ -1447,17 +1447,19 @@
 cs2D
-)
+ ) 
 graphics
 ).st
@@ -1454,16 +1454,17 @@
 graphics
+ 
 ).stream

set canvas size