summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/docx/StylesXml.java
blob: 78ba65f40cf68b60c27e11d6361c4bf2cb423731 (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
package org.zwobble.mammoth.internal.docx;

import org.zwobble.mammoth.internal.documents.Style;
import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlElementList;

import java.util.Map;
import java.util.Optional;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Maps.immutableEntry;
import static org.zwobble.mammoth.internal.util.MammothMaps.toMap;

public class StylesXml {
    public static Styles readStylesXmlElement(XmlElement element) {
        XmlElementList styleElements = element.findChildren("w:style");
        return new Styles(
            readStyles(styleElements, "paragraph"),
            readStyles(styleElements, "character"));
    }

    private static Map<String, Style> readStyles(XmlElementList styleElements, String styleType) {
        return toMap(
            filter(styleElements, styleElement -> styleElement.getAttribute("w:type").equals(styleType)),
            StylesXml::readStyle);
    }

    private static Map.Entry<String, Style> readStyle(XmlElement element) {
        String styleId = element.getAttribute("w:styleId");
        Optional<String> styleName = element.findChildOrEmpty("w:name").getAttributeOrNone("w:val");
        return immutableEntry(styleId, new Style(styleId, styleName));
    }
}