From a549566d565f0fe0c62ba70ec58ef6c42858857c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 16 Feb 2016 22:34:10 +0000 Subject: Introduce Options object --- src/main/java/org/zwobble/mammoth/Mammoth.java | 24 ++++++++++++++++------ .../org/zwobble/mammoth/tests/MammothTests.java | 10 ++++----- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 1e14482..d0c4aff 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -15,19 +15,31 @@ import java.util.zip.ZipFile; import static org.zwobble.mammoth.util.MammothLists.list; public class Mammoth { + public static class Options { + // TODO: what default prefix? + public static final Options DEFAULT = new Options("document"); + + private final String idPrefix; + + public Options(String idPrefix) { + this.idPrefix = idPrefix; + } + + public Options idPrefix(String prefix) { + return new Options(prefix); + } + } + @FunctionalInterface private interface BodyReaders { BodyXmlReader forName(String name); } - // TODO: provide Options builder public static Result convertToHtml(File file) { - // TODO: prefix - String idPrefix = "document"; - return convertToHtml(file, idPrefix); + return convertToHtml(file, Options.DEFAULT); } - public static Result convertToHtml(File file, String idPrefix) { + public static Result convertToHtml(File file, Options options) { try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) { Styles styles = readStyles(zipFile); Numbering numbering = Numbering.EMPTY; @@ -44,7 +56,7 @@ public class Mammoth { DocumentXmlReader reader = new DocumentXmlReader(bodyReaders.forName("document"), notes); return reader.readElement(parseOfficeXml(zipFile, "word/document.xml")); }) - .map(nodes -> DocumentConverter.convertToHtml(idPrefix, nodes)) + .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, nodes)) .map(Html::stripEmpty) .map(Html::collapse) .map(Html::write); diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 9a44cbf..fd6d3c5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -35,7 +35,7 @@ public class MammothTests { @Test public void footnotesAreAppendedToText() { assertThat( - convertToHtml("footnotes.docx", "doc-42"), + convertToHtml("footnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42")), deepEquals(success( "

Ouch" + "[1]." + @@ -47,7 +47,7 @@ public class MammothTests { @Test public void endNotesAreAppendedToText() { assertThat( - convertToHtml("endnotes.docx", "doc-42"), + convertToHtml("endnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42")), deepEquals(success( "

Ouch" + "[1]." + @@ -59,7 +59,7 @@ public class MammothTests { @Test public void relationshipsAreReadForEachFileContainingBodyXml() { assertThat( - convertToHtml("footnote-hyperlink.docx", "doc-42"), + convertToHtml("footnote-hyperlink.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42")), deepEquals(success( "

[1]

" + "
  1. Example ↑

"))); @@ -70,8 +70,8 @@ public class MammothTests { return Mammoth.convertToHtml(file); } - private Result convertToHtml(String name, String idPrefix) { + private Result convertToHtml(String name, Mammoth.Options options) { File file = TestData.file(name); - return Mammoth.convertToHtml(file, idPrefix); + return Mammoth.convertToHtml(file, options); } } -- cgit v1.2.3