aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/Mammoth.java
blob: 69600277ac4f5b6415d87f769eb4523b478238fb (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.zwobble.mammoth;

import org.zwobble.mammoth.documents.Notes;
import org.zwobble.mammoth.docx.*;
import org.zwobble.mammoth.html.Html;
import org.zwobble.mammoth.results.Result;
import org.zwobble.mammoth.util.MammothLists;
import org.zwobble.mammoth.xml.XmlElement;

import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.zip.ZipFile;

import static org.zwobble.mammoth.util.MammothLists.list;

public class Mammoth {
    public static class Options {
        // TODO: what default prefix?
        public static final Options DEFAULT = new Options("document", false);

        private final String idPrefix;
        private final boolean preserveEmptyParagraphs;

        public Options(String idPrefix, boolean preserveEmptyParagraphs) {
            this.idPrefix = idPrefix;
            this.preserveEmptyParagraphs = preserveEmptyParagraphs;
        }

        public Options idPrefix(String prefix) {
            return new Options(prefix, preserveEmptyParagraphs);
        }

        public Options preserveEmptyParagraphs() {
            return new Options(idPrefix, true);
        }
    }

    @FunctionalInterface
    private interface BodyReaders {
        BodyXmlReader forName(String name);
    }

    public static Result<String> convertToHtml(File file) {
        return convertToHtml(file, Options.DEFAULT);
    }

    public static Result<String> convertToHtml(File file, Options options) {
        try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
            Styles styles = readStyles(zipFile);
            Numbering numbering = Numbering.EMPTY;
            ContentTypes contentTypes = ContentTypes.DEFAULT;
            FileReader fileReader = uri -> {
                throw new UnsupportedOperationException();
            };
            BodyReaders bodyReaders = name -> {
                Relationships relationships = readRelationships(zipFile, name);
                return new BodyXmlReader(styles, numbering, relationships, contentTypes, zipFile, fileReader);
            };
            return readNotes(zipFile, bodyReaders)
                .flatMap(notes -> {
                    DocumentXmlReader reader = new DocumentXmlReader(bodyReaders.forName("document"), notes);
                    return reader.readElement(parseOfficeXml(zipFile, "word/document.xml"));
                })
                .map(nodes -> DocumentConverter.convertToHtml(options.idPrefix, options.preserveEmptyParagraphs, nodes))
                .map(Html::stripEmpty)
                .map(Html::collapse)
                .map(Html::write);
        } catch (IOException e) {
            throw new UnsupportedOperationException("Should return a result of failure");   
        }
    }

    private static Result<Notes> readNotes(DocxFile file, BodyReaders bodyReaders) {
        return Result.map(
            tryParseOfficeXml(file, "word/footnotes.xml")
                .map(NotesXmlReader.footnote(bodyReaders.forName("footnotes"))::readElement)
                .orElse(Result.success(list())),
            tryParseOfficeXml(file, "word/endnotes.xml")
                .map(NotesXmlReader.endnote(bodyReaders.forName("endnotes"))::readElement)
                .orElse(Result.success(list())),
            MammothLists::concat).map(Notes::new);
    }

    private static Styles readStyles(DocxFile file) {
        return tryParseOfficeXml(file, "word/styles.xml")
            .map(StylesXml::readStylesXmlElement)
            .orElse(Styles.EMPTY);
    }

    private static Relationships readRelationships(DocxFile zipFile, String name) {
        return tryParseOfficeXml(zipFile, "word/_rels/" + name + ".xml.rels")
            .map(RelationshipsXml::readRelationshipsXmlElement)
            .orElse(Relationships.EMPTY);
    }

    private static Optional<XmlElement> tryParseOfficeXml(DocxFile zipFile, String name) {
        try {
            return zipFile.tryGetInputStream(name).map(OfficeXml::parseXml);
        } catch (IOException exception) {
            // TODO: wrap in more specific exception and catch at the top
            throw new RuntimeException(exception);
        }
    }

    private static XmlElement parseOfficeXml(DocxFile zipFile, String name) {
        return tryParseOfficeXml(zipFile, name)
            // TODO: wrap in more specific exception and catch at the top
            .orElseThrow(() -> new RuntimeException(new IOException("Missing entry in file: " + name)));
    }
}