blob: d466beddba356d892e1afdafe99f5907d9320479 (
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
|
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.xml.XmlElement;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;
public class Mammoth {
public static Result<String> convertToHtml(File file) {
try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
XmlElement documentXml = OfficeXml.parseXml(zipFile.getInputStream("word/document.xml"));
Styles styles = Styles.EMPTY;
Numbering numbering = Numbering.EMPTY;
Relationships relationships = Relationships.EMPTY;
ContentTypes contentTypes = ContentTypes.DEFAULT;
Notes notes = Notes.EMPTY;
FileReader fileReader = uri -> {
throw new UnsupportedOperationException();
};
DocumentXmlReader reader = new DocumentXmlReader(new BodyXmlReader(
styles,
numbering,
relationships,
contentTypes,
zipFile,
fileReader), notes);
// TODO: prefix
String idPrefix = "document";
return reader.readElement(documentXml)
.map(nodes -> DocumentConverter.convertToHtml(idPrefix, nodes))
.map(Html::stripEmpty)
.map(Html::collapse)
.map(Html::write);
} catch (IOException e) {
throw new UnsupportedOperationException("Should return a result of failure");
}
}
}
|