Skip to content

Commit

Permalink
Fixes #134: Retain empty P in empty <td> (#137)
Browse files Browse the repository at this point in the history
Co-authored-by: Eliot Kimber <ekimber@contrext.com>
  • Loading branch information
drmacro and contrext authored Jan 14, 2024
1 parent 9493074 commit 12c47f5
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions src/main/java/org/wordinator/xml2docx/generator/DocxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2963,49 +2963,54 @@ private XWPFTableRow makeTableRow(
ctTcPr.setVMerge(CTVMerge.Factory.newInstance());
}
} else {
// Cells always have at least one paragraph.
cell.removeParagraph(0);

// convert the contents of the cell
boolean hasMore = cursor.toFirstChild();
while (hasMore) {
if (cursor.getName().equals(DocxConstants.QNAME_P_ELEM)) {
XWPFParagraph p = cell.addParagraph();
makeParagraph(p, cursor);
if (null != align) {
if ("JUSTIFY".equalsIgnoreCase(align)) {
// Issue 18: "BOTH" is the better match to "JUSTIFY"
align = "BOTH"; // Slight mistmatch between markup and model
}
if ("CHAR".equalsIgnoreCase(align)) {
// I'm not sure this is the best mapping but it seemed close enough
align = "NUM_TAB"; // Slight mistmatch between markup and model
// Issue 134: If <td> is empty, hasMore will be false.
if (!hasMore) {
// Leave the empty paragraph, which is required by Word.
} else {
// Cells always have at least one paragraph.
cell.removeParagraph(0);

while (hasMore) {
if (cursor.getName().equals(DocxConstants.QNAME_P_ELEM)) {
XWPFParagraph p = cell.addParagraph();
makeParagraph(p, cursor);
if (null != align) {
if ("JUSTIFY".equalsIgnoreCase(align)) {
// Issue 18: "BOTH" is the better match to "JUSTIFY"
align = "BOTH"; // Slight mistmatch between markup and model
}
if ("CHAR".equalsIgnoreCase(align)) {
// I'm not sure this is the best mapping but it seemed close enough
align = "NUM_TAB"; // Slight mistmatch between markup and model
}
ParagraphAlignment alignment = ParagraphAlignment.valueOf(align.toUpperCase());
p.setAlignment(alignment);
}
ParagraphAlignment alignment = ParagraphAlignment.valueOf(align.toUpperCase());
p.setAlignment(alignment);
} else if (cursor.getName().equals(DocxConstants.QNAME_TABLE_ELEM)) {
// record how many tables were in the cell previously
int preTables = cell.getCTTc().getTblList().size();

CTTbl ctTbl = cell.getCTTc().addNewTbl();
ctTbl = cell.getCTTc().addNewTbl();
CTTblPr tblPr = ctTbl.addNewTblPr();
tblPr.addNewTblW();

XWPFTable nestedTable = new XWPFTable(ctTbl, cell);
makeTable(nestedTable, cursor.getObject());

// for some reason this inserts two tables, where the
// first one is empty. we need to remove that one.
// luckily, the number of tables we used to have equals
// the index of the first new table
cell.getCTTc().removeTbl(preTables);
} else {
log.warn("Table cell contains unknown element {} -- skipping", cursor.getName());
}
} else if (cursor.getName().equals(DocxConstants.QNAME_TABLE_ELEM)) {
// record how many tables were in the cell previously
int preTables = cell.getCTTc().getTblList().size();

CTTbl ctTbl = cell.getCTTc().addNewTbl();
ctTbl = cell.getCTTc().addNewTbl();
CTTblPr tblPr = ctTbl.addNewTblPr();
tblPr.addNewTblW();

XWPFTable nestedTable = new XWPFTable(ctTbl, cell);
makeTable(nestedTable, cursor.getObject());

// for some reason this inserts two tables, where the
// first one is empty. we need to remove that one.
// luckily, the number of tables we used to have equals
// the index of the first new table
cell.getCTTc().removeTbl(preTables);
} else {
log.warn("Table cell contains unknown element {} -- skipping", cursor.getName());

hasMore = cursor.toNextSibling();
}

hasMore = cursor.toNextSibling();
}
}
cursor.pop();
Expand Down

0 comments on commit 12c47f5

Please sign in to comment.