From c1604e510cc831a00f6105df35c68a856f061b65 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 21 Feb 2016 09:44:16 +0000 Subject: Allow input streams to be read --- src/main/java/org/zwobble/mammoth/Mammoth.java | 42 +++++++++++++++++++------- 1 file changed, 31 insertions(+), 11 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 93c639a..4963c3a 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -7,6 +7,7 @@ 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.InMemoryDocxFile; import org.zwobble.mammoth.docx.ZippedDocxFile; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.results.Result; @@ -18,6 +19,8 @@ import org.zwobble.mammoth.util.Casts; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; import java.util.List; import java.util.Optional; import java.util.zip.ZipFile; @@ -61,30 +64,47 @@ public class Mammoth { .mapParagraph(ParagraphMatcher.styleName("Normal"), HtmlPath.element("p")) .build(); - public static Result convertToHtml(File file) { + public static Result convertToHtml(InputStream stream) throws IOException { + return convertToHtml(stream, Options.DEFAULT); + } + + public static Result convertToHtml(InputStream stream, Options options) throws IOException { + return withDocxFile(stream, zipFile -> + convertToHtml(Optional.empty(), zipFile, options)); + } + + public static Result convertToHtml(File file) throws IOException { return convertToHtml(file, Options.DEFAULT); } - public static Result convertToHtml(File file, Options options) { + public static Result convertToHtml(File file, Options options) throws IOException { return withDocxFile(file, zipFile -> - readDocument(Optional.of(file.toPath()), zipFile) - .flatMap(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, DEFAULT_STYLE_MAP, nodes)) - .map(Html::stripEmpty) - .map(Html::collapse) - .map(Html::write)); + convertToHtml(Optional.of(file.toPath()), zipFile, options)); } - public static Result extractRawText(File file) { + private static Result convertToHtml(Optional path, DocxFile zipFile, Options options) { + return readDocument(path, zipFile) + .flatMap(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, DEFAULT_STYLE_MAP, nodes)) + .map(Html::stripEmpty) + .map(Html::collapse) + .map(Html::write); + } + + public static Result extractRawText(File file) throws IOException { return withDocxFile(file, zipFile -> readDocument(Optional.of(file.toPath()), zipFile) .map(Mammoth::extractRawTextOfChildren)); } - private static T withDocxFile(File file, Function function) { + private static T withDocxFile(File file, Function function) throws IOException { 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 T withDocxFile(InputStream stream, Function function) throws IOException { + try (DocxFile zipFile = InMemoryDocxFile.fromStream(stream)) { + return function.apply(zipFile); } } -- cgit v1.2.3