From d4ac19bac1d0e1315d129c39de56121def95007b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 24 Feb 2016 20:57:33 +0000 Subject: Rename Mammoth to DocumentConverter --- .../org/zwobble/mammoth/DocumentConverter.java | 108 +++++++++++++++++++++ src/main/java/org/zwobble/mammoth/Mammoth.java | 108 --------------------- .../org/zwobble/mammoth/tests/MammothTests.java | 16 +-- 3 files changed, 116 insertions(+), 116 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/DocumentConverter.java delete mode 100644 src/main/java/org/zwobble/mammoth/Mammoth.java (limited to 'src') diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java new file mode 100644 index 0000000..31464e7 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -0,0 +1,108 @@ +package org.zwobble.mammoth; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; +import org.zwobble.mammoth.internal.DocumentToHtml; +import org.zwobble.mammoth.internal.DocumentToHtmlOptions; +import org.zwobble.mammoth.internal.documents.DocumentElement; +import org.zwobble.mammoth.internal.documents.HasChildren; +import org.zwobble.mammoth.internal.documents.Paragraph; +import org.zwobble.mammoth.internal.documents.Text; +import org.zwobble.mammoth.internal.docx.DocxFile; +import org.zwobble.mammoth.internal.docx.InMemoryDocxFile; +import org.zwobble.mammoth.internal.docx.ZippedDocxFile; +import org.zwobble.mammoth.internal.html.Html; +import org.zwobble.mammoth.internal.styles.DefaultStyles; +import org.zwobble.mammoth.internal.util.Casts; +import org.zwobble.mammoth.results.Result; + +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; + +import static org.zwobble.mammoth.internal.docx.DocumentReader.readDocument; +import static org.zwobble.mammoth.internal.util.MammothLists.list; + +public class DocumentConverter { + private final DocumentToHtmlOptions options; + + public DocumentConverter() { + this(DocumentToHtmlOptions.DEFAULT.addStyleMap(DefaultStyles.DEFAULT_STYLE_MAP)); + } + + private DocumentConverter(DocumentToHtmlOptions options) { + this.options = options; + } + + public DocumentConverter idPrefix(String idPrefix) { + return new DocumentConverter(options.idPrefix(idPrefix)); + } + + public DocumentConverter preserveEmptyParagraphs() { + return new DocumentConverter(options.preserveEmptyParagraphs()); + } + + public DocumentConverter addStyleMap(String styleMap) { + return new DocumentConverter(options.addStyleMap(styleMap)); + } + + public Result convertToHtml(InputStream stream) throws IOException { + return withDocxFile(stream, zipFile -> + convertToHtml(Optional.empty(), zipFile)); + } + + public Result convertToHtml(File file) throws IOException { + return withDocxFile(file, zipFile -> + convertToHtml(Optional.of(file.toPath()), zipFile)); + } + + private Result convertToHtml(Optional path, DocxFile zipFile) { + return readDocument(path, zipFile) + .flatMap(nodes -> DocumentToHtml.convertToHtml(nodes, options)) + .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(DocumentConverter::extractRawTextOfChildren)); + } + + private static T withDocxFile(File file, Function function) throws IOException { + try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { + return function.apply(zipFile); + } + } + + private static T withDocxFile(InputStream stream, Function function) throws IOException { + try (DocxFile zipFile = InMemoryDocxFile.fromStream(stream)) { + return function.apply(zipFile); + } + } + + private static String extractRawTextOfChildren(HasChildren parent) { + return extractRawText(parent.getChildren()); + } + + private static String extractRawText(List nodes) { + return String.join("", Iterables.transform(nodes, node -> extractRawText(node))); + } + + private static String extractRawText(DocumentElement node) { + return Casts.tryCast(Text.class, node) + .map(Text::getValue) + .orElseGet(() -> { + List children = Casts.tryCast(HasChildren.class, node) + .map(HasChildren::getChildren) + .orElse(list()); + String suffix = node instanceof Paragraph ? "\n\n" : ""; + return extractRawText(children) + suffix; + }); + } +} diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java deleted file mode 100644 index e979afc..0000000 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.zwobble.mammoth; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import org.zwobble.mammoth.internal.DocumentToHtml; -import org.zwobble.mammoth.internal.DocumentToHtmlOptions; -import org.zwobble.mammoth.internal.documents.DocumentElement; -import org.zwobble.mammoth.internal.documents.HasChildren; -import org.zwobble.mammoth.internal.documents.Paragraph; -import org.zwobble.mammoth.internal.documents.Text; -import org.zwobble.mammoth.internal.docx.DocxFile; -import org.zwobble.mammoth.internal.docx.InMemoryDocxFile; -import org.zwobble.mammoth.internal.docx.ZippedDocxFile; -import org.zwobble.mammoth.internal.html.Html; -import org.zwobble.mammoth.internal.styles.DefaultStyles; -import org.zwobble.mammoth.internal.util.Casts; -import org.zwobble.mammoth.results.Result; - -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; - -import static org.zwobble.mammoth.internal.docx.DocumentReader.readDocument; -import static org.zwobble.mammoth.internal.util.MammothLists.list; - -public class Mammoth { - private final DocumentToHtmlOptions options; - - public Mammoth() { - this(DocumentToHtmlOptions.DEFAULT.addStyleMap(DefaultStyles.DEFAULT_STYLE_MAP)); - } - - private Mammoth(DocumentToHtmlOptions options) { - this.options = options; - } - - public Mammoth idPrefix(String idPrefix) { - return new Mammoth(options.idPrefix(idPrefix)); - } - - public Mammoth preserveEmptyParagraphs() { - return new Mammoth(options.preserveEmptyParagraphs()); - } - - public Mammoth addStyleMap(String styleMap) { - return new Mammoth(options.addStyleMap(styleMap)); - } - - public Result convertToHtml(InputStream stream) throws IOException { - return withDocxFile(stream, zipFile -> - convertToHtml(Optional.empty(), zipFile)); - } - - public Result convertToHtml(File file) throws IOException { - return withDocxFile(file, zipFile -> - convertToHtml(Optional.of(file.toPath()), zipFile)); - } - - private Result convertToHtml(Optional path, DocxFile zipFile) { - return readDocument(path, zipFile) - .flatMap(nodes -> DocumentToHtml.convertToHtml(nodes, options)) - .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) throws IOException { - try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { - return function.apply(zipFile); - } - } - - private static T withDocxFile(InputStream stream, Function function) throws IOException { - try (DocxFile zipFile = InMemoryDocxFile.fromStream(stream)) { - return function.apply(zipFile); - } - } - - private static String extractRawTextOfChildren(HasChildren parent) { - return extractRawText(parent.getChildren()); - } - - private static String extractRawText(List nodes) { - return String.join("", Iterables.transform(nodes, node -> extractRawText(node))); - } - - private static String extractRawText(DocumentElement node) { - return Casts.tryCast(Text.class, node) - .map(Text::getValue) - .orElseGet(() -> { - List children = Casts.tryCast(HasChildren.class, node) - .map(HasChildren::getChildren) - .orElse(list()); - String suffix = node instanceof Paragraph ? "\n\n" : ""; - return extractRawText(children) + suffix; - }); - } -} diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 288c6a2..efb95f0 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -1,7 +1,7 @@ package org.zwobble.mammoth.tests; import org.junit.Test; -import org.zwobble.mammoth.Mammoth; +import org.zwobble.mammoth.DocumentConverter; import org.zwobble.mammoth.results.Result; import java.io.File; @@ -79,7 +79,7 @@ public class MammothTests { Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png")); assertThat( - new Mammoth().convertToHtml(documentPath.toFile()), + new DocumentConverter().convertToHtml(documentPath.toFile()), deepEquals(success("

"))); } finally { tempDirectory.toFile().delete(); @@ -93,7 +93,7 @@ public class MammothTests { Path documentPath = tempDirectory.resolve("external-picture.docx"); Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); assertThat( - new Mammoth().convertToHtml(documentPath.toUri().toURL().openStream()), + new DocumentConverter().convertToHtml(documentPath.toUri().toURL().openStream()), allOf( hasProperty("value", equalTo("")), hasProperty("warnings", contains( @@ -110,7 +110,7 @@ public class MammothTests { Path documentPath = tempDirectory.resolve("external-picture.docx"); Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); assertThat( - new Mammoth().convertToHtml(documentPath.toFile()), + new DocumentConverter().convertToHtml(documentPath.toFile()), allOf( hasProperty("value", equalTo("")), hasProperty("warnings", contains( @@ -191,17 +191,17 @@ public class MammothTests { @Test public void canExtractRawText() throws IOException { assertThat( - Mammoth.extractRawText(TestData.file("simple-list.docx")), + DocumentConverter.extractRawText(TestData.file("simple-list.docx")), deepEquals(success("Apple\n\nBanana\n\n"))); } private Result convertToHtml(String name) throws IOException { File file = TestData.file(name); - return new Mammoth().convertToHtml(file); + return new DocumentConverter().convertToHtml(file); } - private Result convertToHtml(String name, Function configure) throws IOException { + private Result convertToHtml(String name, Function configure) throws IOException { File file = TestData.file(name); - return configure.apply(new Mammoth()).convertToHtml(file); + return configure.apply(new DocumentConverter()).convertToHtml(file); } } -- cgit v1.2.3