From b9ccb58ba0e6c0557d18f45a52a6a216177dbd56 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 25 Mar 2016 11:18:02 +0000 Subject: Add extractRawText for streams, as documented --- .../java/org/zwobble/mammoth/DocumentConverter.java | 18 ++++++++++++++++-- .../java/org/zwobble/mammoth/tests/MammothTests.java | 9 ++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index 557adc6..94fb09c 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -91,6 +91,16 @@ public class DocumentConverter { .map(Html::write); } + /** + * Extract the raw text of the document. + * This will ignore all formatting in the document. + * Each paragraph is followed by two newlines. + */ + public Result extractRawText(InputStream stream) throws IOException { + return withDocxFile(stream, zipFile -> + extractRawText(Optional.empty(), zipFile)); + } + /** * Extract the raw text of the document. * This will ignore all formatting in the document. @@ -98,8 +108,12 @@ public class DocumentConverter { */ public Result extractRawText(File file) throws IOException { return withDocxFile(file, zipFile -> - readDocument(Optional.of(file.toPath()), zipFile) - .map(DocumentConverter::extractRawTextOfChildren)); + extractRawText(Optional.of(file.toPath()), zipFile)); + } + + private InternalResult extractRawText(Optional path, DocxFile zipFile) { + return readDocument(path, zipFile) + .map(DocumentConverter::extractRawTextOfChildren); } private static T withDocxFile(File file, Function function) throws IOException { diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 0ea0e90..7dc373c 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -200,12 +200,19 @@ public class MammothTests { } @Test - public void canExtractRawText() throws IOException { + public void canExtractRawTextFromFile() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx")), deepEquals(success("Apple\n\nBanana\n\n"))); } + @Test + public void canExtractRawTextFromStream() throws IOException { + assertThat( + new DocumentConverter().extractRawText(TestData.file("simple-list.docx").toURI().toURL().openStream()), + deepEquals(success("Apple\n\nBanana\n\n"))); + } + private Result convertToHtml(String name) throws IOException { File file = TestData.file(name); return new DocumentConverter().convertToHtml(file); -- cgit v1.2.3