diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-20 15:23:54 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-20 15:23:54 +0000 |
| commit | 7ecf37adc3faff67781bdf46e624b54a35da9a54 (patch) | |
| tree | ca77a5b94006b4e60983a773c2b260ca2fa763d6 /src | |
| parent | 32aee86473c4e0865e9ad181be2b8418cd93c9a0 (diff) | |
Return result from document conversion
Diffstat (limited to 'src')
3 files changed, 22 insertions, 8 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index d04d012..294e403 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -7,6 +7,8 @@ import com.google.common.io.ByteStreams; import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; +import org.zwobble.mammoth.results.Result; +import org.zwobble.mammoth.results.Warning; import org.zwobble.mammoth.styles.StyleMap; import org.zwobble.mammoth.util.MammothLists; @@ -20,9 +22,11 @@ import static org.zwobble.mammoth.util.MammothLists.*; import static org.zwobble.mammoth.util.MammothMaps.map; public class DocumentConverter { - public static List<HtmlNode> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, Document document) { + public static Result<List<HtmlNode>> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, Document document) { DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap); - return documentConverter.convertToHtml(document); + return new Result<>( + documentConverter.convertToHtml(document), + documentConverter.warnings.build()); } private static List<Note> findNotes(Document document, Iterable<NoteReference> noteReferences) { @@ -32,14 +36,18 @@ public class DocumentConverter { reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get())); } - public static List<HtmlNode> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, DocumentElement element) { - return new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap).convertToHtml(element); + public static Result<List<HtmlNode>> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, DocumentElement element) { + DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap); + return new Result<>( + documentConverter.convertToHtml(element), + documentConverter.warnings.build()); } private final String idPrefix; private final boolean preserveEmptyParagraphs; private final StyleMap styleMap; private final List<NoteReference> noteReferences = new ArrayList<>(); + private final ImmutableList.Builder<Warning> warnings = ImmutableList.builder(); private DocumentConverter(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap) { this.idPrefix = idPrefix; diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 6db7308..b6bba55 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -49,7 +49,7 @@ public class Mammoth { public static Result<String> convertToHtml(File file, Options options) { return withDocxFile(file, zipFile -> readDocument(zipFile) - .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, StyleMap.EMPTY, nodes)) + .flatMap(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, StyleMap.EMPTY, nodes)) .map(Html::stripEmpty) .map(Html::collapse) .map(Html::write)); diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java index 295814a..429df1e 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java @@ -5,6 +5,7 @@ import org.zwobble.mammoth.DocumentConverter; import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.html.HtmlNode; +import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.styles.HtmlPath; import org.zwobble.mammoth.styles.ParagraphMatcher; import org.zwobble.mammoth.styles.StyleMap; @@ -18,6 +19,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.zwobble.mammoth.documents.VerticalAlignment.SUBSCRIPT; import static org.zwobble.mammoth.documents.VerticalAlignment.SUPERSCRIPT; +import static org.zwobble.mammoth.results.Result.success; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; import static org.zwobble.mammoth.util.MammothLists.list; @@ -37,7 +39,7 @@ public class DocumentConverterTests { public void forceWriteIsInsertedIntoParagraphIfEmptyParagraphsShouldBePreserved() { assertThat( DocumentConverter.convertToHtml("", true, StyleMap.EMPTY, make(a(PARAGRAPH))), - deepEquals(list(Html.element("p", list(Html.FORCE_WRITE))))); + deepEquals(success(list(Html.element("p", list(Html.FORCE_WRITE)))))); } @Test @@ -244,7 +246,9 @@ public class DocumentConverterTests { } private List<HtmlNode> convertToHtml(Document document) { - return DocumentConverter.convertToHtml("doc-42-", false, StyleMap.EMPTY, document); + Result<List<HtmlNode>> result = DocumentConverter.convertToHtml("doc-42-", false, StyleMap.EMPTY, document); + assertThat(result.getWarnings(), hasSize(0)); + return result.getValue(); } private List<HtmlNode> convertToHtml(DocumentElement element) { @@ -252,6 +256,8 @@ public class DocumentConverterTests { } private List<HtmlNode> convertToHtml(DocumentElement element, StyleMap styleMap) { - return DocumentConverter.convertToHtml("doc-42-", false, styleMap, element); + Result<List<HtmlNode>> result = DocumentConverter.convertToHtml("doc-42-", false, styleMap, element); + assertThat(result.getWarnings(), hasSize(0)); + return result.getValue(); } } |
