1 26 package org.nightlabs.editor2d.iofilter; 27 28 import java.awt.Graphics2D ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.OutputStreamWriter ; 32 import java.io.UnsupportedEncodingException ; 33 import java.io.Writer ; 34 import java.util.Iterator ; 35 36 import org.apache.batik.dom.GenericDOMImplementation; 37 import org.apache.batik.svggen.SVGGraphics2D; 38 import org.apache.batik.svggen.SVGGraphics2DIOException; 39 import org.nightlabs.editor2d.DrawComponent; 40 import org.nightlabs.editor2d.DrawComponentContainer; 41 import org.nightlabs.editor2d.render.Renderer; 42 import org.nightlabs.io.AbstractIOFilter; 43 import org.nightlabs.io.ReadException; 44 import org.nightlabs.io.WriteException; 45 import org.w3c.dom.DOMImplementation ; 46 import org.w3c.dom.Document ; 47 48 52 public class SVGFilter 53 extends AbstractIOFilter 54 { 55 56 59 public SVGFilter() 60 { 61 super(); 62 setFileExtension(DEFAULT_FILE_EXTENSION); 63 } 64 65 69 public Object read(InputStream arg0) 70 throws ReadException 71 { 72 return null; 73 } 74 75 80 public void write(Object arg0, OutputStream arg1) 81 throws WriteException 82 { 83 if (arg0 instanceof DrawComponent) 84 { 85 DrawComponent dc = (DrawComponent) arg0; 86 87 DOMImplementation domImpl = 89 GenericDOMImplementation.getDOMImplementation(); 90 91 Document document = domImpl.createDocument(null, "svg", null); 93 94 SVGGraphics2D svgGenerator = new SVGGraphics2D(document); 96 97 paintDrawComponent(dc, svgGenerator); 99 100 boolean useCSS = true; try { 104 Writer out = new OutputStreamWriter (arg1, getEncoding()); 105 svgGenerator.stream(out, useCSS); 106 } catch (UnsupportedEncodingException usee) { 107 throw new RuntimeException (usee); 109 } catch (SVGGraphics2DIOException svge) { 110 throw new RuntimeException (svge); 111 } 112 } 113 } 114 115 public static final String DEFAULT_ENCODING = "UTF-8"; 116 public static final String DEFAULT_FILE_EXTENSION = "svg"; 117 118 protected String encoding = DEFAULT_ENCODING; 119 public String getEncoding() { 120 return encoding; 121 } 122 public void setEncoding(String encoding) { 123 this.encoding = encoding; 124 } 125 126 protected void paintDrawComponent(DrawComponent dc, Graphics2D g2d) 127 { 128 if (dc instanceof DrawComponentContainer) { 129 DrawComponentContainer dcContainer = (DrawComponentContainer) dc; 130 for (Iterator it = dcContainer.getDrawComponents().iterator(); it.hasNext(); ) { 131 DrawComponent drawComponent = (DrawComponent) it.next(); 132 paintDrawComponent(drawComponent, g2d); 133 } 134 } 135 else { 136 Renderer r = dc.getRenderer(); 137 if (r != null) 138 r.paint(dc, g2d); 139 } 140 } 141 142 } 143 | Popular Tags |