]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / svg / SVGUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.svg;\r
13 \r
14 import java.util.Locale;\r
15 \r
16 //import org.apache.batik.bridge.BridgeContext;\r
17 //import org.apache.batik.bridge.GVTBuilder;\r
18 //import org.apache.batik.bridge.UserAgentAdapter;\r
19 //import org.apache.batik.dom.svg.SAXSVGDocumentFactory;\r
20 //import org.apache.batik.dom.svg12.SVG12DOMImplementation;\r
21 //import org.apache.batik.dom.util.DOMUtilities;\r
22 //import org.apache.batik.gvt.GraphicsNode;\r
23 //import org.apache.batik.util.XMLResourceDescriptor;\r
24 //import org.w3c.dom.svg.SVGDocument;\r
25 //import org.w3c.dom.svg.SVGSVGElement;\r
26 \r
27 \r
28 /**\r
29  * Utility methods for DOM and SVG document handling.\r
30  * \r
31  * Currently disabled.\r
32  * \r
33  * @author Tuukka Lehtonen\r
34  */\r
35 final class SVGUtils {\r
36 \r
37     public static final Locale        formatLocale = Locale.US;\r
38 \r
39 //    private static final NumberFormat nf           = NumberFormat.getNumberInstance(formatLocale);\r
40 \r
41     /**\r
42      * Attempts to parse an SVG document form the specified input stream.\r
43      * \r
44      * @param inp\r
45      * @return\r
46      * @throws IOException\r
47      */\r
48 //    public static SVGDocument parseSVGStream(InputStream inp) throws IOException {\r
49 //        ClassLoader ldr = Thread.currentThread().getContextClassLoader();\r
50 //        Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());\r
51 //        try {\r
52 //            String parser = XMLResourceDescriptor.getXMLParserClassName();\r
53 //            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);\r
54 //            SVGDocument doc = f.createSVGDocument(null, inp);\r
55 //            return doc;\r
56 //        } finally {\r
57 //            Thread.currentThread().setContextClassLoader(ldr);\r
58 //        }\r
59 //    }\r
60 \r
61     /**\r
62      * Attempts to parse a String as svg/xml and return the resulting\r
63      * SVGDocument.\r
64      * \r
65      * @param svgData the svg/xml data\r
66      * @return the resulting SVGDocument instance\r
67      * @throws IOException when parsing as svg fails\r
68      */\r
69 //    public static SVGDocument parseSVGString(String svgData) throws IOException {\r
70 //        ClassLoader ldr = Thread.currentThread().getContextClassLoader();\r
71 //        Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());\r
72 //        try {\r
73 //            String parser = XMLResourceDescriptor.getXMLParserClassName();\r
74 //            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);\r
75 //            StringReader reader = new StringReader(svgData);\r
76 //            SVGDocument doc = f.createSVGDocument(null, reader);\r
77 //            return doc;\r
78 //        } finally {\r
79 //            Thread.currentThread().setContextClassLoader(ldr);\r
80 //        }\r
81 //    }\r
82     \r
83     /**\r
84      * Attempts to parse a String as svg/xml and return the root SVGSVGElement\r
85      * of the resulting SVGDocument.\r
86      * \r
87      * @param svgData the svg/xml data\r
88      * @return the resulting SVGDocument instance\r
89      * @throws IOException when parsing as svg fails\r
90      */\r
91 //    public static SVGSVGElement parseSVGStringToRoot(String svgData) throws IOException {\r
92 //        SVGDocument doc = parseSVGString(svgData);\r
93 //        return doc.getRootElement();\r
94 //    }\r
95     \r
96     /**\r
97      * Transcodes an SVGDocument instance into String form.\r
98      * \r
99      * @param doc the SVGDocument to transcode\r
100      * @return the resulting string\r
101      * @throws IOException if the document contains invalid data\r
102      */\r
103 //    public static String svgToString(SVGDocument doc) throws IOException {\r
104 //        StringWriter writer = new StringWriter();\r
105 //        DOMUtilities.writeDocument(doc, writer);\r
106 //        writer.close();\r
107 //        return writer.toString();\r
108 //    }\r
109     \r
110     /**\r
111      * Retrieves SVG document bounding box by constructing the corresponding GVT\r
112      * tree and getting the bounding information from there.\r
113      * \r
114      * <p>\r
115      * This allows us to get the bounding box of the actual rendered output, not\r
116      * just the bounding box of the primitives.\r
117      * </p>\r
118      * \r
119      * <p>\r
120      * BEWARE: using the specified document with a JSVGCanvas after invoking\r
121      * this method seems to break the functionality of the DOM/GVT binding in\r
122      * the canvas. If you need to do that, \r
123      * \r
124      * @param doc\r
125      * @param defaultValue the rectangle to return if the document has no\r
126      *        bounding box\r
127      * @return <code>null</code> if the document has no graphical elements,\r
128      *         i.e. no real bounding box and the given defaultValue was\r
129      *         <code>null</code>\r
130      */\r
131 //    public static Rectangle2D getDocumentBoundingBox(SVGDocument doc, Rectangle2D defaultValue, boolean cloneDocument) {\r
132 //        if (cloneDocument)\r
133 //            doc = (SVGDocument) DOMUtilities.deepCloneDocument(doc, SVG12DOMImplementation.getDOMImplementation());\r
134 //\r
135 //        GVTBuilder builder = new GVTBuilder();\r
136 //        BridgeContext ctx = new BridgeContext(new UserAgentAdapter());\r
137 //        GraphicsNode gvtRoot = builder.build(ctx, doc);\r
138 //\r
139 //        Rectangle2D bounds = gvtRoot.getSensitiveBounds();\r
140 //        if (bounds == null)\r
141 //            return defaultValue;\r
142 //        \r
143 //        if (bounds.isEmpty())\r
144 //            return defaultValue;\r
145 //        \r
146 //        Rectangle2D bbox = new Rectangle2D.Float();\r
147 //        bbox.setFrame(bounds);\r
148 //        return bbox;\r
149 //    }\r
150 \r
151 }\r