DocxImporter.java
7.25 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.poi.POIXMLException
* org.apache.poi.xwpf.usermodel.UnderlinePatterns
* org.apache.poi.xwpf.usermodel.VerticalAlign
* org.apache.poi.xwpf.usermodel.XWPFDocument
* org.apache.poi.xwpf.usermodel.XWPFParagraph
* org.apache.poi.xwpf.usermodel.XWPFPicture
* org.apache.poi.xwpf.usermodel.XWPFPictureData
* org.apache.poi.xwpf.usermodel.XWPFRun
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.offline;
import com.day.cq.wcm.offline.HtmlUtil;
import com.day.cq.wcm.offline.Paragraph;
import com.day.cq.wcm.offline.Picture;
import com.day.cq.wcm.offline.TextDocumentImporter;
import com.day.cq.wcm.offline.TextImportException;
import com.day.cq.wcm.offline.WordStyleSupport;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.poi.POIXMLException;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DocxImporter
implements TextDocumentImporter {
private static final Logger log = LoggerFactory.getLogger(DocxImporter.class);
private final XWPFDocument document;
private final List<XWPFParagraph> paragraphs;
public DocxImporter(InputStream stream) throws TextImportException, IOException {
try {
this.document = new XWPFDocument(stream);
ArrayList<XWPFParagraph> tmp = new ArrayList<XWPFParagraph>();
for (XWPFParagraph p : this.document.getParagraphs()) {
if (p.isEmpty() || p.isPageBreak()) continue;
tmp.add(p);
}
this.paragraphs = Collections.unmodifiableList(tmp);
}
catch (POIXMLException ex) {
throw new TextImportException("not a docx file", (Throwable)ex);
}
}
public String getTitle() {
for (XWPFParagraph p : this.paragraphs) {
String t = p.getText().trim();
if ("".equals(t)) continue;
return t;
}
return null;
}
public int getNumberOfParagraphs() {
return this.paragraphs.size();
}
public Paragraph getParagraph(int index) {
return new DocxParagraph(this.paragraphs.get(index));
}
private class DocxPicture
implements Picture {
private final XWPFPicture picture;
public DocxPicture(XWPFPicture picture) {
this.picture = picture;
}
public String getMediaType() {
switch (this.picture.getPictureData().getPictureType()) {
case 8: {
return "image/gif";
}
case 5: {
return "image/jepg";
}
case 6: {
return "image/png";
}
}
log.error("Unknown picture type " + this.picture.getPictureData().getPictureType() + " - need to define media type mapping");
return null;
}
public byte[] getBytes() {
return this.picture.getPictureData().getData();
}
public String toString() {
return this.getMediaType() + " (" + this.getBytes().length + " bytes)";
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private class DocxParagraph
implements Paragraph {
private final XWPFParagraph p;
private final String text;
private final String textHTML;
private final List<Picture> pictures;
public DocxParagraph(XWPFParagraph p) {
this.p = p;
String classname = WordStyleSupport.makeClassName(p.getStyle());
String container = "p";
String elemname = WordStyleSupport.toHtmlElement(classname);
if (elemname != null) {
container = elemname;
classname = null;
}
StringBuilder sbtext = new StringBuilder();
StringBuilder sbhtml = new StringBuilder();
ArrayList<DocxPicture> pics = new ArrayList<DocxPicture>();
for (XWPFRun run : this.p.getRuns()) {
String textdata;
if (run.isBold()) {
sbhtml.append("<b>");
}
if (run.isItalic()) {
sbhtml.append("<i>");
}
if (run.isStrike()) {
sbhtml.append("<del>");
}
if (run.getUnderline() != UnderlinePatterns.NONE) {
sbhtml.append("<u>");
}
if (run.getSubscript() == VerticalAlign.SUBSCRIPT) {
sbhtml.append("<sub>");
}
if (run.getSubscript() == VerticalAlign.SUPERSCRIPT) {
sbhtml.append("<sup>");
}
if ((textdata = run.getText(0)) != null) {
sbtext.append(textdata);
sbhtml.append(HtmlUtil.escapeHtmlText(textdata));
}
if (run.getSubscript() == VerticalAlign.SUPERSCRIPT) {
sbhtml.append("</sup>");
}
if (run.getSubscript() == VerticalAlign.SUBSCRIPT) {
sbhtml.append("</sub>");
}
if (run.getUnderline() != UnderlinePatterns.NONE) {
sbhtml.append("</u>");
}
if (run.isStrike()) {
sbhtml.append("</del>");
}
if (run.isItalic()) {
sbhtml.append("</i>");
}
if (run.isBold()) {
sbhtml.append("</b>");
}
for (XWPFPicture pic : run.getEmbeddedPictures()) {
pics.add(new DocxPicture(pic));
}
}
String result = sbhtml.toString().trim();
if (result.length() > 0) {
StringBuilder tmp = new StringBuilder();
tmp.append("<");
tmp.append(container);
if (classname != null) {
tmp.append(" class='" + HtmlUtil.escapeHtmlAttr(classname) + "'");
}
tmp.append(">");
tmp.append(result);
tmp.append("</");
tmp.append(container);
tmp.append(">");
result = tmp.toString();
}
this.text = sbtext.toString().trim();
this.textHTML = result;
this.pictures = Collections.unmodifiableList(pics);
}
@Override
public String getText() {
return this.text;
}
@Override
public String getHTML() {
return this.textHTML;
}
@Override
public List<Picture> getPictures() {
return this.pictures;
}
}
}