From 2058006d34b708cc873cce1b36589d59298a37b0 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Fri, 28 Feb 2020 13:56:32 +0100 Subject: [PATCH] HTML conversion for RDF from xml. --- .../java/writer2latex/rdf/DocumentPart.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/main/java/writer2latex/rdf/DocumentPart.java b/src/main/java/writer2latex/rdf/DocumentPart.java index 70171a1..d6e73fe 100644 --- a/src/main/java/writer2latex/rdf/DocumentPart.java +++ b/src/main/java/writer2latex/rdf/DocumentPart.java @@ -1,5 +1,16 @@ package writer2latex.rdf; +import java.io.StringWriter; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Result; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -78,15 +89,31 @@ public class DocumentPart { Element excerptContentNode = excerptDoc.getContentNode(); StringBuilder bodyBuilder; Document document = excerptContentNode.getOwnerDocument(); - DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); - LSSerializer serializer = domImplLS.createLSSerializer(); - serializer.getDomConfig().setParameter("xml-declaration", false); + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = null; + try { + transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.METHOD, "html"); + } catch (TransformerConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + bodyBuilder = new StringBuilder(); NodeList excerptContentNodes = excerptContentNode.getChildNodes(); int i = 0; while (excerptContentNodes.getLength() > i) { Node child = excerptContentNodes.item(i); - bodyBuilder.append(serializer.writeToString(child)); + DOMSource source = new DOMSource(child); + StringWriter sw = new StringWriter(); + Result streamResult = new StreamResult(sw); + try { + transformer.transform(source, streamResult ); + } catch (TransformerException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + bodyBuilder.append(sw.toString()); i++; }