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