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