Skip to content

Commit cb74aeb

Browse files
committed
Fixed #551 NPE when using relative image width in an HTML table.
1 parent 95c7728 commit cb74aeb

28 files changed

+151
-7
lines changed

plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/generator/M2DocEvaluator.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ private int getAbsoluteImageWidth(XWPFParagraph paragraph, MImage image) {
10671067
final CTSectPr section = paragraph.getCTP().getPPr().getSectPr();
10681068
res = getAbsoluteWidthFromSection(image, section);
10691069
} else {
1070-
// no section attached to the paragraph try the containing boby
1070+
// no section attached to the paragraph try the containing body
10711071
final IBody body = paragraph.getBody();
10721072
if (body instanceof XWPFDocument) {
10731073
final CTSectPr section = ((XWPFDocument) body).getDocument().getBody().getSectPr();
@@ -1078,8 +1078,20 @@ private int getAbsoluteImageWidth(XWPFParagraph paragraph, MImage image) {
10781078
.getSectPr();
10791079
res = getAbsoluteWidthFromSection(image, section);
10801080
} else if (body instanceof XWPFTableCell) {
1081-
final int cellWidth = ((XWPFTableCell) body).getWidth();
1082-
res = (int) (cellWidth * image.getRelativeWidth() / 100.0d / UNIT_CONVERSION);
1081+
final XWPFTableCell cell = (XWPFTableCell) body;
1082+
final int cellWidth;
1083+
// avoid an NPE from POI
1084+
if (cell.getCTTc() != null && cell.getCTTc().getTcPr() != null
1085+
&& cell.getCTTc().getTcPr().getTcW() != null && cell.getCTTc().getTcPr().getTcW().getW() != null) {
1086+
cellWidth = cell.getWidth();
1087+
} else {
1088+
cellWidth = 0;
1089+
}
1090+
if (cellWidth > 0) {
1091+
res = (int) (cellWidth * image.getRelativeWidth() / 100.0d / UNIT_CONVERSION);
1092+
} else {
1093+
res = image.getWidth();
1094+
}
10831095
} else {
10841096
throw new UnsupportedOperationException("unknown type of IBody : " + body.getClass());
10851097
}
@@ -1133,7 +1145,7 @@ private int getAbsoluteImageHeight(XWPFParagraph paragraph, MImage image) {
11331145
final CTSectPr section = paragraph.getCTP().getPPr().getSectPr();
11341146
res = getAbsoluteHeightFromSection(image, section);
11351147
} else {
1136-
// no section attached to the paragraph try the containing boby
1148+
// no section attached to the paragraph try the containing body
11371149
final IBody body = paragraph.getBody();
11381150
if (body instanceof XWPFDocument) {
11391151
final CTSectPr section = ((XWPFDocument) body).getDocument().getBody().getSectPr();
@@ -1144,8 +1156,12 @@ private int getAbsoluteImageHeight(XWPFParagraph paragraph, MImage image) {
11441156
.getSectPr();
11451157
res = getAbsoluteHeightFromSection(image, section);
11461158
} else if (body instanceof XWPFTableCell) {
1147-
// TODO technically the cell height depend on its content
1148-
res = image.getHeight();
1159+
final int rowHeight = ((XWPFTableCell) body).getTableRow().getHeight();
1160+
if (rowHeight > 0) {
1161+
res = (int) (rowHeight * image.getRelativeHeight() / 100.0d / UNIT_CONVERSION);
1162+
} else {
1163+
res = image.getHeight();
1164+
}
11491165
} else {
11501166
throw new UnsupportedOperationException("unknown type of IBody : " + body.getClass());
11511167
}
25.7 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<img src="Mona_Lisa.jpg" height="30%">
6+
</table>
7+
</body>
8+
</html>
9+
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
=== PROPERTIES ===
3+
version: 5.0.0
4+
metamodels:
5+
imports:
6+
=== HEADER ===
7+
8+
=== BODY ===
9+
10+
A simple demonstration of a query :
11+
table
12+
row
13+
14+
15+
16+
17+
18+
19+
20+
row
21+
22+
23+
24+
[query: .fromHTMLURI('doc.html')]
25+
26+
27+
28+
row
29+
30+
31+
32+
33+
34+
35+
36+
37+
End of demonstration.
38+
=== FOOTER ===
39+
40+
=== TEMPLATES ===

tests/org.obeonetwork.m2doc.html.tests/resources/html/imgWithRelativeHeightInTable/imgWithRelativeHeightInTable-expected-generation-messages.txt

Whitespace-only changes.

tests/org.obeonetwork.m2doc.html.tests/resources/html/imgWithRelativeHeightInTable/imgWithRelativeHeightInTable-expected-validation.docx

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="imgWithWidth" templateFileName="imgWithWidth-template.docx" resultFileName="imgWithWidth-actual-generation.docx" validationFileName="imgWithWidth-actual-validation.docx"/>

0 commit comments

Comments
 (0)