]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGTextFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / svg / SVGTextFactory.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGTextFactory.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGTextFactory.java
new file mode 100644 (file)
index 0000000..d67fc84
--- /dev/null
@@ -0,0 +1,120 @@
+/*******************************************************************************\r
+ * Copyright (c) 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.property.svg;\r
+\r
+import java.io.IOException;\r
+import java.io.StringReader;\r
+import java.io.StringWriter;\r
+\r
+import javax.xml.parsers.DocumentBuilder;\r
+import javax.xml.parsers.DocumentBuilderFactory;\r
+import javax.xml.parsers.ParserConfigurationException;\r
+\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.G2DResource;\r
+import org.w3c.dom.Document;\r
+import org.w3c.dom.bootstrap.DOMImplementationRegistry;\r
+import org.w3c.dom.ls.DOMImplementationLS;\r
+import org.w3c.dom.ls.LSOutput;\r
+import org.w3c.dom.ls.LSSerializer;\r
+import org.xml.sax.InputSource;\r
+import org.xml.sax.SAXException;\r
+\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class SVGTextFactory extends ReadFactoryImpl<SVGInput, String> {\r
+\r
+    @Override\r
+    public String perform(ReadGraph graph, SVGInput input) throws DatabaseException {\r
+        G2DResource G2D = G2DResource.getInstance(graph);\r
+        Resource element = input.getDatum();\r
+        String exp = graph.getPossibleRelatedValue(element, G2D.HasSVGDocument, Bindings.STRING);\r
+        return exp != null ? XMLPrettyPrinter.safePrettyPrint(exp) : "";\r
+    }\r
+\r
+}\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+class XMLPrettyPrinter {\r
+\r
+    private static DOMImplementationRegistry registry;\r
+\r
+    static {\r
+        try {\r
+            registry = DOMImplementationRegistry.newInstance();\r
+        } catch (ClassCastException e) {\r
+            e.printStackTrace();\r
+        } catch (ClassNotFoundException e) {\r
+            e.printStackTrace();\r
+        } catch (InstantiationException e) {\r
+            e.printStackTrace();\r
+        } catch (IllegalAccessException e) {\r
+            e.printStackTrace();\r
+        }\r
+    }\r
+\r
+    public static Document parseDocument(String document) throws IOException, SAXException, ParserConfigurationException {\r
+        InputSource xmlSource = new InputSource(new StringReader(document)); \r
+        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();\r
+        dbFactory.setExpandEntityReferences(false);\r
+        dbFactory.setValidating(false);\r
+//        dbFactory.setFeature("http://xml.org/sax/features/validation", false);\r
+//        dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);\r
+        dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);\r
+//        dbFactory.setFeature("http://xml.org/sax/features/namespaces", false);\r
+//        dbFactory.setFeature("http://apache.org/xml/features/validation/schema", false);\r
+//        dbFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);\r
+        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();\r
+        return dBuilder.parse(xmlSource);\r
+    }\r
+\r
+    public static String prettyPrint(String document) throws IOException, SAXException, ParserConfigurationException {\r
+//        long start = System.nanoTime();\r
+        Document doc = parseDocument(document);\r
+\r
+        DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");\r
+        LSSerializer serializer = impl.createLSSerializer();\r
+        serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);\r
+        LSOutput lsOutput = impl.createLSOutput();\r
+        lsOutput.setEncoding("UTF-8");\r
+        StringWriter stringWriter = new StringWriter();\r
+        lsOutput.setCharacterStream(stringWriter);\r
+        serializer.setNewLine("\n");\r
+        serializer.write(doc, lsOutput);\r
+        String formatted = stringWriter.toString();\r
+\r
+//        long duration = System.nanoTime() - start;\r
+//        System.out.println("Pretty print took " + 1e-9*duration + "s.");\r
+//        System.out.println("original: " + document);\r
+//        System.out.println("formatted: " + formatted);\r
+\r
+        return formatted;\r
+    }\r
+\r
+    public static String safePrettyPrint(String document) {\r
+        try {\r
+            return prettyPrint(document);\r
+        } catch (Exception e) {\r
+            e.printStackTrace();\r
+            return "";\r
+        }\r
+    }\r
+\r
+}
\ No newline at end of file