aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java27
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/DocumentReader.java38
2 files changed, 37 insertions, 28 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index 6017825..7c1d9c8 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -1,16 +1,21 @@
package org.zwobble.mammoth;
+import com.google.common.base.Function;
import com.google.common.collect.Iterables;
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.docx.DocxFile;
+import org.zwobble.mammoth.docx.ZippedDocxFile;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.results.Result;
import org.zwobble.mammoth.util.Casts;
import java.io.File;
+import java.io.IOException;
import java.util.List;
+import java.util.zip.ZipFile;
import static org.zwobble.mammoth.docx.DocumentReader.readDocument;
import static org.zwobble.mammoth.util.MammothLists.list;
@@ -41,15 +46,25 @@ public class Mammoth {
}
public static Result<String> convertToHtml(File file, Options options) {
- return readDocument(file)
- .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes))
- .map(Html::stripEmpty)
- .map(Html::collapse)
- .map(Html::write);
+ return withDocxFile(file, zipFile ->
+ readDocument(zipFile)
+ .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes))
+ .map(Html::stripEmpty)
+ .map(Html::collapse)
+ .map(Html::write));
}
public static Result<String> extractRawText(File file) {
- return readDocument(file).map(Mammoth::extractRawTextOfChildren);
+ return withDocxFile(file, zipFile ->
+ readDocument(zipFile).map(Mammoth::extractRawTextOfChildren));
+ }
+
+ private static <T> T withDocxFile(File file, Function<DocxFile, T> function) {
+ try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
+ return function.apply(zipFile);
+ } catch (IOException e) {
+ throw new UnsupportedOperationException("Should return a result of failure");
+ }
}
private static String extractRawTextOfChildren(HasChildren parent) {
diff --git a/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java b/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java
index e361ac3..6d5af48 100644
--- a/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java
+++ b/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java
@@ -6,10 +6,8 @@ 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;
@@ -19,26 +17,22 @@ public class DocumentReader {
BodyXmlReader forName(String name);
}
- public static Result<Document> 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");
- }
+ public static Result<Document> readDocument(DocxFile zipFile) {
+ 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"));
+ });
}
private static Result<Notes> readNotes(DocxFile file, BodyReaders bodyReaders) {