]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGTextFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / property / svg / SVGTextFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2011 Association for Decentralized Information Management in
3  * 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.modeling.ui.property.svg;
13
14 import java.io.IOException;
15 import java.io.StringReader;
16 import java.io.StringWriter;
17
18 import javax.xml.parsers.DocumentBuilder;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.parsers.ParserConfigurationException;
21
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;
35
36
37 /**
38  * @author Antti Villberg
39  */
40 public class SVGTextFactory extends ReadFactoryImpl<SVGInput, String> {
41
42     @Override
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) : "";
48     }
49
50 }
51
52 /**
53  * @author Antti Villberg
54  */
55 class XMLPrettyPrinter {
56
57     private static DOMImplementationRegistry registry;
58
59     static {
60         try {
61             registry = DOMImplementationRegistry.newInstance();
62         } catch (ClassCastException e) {
63             e.printStackTrace();
64         } catch (ClassNotFoundException e) {
65             e.printStackTrace();
66         } catch (InstantiationException e) {
67             e.printStackTrace();
68         } catch (IllegalAccessException e) {
69             e.printStackTrace();
70         }
71     }
72
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);
86     }
87
88     public static String prettyPrint(String document) throws IOException, SAXException, ParserConfigurationException {
89 //        long start = System.nanoTime();
90         Document doc = parseDocument(document);
91
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();
102
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);
107
108         return formatted;
109     }
110
111     public static String safePrettyPrint(String document) {
112         try {
113             return prettyPrint(document);
114         } catch (Exception e) {
115             e.printStackTrace();
116             return "";
117         }
118     }
119
120 }