From 3153e2a5cf92729d1f62b6c8e380405ccf5ff238 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 13 Feb 2016 18:54:16 +0000 Subject: Read run style --- .../org/zwobble/mammoth/docx/BodyXmlReader.java | 38 +++++++++++++++++----- .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 15 +++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java index 4c826de..8cfc4f6 100644 --- a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java @@ -8,6 +8,7 @@ import org.zwobble.mammoth.xml.XmlElementLike; import org.zwobble.mammoth.xml.XmlNode; import java.util.Optional; +import java.util.function.Function; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; @@ -44,8 +45,15 @@ public class BodyXmlReader { } private ReadResult readRun(XmlElement element) { - return readElements(element.children()) - .map(children -> new Run(Optional.empty(), children)); + XmlElementLike properties = element.findChildOrEmpty("w:rPr"); + return ReadResult.map( + readRunStyle(properties), + readElements(element.children()), + (style, children) -> new Run(style, children)); + } + + private Result> readRunStyle(XmlElementLike properties) { + return readStyle(properties, "w:rStyle", "Run", styles::findCharacterStyleById); } public ReadResult readElements(Iterable nodes) { @@ -61,24 +69,38 @@ public class BodyXmlReader { return ReadResult.map( readParagraphStyle(properties), readElements(element.children()), - (paragraphStyle, children) -> new Paragraph(paragraphStyle, numbering, children)); + (style, children) -> new Paragraph(style, numbering, children)); } private Result> readParagraphStyle(XmlElementLike properties) { - return readVal(properties, "w:pStyle") - .map(this::findParagraphStyleById) + return readStyle(properties, "w:pStyle", "Paragraph", styles::findParagraphStyleById); + } + + private Result> readStyle( + XmlElementLike properties, + String styleTagName, + String styleType, + Function> findStyleById) + { + return readVal(properties, styleTagName) + .map(styleId -> findStyleById(styleType, styleId, findStyleById)) .orElse(Result.empty()); } - private Result> findParagraphStyleById(String styleId) { - Optional