From e7bbba7e069c1e55e9267bac3d7f23cb7bbeac01 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 20 Feb 2016 11:27:26 +0000 Subject: Move document reading into its own class --- src/main/java/org/zwobble/mammoth/Mammoth.java | 82 ++-------------------- .../org/zwobble/mammoth/docx/DocumentReader.java | 81 +++++++++++++++++++++ 2 files changed, 88 insertions(+), 75 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/docx/DocumentReader.java (limited to 'src/main/java/org/zwobble') diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 6e4766e..6017825 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -1,20 +1,18 @@ package org.zwobble.mammoth; import com.google.common.collect.Iterables; -import org.zwobble.mammoth.documents.*; -import org.zwobble.mammoth.docx.*; +import org.zwobble.mammoth.documents.DocumentElement; +import org.zwobble.mammoth.documents.HasChildren; +import org.zwobble.mammoth.documents.Paragraph; +import org.zwobble.mammoth.documents.Text; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.util.Casts; -import org.zwobble.mammoth.util.MammothLists; -import org.zwobble.mammoth.xml.XmlElement; import java.io.File; -import java.io.IOException; import java.util.List; -import java.util.Optional; -import java.util.zip.ZipFile; +import static org.zwobble.mammoth.docx.DocumentReader.readDocument; import static org.zwobble.mammoth.util.MammothLists.list; public class Mammoth { @@ -38,17 +36,12 @@ public class Mammoth { } } - @FunctionalInterface - private interface BodyReaders { - BodyXmlReader forName(String name); - } - public static Result convertToHtml(File file) { return convertToHtml(file, Options.DEFAULT); } public static Result convertToHtml(File file, Options options) { - return readDocument(file, options) + return readDocument(file) .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes)) .map(Html::stripEmpty) .map(Html::collapse) @@ -56,8 +49,7 @@ public class Mammoth { } public static Result extractRawText(File file) { - return readDocument(file, Options.DEFAULT) - .map(document -> extractRawTextOfChildren(document)); + return readDocument(file).map(Mammoth::extractRawTextOfChildren); } private static String extractRawTextOfChildren(HasChildren parent) { @@ -79,64 +71,4 @@ public class Mammoth { return extractRawText(children) + suffix; }); } - - private static Result readDocument(File file, Options options) { - try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { - Styles styles = readStyles(zipFile); - Numbering numbering = Numbering.EMPTY; - ContentTypes contentTypes = ContentTypes.DEFAULT; - FileReader fileReader = uri -> { - throw new UnsupportedOperationException(); - }; - 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")); - }); - } catch (IOException e) { - throw new UnsupportedOperationException("Should return a result of failure"); - } - } - - 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 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) { - return tryParseOfficeXml(zipFile, name) - // TODO: wrap in more specific exception and catch at the top - .orElseThrow(() -> new RuntimeException(new IOException("Missing entry in file: " + name))); - } } diff --git a/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java b/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java new file mode 100644 index 0000000..e361ac3 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java @@ -0,0 +1,81 @@ +package org.zwobble.mammoth.docx; + +import org.zwobble.mammoth.documents.Document; +import org.zwobble.mammoth.documents.Notes; +import org.zwobble.mammoth.results.Result; +import org.zwobble.mammoth.util.MammothLists; +import org.zwobble.mammoth.xml.XmlElement; + +import java.io.File; +import java.io.IOException; +import java.util.Optional; +import java.util.zip.ZipFile; + +import static org.zwobble.mammoth.util.MammothLists.list; + +public class DocumentReader { + @FunctionalInterface + private interface BodyReaders { + BodyXmlReader forName(String name); + } + + public static Result readDocument(File file) { + try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { + Styles styles = readStyles(zipFile); + Numbering numbering = Numbering.EMPTY; + ContentTypes contentTypes = ContentTypes.DEFAULT; + FileReader fileReader = uri -> { + throw new UnsupportedOperationException(); + }; + 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")); + }); + } catch (IOException e) { + throw new UnsupportedOperationException("Should return a result of failure"); + } + } + + 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 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) { + return tryParseOfficeXml(zipFile, 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