Mercurial > hg > truffle
annotate src/share/tools/IdealGraphVisualizer/BatikSVGProxy/src/com/sun/hotspot/igv/svg/BatikSVG.java @ 18983:43baadc1913a
Automatically install Batik for SVG export in IGV
author | Tom Rodriguez <tom.rodriguez@oracle.com> |
---|---|
date | Tue, 27 Jan 2015 15:47:34 -0800 |
parents | bf4f8817e3bd |
children |
rev | line source |
---|---|
222 | 1 /* |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
222
diff
changeset
|
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
222 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
222
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
222
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
222
diff
changeset
|
21 * questions. |
222 | 22 * |
23 */ | |
24 package com.sun.hotspot.igv.svg; | |
25 | |
26 import java.awt.Graphics2D; | |
27 import java.io.Writer; | |
18983
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
28 import java.io.File; |
222 | 29 import java.lang.reflect.Constructor; |
30 import java.lang.reflect.InvocationTargetException; | |
31 import java.lang.reflect.Method; | |
18983
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
32 import java.net.MalformedURLException; |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
33 import java.net.URL; |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
34 import java.net.URLClassLoader; |
222 | 35 import org.w3c.dom.DOMImplementation; |
36 | |
37 /** | |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
38 * Utility class |
222 | 39 * @author Thomas Wuerthinger |
40 */ | |
41 public class BatikSVG { | |
42 | |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
43 private BatikSVG() { |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
44 } |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
45 |
222 | 46 private static Constructor SVGGraphics2DConstructor; |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
47 private static Method streamMethod; |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
48 private static Method createDefaultMethod; |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
49 private static Method getDOMImplementationMethod; |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
50 private static Method setEmbeddedFontsOnMethod; |
2955
bf4f8817e3bd
IdealGraphVisualizer: eliminate compiler warnings by replacing calls to deprecated API methods, introducing generics parameters where missing, and when absolutely necessary adding @SuppressWarnings annotations
Peter Hofer <peter.hofer@jku.at>
parents:
2898
diff
changeset
|
51 private static Class<?> classSVGGraphics2D; |
222 | 52 |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
53 /** |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
54 * Creates a graphics object that allows to be exported to SVG data using the {@link #printToStream(Graphics2D, Writer, boolean) printToStream} method. |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
55 * @return the newly created Graphics2D object or null if the library does not exist |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
56 */ |
222 | 57 public static Graphics2D createGraphicsObject() { |
58 try { | |
59 if (SVGGraphics2DConstructor == null) { | |
18983
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
60 String batikJar = System.getenv().get("IGV_BATIK_JAR"); |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
61 if (batikJar == null) { |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
62 return null; |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
63 } |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
64 // Load batik in it's own class loader since some it's support jars interfere with the JDK |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
65 URL url = new File(batikJar).toURI().toURL(); |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
66 ClassLoader cl = new URLClassLoader(new URL[] { url }); |
2955
bf4f8817e3bd
IdealGraphVisualizer: eliminate compiler warnings by replacing calls to deprecated API methods, introducing generics parameters where missing, and when absolutely necessary adding @SuppressWarnings annotations
Peter Hofer <peter.hofer@jku.at>
parents:
2898
diff
changeset
|
67 Class<?> classGenericDOMImplementation = cl.loadClass("org.apache.batik.dom.GenericDOMImplementation"); |
bf4f8817e3bd
IdealGraphVisualizer: eliminate compiler warnings by replacing calls to deprecated API methods, introducing generics parameters where missing, and when absolutely necessary adding @SuppressWarnings annotations
Peter Hofer <peter.hofer@jku.at>
parents:
2898
diff
changeset
|
68 Class<?> classSVGGeneratorContext = cl.loadClass("org.apache.batik.svggen.SVGGeneratorContext"); |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
69 classSVGGraphics2D = cl.loadClass("org.apache.batik.svggen.SVGGraphics2D"); |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
70 getDOMImplementationMethod = classGenericDOMImplementation.getDeclaredMethod("getDOMImplementation", new Class[0]); |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
71 createDefaultMethod = classSVGGeneratorContext.getDeclaredMethod("createDefault", new Class[]{org.w3c.dom.Document.class}); |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
72 setEmbeddedFontsOnMethod = classSVGGeneratorContext.getDeclaredMethod("setEmbeddedFontsOn", new Class[]{boolean.class}); |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
73 streamMethod = classSVGGraphics2D.getDeclaredMethod("stream", Writer.class, boolean.class); |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
74 SVGGraphics2DConstructor = classSVGGraphics2D.getConstructor(classSVGGeneratorContext, boolean.class); |
222 | 75 } |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
76 DOMImplementation dom = (DOMImplementation) getDOMImplementationMethod.invoke(null); |
222 | 77 org.w3c.dom.Document document = dom.createDocument("http://www.w3.org/2000/svg", "svg", null); |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
78 Object ctx = createDefaultMethod.invoke(null, document); |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
79 setEmbeddedFontsOnMethod.invoke(ctx, true); |
222 | 80 Graphics2D svgGenerator = (Graphics2D) SVGGraphics2DConstructor.newInstance(ctx, true); |
81 return svgGenerator; | |
82 } catch (ClassNotFoundException e) { | |
83 return null; | |
84 } catch (NoSuchMethodException e) { | |
85 return null; | |
86 } catch (IllegalAccessException e) { | |
87 return null; | |
88 } catch (InvocationTargetException e) { | |
89 return null; | |
90 } catch (InstantiationException e) { | |
91 return null; | |
18983
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
92 } catch (MalformedURLException e) { |
43baadc1913a
Automatically install Batik for SVG export in IGV
Tom Rodriguez <tom.rodriguez@oracle.com>
parents:
2955
diff
changeset
|
93 return null; |
222 | 94 } |
95 } | |
96 | |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
97 /** |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
98 * Serializes a graphics object to a stream in SVG format. |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
99 * @param svgGenerator the graphics object. Only graphics objects created by the {@link #createGraphicsObject() createGraphicsObject} method are valid. |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
100 * @param stream the stream to which the data is written |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
101 * @param useCSS whether to use CSS styles in the SVG output |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
102 */ |
222 | 103 public static void printToStream(Graphics2D svgGenerator, Writer stream, boolean useCSS) { |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
104 assert classSVGGraphics2D != null; |
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
105 assert classSVGGraphics2D.isInstance(svgGenerator); |
222 | 106 try { |
2898
53ddfe305645
IdealGraphVisualizer: merge changes from the IGV project on Kenai and add Oracle copyright notices where necessary
Peter Hofer <peter.hofer@jku.at>
parents:
1552
diff
changeset
|
107 streamMethod.invoke(svgGenerator, stream, useCSS); |
222 | 108 } catch (IllegalAccessException e) { |
109 assert false; | |
110 } catch (InvocationTargetException e) { | |
111 assert false; | |
112 } | |
113 } | |
114 } |