From b2fa6ec2d570a54ac67d3145cdeb6f6e710dc759 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Wed, 19 Feb 2020 21:05:58 +0100 Subject: [PATCH] Dummy files for RDF output --- .../writer2latex/rdf/ContainerWriter.java | 51 +++++++++++++++ .../java/writer2latex/rdf/RDFConverter.java | 63 +++++++++++++++++++ src/main/java/writer2latex/rdf/RDFWriter.java | 59 +++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 src/main/java/writer2latex/rdf/ContainerWriter.java create mode 100644 src/main/java/writer2latex/rdf/RDFConverter.java create mode 100644 src/main/java/writer2latex/rdf/RDFWriter.java diff --git a/src/main/java/writer2latex/rdf/ContainerWriter.java b/src/main/java/writer2latex/rdf/ContainerWriter.java new file mode 100644 index 0000000..0784541 --- /dev/null +++ b/src/main/java/writer2latex/rdf/ContainerWriter.java @@ -0,0 +1,51 @@ +package writer2latex.rdf; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.DOMImplementation; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentType; +import org.w3c.dom.Element; + +import writer2latex.base.DOMDocument; + +/** This class creates the required META-INF/container.xml file for an EPUB package + * (see http://www.idpf.org/ocf/ocf1.0/download/ocf10.htm). + */ +public class ContainerWriter extends DOMDocument { + + public ContainerWriter() { + super("container", "xml"); + + // create DOM + Document contentDOM = null; + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + DOMImplementation domImpl = builder.getDOMImplementation(); + DocumentType doctype = domImpl.createDocumentType("container","",""); + contentDOM = domImpl.createDocument("urn:oasis:names:tc:opendocument:xmlns:container","container",doctype); + } + catch (ParserConfigurationException t) { // this should never happen + throw new RuntimeException(t); + } + + // Populate the DOM tree + Element container = contentDOM.getDocumentElement(); + container.setAttribute("version", "1.0"); + container.setAttribute("xmlns","urn:oasis:names:tc:opendocument:xmlns:container"); + + Element rootfiles = contentDOM.createElement("rootfiles"); + container.appendChild(rootfiles); + + Element rootfile = contentDOM.createElement("rootfile"); + rootfile.setAttribute("full-path", "OEBPS/book.opf"); + rootfile.setAttribute("media-type", "application/oebps-package+xml"); + rootfiles.appendChild(rootfile); + + setContentDOM(contentDOM); + } + +} diff --git a/src/main/java/writer2latex/rdf/RDFConverter.java b/src/main/java/writer2latex/rdf/RDFConverter.java new file mode 100644 index 0000000..6408a2f --- /dev/null +++ b/src/main/java/writer2latex/rdf/RDFConverter.java @@ -0,0 +1,63 @@ +package writer2latex.rdf; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +import org.w3c.dom.Element; + +import writer2latex.api.ConverterResult; +import writer2latex.api.OutputFile; +import writer2latex.base.ConverterResultImpl; +import writer2latex.epub.EPUBWriter; +import writer2latex.xhtml.Html5Converter; +import writer2latex.xhtml.Xhtml11Converter; +import writer2latex.xhtml.XhtmlDocument; + + +public final class RDFConverter extends Xhtml11Converter { + + // Constructor + public RDFConverter() { + super(); + this.isRDF = true; + } + + @Override public ConverterResult convert(InputStream is, String sTargetFileName) throws IOException { + setOpenPubStructure(false); + ConverterResult xhtmlResult = super.convert(is, "chapter"); + Iterator excerptIterator = this.outFiles.iterator(); + while (excerptIterator.hasNext()) { + XhtmlDocument excerptDoc = excerptIterator.next(); + Element excerptContentNode = excerptDoc.getContentNode(); + String exPath = excerptContentNode.getAttribute("path"); + System.out.println(exPath); + } + /* + * Iterator iterator = xhtmlResult.iterator(); + * while(iterator.hasNext()) { OutputFile of = iterator.next(); + * System.out.println(of.getFileName()); } + */ + return createPackage(xhtmlResult,sTargetFileName); + } + + @Override public ConverterResult convert(org.w3c.dom.Document dom, String sTargetFileName, boolean bDestructive) throws IOException { + ConverterResult xhtmlResult = super.convert(dom, "chapter", bDestructive); + //System.out.println(this.outFiles.size()); + /* + * Iterator iterator = xhtmlResult.iterator(); + * while(iterator.hasNext()) { OutputFile outfile = iterator.next(); + * System.out.println(outfile.isMasterDocument() + outfile.getFileName()); } + */ + return createPackage(xhtmlResult,sTargetFileName); + } + + private ConverterResult createPackage(ConverterResult xhtmlResult, String sTargetFileName) { + ConverterResultImpl rdfResult = new ConverterResultImpl(); + RDFWriter rdfWriter = new RDFWriter(xhtmlResult,sTargetFileName,3,getXhtmlConfig()); + rdfResult.addDocument(rdfWriter); + rdfResult.setMetaData(xhtmlResult.getMetaData()); + return rdfResult; + } + + } \ No newline at end of file diff --git a/src/main/java/writer2latex/rdf/RDFWriter.java b/src/main/java/writer2latex/rdf/RDFWriter.java new file mode 100644 index 0000000..e6ac21f --- /dev/null +++ b/src/main/java/writer2latex/rdf/RDFWriter.java @@ -0,0 +1,59 @@ +package writer2latex.rdf; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Iterator; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import writer2latex.api.ConverterResult; +import writer2latex.api.OutputFile; +import writer2latex.util.Misc; +import writer2latex.xhtml.XhtmlConfig; + + +public class RDFWriter implements OutputFile { + + + private ConverterResult xhtmlResult; + private String sFileName; + private int nVersion; + private XhtmlConfig config; + + + public RDFWriter(ConverterResult xhtmlResult, String sFileName, int nVersion, XhtmlConfig config) { + this.xhtmlResult = xhtmlResult; + this.sFileName = Misc.removeExtension(sFileName); + this.nVersion = nVersion; + this.config = config; + } + + @Override public String getFileName() { + return sFileName+".rdf"; + } + + @Override public String getMIMEType() { + return "application/epub+zip"; + } + + @Override public boolean isMasterDocument() { + return true; + } + + @Override public boolean containsMath() { + // We don't really care about this + return nVersion==3; + } + + @Override public void write(OutputStream os) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + // Finally XHTML content + Iterator iter = xhtmlResult.iterator(); + while (iter.hasNext()) { + OutputFile excerptFile = iter.next(); + } + + } +}