aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java94
-rw-r--r--src/main/java/org/zwobble/mammoth/internal/DocumentConverter.java16
-rw-r--r--src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java41
-rw-r--r--src/main/java/org/zwobble/mammoth/internal/styles/DefaultStyles.java49
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java13
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/MammothTests.java21
6 files changed, 139 insertions, 95 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index 9e9af83..b8dac98 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -1,9 +1,9 @@
package org.zwobble.mammoth;
import com.google.common.base.Function;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import org.zwobble.mammoth.internal.DocumentConverter;
+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;
@@ -12,10 +12,9 @@ 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.results.Result;
-import org.zwobble.mammoth.internal.styles.*;
-import org.zwobble.mammoth.internal.styles.parsing.StyleMapParser;
+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;
@@ -29,90 +28,37 @@ import static org.zwobble.mammoth.internal.docx.DocumentReader.readDocument;
import static org.zwobble.mammoth.internal.util.MammothLists.list;
public class Mammoth {
- public static class Options {
- public static final Options DEFAULT = new Options("", false);
-
- private final String idPrefix;
- private final boolean preserveEmptyParagraphs;
-
- public Options(String idPrefix, boolean preserveEmptyParagraphs) {
- this.idPrefix = idPrefix;
- this.preserveEmptyParagraphs = preserveEmptyParagraphs;
- }
+ private final DocumentToHtmlOptions options;
- public Options idPrefix(String prefix) {
- return new Options(prefix, preserveEmptyParagraphs);
- }
+ public Mammoth() {
+ this(DocumentToHtmlOptions.DEFAULT.styleMap(DefaultStyles.DEFAULT_STYLE_MAP));
+ }
- public Options preserveEmptyParagraphs() {
- return new Options(idPrefix, true);
- }
+ private Mammoth(DocumentToHtmlOptions options) {
+ this.options = options;
}
- private static final StyleMap DEFAULT_STYLE_MAP = StyleMapParser.parse(ImmutableList.of(
- "p.Heading1 => h1:fresh",
- "p.Heading2 => h2:fresh",
- "p.Heading3 => h3:fresh",
- "p.Heading4 => h4:fresh",
- "p[style-name='Heading 1'] => h1:fresh",
- "p[style-name='Heading 2'] => h2:fresh",
- "p[style-name='Heading 3'] => h3:fresh",
- "p[style-name='Heading 4'] => h4:fresh",
- "p[style-name='heading 1'] => h1:fresh",
- "p[style-name='heading 2'] => h2:fresh",
- "p[style-name='heading 3'] => h3:fresh",
- "p[style-name='heading 4'] => h4:fresh",
- "p[style-name='heading 4'] => h4:fresh",
-
- "r[style-name='Strong'] => strong",
-
- "p[style-name='footnote text'] => p",
- "r[style-name='footnote reference'] =>",
- "p[style-name='endnote text'] => p",
- "r[style-name='endnote reference'] =>",
-
- // LibreOffice
- "p[style-name='Footnote'] => p",
- "r[style-name='Footnote anchor'] =>",
- "p[style-name='Endnote'] => p",
- "r[style-name='Endnote anchor'] =>",
-
- "p:unordered-list(1) => ul > li:fresh",
- "p:unordered-list(2) => ul|ol > li > ul > li:fresh",
- "p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh",
- "p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
- "p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
- "p:ordered-list(1) => ol > li:fresh",
- "p:ordered-list(2) => ul|ol > li > ol > li:fresh",
- "p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh",
- "p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
- "p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
-
- "r[style-name='Hyperlink'] =>",
-
- "p[style-name='Normal'] => p:fresh"));
-
- public static Result<String> convertToHtml(InputStream stream) throws IOException {
- return convertToHtml(stream, Options.DEFAULT);
+ public Mammoth idPrefix(String idPrefix) {
+ return new Mammoth(options.idPrefix(idPrefix));
}
- public static Result<String> convertToHtml(InputStream stream, Options options) throws IOException {
- return withDocxFile(stream, zipFile ->
- convertToHtml(Optional.empty(), zipFile, options));
+ public Mammoth preserveEmptyParagraphs() {
+ return new Mammoth(options.preserveEmptyParagraphs());
}
- public static Result<String> convertToHtml(File file) throws IOException {
- return convertToHtml(file, Options.DEFAULT);
+ public Result<String> convertToHtml(InputStream stream) throws IOException {
+ return withDocxFile(stream, zipFile ->
+ convertToHtml(Optional.empty(), zipFile));
}
- public static Result<String> convertToHtml(File file, Options options) throws IOException {
+ public Result<String> convertToHtml(File file) throws IOException {
return withDocxFile(file, zipFile ->
- convertToHtml(Optional.of(file.toPath()), zipFile, options));
+ convertToHtml(Optional.of(file.toPath()), zipFile));
}
- private static Result<String> convertToHtml(Optional<Path> path, DocxFile zipFile, Options options) {
+ private Result<String> convertToHtml(Optional<Path> path, DocxFile zipFile) {
return readDocument(path, zipFile)
- .flatMap(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, DEFAULT_STYLE_MAP, nodes))
+ .flatMap(nodes -> DocumentConverter.convertToHtml(nodes, options))
.map(Html::stripEmpty)
.map(Html::collapse)
.map(Html::write);
diff --git a/src/main/java/org/zwobble/mammoth/internal/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/internal/DocumentConverter.java
index 5aa3c08..ab28651 100644
--- a/src/main/java/org/zwobble/mammoth/internal/DocumentConverter.java
+++ b/src/main/java/org/zwobble/mammoth/internal/DocumentConverter.java
@@ -24,8 +24,8 @@ import static org.zwobble.mammoth.internal.util.MammothLists.*;
import static org.zwobble.mammoth.internal.util.MammothMaps.map;
public class DocumentConverter {
- public static Result<List<HtmlNode>> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, Document document) {
- DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap);
+ public static Result<List<HtmlNode>> convertToHtml(Document document, DocumentToHtmlOptions options) {
+ DocumentConverter documentConverter = new DocumentConverter(options);
return new Result<>(
documentConverter.convertToHtml(document),
documentConverter.warnings.build());
@@ -38,8 +38,8 @@ public class DocumentConverter {
reference -> document.getNotes().findNote(reference.getNoteType(), reference.getNoteId()).get()));
}
- public static Result<List<HtmlNode>> convertToHtml(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, DocumentElement element) {
- DocumentConverter documentConverter = new DocumentConverter(idPrefix, preserveEmptyParagraphs, styleMap);
+ public static Result<List<HtmlNode>> convertToHtml(DocumentElement element, DocumentToHtmlOptions options) {
+ DocumentConverter documentConverter = new DocumentConverter(options);
return new Result<>(
documentConverter.convertToHtml(element),
documentConverter.warnings.build());
@@ -51,10 +51,10 @@ public class DocumentConverter {
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;
- this.preserveEmptyParagraphs = preserveEmptyParagraphs;
- this.styleMap = styleMap;
+ private DocumentConverter(DocumentToHtmlOptions options) {
+ this.idPrefix = options.idPrefix();
+ this.preserveEmptyParagraphs = options.shouldPreserveEmptyParagraphs();
+ this.styleMap = options.styleMap();
}
private List<HtmlNode> convertToHtml(Document document) {
diff --git a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java
new file mode 100644
index 0000000..f7cbf0d
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java
@@ -0,0 +1,41 @@
+package org.zwobble.mammoth.internal;
+
+import org.zwobble.mammoth.internal.styles.StyleMap;
+
+public class DocumentToHtmlOptions {
+ public static final DocumentToHtmlOptions DEFAULT = new DocumentToHtmlOptions("", false, StyleMap.EMPTY);
+
+ private final String idPrefix;
+ private final boolean preserveEmptyParagraphs;
+ private final StyleMap styleMap;
+
+ public DocumentToHtmlOptions(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap) {
+ this.idPrefix = idPrefix;
+ this.preserveEmptyParagraphs = preserveEmptyParagraphs;
+ this.styleMap = styleMap;
+ }
+
+ public DocumentToHtmlOptions idPrefix(String prefix) {
+ return new DocumentToHtmlOptions(prefix, preserveEmptyParagraphs, styleMap);
+ }
+
+ public DocumentToHtmlOptions preserveEmptyParagraphs() {
+ return new DocumentToHtmlOptions(idPrefix, true, styleMap);
+ }
+
+ public DocumentToHtmlOptions styleMap(StyleMap styleMap) {
+ return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap);
+ }
+
+ public String idPrefix() {
+ return idPrefix;
+ }
+
+ public boolean shouldPreserveEmptyParagraphs() {
+ return preserveEmptyParagraphs;
+ }
+
+ public StyleMap styleMap() {
+ return styleMap;
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/DefaultStyles.java b/src/main/java/org/zwobble/mammoth/internal/styles/DefaultStyles.java
new file mode 100644
index 0000000..4fcba76
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/internal/styles/DefaultStyles.java
@@ -0,0 +1,49 @@
+package org.zwobble.mammoth.internal.styles;
+
+import com.google.common.collect.ImmutableList;
+import org.zwobble.mammoth.internal.styles.parsing.StyleMapParser;
+
+public class DefaultStyles {
+ public static final StyleMap DEFAULT_STYLE_MAP = StyleMapParser.parse(ImmutableList.of(
+ "p.Heading1 => h1:fresh",
+ "p.Heading2 => h2:fresh",
+ "p.Heading3 => h3:fresh",
+ "p.Heading4 => h4:fresh",
+ "p[style-name='Heading 1'] => h1:fresh",
+ "p[style-name='Heading 2'] => h2:fresh",
+ "p[style-name='Heading 3'] => h3:fresh",
+ "p[style-name='Heading 4'] => h4:fresh",
+ "p[style-name='heading 1'] => h1:fresh",
+ "p[style-name='heading 2'] => h2:fresh",
+ "p[style-name='heading 3'] => h3:fresh",
+ "p[style-name='heading 4'] => h4:fresh",
+ "p[style-name='heading 4'] => h4:fresh",
+
+ "r[style-name='Strong'] => strong",
+
+ "p[style-name='footnote text'] => p",
+ "r[style-name='footnote reference'] =>",
+ "p[style-name='endnote text'] => p",
+ "r[style-name='endnote reference'] =>",
+
+ // LibreOffice
+ "p[style-name='Footnote'] => p",
+ "r[style-name='Footnote anchor'] =>",
+ "p[style-name='Endnote'] => p",
+ "r[style-name='Endnote anchor'] =>",
+
+ "p:unordered-list(1) => ul > li:fresh",
+ "p:unordered-list(2) => ul|ol > li > ul > li:fresh",
+ "p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh",
+ "p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
+ "p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
+ "p:ordered-list(1) => ol > li:fresh",
+ "p:ordered-list(2) => ul|ol > li > ol > li:fresh",
+ "p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh",
+ "p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
+ "p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
+
+ "r[style-name='Hyperlink'] =>",
+
+ "p[style-name='Normal'] => p:fresh"));
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
index 40b6d99..4c40800 100644
--- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java
@@ -2,6 +2,7 @@ package org.zwobble.mammoth.tests;
import org.junit.Test;
import org.zwobble.mammoth.internal.DocumentConverter;
+import org.zwobble.mammoth.internal.DocumentToHtmlOptions;
import org.zwobble.mammoth.internal.documents.*;
import org.zwobble.mammoth.internal.html.Html;
import org.zwobble.mammoth.internal.html.HtmlNode;
@@ -39,8 +40,9 @@ public class DocumentConverterTests {
@Test
public void forceWriteIsInsertedIntoParagraphIfEmptyParagraphsShouldBePreserved() {
+ DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT.preserveEmptyParagraphs();
assertThat(
- DocumentConverter.convertToHtml("", true, StyleMap.EMPTY, make(a(PARAGRAPH))),
+ DocumentConverter.convertToHtml(make(a(PARAGRAPH)), options),
deepEquals(success(list(Html.element("p", list(Html.FORCE_WRITE))))));
}
@@ -286,7 +288,9 @@ public class DocumentConverterTests {
}
private List<HtmlNode> convertToHtml(Document document) {
- Result<List<HtmlNode>> result = DocumentConverter.convertToHtml("doc-42-", false, StyleMap.EMPTY, document);
+ DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT
+ .idPrefix("doc-42-");
+ Result<List<HtmlNode>> result = DocumentConverter.convertToHtml(document, options);
assertThat(result.getWarnings(), hasSize(0));
return result.getValue();
}
@@ -306,6 +310,9 @@ public class DocumentConverterTests {
}
private Result<List<HtmlNode>> convertToHtmlResult(DocumentElement element, StyleMap styleMap) {
- return DocumentConverter.convertToHtml("doc-42-", false, styleMap, element);
+ DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT
+ .idPrefix("doc-42-")
+ .styleMap(styleMap);
+ return DocumentConverter.convertToHtml(element, options);
}
}
diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
index 52efde2..6e5794a 100644
--- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
@@ -8,6 +8,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.function.Function;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
@@ -39,7 +40,7 @@ public class MammothTests {
@Test
public void emptyParagraphsArePreservedIfIgnoreEmptyParagraphsIsFalse() throws IOException {
assertThat(
- convertToHtml("empty.docx", Mammoth.Options.DEFAULT.preserveEmptyParagraphs()),
+ convertToHtml("empty.docx", mammoth -> mammoth.preserveEmptyParagraphs()),
deepEquals(success("<p></p>")));
}
@@ -78,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(
- Mammoth.convertToHtml(documentPath.toFile()),
+ new Mammoth().convertToHtml(documentPath.toFile()),
deepEquals(success("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")));
} finally {
tempDirectory.toFile().delete();
@@ -92,7 +93,7 @@ public class MammothTests {
Path documentPath = tempDirectory.resolve("external-picture.docx");
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
assertThat(
- Mammoth.convertToHtml(documentPath.toUri().toURL().openStream()),
+ new Mammoth().convertToHtml(documentPath.toUri().toURL().openStream()),
allOf(
hasProperty("value", equalTo("")),
hasProperty("warnings", contains(
@@ -109,7 +110,7 @@ public class MammothTests {
Path documentPath = tempDirectory.resolve("external-picture.docx");
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
assertThat(
- Mammoth.convertToHtml(documentPath.toFile()),
+ new Mammoth().convertToHtml(documentPath.toFile()),
allOf(
hasProperty("value", equalTo("")),
hasProperty("warnings", contains(
@@ -129,7 +130,7 @@ public class MammothTests {
@Test
public void footnotesAreAppendedToText() throws IOException {
assertThat(
- convertToHtml("footnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")),
+ convertToHtml("footnotes.docx", mammoth -> mammoth.idPrefix("doc-42-")),
deepEquals(success(
"<p>Ouch" +
"<sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup>." +
@@ -141,7 +142,7 @@ public class MammothTests {
@Test
public void endNotesAreAppendedToText() throws IOException {
assertThat(
- convertToHtml("endnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")),
+ convertToHtml("endnotes.docx", mammoth -> mammoth.idPrefix("doc-42-")),
deepEquals(success(
"<p>Ouch" +
"<sup><a href=\"#doc-42-endnote-2\" id=\"doc-42-endnote-ref-2\">[1]</a></sup>." +
@@ -153,7 +154,7 @@ public class MammothTests {
@Test
public void relationshipsAreReadForEachFileContainingBodyXml() throws IOException {
assertThat(
- convertToHtml("footnote-hyperlink.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")),
+ convertToHtml("footnote-hyperlink.docx", mammoth -> mammoth.idPrefix("doc-42-")),
deepEquals(success(
"<p><sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup></p>" +
"<ol><li id=\"doc-42-footnote-1\"><p> <a href=\"http://www.example.com\">Example</a> <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li></ol>")));
@@ -175,11 +176,11 @@ public class MammothTests {
private Result<String> convertToHtml(String name) throws IOException {
File file = TestData.file(name);
- return Mammoth.convertToHtml(file);
+ return new Mammoth().convertToHtml(file);
}
- private Result<String> convertToHtml(String name, Mammoth.Options options) throws IOException {
+ private Result<String> convertToHtml(String name, Function<Mammoth, Mammoth> configure) throws IOException {
File file = TestData.file(name);
- return Mammoth.convertToHtml(file, options);
+ return configure.apply(new Mammoth()).convertToHtml(file);
}
}