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/main/java/org/zwobble/mammoth/docx | |
| parent | cf6808f01f8aff295548f2de62b86498e4996141 (diff) | |
Warn when referenced style is not defined
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/docx')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java | 27 | ||||
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/docx/ReadResult.java | 17 |
2 files changed, 37 insertions, 7 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)); } |
