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

import org.zwobble.mammoth.images.ImageConverter;
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());
    }

    /**
     * Source documents may reference files outside of the source document.
     * Call this to disable access to any such external files during the conversion process.
     * This is highly recommended when converting untrusted user input.
     */
    public DocumentConverter disableExternalFileAccess() {
        return new DocumentConverter(options.disableExternalFileAccess());
    }

    /**
     * By default, images are converted to {@code <img>} elements with the source included inline in the {@code src} attribute.
     * Call this to change how images are converted.
     */
    public DocumentConverter imageConverter(ImageConverter.ImgElement imageConverter) {
        return new DocumentConverter(options.imageConverter(imageConverter));
    }

    /**
     * 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();
    }
}