|
1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 package com.sun.hotspot.igv.svg; |
|
25 |
|
26 import java.awt.Graphics2D; |
|
27 import java.io.Writer; |
|
28 import java.lang.reflect.Constructor; |
|
29 import java.lang.reflect.InvocationTargetException; |
|
30 import java.lang.reflect.Method; |
|
31 import org.w3c.dom.DOMImplementation; |
|
32 |
|
33 /** |
|
34 * |
|
35 * @author Thomas Wuerthinger |
|
36 */ |
|
37 public class BatikSVG { |
|
38 |
|
39 private static Constructor SVGGraphics2DConstructor; |
|
40 private static Method Method_stream; |
|
41 private static Method Method_createDefault; |
|
42 private static Method Method_getDOMImplementation; |
|
43 private static Method Method_setEmbeddedFontsOn; |
|
44 |
|
45 public static Graphics2D createGraphicsObject() { |
|
46 try { |
|
47 if (SVGGraphics2DConstructor == null) { |
|
48 ClassLoader cl = BatikSVG.class.getClassLoader(); |
|
49 Class Class_GenericDOMImplementation = cl.loadClass("org.apache.batik.dom.GenericDOMImplementation"); |
|
50 Class Class_SVGGeneratorContext = cl.loadClass("org.apache.batik.svggen.SVGGeneratorContext"); |
|
51 Class Class_SVGGraphics2D = cl.loadClass("org.apache.batik.svggen.SVGGraphics2D"); |
|
52 Method_getDOMImplementation = Class_GenericDOMImplementation.getDeclaredMethod("getDOMImplementation", new Class[0]); |
|
53 Method_createDefault = Class_SVGGeneratorContext.getDeclaredMethod("createDefault", new Class[]{org.w3c.dom.Document.class}); |
|
54 Method_setEmbeddedFontsOn = Class_SVGGeneratorContext.getDeclaredMethod("setEmbeddedFontsOn", new Class[]{boolean.class}); |
|
55 Method_stream = Class_SVGGraphics2D.getDeclaredMethod("stream", Writer.class, boolean.class); |
|
56 SVGGraphics2DConstructor = Class_SVGGraphics2D.getConstructor(Class_SVGGeneratorContext, boolean.class); |
|
57 } |
|
58 DOMImplementation dom = (DOMImplementation) Method_getDOMImplementation.invoke(null); |
|
59 org.w3c.dom.Document document = dom.createDocument("http://www.w3.org/2000/svg", "svg", null); |
|
60 Object ctx = Method_createDefault.invoke(null, document); |
|
61 Method_setEmbeddedFontsOn.invoke(ctx, true); |
|
62 Graphics2D svgGenerator = (Graphics2D) SVGGraphics2DConstructor.newInstance(ctx, true); |
|
63 return svgGenerator; |
|
64 } catch (ClassNotFoundException e) { |
|
65 return null; |
|
66 } catch (NoSuchMethodException e) { |
|
67 return null; |
|
68 } catch (IllegalAccessException e) { |
|
69 return null; |
|
70 } catch (InvocationTargetException e) { |
|
71 return null; |
|
72 } catch (InstantiationException e) { |
|
73 return null; |
|
74 } |
|
75 } |
|
76 |
|
77 public static void printToStream(Graphics2D svgGenerator, Writer stream, boolean useCSS) { |
|
78 try { |
|
79 Method_stream.invoke(svgGenerator, stream, useCSS); |
|
80 } catch (IllegalAccessException e) { |
|
81 assert false; |
|
82 } catch (InvocationTargetException e) { |
|
83 assert false; |
|
84 } |
|
85 } |
|
86 } |