Mercurial > hg > graal-compiler
comparison visualizer/BatikSVGProxy/src/com/sun/hotspot/igv/svg/BatikSVG.java @ 4512:015fb895586b
Moved visualizer to new directory.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Tue, 07 Feb 2012 22:41:09 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4511:6cb549627941 | 4512:015fb895586b |
---|---|
1 /* | |
2 * Copyright (c) 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * 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 * Utility class | |
35 * @author Thomas Wuerthinger | |
36 */ | |
37 public class BatikSVG { | |
38 | |
39 private BatikSVG() { | |
40 } | |
41 | |
42 private static Constructor SVGGraphics2DConstructor; | |
43 private static Method streamMethod; | |
44 private static Method createDefaultMethod; | |
45 private static Method getDOMImplementationMethod; | |
46 private static Method setEmbeddedFontsOnMethod; | |
47 private static Class<?> classSVGGraphics2D; | |
48 | |
49 /** | |
50 * Creates a graphics object that allows to be exported to SVG data using the {@link #printToStream(Graphics2D, Writer, boolean) printToStream} method. | |
51 * @return the newly created Graphics2D object or null if the library does not exist | |
52 */ | |
53 public static Graphics2D createGraphicsObject() { | |
54 try { | |
55 if (SVGGraphics2DConstructor == null) { | |
56 ClassLoader cl = BatikSVG.class.getClassLoader(); | |
57 Class<?> classGenericDOMImplementation = cl.loadClass("org.apache.batik.dom.GenericDOMImplementation"); | |
58 Class<?> classSVGGeneratorContext = cl.loadClass("org.apache.batik.svggen.SVGGeneratorContext"); | |
59 classSVGGraphics2D = cl.loadClass("org.apache.batik.svggen.SVGGraphics2D"); | |
60 getDOMImplementationMethod = classGenericDOMImplementation.getDeclaredMethod("getDOMImplementation", new Class[0]); | |
61 createDefaultMethod = classSVGGeneratorContext.getDeclaredMethod("createDefault", new Class[]{org.w3c.dom.Document.class}); | |
62 setEmbeddedFontsOnMethod = classSVGGeneratorContext.getDeclaredMethod("setEmbeddedFontsOn", new Class[]{boolean.class}); | |
63 streamMethod = classSVGGraphics2D.getDeclaredMethod("stream", Writer.class, boolean.class); | |
64 SVGGraphics2DConstructor = classSVGGraphics2D.getConstructor(classSVGGeneratorContext, boolean.class); | |
65 } | |
66 DOMImplementation dom = (DOMImplementation) getDOMImplementationMethod.invoke(null); | |
67 org.w3c.dom.Document document = dom.createDocument("http://www.w3.org/2000/svg", "svg", null); | |
68 Object ctx = createDefaultMethod.invoke(null, document); | |
69 setEmbeddedFontsOnMethod.invoke(ctx, true); | |
70 Graphics2D svgGenerator = (Graphics2D) SVGGraphics2DConstructor.newInstance(ctx, true); | |
71 return svgGenerator; | |
72 } catch (ClassNotFoundException e) { | |
73 return null; | |
74 } catch (NoSuchMethodException e) { | |
75 return null; | |
76 } catch (IllegalAccessException e) { | |
77 return null; | |
78 } catch (InvocationTargetException e) { | |
79 return null; | |
80 } catch (InstantiationException e) { | |
81 return null; | |
82 } | |
83 } | |
84 | |
85 /** | |
86 * Serializes a graphics object to a stream in SVG format. | |
87 * @param svgGenerator the graphics object. Only graphics objects created by the {@link #createGraphicsObject() createGraphicsObject} method are valid. | |
88 * @param stream the stream to which the data is written | |
89 * @param useCSS whether to use CSS styles in the SVG output | |
90 */ | |
91 public static void printToStream(Graphics2D svgGenerator, Writer stream, boolean useCSS) { | |
92 assert classSVGGraphics2D != null; | |
93 assert classSVGGraphics2D.isInstance(svgGenerator); | |
94 try { | |
95 streamMethod.invoke(svgGenerator, stream, useCSS); | |
96 } catch (IllegalAccessException e) { | |
97 assert false; | |
98 } catch (InvocationTargetException e) { | |
99 assert false; | |
100 } | |
101 } | |
102 } |