X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fproperty%2Fsvg%2FSVGTextFactory.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fproperty%2Fsvg%2FSVGTextFactory.java;h=d67fc84eda7bd80d1f5d2e5f81af98f1ca28411b;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..d67fc84ed --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/property/svg/SVGTextFactory.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * 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 { + + @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 ""; + } + } + +} \ No newline at end of file