aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/docx/EmbeddedStyleMap.java
blob: 4acce3faa00111cba0f746adfa9afa81d3a7eb3c (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.zwobble.mammoth.internal.docx;

import org.zwobble.mammoth.internal.archives.Archive;
import org.zwobble.mammoth.internal.archives.Archives;
import org.zwobble.mammoth.internal.archives.MutableArchive;
import org.zwobble.mammoth.internal.util.Streams;
import org.zwobble.mammoth.internal.xml.*;
import org.zwobble.mammoth.internal.xml.parsing.XmlParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.zwobble.mammoth.internal.util.Casts.tryCast;
import static org.zwobble.mammoth.internal.util.Iterables.findIndex;
import static org.zwobble.mammoth.internal.util.Maps.map;

public class EmbeddedStyleMap {
    private static final String STYLE_MAP_PATH = "mammoth/style-map";
    private static final String ABSOLUTE_STYLE_MAP_PATH = "/" + STYLE_MAP_PATH;
    private static final String RELATIONSHIPS_PATH = "word/_rels/document.xml.rels";
    private static final String CONTENT_TYPES_PATH = "[Content_Types].xml";

    public static final NamespacePrefixes RELATIONSHIPS_NAMESPACES = NamespacePrefixes.builder()
        .defaultPrefix("http://schemas.openxmlformats.org/package/2006/relationships")
        .build();

    public static final NamespacePrefixes CONTENT_TYPES_NAMESPACES = NamespacePrefixes.builder()
        .defaultPrefix("http://schemas.openxmlformats.org/package/2006/content-types")
        .build();

    public static Optional<String> readStyleMap(Archive file) throws IOException {
        return file.tryGetInputStream(STYLE_MAP_PATH).map(Streams::toString);
    }

    public static void embedStyleMap(MutableArchive archive, String styleMap) throws IOException {
        archive.writeEntry(STYLE_MAP_PATH, styleMap);
        updateRelationships(archive);
        updateContentTypes(archive);
    }

    private static void updateRelationships(MutableArchive archive) throws IOException {
        XmlParser parser = new XmlParser(RELATIONSHIPS_NAMESPACES);
        XmlElement relationships = parser.parseStream(Archives.getInputStream(archive, RELATIONSHIPS_PATH));
        XmlElement relationship = XmlNodes.element("Relationship", map(
            "Id", "rMammothStyleMap",
            "Type", "http://schemas.zwobble.org/mammoth/style-map",
            "Target", ABSOLUTE_STYLE_MAP_PATH
        ));
        XmlElement updatedRelationships = updateOrAddElement(relationships, relationship, "Id");
        archive.writeEntry(RELATIONSHIPS_PATH, XmlWriter.toString(updatedRelationships, RELATIONSHIPS_NAMESPACES));
    }

    private static void updateContentTypes(MutableArchive archive) throws IOException {
        XmlParser parser = new XmlParser(CONTENT_TYPES_NAMESPACES);
        XmlElement contentTypes = parser.parseStream(Archives.getInputStream(archive, CONTENT_TYPES_PATH));
        XmlElement override = XmlNodes.element("Override", map(
            "PartName", ABSOLUTE_STYLE_MAP_PATH,
            "ContentType", "text/prs.mammoth.style-map"
        ));
        XmlElement updatedRelationships = updateOrAddElement(contentTypes, override, "PartName");
        archive.writeEntry(CONTENT_TYPES_PATH, XmlWriter.toString(updatedRelationships, CONTENT_TYPES_NAMESPACES));
    }

    private static XmlElement updateOrAddElement(XmlElement parent, XmlElement element, String identifyingAttribute) {
        int index = findIndex(parent.getChildren(), child ->
            tryCast(XmlElement.class, child)
                .map(childElement ->
                    childElement.getName().equals(element.getName()) &&
                    childElement.getAttributeOrNone(identifyingAttribute).equals(element.getAttributeOrNone(identifyingAttribute))
                )
                .orElse(false)
        );
        List<XmlNode> children = new ArrayList<>(parent.getChildren());

        if (index == -1) {
            children.add(element);
        } else {
            children.set(index, element);
        }
        return new XmlElement(parent.getName(), parent.getAttributes(), children);
    }
}