From 52140d52210eda0f25ecab9f89b5c73473e9d8e0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Apr 2016 18:57:29 +0100 Subject: Add CLI for .NET version --- .../org/zwobble/mammoth/DocumentConverter.java | 83 ++-------------------- 1 file changed, 5 insertions(+), 78 deletions(-) (limited to 'src/main/java/org/zwobble/mammoth/DocumentConverter.java') diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index f7112f4..3a21fed 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -1,31 +1,11 @@ package org.zwobble.mammoth; -import org.zwobble.mammoth.internal.conversion.DocumentToHtml; +import org.zwobble.mammoth.internal.InternalDocumentConverter; import org.zwobble.mammoth.internal.conversion.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.results.InternalResult; -import org.zwobble.mammoth.internal.util.Casts; -import org.zwobble.mammoth.internal.util.PassThroughException; 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.function.Function; -import java.util.zip.ZipFile; - -import static org.zwobble.mammoth.internal.docx.DocumentReader.readDocument; -import static org.zwobble.mammoth.internal.util.Iterables.lazyMap; -import static org.zwobble.mammoth.internal.util.Lists.list; public class DocumentConverter { private final DocumentToHtmlOptions options; @@ -77,26 +57,14 @@ public class DocumentConverter { * means that relative paths to other files, such as images, cannot be resolved. */ public Result convertToHtml(InputStream stream) throws IOException { - return PassThroughException.unwrap(() -> - withDocxFile(stream, zipFile -> - convertToHtml(Optional.empty(), zipFile)).toResult()); + return new InternalDocumentConverter(options).convertToHtml(stream).toResult(); } /** * Converts {@code file} into an HTML string. */ public Result convertToHtml(File file) throws IOException { - return PassThroughException.unwrap(() -> - withDocxFile(file, zipFile -> - convertToHtml(Optional.of(file.toPath()), zipFile)).toResult()); - } - - private InternalResult convertToHtml(Optional path, DocxFile zipFile) { - return readDocument(path, zipFile) - .flatMap(nodes -> DocumentToHtml.convertToHtml(nodes, options)) - .map(Html::stripEmpty) - .map(Html::collapse) - .map(Html::write); + return new InternalDocumentConverter(options).convertToHtml(file).toResult(); } /** @@ -105,9 +73,7 @@ public class DocumentConverter { * Each paragraph is followed by two newlines. */ public Result extractRawText(InputStream stream) throws IOException { - return PassThroughException.unwrap(() -> - withDocxFile(stream, zipFile -> - extractRawText(Optional.empty(), zipFile)).toResult()); + return new InternalDocumentConverter(options).extractRawText(stream).toResult(); } /** @@ -116,45 +82,6 @@ public class DocumentConverter { * Each paragraph is followed by two newlines. */ public Result extractRawText(File file) throws IOException { - return PassThroughException.unwrap(() -> - withDocxFile(file, zipFile -> - extractRawText(Optional.of(file.toPath()), zipFile)).toResult()); - } - - private InternalResult extractRawText(Optional path, DocxFile zipFile) { - return readDocument(path, 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("", lazyMap(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; - }); + return new InternalDocumentConverter(options).extractRawText(file).toResult(); } } -- cgit v1.2.3