From 95836987d6cef1a80035a685057567a4285c9f1a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 16 Feb 2016 22:30:30 +0000 Subject: Wire up relationships and note reading --- src/main/java/org/zwobble/mammoth/Mammoth.java | 69 +++++++++++++++++++------- 1 file changed, 52 insertions(+), 17 deletions(-) (limited to 'src/main/java/org/zwobble/mammoth/Mammoth.java') diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 5830c6a..1e14482 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -4,6 +4,7 @@ import org.zwobble.mammoth.documents.Notes; import org.zwobble.mammoth.docx.*; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.results.Result; +import org.zwobble.mammoth.util.MammothLists; import org.zwobble.mammoth.xml.XmlElement; import java.io.File; @@ -11,27 +12,38 @@ import java.io.IOException; import java.util.Optional; import java.util.zip.ZipFile; +import static org.zwobble.mammoth.util.MammothLists.list; + public class Mammoth { + @FunctionalInterface + private interface BodyReaders { + BodyXmlReader forName(String name); + } + + // TODO: provide Options builder public static Result convertToHtml(File file) { + // TODO: prefix + String idPrefix = "document"; + return convertToHtml(file, idPrefix); + } + + public static Result convertToHtml(File file, String idPrefix) { try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { Styles styles = readStyles(zipFile); Numbering numbering = Numbering.EMPTY; - Relationships relationships = Relationships.EMPTY; ContentTypes contentTypes = ContentTypes.DEFAULT; - Notes notes = Notes.EMPTY; FileReader fileReader = uri -> { throw new UnsupportedOperationException(); }; - DocumentXmlReader reader = new DocumentXmlReader(new BodyXmlReader( - styles, - numbering, - relationships, - contentTypes, - zipFile, - fileReader), notes); - // TODO: prefix - String idPrefix = "document"; - return reader.readElement(parseOfficeXml(zipFile, "word/document.xml")) + BodyReaders bodyReaders = name -> { + Relationships relationships = readRelationships(zipFile, name); + return new BodyXmlReader(styles, numbering, relationships, contentTypes, zipFile, fileReader); + }; + return readNotes(zipFile, bodyReaders) + .flatMap(notes -> { + DocumentXmlReader reader = new DocumentXmlReader(bodyReaders.forName("document"), notes); + return reader.readElement(parseOfficeXml(zipFile, "word/document.xml")); + }) .map(nodes -> DocumentConverter.convertToHtml(idPrefix, nodes)) .map(Html::stripEmpty) .map(Html::collapse) @@ -41,18 +53,41 @@ public class Mammoth { } } - private static Styles readStyles(DocxFile file) throws IOException { + private static Result readNotes(DocxFile file, BodyReaders bodyReaders) { + return Result.map( + tryParseOfficeXml(file, "word/footnotes.xml") + .map(NotesXmlReader.footnote(bodyReaders.forName("footnotes"))::readElement) + .orElse(Result.success(list())), + tryParseOfficeXml(file, "word/endnotes.xml") + .map(NotesXmlReader.endnote(bodyReaders.forName("endnotes"))::readElement) + .orElse(Result.success(list())), + MammothLists::concat).map(Notes::new); + } + + private static Styles readStyles(DocxFile file) { return tryParseOfficeXml(file, "word/styles.xml") .map(StylesXml::readStylesXmlElement) .orElse(Styles.EMPTY); } - private static Optional tryParseOfficeXml(DocxFile zipFile, String name) throws IOException { - return zipFile.tryGetInputStream(name).map(OfficeXml::parseXml); + private static Relationships readRelationships(DocxFile zipFile, String name) { + return tryParseOfficeXml(zipFile, "word/_rels/" + name + ".xml.rels") + .map(RelationshipsXml::readRelationshipsXmlElement) + .orElse(Relationships.EMPTY); + } + + private static Optional tryParseOfficeXml(DocxFile zipFile, String name) { + try { + return zipFile.tryGetInputStream(name).map(OfficeXml::parseXml); + } catch (IOException exception) { + // TODO: wrap in more specific exception and catch at the top + throw new RuntimeException(exception); + } } - private static XmlElement parseOfficeXml(DocxFile zipFile, String name) throws IOException { + private static XmlElement parseOfficeXml(DocxFile zipFile, String name) { return tryParseOfficeXml(zipFile, name) - .orElseThrow(() -> new IOException("Missing entry in file: " + name)); + // TODO: wrap in more specific exception and catch at the top + .orElseThrow(() -> new RuntimeException(new IOException("Missing entry in file: " + name))); } } -- cgit v1.2.3