diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-03-25 11:18:02 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-03-25 11:18:02 +0000 |
| commit | b9ccb58ba0e6c0557d18f45a52a6a216177dbd56 (patch) | |
| tree | f989282769cb2e2535bb63f183d7acc797221d8c /src | |
| parent | de4c76d2eca62b38b0d84e3292a3fd343ff378ca (diff) | |
Add extractRawText for streams, as documented
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/DocumentConverter.java | 18 | ||||
| -rw-r--r-- | src/test/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 @@ -96,10 +96,24 @@ public class DocumentConverter { * This will ignore all formatting in the document. * Each paragraph is followed by two newlines. */ + public Result<String> 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. + * Each paragraph is followed by two newlines. + */ public Result<String> 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<String> extractRawText(Optional<Path> path, DocxFile zipFile) { + return readDocument(path, zipFile) + .map(DocumentConverter::extractRawTextOfChildren); } private static <T> T withDocxFile(File file, Function<DocxFile, T> 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<String> convertToHtml(String name) throws IOException { File file = TestData.file(name); return new DocumentConverter().convertToHtml(file); |
