From 56dad2617a86aeb83cf4f7f56519af621979851d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 24 Feb 2016 21:56:31 +0000 Subject: Use Set instead of List for warnings --- .../org/zwobble/mammoth/DocumentConverter.java | 9 +++-- src/main/java/org/zwobble/mammoth/Result.java | 4 +- .../zwobble/mammoth/internal/DocumentToHtml.java | 3 +- .../mammoth/internal/DocumentToHtmlOptions.java | 19 ++++++--- .../mammoth/internal/docx/BodyXmlReader.java | 4 +- .../zwobble/mammoth/internal/docx/ReadResult.java | 18 +++++---- .../mammoth/internal/results/InternalResult.java | 22 ++++++----- .../zwobble/mammoth/internal/util/MammothSets.java | 15 ++++++++ .../mammoth/tests/DeepReflectionMatcher.java | 45 ++++++++++++++++++++-- .../mammoth/tests/DocumentConverterTests.java | 5 ++- .../org/zwobble/mammoth/tests/MammothTests.java | 11 ++++++ .../org/zwobble/mammoth/tests/ResultMatchers.java | 21 ++++++++++ .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 16 +++----- 13 files changed, 144 insertions(+), 48 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/internal/util/MammothSets.java create mode 100644 src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index 76c1901..e11ec8b 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -12,9 +12,8 @@ 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.styles.DefaultStyles; -import org.zwobble.mammoth.internal.util.Casts; import org.zwobble.mammoth.internal.results.InternalResult; +import org.zwobble.mammoth.internal.util.Casts; import java.io.File; import java.io.IOException; @@ -31,7 +30,7 @@ public class DocumentConverter { private final DocumentToHtmlOptions options; public DocumentConverter() { - this(DocumentToHtmlOptions.DEFAULT.addStyleMap(DefaultStyles.DEFAULT_STYLE_MAP)); + this(DocumentToHtmlOptions.DEFAULT); } private DocumentConverter(DocumentToHtmlOptions options) { @@ -50,6 +49,10 @@ public class DocumentConverter { return new DocumentConverter(options.addStyleMap(styleMap)); } + public DocumentConverter disableDefaultStyleMap() { + return new DocumentConverter(options.disableDefaultStyleMap()); + } + public Result convertToHtml(InputStream stream) throws IOException { return withDocxFile(stream, zipFile -> convertToHtml(Optional.empty(), zipFile)); diff --git a/src/main/java/org/zwobble/mammoth/Result.java b/src/main/java/org/zwobble/mammoth/Result.java index 2660fd4..1c09559 100644 --- a/src/main/java/org/zwobble/mammoth/Result.java +++ b/src/main/java/org/zwobble/mammoth/Result.java @@ -1,8 +1,8 @@ package org.zwobble.mammoth; -import java.util.List; +import java.util.Set; public interface Result { T getValue(); - List getWarnings(); + Set getWarnings(); } diff --git a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java index 45ff92e..16210cd 100644 --- a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java +++ b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtml.java @@ -2,6 +2,7 @@ package org.zwobble.mammoth.internal; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.io.ByteStreams; import org.zwobble.mammoth.internal.documents.*; @@ -47,7 +48,7 @@ public class DocumentToHtml { private final boolean preserveEmptyParagraphs; private final StyleMap styleMap; private final List noteReferences = new ArrayList<>(); - private final ImmutableList.Builder warnings = ImmutableList.builder(); + private final ImmutableSet.Builder warnings = ImmutableSet.builder(); private DocumentToHtml(DocumentToHtmlOptions options) { this.idPrefix = options.idPrefix(); diff --git a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java index 4cd6372..05218a9 100644 --- a/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java +++ b/src/main/java/org/zwobble/mammoth/internal/DocumentToHtmlOptions.java @@ -1,27 +1,30 @@ package org.zwobble.mammoth.internal; +import org.zwobble.mammoth.internal.styles.DefaultStyles; import org.zwobble.mammoth.internal.styles.StyleMap; import org.zwobble.mammoth.internal.styles.parsing.StyleMapParser; public class DocumentToHtmlOptions { - public static final DocumentToHtmlOptions DEFAULT = new DocumentToHtmlOptions("", false, StyleMap.EMPTY); + public static final DocumentToHtmlOptions DEFAULT = new DocumentToHtmlOptions("", false, StyleMap.EMPTY, false); private final String idPrefix; private final boolean preserveEmptyParagraphs; private final StyleMap styleMap; + private final boolean disableDefaultStyleMap; - public DocumentToHtmlOptions(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap) { + public DocumentToHtmlOptions(String idPrefix, boolean preserveEmptyParagraphs, StyleMap styleMap, boolean disableDefaultStyleMap) { this.idPrefix = idPrefix; this.preserveEmptyParagraphs = preserveEmptyParagraphs; this.styleMap = styleMap; + this.disableDefaultStyleMap = disableDefaultStyleMap; } public DocumentToHtmlOptions idPrefix(String prefix) { - return new DocumentToHtmlOptions(prefix, preserveEmptyParagraphs, styleMap); + return new DocumentToHtmlOptions(prefix, preserveEmptyParagraphs, styleMap, disableDefaultStyleMap); } public DocumentToHtmlOptions preserveEmptyParagraphs() { - return new DocumentToHtmlOptions(idPrefix, true, styleMap); + return new DocumentToHtmlOptions(idPrefix, true, styleMap, disableDefaultStyleMap); } public DocumentToHtmlOptions addStyleMap(String styleMap) { @@ -29,7 +32,11 @@ public class DocumentToHtmlOptions { } public DocumentToHtmlOptions addStyleMap(StyleMap styleMap) { - return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, this.styleMap.update(styleMap)); + return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, this.styleMap.update(styleMap), disableDefaultStyleMap); + } + + public DocumentToHtmlOptions disableDefaultStyleMap() { + return new DocumentToHtmlOptions(idPrefix, preserveEmptyParagraphs, styleMap, true); } public String idPrefix() { @@ -41,6 +48,6 @@ public class DocumentToHtmlOptions { } public StyleMap styleMap() { - return styleMap; + return disableDefaultStyleMap ? styleMap : DefaultStyles.DEFAULT_STYLE_MAP.update(styleMap); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java index 0edc7a7..9951cbc 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java @@ -18,7 +18,7 @@ import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static org.zwobble.mammoth.internal.docx.ReadResult.EMPTY_SUCCESS; import static org.zwobble.mammoth.internal.docx.ReadResult.success; -import static org.zwobble.mammoth.internal.util.MammothLists.list; +import static org.zwobble.mammoth.internal.util.MammothSets.set; import static org.zwobble.mammoth.internal.util.MammothStrings.trimLeft; public class BodyXmlReader { @@ -213,7 +213,7 @@ public class BodyXmlReader { } else { return new InternalResult<>( Optional.of(new Style(styleId, Optional.empty())), - list(styleType + " style with ID " + styleId + " was referenced but not defined in the document")); + set(styleType + " style with ID " + styleId + " was referenced but not defined in the document")); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java b/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java index b390649..8f3053d 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/ReadResult.java @@ -1,14 +1,18 @@ package org.zwobble.mammoth.internal.docx; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import org.zwobble.mammoth.internal.documents.DocumentElement; import org.zwobble.mammoth.internal.results.InternalResult; import java.util.List; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; import static org.zwobble.mammoth.internal.util.MammothLists.list; +import static org.zwobble.mammoth.internal.util.MammothSets.set; public class ReadResult { public static final ReadResult EMPTY_SUCCESS = success(list()); @@ -16,7 +20,7 @@ public class ReadResult { public static ReadResult concat(Iterable results) { ImmutableList.Builder elements = ImmutableList.builder(); ImmutableList.Builder extra = ImmutableList.builder(); - ImmutableList.Builder warnings = ImmutableList.builder(); + ImmutableSet.Builder warnings = ImmutableSet.builder(); for (ReadResult result : results) { elements.addAll(result.elements); extra.addAll(result.extra); @@ -33,7 +37,7 @@ public class ReadResult { return new ReadResult( list(function.apply(first.getValue(), second.elements)), second.extra, - concat(first.getWarnings(), second.warnings)); + Sets.union(first.getWarnings(), second.warnings).immutableCopy()); } public static ReadResult success(DocumentElement element) { @@ -41,22 +45,22 @@ public class ReadResult { } public static ReadResult success(List elements) { - return new ReadResult(elements, list(), list()); + return new ReadResult(elements, list(), set()); } public static ReadResult emptyWithWarning(String warning) { - return new ReadResult(list(), list(), list(warning)); + return new ReadResult(list(), list(), set(warning)); } public static ReadResult withWarning(DocumentElement element, String warning) { - return new ReadResult(list(element), list(), list(warning)); + return new ReadResult(list(element), list(), set(warning)); } private final List elements; private final List extra; - private final List warnings; + private final Set warnings; - public ReadResult(List elements, List extra, List warnings) { + public ReadResult(List elements, List extra, Set warnings) { this.elements = elements; this.extra = extra; this.warnings = warnings; diff --git a/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java b/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java index 279692b..6ca3377 100644 --- a/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java +++ b/src/main/java/org/zwobble/mammoth/internal/results/InternalResult.java @@ -1,20 +1,22 @@ package org.zwobble.mammoth.internal.results; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import org.zwobble.mammoth.internal.documents.Style; -import org.zwobble.mammoth.internal.util.MammothLists; import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; -import static org.zwobble.mammoth.internal.util.MammothLists.list; +import static org.zwobble.mammoth.internal.util.MammothSets.set; public class InternalResult implements org.zwobble.mammoth.Result { public static InternalResult> concat(Iterable> results) { ImmutableList.Builder elements = ImmutableList.builder(); - ImmutableList.Builder warnings = ImmutableList.builder(); + ImmutableSet.Builder warnings = ImmutableSet.builder(); for (InternalResult result : results) { elements.add(result.value); warnings.addAll(result.warnings); @@ -29,21 +31,21 @@ public class InternalResult implements org.zwobble.mammoth.Result { { return new InternalResult<>( function.apply(first.value, second.value), - MammothLists.concat(first.warnings, second.warnings)); + Sets.union(first.warnings, second.warnings).immutableCopy()); } public static InternalResult> empty() { - return new InternalResult<>(Optional.empty(), list()); + return new InternalResult<>(Optional.empty(), set()); } public static InternalResult success(T value) { - return new InternalResult<>(value, list()); + return new InternalResult<>(value, set()); } private final T value; - private final List warnings; + private final Set warnings; - public InternalResult(T value, List warnings) { + public InternalResult(T value, Set warnings) { this.value = value; this.warnings = warnings; } @@ -52,7 +54,7 @@ public class InternalResult implements org.zwobble.mammoth.Result { return value; } - public List getWarnings() { + public Set getWarnings() { return warnings; } @@ -64,6 +66,6 @@ public class InternalResult implements org.zwobble.mammoth.Result { InternalResult intermediateResult = function.apply(value); return new InternalResult<>( intermediateResult.value, - MammothLists.concat(warnings, intermediateResult.warnings)); + Sets.union(warnings, intermediateResult.warnings).immutableCopy()); } } diff --git a/src/main/java/org/zwobble/mammoth/internal/util/MammothSets.java b/src/main/java/org/zwobble/mammoth/internal/util/MammothSets.java new file mode 100644 index 0000000..a9658ce --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/util/MammothSets.java @@ -0,0 +1,15 @@ +package org.zwobble.mammoth.internal.util; + +import com.google.common.collect.ImmutableSet; + +import java.util.Set; + +public class MammothSets { + public static Set set() { + return ImmutableSet.of(); + } + + public static Set set(T value1) { + return ImmutableSet.of(value1); + } +} diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java index 72a6529..8538534 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java +++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java @@ -4,17 +4,16 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; -import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.hamcrest.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; +import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; @@ -35,11 +34,20 @@ public class DeepReflectionMatcher extends TypeSafeDiagnosingMatcher { return matchesSafely("", expected, item, mismatchDescription); } + private static boolean deepEquals(Object expected, Object actual) { + Description description = new Description.NullDescription(); + return matchesSafely("", expected, actual, description); + } + private static boolean matchesSafely(String path, T expected, T actual, Description mismatchDescription) { if (expected instanceof List && actual instanceof List) { return matchesList(path, (List)expected, (List)actual, mismatchDescription); } + if (expected instanceof Set && actual instanceof Set) { + return matchesSet(path, (Set)expected, (Set)actual, mismatchDescription); + } + if (expected instanceof Map && actual instanceof Map) { return matchesMap(path, (Map)expected, (Map)actual, mismatchDescription); } @@ -89,6 +97,32 @@ public class DeepReflectionMatcher extends TypeSafeDiagnosingMatcher { return true; } + private static boolean matchesSet(String path, Set expected, Set actual, Description mismatchDescription) { + List missing = ImmutableList.copyOf(filter( + expected, + expectedElement -> !any(actual, actualElement -> deepEquals(expectedElement, actualElement)))); + + if (!missing.isEmpty()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("missing elements:" + + indentedList(transform(missing, DeepReflectionMatcher::describeValue))); + return false; + } + + List extra = ImmutableList.copyOf(filter( + actual, + actualElement -> !any(expected, expectedElement -> deepEquals(expectedElement, actualElement)))); + + if (!extra.isEmpty()) { + appendPath(mismatchDescription, path); + mismatchDescription.appendText("extra elements:" + + indentedList(transform(extra, DeepReflectionMatcher::describeValue))); + return false; + } + + return true; + } + private static boolean matchesMap(String path, Map expected, Map actual, Description mismatchDescription) { if (!handleExtraElements(path, expected, actual, mismatchDescription, "extra elements:")) { return false; @@ -172,6 +206,9 @@ public class DeepReflectionMatcher extends TypeSafeDiagnosingMatcher { } else if (value instanceof List) { List list = (List)value; return "[" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "]"; + } else if (value instanceof Set) { + Set list = (Set)value; + return "{" + indentedList(transform(list, DeepReflectionMatcher::describeValue)) + "}"; } else if (value instanceof Map) { Map map = (Map)value; String entries = indentedList( diff --git a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java index befba22..aa89eb8 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/DocumentConverterTests.java @@ -24,6 +24,7 @@ import static org.zwobble.mammoth.internal.documents.VerticalAlignment.SUPERSCRI import static org.zwobble.mammoth.internal.results.InternalResult.success; import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.internal.util.MammothMaps.map; +import static org.zwobble.mammoth.internal.util.MammothSets.set; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; @@ -79,7 +80,7 @@ public class DocumentConverterTests { deepEquals(new InternalResult<>( list(Html.element("p")), - list("Unrecognised paragraph style: Tips Paragraph (Style ID: TipsParagraph)")))); + set("Unrecognised paragraph style: Tips Paragraph (Style ID: TipsParagraph)")))); } @Test @@ -106,7 +107,7 @@ public class DocumentConverterTests { deepEquals(new InternalResult<>( list(Html.text("Hello")), - list("Unrecognised run style: Tips Run (Style ID: TipsRun)")))); + set("Unrecognised run style: Tips Run (Style ID: TipsRun)")))); } @Test diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index bc997a2..3319ebb 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -13,7 +13,9 @@ import java.util.function.Function; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import static org.zwobble.mammoth.internal.results.InternalResult.success; +import static org.zwobble.mammoth.internal.util.MammothLists.list; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; +import static org.zwobble.mammoth.tests.ResultMatchers.isResult; public class MammothTests { @Test @@ -188,6 +190,15 @@ public class MammothTests { deepEquals(success("

The Sunset Tree

"))); } + @Test + public void canDisableDefaultStyleMap() throws IOException { + assertThat( + convertToHtml("simple-list.docx", mammoth -> mammoth.disableDefaultStyleMap()), + isResult( + equalTo("

Apple

Banana

"), + list("Unrecognised paragraph style: List Paragraph (Style ID: ListParagraph)"))); + } + @Test public void canExtractRawText() throws IOException { assertThat( diff --git a/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java b/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java new file mode 100644 index 0000000..a5b9585 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/ResultMatchers.java @@ -0,0 +1,21 @@ +package org.zwobble.mammoth.tests; + +import com.google.common.collect.ImmutableSet; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.zwobble.mammoth.Result; + +import static org.hamcrest.Matchers.hasProperty; +import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; + +public class ResultMatchers { + public static Matcher> isResult(Matcher valueMatcher, Iterable warnings) { + return Matchers.allOf( + hasProperty("value", valueMatcher), + hasWarnings(warnings)); + } + + public static Matcher> hasWarnings(Iterable warnings) { + return hasProperty("warnings", deepEquals(ImmutableSet.copyOf(warnings))); + } +} diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index 049db81..3141620 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -23,12 +23,12 @@ import java.util.function.Function; import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.*; import static org.zwobble.mammoth.internal.documents.NoteReference.endnoteReference; import static org.zwobble.mammoth.internal.documents.NoteReference.footnoteReference; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; +import static org.zwobble.mammoth.tests.ResultMatchers.hasWarnings; +import static org.zwobble.mammoth.tests.ResultMatchers.isResult; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.*; import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.NUMBERING; @@ -475,7 +475,7 @@ public class BodyXmlTests { assertThat( result, - hasProperty("warnings", deepEquals(list("Image of type image/x-emf is unlikely to display in web browsers")))); + hasWarnings(list("Image of type image/x-emf is unlikely to display in web browsers"))); } @Test @@ -594,7 +594,7 @@ public class BodyXmlTests { private static DocumentElement readSuccess(Maker reader, XmlElement element) { InternalResult result = read(reader, element); - assertThat(result.getWarnings(), deepEquals(list())); + assertThat(result.getWarnings(), emptyIterable()); return result.getValue(); } @@ -652,12 +652,6 @@ public class BodyXmlTests { return hasProperty("numbering", deepEquals(expected)); } - private Matcher> isResult(Matcher valueMatcher, List warnings) { - return Matchers.allOf( - hasProperty("value", valueMatcher), - hasProperty("warnings", deepEquals(warnings))); - } - private Run run(DocumentElement... children) { return make(a(RUN, with(CHILDREN, asList(children)))); } -- cgit v1.2.3