KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > iofilter > SVGFilter


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26 package org.nightlabs.editor2d.iofilter;
27
28 import java.awt.Graphics2D JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.OutputStreamWriter JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.util.Iterator JavaDoc;
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 JavaDoc;
46 import org.w3c.dom.Document JavaDoc;
47
48 /**
49  * @author Daniel.Mazurek <at> NightLabs <dot> de
50  *
51  */

52 public class SVGFilter
53 extends AbstractIOFilter
54 {
55
56     /**
57      * sets the fileExtension to SVG
58      */

59     public SVGFilter()
60     {
61         super();
62         setFileExtension(DEFAULT_FILE_EXTENSION);
63     }
64
65     /**
66      * returns null, because this IOFilter only supports SVG export (write)
67      * @see org.nightlabs.io.IOFilter#read(java.io.InputStream)
68      */

69     public Object JavaDoc read(InputStream JavaDoc arg0)
70     throws ReadException
71     {
72         return null;
73     }
74
75     /**
76      * writes the given DrawComponent to the given outputStream as SVG-Content
77      *
78      * @see org.nightlabs.io.IOFilter#write(java.lang.Object, java.io.OutputStream)
79      */

80     public void write(Object JavaDoc arg0, OutputStream JavaDoc arg1)
81     throws WriteException
82     {
83         if (arg0 instanceof DrawComponent)
84         {
85             DrawComponent dc = (DrawComponent) arg0;
86             
87       // Get a DOMImplementation
88
DOMImplementation domImpl =
89           GenericDOMImplementation.getDOMImplementation();
90
91       // Create an instance of org.w3c.dom.Document
92
Document JavaDoc document = domImpl.createDocument(null, "svg", null);
93
94       // Create an instance of the SVG Generator
95
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
96       
97       // render the content into the SVG Graphics2D implementation
98
paintDrawComponent(dc, svgGenerator);
99       
100       // Finally, stream out SVG to the outputStream using UTF-8
101
// character to byte encoding
102
boolean useCSS = true; // we want to use CSS style attribute
103
try {
104         Writer JavaDoc out = new OutputStreamWriter JavaDoc(arg1, getEncoding());
105         svgGenerator.stream(out, useCSS);
106       } catch (UnsupportedEncodingException JavaDoc usee) {
107 // throw new WriteException(usee);
108
throw new RuntimeException JavaDoc(usee);
109       } catch (SVGGraphics2DIOException svge) {
110         throw new RuntimeException JavaDoc(svge);
111       }
112         }
113     }
114
115     public static final String JavaDoc DEFAULT_ENCODING = "UTF-8";
116     public static final String JavaDoc DEFAULT_FILE_EXTENSION = "svg";
117     
118     protected String JavaDoc encoding = DEFAULT_ENCODING;
119     public String JavaDoc getEncoding() {
120         return encoding;
121     }
122     public void setEncoding(String JavaDoc encoding) {
123         this.encoding = encoding;
124     }
125
126     protected void paintDrawComponent(DrawComponent dc, Graphics2D JavaDoc g2d)
127     {
128         if (dc instanceof DrawComponentContainer) {
129             DrawComponentContainer dcContainer = (DrawComponentContainer) dc;
130             for (Iterator JavaDoc 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