diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-13 18:41:35 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-13 18:41:35 +0000 |
| commit | 1425e365a53b4a2ff3ecd12c754da453b7aa026f (patch) | |
| tree | f4e649258c590944e09513cbd08edfcbafa79b52 /src | |
| parent | cf6808f01f8aff295548f2de62b86498e4996141 (diff) | |
Warn when referenced style is not defined
Diffstat (limited to 'src')
6 files changed, 71 insertions, 12 deletions
diff --git a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java index 26958dd..a9e99f1 100644 --- a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java @@ -1,6 +1,7 @@ package org.zwobble.mammoth.docx; import org.zwobble.mammoth.documents.*; +import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.util.MammothOptionals; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlElementLike; @@ -12,6 +13,8 @@ import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static org.zwobble.mammoth.docx.ReadResult.EMPTY_SUCCESS; import static org.zwobble.mammoth.docx.ReadResult.success; +import static org.zwobble.mammoth.results.Warning.warning; +import static org.zwobble.mammoth.util.MammothLists.list; public class BodyXmlReader { private final Styles styles; @@ -49,16 +52,28 @@ public class BodyXmlReader { private ReadResult readParagraph(XmlElement element) { XmlElementLike properties = element.findChildOrEmpty("w:pPr"); - Optional<Style> paragraphStyle = readParagraphStyle(properties); Optional<NumberingLevel> numbering = readNumbering(properties); - return readElements(element.children()) - .map(children -> new Paragraph(paragraphStyle, numbering, children)); + return ReadResult.map( + readParagraphStyle(properties), + readElements(element.children()), + (paragraphStyle, children) -> new Paragraph(paragraphStyle, numbering, children)); } - private Optional<Style> readParagraphStyle(XmlElementLike properties) { + private Result<Optional<Style>> readParagraphStyle(XmlElementLike properties) { return readVal(properties, "w:pStyle") - .map(styleId -> styles.findParagraphStyleById(styleId) - .orElse(new Style(styleId, Optional.empty()))); + .map(this::findParagraphStyleById) + .orElse(Result.empty()); + } + + private Result<Optional<Style>> findParagraphStyleById(String styleId) { + Optional<Style> style = styles.findParagraphStyleById(styleId); + if (style.isPresent()) { + return Result.success(style); + } else { + return new Result<>( + Optional.of(new Style(styleId, Optional.empty())), + list(warning("Paragraph style with ID " + styleId + " was referenced but not defined in the document"))); + } } private Optional<NumberingLevel> readNumbering(XmlElementLike properties) { diff --git a/src/main/java/org/zwobble/mammoth/docx/ReadResult.java b/src/main/java/org/zwobble/mammoth/docx/ReadResult.java index 9fd2e6c..7c672b2 100644 --- a/src/main/java/org/zwobble/mammoth/docx/ReadResult.java +++ b/src/main/java/org/zwobble/mammoth/docx/ReadResult.java @@ -1,11 +1,12 @@ package org.zwobble.mammoth.docx; import com.google.common.collect.ImmutableList; -import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.documents.DocumentElement; +import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.results.Warning; import java.util.List; +import java.util.function.BiFunction; import java.util.function.Function; import static org.zwobble.mammoth.util.MammothLists.list; @@ -23,6 +24,20 @@ public class ReadResult { return new ReadResult(elements.build(), warnings.build()); } + public static <T> ReadResult map( + Result<T> first, + ReadResult second, + BiFunction<T, List<DocumentElement>, DocumentElement> function) + { + ImmutableList.Builder<Warning> warnings = ImmutableList.builder(); + warnings.addAll(first.getWarnings()); + warnings.addAll(second.warnings); + + return new ReadResult( + list(function.apply(first.getValue(), second.elements)), + warnings.build()); + } + public static ReadResult success(DocumentElement element) { return success(list(element)); } diff --git a/src/main/java/org/zwobble/mammoth/results/Result.java b/src/main/java/org/zwobble/mammoth/results/Result.java index 846841e..be0819a 100644 --- a/src/main/java/org/zwobble/mammoth/results/Result.java +++ b/src/main/java/org/zwobble/mammoth/results/Result.java @@ -1,8 +1,10 @@ package org.zwobble.mammoth.results; import com.google.common.collect.ImmutableList; +import org.zwobble.mammoth.documents.Style; import java.util.List; +import java.util.Optional; import java.util.function.Function; import static org.zwobble.mammoth.util.MammothLists.list; @@ -18,6 +20,10 @@ public class Result<T> { return new Result<>(elements.build(), warnings.build()); } + public static Result<Optional<Style>> empty() { + return new Result<>(Optional.empty(), list()); + } + public static <T> Result<T> success(T value) { return new Result<>(value, list()); } diff --git a/src/main/java/org/zwobble/mammoth/results/Warning.java b/src/main/java/org/zwobble/mammoth/results/Warning.java index c1b4e4b..25579c2 100644 --- a/src/main/java/org/zwobble/mammoth/results/Warning.java +++ b/src/main/java/org/zwobble/mammoth/results/Warning.java @@ -1,4 +1,16 @@ package org.zwobble.mammoth.results; public class Warning { + public static Warning warning(String message) { + return new Warning(message); + } + private final String message; + + private Warning(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } } diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 2096cbd..e280d11 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -6,6 +6,7 @@ import org.zwobble.mammoth.results.Result; import java.io.File; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.zwobble.mammoth.results.Result.success; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; @@ -14,8 +15,8 @@ public class MammothTests { @Test public void emptyParagraphsAreIgnoredByDefault() { assertThat( - convertToHtml("empty.docx"), - deepEquals(success(""))); + convertToHtml("empty.docx").getValue(), + is("")); } @Test 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 8eebec5..6a0a392 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -2,12 +2,14 @@ package org.zwobble.mammoth.tests.docx; import com.natpryce.makeiteasy.Maker; import org.hamcrest.Matcher; +import org.hamcrest.Matchers; import org.junit.Test; import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.docx.BodyXmlReader; import org.zwobble.mammoth.docx.Numbering; import org.zwobble.mammoth.docx.Styles; +import org.zwobble.mammoth.results.Warning; import org.zwobble.mammoth.tests.DeepReflectionMatcher; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlNode; @@ -20,6 +22,7 @@ import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasProperty; +import static org.zwobble.mammoth.results.Warning.warning; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.CHILDREN; import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH; @@ -77,13 +80,14 @@ public class BodyXmlTests { @Test public void paragraphHasStyleIdReadFromParagraphPropertiesIfPresent() { - // TODO: emit warning due to missing style XmlElement element = paragraphXml(list( element("w:pPr", list( element("w:pStyle", map("w:val", "Heading1")))))); assertThat( - readSuccess(a(bodyReader), element), - hasStyle(Optional.of(new Style("Heading1", Optional.empty())))); + read(a(bodyReader), element), + isResult( + hasStyle(Optional.of(new Style("Heading1", Optional.empty()))), + list(warning("Paragraph style with ID Heading1 was referenced but not defined in the document")))); } @Test @@ -190,6 +194,12 @@ public class BodyXmlTests { return hasProperty("numbering", deepEquals(expected)); } + private <T> Matcher<Result<? extends T>> isResult(Matcher<T> valueMatcher, List<Warning> warnings) { + return Matchers.allOf( + hasProperty("value", valueMatcher), + hasProperty("warnings", deepEquals(warnings))); + } + private Run run(DocumentElement... children) { return new Run(asList(children)); } |
