aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-01-03 10:59:27 +0000
committerMichael Williamson <mike@zwobble.org>2016-01-03 10:59:27 +0000
commitc8ff305d5cbbbd472b6dc04c40f25c0190ff3a04 (patch)
tree39560a49a803ce99bb223bcafe3b946e34fb0350 /src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
parentd5da134fe3107acc53987ed6ca89cefd72e0397e (diff)
Rename non-static readers
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java')
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
new file mode 100644
index 0000000..9d8713a
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
@@ -0,0 +1,57 @@
+package org.zwobble.mammoth.docx;
+
+import com.google.common.collect.ImmutableList;
+import org.zwobble.mammoth.documents.*;
+import org.zwobble.mammoth.xml.XmlElement;
+import org.zwobble.mammoth.xml.XmlElementLike;
+import org.zwobble.mammoth.xml.XmlNode;
+
+import java.util.List;
+import java.util.Optional;
+
+import static com.google.common.collect.Iterables.concat;
+import static com.google.common.collect.Iterables.filter;
+import static com.google.common.collect.Iterables.transform;
+
+public class BodyXmlReader {
+ private final Styles styles;
+
+ public BodyXmlReader(Styles styles) {
+ this.styles = styles;
+ }
+
+ public List<DocumentElement> readElement(XmlElement element) {
+ switch (element.getName()) {
+ case "w:t":
+ return ImmutableList.of(new TextElement(element.innerText()));
+ case "w:r":
+ return ImmutableList.of(new RunElement(readElements(element.children())));
+ case "w:p":
+ return ImmutableList.of(readParagraph(element));
+
+ case "w:pPr":
+ return ImmutableList.of();
+
+ default:
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ public List<DocumentElement> readElements(Iterable<XmlNode> nodes) {
+ return ImmutableList.copyOf(
+ concat(
+ transform(
+ filter(nodes, XmlElement.class),
+ this::readElement)));
+ }
+
+ private ParagraphElement readParagraph(XmlElement element) {
+ return new ParagraphElement(readParagraphStyle(element), readElements(element.children()));
+ }
+
+ private Optional<Style> readParagraphStyle(XmlElement paragraph) {
+ XmlElementLike properties = paragraph.findChildOrEmpty("w:pPr");
+ return properties.findChildOrEmpty("w:pStyle").getAttributeOrNone("w:val")
+ .map(styleId -> styles.findParagraphStyleById(styleId).orElse(new Style(styleId, Optional.empty())));
+ }
+}