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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
package org.zwobble.mammoth.internal.docx;
import org.zwobble.mammoth.internal.documents.*;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.util.Casts;
import org.zwobble.mammoth.internal.util.InputStreamSupplier;
import org.zwobble.mammoth.internal.util.Lists;
import org.zwobble.mammoth.internal.util.Optionals;
import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlElementLike;
import org.zwobble.mammoth.internal.xml.XmlElementList;
import org.zwobble.mammoth.internal.xml.XmlNode;
import java.util.*;
import java.util.function.Function;
import static org.zwobble.mammoth.internal.docx.ReadResult.EMPTY_SUCCESS;
import static org.zwobble.mammoth.internal.docx.ReadResult.success;
import static org.zwobble.mammoth.internal.util.Iterables.lazyFilter;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.entry;
import static org.zwobble.mammoth.internal.util.Maps.lookup;
import static org.zwobble.mammoth.internal.util.Sets.set;
import static org.zwobble.mammoth.internal.util.Strings.trimLeft;
public class BodyXmlReader {
private static final Set<String> IMAGE_TYPES_SUPPORTED_BY_BROWSERS = set(
"image/png", "image/gif", "image/jpeg", "image/svg+xml", "image/tiff");
private final Styles styles;
private final Numbering numbering;
private final Relationships relationships;
private final ContentTypes contentTypes;
private final DocxFile file;
private final FileReader fileReader;
public BodyXmlReader(
Styles styles,
Numbering numbering,
Relationships relationships,
ContentTypes contentTypes,
DocxFile file,
FileReader fileReader)
{
this.styles = styles;
this.numbering = numbering;
this.relationships = relationships;
this.contentTypes = contentTypes;
this.file = file;
this.fileReader = fileReader;
}
public ReadResult readElement(XmlElement element) {
switch (element.getName()) {
case "w:t":
return success(new Text(element.innerText()));
case "w:r":
return readRun(element);
case "w:p":
return readParagraph(element);
case "w:tab":
return success(Tab.TAB);
case "w:br":
return readBreak(element);
case "w:tbl":
return readTable(element);
case "w:tr":
return readElements(element.getChildren()).map(TableRow::new);
case "w:tc":
return readTableCell(element);
case "w:hyperlink":
return readHyperlink(element);
case "w:bookmarkStart":
return readBookmark(element);
case "w:footnoteReference":
return readNoteReference(NoteType.FOOTNOTE, element);
case "w:endnoteReference":
return readNoteReference(NoteType.ENDNOTE, element);
case "w:pict":
return readPict(element);
case "v:imagedata":
return readImagedata(element);
case "wp:inline":
case "wp:anchor":
return readInline(element);
case "w:ins":
case "w:smartTag":
case "w:drawing":
case "v:roundrect":
case "v:shape":
case "v:textbox":
case "w:txbxContent":
return readElements(element.getChildren());
case "office-word:wrap":
case "v:shadow":
case "v:shapetype":
case "w:bookmarkEnd":
case "w:sectPr":
case "w:proofErr":
case "w:lastRenderedPageBreak":
case "w:commentRangeStart":
case "w:commentRangeEnd":
case "w:commentReference":
case "w:del":
case "w:footnoteRef":
case "w:endnoteRef":
case "w:pPr":
case "w:rPr":
case "w:tblPr":
case "w:tblGrid":
case "w:tcPr":
return EMPTY_SUCCESS;
default:
String warning = "An unrecognised element was ignored: " + element.getName();
return ReadResult.emptyWithWarning(warning);
}
}
private ReadResult readRun(XmlElement element) {
XmlElementLike properties = element.findChildOrEmpty("w:rPr");
return ReadResult.map(
readRunStyle(properties),
readElements(element.getChildren()),
(style, children) -> new Run(
isBold(properties),
isItalic(properties),
isUnderline(properties),
isStrikethrough(properties),
readVerticalAlignment(properties),
style,
children));
}
private boolean isBold(XmlElementLike properties) {
return properties.hasChild("w:b");
}
private boolean isItalic(XmlElementLike properties) {
return properties.hasChild("w:i");
}
private boolean isUnderline(XmlElementLike properties) {
return properties.hasChild("w:u");
}
private boolean isStrikethrough(XmlElementLike properties) {
return properties.hasChild("w:strike");
}
private VerticalAlignment readVerticalAlignment(XmlElementLike properties) {
String verticalAlignment = readVal(properties, "w:vertAlign").orElse("");
switch (verticalAlignment) {
case "superscript":
return VerticalAlignment.SUPERSCRIPT;
case "subscript":
return VerticalAlignment.SUBSCRIPT;
default:
// TODO: warn if set?
return VerticalAlignment.BASELINE;
}
}
private InternalResult<Optional<Style>> readRunStyle(XmlElementLike properties) {
return readStyle(properties, "w:rStyle", "Run", styles::findCharacterStyleById);
}
public ReadResult readElements(Iterable<XmlNode> nodes) {
return ReadResult.flatMap(lazyFilter(nodes, XmlElement.class), this::readElement);
}
private ReadResult readParagraph(XmlElement element) {
XmlElementLike properties = element.findChildOrEmpty("w:pPr");
Optional<NumberingLevel> numbering = readNumbering(properties);
return ReadResult.map(
readParagraphStyle(properties),
readElements(element.getChildren()),
(style, children) -> new Paragraph(style, numbering, children)).appendExtra();
}
private InternalResult<Optional<Style>> readParagraphStyle(XmlElementLike properties) {
return readStyle(properties, "w:pStyle", "Paragraph", styles::findParagraphStyleById);
}
private InternalResult<Optional<Style>> readStyle(
XmlElementLike properties,
String styleTagName,
String styleType,
Function<String, Optional<Style>> findStyleById)
{
return readVal(properties, styleTagName)
.map(styleId -> findStyleById(styleType, styleId, findStyleById))
.orElse(InternalResult.empty());
}
private InternalResult<Optional<Style>> findStyleById(
String styleType,
String styleId,
Function<String, Optional<Style>> findStyleById)
{
Optional<Style> style = findStyleById.apply(styleId);
if (style.isPresent()) {
return InternalResult.success(style);
} else {
return new InternalResult<>(
Optional.of(new Style(styleId, Optional.empty())),
list(styleType + " style with ID " + styleId + " was referenced but not defined in the document"));
}
}
private Optional<NumberingLevel> readNumbering(XmlElementLike properties) {
XmlElementLike numberingProperties = properties.findChildOrEmpty("w:numPr");
return Optionals.flatMap(
readVal(numberingProperties, "w:numId"),
readVal(numberingProperties, "w:ilvl"),
numbering::findLevel);
}
private ReadResult readBreak(XmlElement element) {
String breakType = element.getAttributeOrNone("w:type").orElse("");
if (breakType.equals("")) {
return success(LineBreak.LINE_BREAK);
} else {
return ReadResult.emptyWithWarning("Unsupported break type: " + breakType);
}
}
private ReadResult readTable(XmlElement element) {
return readElements(element.getChildren())
.flatMap(this::calculateRowspans)
.map(Table::new);
}
private ReadResult calculateRowspans(List<DocumentElement> rows) {
Optional<String> error = checkTableRows(rows);
if (error.isPresent()) {
return ReadResult.withWarning(rows, error.get());
}
Map<Map.Entry<Integer, Integer>, Integer> rowspans = new HashMap<>();
Set<Map.Entry<Integer, Integer>> merged = new HashSet<>();
Map<Integer, Map.Entry<Integer, Integer>> lastCellForColumn = new HashMap<>();
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex += 1) {
TableRow row = (TableRow) rows.get(rowIndex);
int columnIndex = 0;
for (int cellIndex = 0; cellIndex < row.getChildren().size(); cellIndex += 1) {
UnmergedTableCell cell = (UnmergedTableCell) row.getChildren().get(cellIndex);
Optional<Map.Entry<Integer, Integer>> spanningCell = lookup(lastCellForColumn, columnIndex);
Map.Entry<Integer, Integer> position = entry(rowIndex, cellIndex);
if (cell.vmerge && spanningCell.isPresent()) {
rowspans.put(spanningCell.get(), lookup(rowspans, spanningCell.get()).get() + 1);
merged.add(position);
} else {
lastCellForColumn.put(columnIndex, position);
rowspans.put(position, 1);
}
columnIndex += cell.colspan;
}
}
return success(Lists.eagerMapWithIndex(rows, (rowIndex, rowElement) -> {
TableRow row = (TableRow) rowElement;
List<DocumentElement> mergedCells = new ArrayList<>();
for (int cellIndex = 0; cellIndex < row.getChildren().size(); cellIndex += 1) {
UnmergedTableCell cell = (UnmergedTableCell) row.getChildren().get(cellIndex);
Map.Entry<Integer, Integer> position = entry(rowIndex, cellIndex);
if (!merged.contains(position)) {
mergedCells.add(new TableCell(
lookup(rowspans, position).get(),
cell.colspan,
cell.children
));
}
}
return new TableRow(mergedCells);
}));
}
private Optional<String> checkTableRows(List<DocumentElement> rows) {
for (DocumentElement rowElement : rows) {
Optional<TableRow> row = Casts.tryCast(TableRow.class, rowElement);
if (!row.isPresent()) {
return Optional.of("unexpected non-row element in table, cell merging may be incorrect");
} else {
for (DocumentElement cell : row.get().getChildren()) {
if (!(cell instanceof UnmergedTableCell)) {
return Optional.of("unexpected non-cell element in table row, cell merging may be incorrect");
}
}
}
}
return Optional.empty();
}
private ReadResult readTableCell(XmlElement element) {
XmlElementLike properties = element.findChildOrEmpty("w:tcPr");
Optional<String> gridSpan = properties
.findChildOrEmpty("w:gridSpan")
.getAttributeOrNone("w:val");
int colspan = gridSpan.map(Integer::parseInt).orElse(1);
return readElements(element.getChildren())
.map(children -> new UnmergedTableCell(readVmerge(properties), colspan, children));
}
private boolean readVmerge(XmlElementLike properties) {
return properties.findChild("w:vMerge")
.map(element -> element.getAttributeOrNone("w:val").map(val -> val.equals("continue")).orElse(true))
.orElse(false);
}
private static class UnmergedTableCell implements DocumentElement {
private final boolean vmerge;
private final int colspan;
private final List<DocumentElement> children;
private UnmergedTableCell(boolean vmerge, int colspan, List<DocumentElement> children) {
this.vmerge = vmerge;
this.colspan = colspan;
this.children = children;
}
@Override
public <T> T accept(DocumentElementVisitor<T> visitor) {
return visitor.visit(new TableCell(1, colspan, children));
}
}
private ReadResult readHyperlink(XmlElement element) {
Optional<String> relationshipId = element.getAttributeOrNone("r:id");
Optional<String> anchor = element.getAttributeOrNone("w:anchor");
ReadResult childrenResult = readElements(element.getChildren());
if (relationshipId.isPresent()) {
return childrenResult.map(children -> Hyperlink.href(
relationships.findRelationshipById(relationshipId.get()).getTarget(), children));
} else if (anchor.isPresent()) {
return childrenResult.map(children -> Hyperlink.anchor(
anchor.get(), children));
} else {
return childrenResult;
}
}
private ReadResult readBookmark(XmlElement element) {
String name = element.getAttribute("w:name");
if (name.equals("_GoBack")) {
return ReadResult.EMPTY_SUCCESS;
} else {
return success(new Bookmark(name));
}
}
private ReadResult readNoteReference(NoteType noteType, XmlElement element) {
String noteId = element.getAttribute("w:id");
return success(new NoteReference(noteType, noteId));
}
private ReadResult readPict(XmlElement element) {
return readElements(element.getChildren()).toExtra();
}
private ReadResult readImagedata(XmlElement element) {
Optional<String> title = element.getAttributeOrNone("o:title");
String relationshipId = element.getAttribute("r:id");
String imagePath = relationshipIdToDocxPath(relationshipId);
return readImage(imagePath, title, () -> DocxFiles.getInputStream(file, imagePath));
}
private ReadResult readInline(XmlElement element) {
Optional<String> altText = element.findChildOrEmpty("wp:docPr").getAttributeOrNone("descr");
XmlElementList blips = element.findChildren("a:graphic")
.findChildren("a:graphicData")
.findChildren("pic:pic")
.findChildren("pic:blipFill")
.findChildren("a:blip");
return readBlips(blips, altText);
}
private ReadResult readBlips(XmlElementList blips, Optional<String> altText) {
return ReadResult.flatMap(blips, blip -> readBlip(blip, altText));
}
private ReadResult readBlip(XmlElement blip, Optional<String> altText) {
Optional<String> embedRelationshipId = blip.getAttributeOrNone("r:embed");
Optional<String> linkRelationshipId = blip.getAttributeOrNone("r:link");
if (embedRelationshipId.isPresent()) {
String imagePath = relationshipIdToDocxPath(embedRelationshipId.get());
return readImage(imagePath, altText, () -> DocxFiles.getInputStream(file, imagePath));
} else if (linkRelationshipId.isPresent()) {
String imagePath = relationships.findRelationshipById(linkRelationshipId.get()).getTarget();
return readImage(imagePath, altText, () -> fileReader.getInputStream(imagePath));
} else {
// TODO: emit warning
return ReadResult.EMPTY_SUCCESS;
}
}
private ReadResult readImage(String imagePath, Optional<String> altText, InputStreamSupplier open) {
Optional<String> contentType = contentTypes.findContentType(imagePath);
Image image = new Image(altText, contentType, open);
String contentTypeString = contentType.orElse("(unknown)");
if (IMAGE_TYPES_SUPPORTED_BY_BROWSERS.contains(contentTypeString)) {
return success(image);
} else {
return ReadResult.withWarning(image, "Image of type " + contentTypeString + " is unlikely to display in web browsers");
}
}
private String relationshipIdToDocxPath(String relationshipId) {
Relationship relationship = relationships.findRelationshipById(relationshipId);
return "word/" + trimLeft(relationship.getTarget(), '/');
}
private Optional<String> readVal(XmlElementLike element, String name) {
return element.findChildOrEmpty(name).getAttributeOrNone("w:val");
}
}
|