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

import org.zwobble.mammoth.internal.InternalDocumentConverter;
import org.zwobble.mammoth.internal.conversion.DocumentToHtmlOptions;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class DocumentConverter {
    private final DocumentToHtmlOptions options;

    public DocumentConverter() {
        this(DocumentToHtmlOptions.DEFAULT);
    }

    private DocumentConverter(DocumentToHtmlOptions options) {
        this.options = options;
    }

    /**
     * A string to prepend to any generated IDs,
     * such as those used by bookmarks, footnotes and endnotes.
     * Defaults to the empty string.
     */
    public DocumentConverter idPrefix(String idPrefix) {
        return new DocumentConverter(options.idPrefix(idPrefix));
    }

    /**
     * By default, empty paragraphs are ignored.
     * Call this to preserve empty paragraphs in the output.
     */
    public DocumentConverter preserveEmptyParagraphs() {
        return new DocumentConverter(options.preserveEmptyParagraphs());
    }

    /**
     * Add a style map to specify the mapping of Word styles to HTML.
     * The most recently added style map has the greatest precedence.
     */
    public DocumentConverter addStyleMap(String styleMap) {
        return new DocumentConverter(options.addStyleMap(styleMap));
    }

    /**
     * By default, any added style maps are combined with the default style map.
     * Call this to stop using the default style map altogether.
     */
    public DocumentConverter disableDefaultStyleMap() {
        return new DocumentConverter(options.disableDefaultStyleMap());
    }

    /**
     * By default, if the document contains an embedded style map, then it is combined with the default style map.
     * Call this to ignore any embedded style maps.
     */
    public DocumentConverter disableEmbeddedStyleMap() {
        return new DocumentConverter(options.disableEmbeddedStyleMap());
    }

    /**
     * Converts {@code stream} into an HTML string.
     * Note that using this method instead of {@link #convertToHtml(File file)}
     * means that relative paths to other files, such as images, cannot be resolved.
     */
    public Result<String> convertToHtml(InputStream stream) throws IOException {
        return new InternalDocumentConverter(options).convertToHtml(stream).toResult();
    }

    /**
     * Converts {@code file} into an HTML string.
     */
    public Result<String> convertToHtml(File file) throws IOException {
        return new InternalDocumentConverter(options).convertToHtml(file).toResult();
    }

    /**
     * Extract the raw text of the document.
     * This will ignore all formatting in the document.
     * Each paragraph is followed by two newlines.
     */
    public Result<String> extractRawText(InputStream stream) throws IOException {
        return new InternalDocumentConverter(options).extractRawText(stream).toResult();
    }

    /**
     * Extract the raw text of the document.
     * This will ignore all formatting in the document.
     * Each paragraph is followed by two newlines.
     */
    public Result<String> extractRawText(File file) throws IOException {
        return new InternalDocumentConverter(options).extractRawText(file).toResult();
    }
}