aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/docx/BodyXml.java
blob: 94faf83b672932512d87fd8092f8f20959f3c2b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.zwobble.mammoth.docx;

import com.google.common.collect.ImmutableList;
import org.zwobble.mammoth.documents.DocumentElement;
import org.zwobble.mammoth.documents.ParagraphElement;
import org.zwobble.mammoth.documents.RunElement;
import org.zwobble.mammoth.documents.TextElement;
import org.zwobble.mammoth.xml.XmlElement;
import org.zwobble.mammoth.xml.XmlNode;

import java.util.List;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;

public class BodyXml {
    public static DocumentElement readBodyXmlElement(XmlElement element) {
        switch (element.getName()) {
            case "w:t":
                return new TextElement(element.innerText());
            case "w:r":
                return new RunElement(readElements(element.children()));
            case "w:p":
                return new ParagraphElement(readElements(element.children()));
            default:
                throw new UnsupportedOperationException();
        }
    }

    private static List<DocumentElement> readElements(Iterable<XmlNode> nodes) {
        return ImmutableList.copyOf(
            transform(
                filter(nodes, XmlElement.class),
                BodyXml::readBodyXmlElement));
    }
}