aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-13 18:54:16 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-13 18:54:16 +0000
commit3153e2a5cf92729d1f62b6c8e380405ccf5ff238 (patch)
tree183838b00109454cdba15a7b066451fec1aeaae4
parent31072b124b9a922ea829ddc6987e2d4f680d85ae (diff)
Read run style
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java38
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java15
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<Optional<Style>> readRunStyle(XmlElementLike properties) {
+ return readStyle(properties, "w:rStyle", "Run", styles::findCharacterStyleById);
}
public ReadResult readElements(Iterable<XmlNode> 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<Optional<Style>> readParagraphStyle(XmlElementLike properties) {
- return readVal(properties, "w:pStyle")
- .map(this::findParagraphStyleById)
+ return readStyle(properties, "w:pStyle", "Paragraph", styles::findParagraphStyleById);
+ }
+
+ private Result<Optional<Style>> readStyle(
+ XmlElementLike properties,
+ String styleTagName,
+ String styleType,
+ Function<String, Optional<Style>> findStyleById)
+ {
+ return readVal(properties, styleTagName)
+ .map(styleId -> findStyleById(styleType, styleId, findStyleById))
.orElse(Result.empty());
}
- private Result<Optional<Style>> findParagraphStyleById(String styleId) {
- Optional<Style> style = styles.findParagraphStyleById(styleId);
+ private Result<Optional<Style>> findStyleById(
+ String styleType,
+ String styleId,
+ Function<String, Optional<Style>> findStyleById)
+ {
+ Optional<Style> style = findStyleById.apply(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")));
+ list(warning(styleType + " style with ID " + styleId + " was referenced but not defined in the document")));
}
+
}
private Optional<NumberingLevel> readNumbering(XmlElementLike properties) {
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 4803d9d..e734d42 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
@@ -151,6 +151,21 @@ public class BodyXmlTests {
hasStyle(Optional.empty()));
}
+ @Test
+ public void whenRunHasStyleIdInStylesThenStyleNameIsReadFromStyles() {
+ XmlElement element = runXml(list(
+ element("w:rPr", list(
+ element("w:rStyle", map("w:val", "Heading1Char"))))));
+
+ Style style = new Style("Heading1Char", Optional.of("Heading 1 Char"));
+ Styles styles = new Styles(
+ map(),
+ map("Heading1Char", style));
+ assertThat(
+ readSuccess(a(bodyReader, with(STYLES, styles)), element),
+ hasStyle(Optional.of(style)));
+ }
+
private static DocumentElement readSuccess(Maker<BodyXmlReader> reader, XmlElement element) {
Result<DocumentElement> result = read(reader, element);
assertThat(result.getWarnings(), deepEquals(list()));