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

import org.zwobble.mammoth.internal.documents.Comment;
import org.zwobble.mammoth.internal.documents.Document;
import org.zwobble.mammoth.internal.documents.Notes;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.xml.XmlElement;

import java.util.List;
import java.util.Optional;

public class DocumentXmlReader {
    private final BodyXmlReader bodyReader;
    private final Notes notes;
    private final List<Comment> comments;

    public DocumentXmlReader(BodyXmlReader bodyReader, Notes notes, List<Comment> comments) {
        this.bodyReader = bodyReader;
        this.notes = notes;
        this.comments = comments;
    }

    public InternalResult<Document> readElement(XmlElement element) {
        Optional<XmlElement> body = element.findChild("w:body");

        if (!body.isPresent()) {
            throw new IllegalArgumentException("Could not find the body element: are you sure this is a docx file?");
        }

        return bodyReader.readElements(body.get().getChildren())
            .toResult()
            .map(children -> new Document(children, notes, comments));
    }
}