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=418af69f27d38334b5f7b38a984403793a785ef3;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=d67fc84eda7bd80d1f5d2e5f81af98f1ca28411b;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;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 index d67fc84ed..418af69f2 100644 --- 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 @@ -1,120 +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 ""; - } - } - +/******************************************************************************* + * 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