diff options
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/DocumentConverter.java')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/DocumentConverter.java | 83 |
1 files changed, 5 insertions, 78 deletions
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<String> 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<String> convertToHtml(File file) throws IOException { - return PassThroughException.unwrap(() -> - withDocxFile(file, zipFile -> - convertToHtml(Optional.of(file.toPath()), zipFile)).toResult()); - } - - private InternalResult<String> convertToHtml(Optional<Path> 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<String> 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<String> extractRawText(File file) throws IOException { - return PassThroughException.unwrap(() -> - withDocxFile(file, zipFile -> - extractRawText(Optional.of(file.toPath()), zipFile)).toResult()); - } - - 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 { - try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { - return function.apply(zipFile); - } - } - - private static <T> T withDocxFile(InputStream stream, Function<DocxFile, T> 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<DocumentElement> 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<DocumentElement> 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(); } } |
