1 /*******************************************************************************
2 * Copyright (c) 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.property.svg;
14 import java.io.IOException;
15 import java.io.StringReader;
16 import java.io.StringWriter;
18 import javax.xml.parsers.DocumentBuilder;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.parsers.ParserConfigurationException;
22 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.diagram.stubs.G2DResource;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
30 import org.w3c.dom.ls.DOMImplementationLS;
31 import org.w3c.dom.ls.LSOutput;
32 import org.w3c.dom.ls.LSSerializer;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
38 * @author Antti Villberg
40 public class SVGTextFactory extends ReadFactoryImpl<SVGInput, String> {
43 public String perform(ReadGraph graph, SVGInput input) throws DatabaseException {
44 G2DResource G2D = G2DResource.getInstance(graph);
45 Resource element = input.getDatum();
46 String exp = graph.getPossibleRelatedValue(element, G2D.HasSVGDocument, Bindings.STRING);
47 return exp != null ? XMLPrettyPrinter.safePrettyPrint(exp) : "";
53 * @author Antti Villberg
55 class XMLPrettyPrinter {
57 private static DOMImplementationRegistry registry;
61 registry = DOMImplementationRegistry.newInstance();
62 } catch (ClassCastException e) {
64 } catch (ClassNotFoundException e) {
66 } catch (InstantiationException e) {
68 } catch (IllegalAccessException e) {
73 public static Document parseDocument(String document) throws IOException, SAXException, ParserConfigurationException {
74 InputSource xmlSource = new InputSource(new StringReader(document));
75 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
76 dbFactory.setExpandEntityReferences(false);
77 dbFactory.setValidating(false);
78 // dbFactory.setFeature("http://xml.org/sax/features/validation", false);
79 // dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
80 dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
81 // dbFactory.setFeature("http://xml.org/sax/features/namespaces", false);
82 // dbFactory.setFeature("http://apache.org/xml/features/validation/schema", false);
83 // dbFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
84 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
85 return dBuilder.parse(xmlSource);
88 public static String prettyPrint(String document) throws IOException, SAXException, ParserConfigurationException {
89 // long start = System.nanoTime();
90 Document doc = parseDocument(document);
92 DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
93 LSSerializer serializer = impl.createLSSerializer();
94 serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
95 LSOutput lsOutput = impl.createLSOutput();
96 lsOutput.setEncoding("UTF-8");
97 StringWriter stringWriter = new StringWriter();
98 lsOutput.setCharacterStream(stringWriter);
99 serializer.setNewLine("\n");
100 serializer.write(doc, lsOutput);
101 String formatted = stringWriter.toString();
103 // long duration = System.nanoTime() - start;
104 // System.out.println("Pretty print took " + 1e-9*duration + "s.");
105 // System.out.println("original: " + document);
106 // System.out.println("formatted: " + formatted);
111 public static String safePrettyPrint(String document) {
113 return prettyPrint(document);
114 } catch (Exception e) {