This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 06d60e6cc4 XWPF: SDT and custom XML table rows; simplify XWPFTable row
insert/remove (#1431)
06d60e6cc4 is described below
commit 06d60e6cc4e05423bcab85f439807d153fec3464
Author: PJ Fanning <[email protected]>
AuthorDate: Sat Oct 17 21:39:35 2026 +0100
XWPF: SDT and custom XML table rows; simplify XWPFTable row insert/remove
(#1431)
* XWPF: find table cells in SDT rows; simplify XWPFTable row insert/remove
getTableCell(CTTc) in XWPFDocument, XWPFHeaderFooter, XWPFTableCell,
XWPFComment and XWPFAbstractFootnoteEndnote expected CTTc -> CTRow -> CTTbl,
so it returned null for cells in rows wrapped in w:sdt. They now share
XWPFTable.getParentTable, which looks through SDT rows.
XWPFTable.addRow(row, pos), insertNewTableRow and removeRow each had a
copy of the same parent dispatch (CTTbl or CTSdtContentRow, index lookup,
fallbacks). Insert and remove with a cursor on the row instead, which works
whatever holds it. Document that inserting before a row in an SDT puts the
new row inside the SDT.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* XWPFTable: read rows wrapped in w:customXml; keep addRow rows live
Rows inside a custom XML row (w:customXml) were skipped, like SDT rows
before bug 66263. Collect rows through any mix of w:sdt and w:customXml,
and handle custom XML rows in getParentTable, row insert and remove, and
the cleanup of wrappers left empty. Add CTCustomXmlRow to the
poi-ooxml-lite report.
addRow(row) and addRow(row, pos) copied the row's XML into the table but
kept the row object pointing at the original, so later changes made
through it were lost. Point the row at the copy in the table instead.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../usermodel/XWPFAbstractFootnoteEndnote.java | 5 +-
.../org/apache/poi/xwpf/usermodel/XWPFComment.java | 5 +-
.../apache/poi/xwpf/usermodel/XWPFDocument.java | 5 +-
.../poi/xwpf/usermodel/XWPFHeaderFooter.java | 5 +-
.../org/apache/poi/xwpf/usermodel/XWPFTable.java | 296 +++++++++------------
.../apache/poi/xwpf/usermodel/XWPFTableCell.java | 5 +-
.../apache/poi/xwpf/usermodel/XWPFTableRow.java | 12 +-
.../apache/poi/xwpf/usermodel/TestXWPFTable.java | 148 +++++++++++
src/resources/ooxml-lite-report.clazz | 4 +
src/resources/ooxml-lite-report.xsb | 1 +
10 files changed, 296 insertions(+), 190 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
index 4aaec31eb2..3979cd1ee8 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java
@@ -256,10 +256,9 @@ public abstract class XWPFAbstractFootnoteEndnote
implements Iterable<XWPFParag
return null;
}
row = ctRow;
- cursor.toParent();
- o = cursor.getObject();
}
- if (!(o instanceof CTTbl tbl)) {
+ CTTbl tbl = XWPFTable.getParentTable(row);
+ if (tbl == null) {
return null;
}
XWPFTable table = getTable(tbl);
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java
index 021192e905..875b87e7ff 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFComment.java
@@ -264,10 +264,9 @@ public class XWPFComment implements IBody {
return null;
}
row = ctRow;
- cursor.toParent();
- o = cursor.getObject();
}
- if (!(o instanceof CTTbl tbl)) {
+ CTTbl tbl = XWPFTable.getParentTable(row);
+ if (tbl == null) {
return null;
}
XWPFTable table = getTable(tbl);
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
index a3fb3472f4..4493742829 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
@@ -1879,10 +1879,9 @@ public class XWPFDocument extends POIXMLDocument
implements Document, IBody {
return null;
}
row = ctRow;
- cursor.toParent();
- o = cursor.getObject();
}
- if (!(o instanceof CTTbl tbl)) {
+ CTTbl tbl = XWPFTable.getParentTable(row);
+ if (tbl == null) {
return null;
}
XWPFTable table = getTable(tbl);
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
index 3cf1e2876d..4d541d4a8f 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java
@@ -570,10 +570,9 @@ public abstract class XWPFHeaderFooter extends
POIXMLDocumentPart implements IBo
return null;
}
row = ctRow;
- cursor.toParent();
- o = cursor.getObject();
}
- if (!(o instanceof CTTbl tbl)) {
+ CTTbl tbl = XWPFTable.getParentTable(row);
+ if (tbl == null) {
return null;
}
XWPFTable table = getTable(tbl);
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
index 68e0f25bac..6e4e70505c 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
@@ -18,7 +18,6 @@ package org.apache.poi.xwpf.usermodel;
import java.math.BigInteger;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
@@ -34,6 +33,8 @@ import org.apache.poi.util.Units;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTCustomXmlPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTCustomXmlRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJcTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
@@ -168,26 +169,16 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
this.ctTbl = table;
// is an empty table: I add one row and one column as default
- // Check if table has any row-like content (either TR or SDT-wrapped
rows)
- if (initRow && table.sizeOfTrArray() == 0 && table.sizeOfSdtArray() ==
0) {
+ // Check if table has any row-like content (TR, or rows wrapped in SDT
or custom XML)
+ if (initRow && table.sizeOfTrArray() == 0 && table.sizeOfSdtArray() == 0
+ && table.sizeOfCustomXmlArray() == 0) {
createEmptyTable(table);
}
- try (XmlCursor cursor = table.newCursor()) {
- cursor.selectPath("./*");
- while (cursor.toNextSelection()) {
- XmlObject xmlObject = cursor.getObject();
- if (xmlObject instanceof CTRow ctRow) {
- processCTRow(ctRow);
- }
- else if (xmlObject instanceof CTSdtRow sdtRow) {
- List<CTRow> rows = new ArrayList<>();
- collectCTRowsInnerSdtRow(sdtRow, rows);
- for (CTRow row : rows) {
- processCTRow(row);
- }
- }
- }
+ List<CTRow> rows = new ArrayList<>();
+ collectRows(table, rows);
+ for (CTRow row : rows) {
+ processCTRow(row);
}
}
@@ -214,13 +205,12 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
}
}
- private void collectCTRowsInnerSdtRow(CTSdtRow sdtRow, List<CTRow> rows) {
- CTSdtContentRow sdtContent = sdtRow.getSdtContent();
- if (sdtContent == null) {
- return;
- }
-
- try (XmlCursor cursor = sdtContent.newCursor()) {
+ /**
+ * Collect the rows in container in document order, including rows wrapped
+ * in SDT rows (w:sdt) or custom XML rows (w:customXml), however deeply
nested.
+ */
+ private static void collectRows(XmlObject container, List<CTRow> rows) {
+ try (XmlCursor cursor = container.newCursor()) {
if (!cursor.toFirstChild()) {
return;
}
@@ -228,8 +218,13 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
XmlObject child = cursor.getObject();
if (child instanceof CTRow ctRow) {
rows.add(ctRow);
- } else if (child instanceof CTSdtRow sdtChild) {
- collectCTRowsInnerSdtRow(sdtChild, rows);
+ } else if (child instanceof CTSdtRow sdtRow) {
+ CTSdtContentRow sdtContent = sdtRow.getSdtContent();
+ if (sdtContent != null) {
+ collectRows(sdtContent, rows);
+ }
+ } else if (child instanceof CTCustomXmlRow customXmlRow) {
+ collectRows(customXmlRow, rows);
}
} while (cursor.toNextSibling());
}
@@ -1150,6 +1145,10 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
/**
* add a new Row to the table
+ * <p>
+ * The row's XML is copied into the table and row is updated to use that
copy,
+ * so later changes made through row are kept. Cells fetched from row
before
+ * this call no longer belong to it.
*
* @param row the row which should be added
*/
@@ -1157,66 +1156,30 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
// Add to end of table
CTRow addedRow = ctTbl.addNewTr();
addedRow.set(row.getCtRow());
+ row.setCtRow(addedRow);
tableRows.add(row);
}
/**
* add a new Row to the table
* at position pos
+ * <p>
+ * If the row currently at pos is inside an SDT row (content control)
+ * or a custom XML row, the new row is added inside that too.
+ * <p>
+ * The row's XML is copied into the table and row is updated to use that
copy,
+ * so later changes made through row are kept. Cells fetched from row
before
+ * this call no longer belong to it.
*
* @param row the row which should be added
*/
public boolean addRow(XWPFTableRow row, int pos) {
if (pos >= 0 && pos <= tableRows.size()) {
- if (pos == tableRows.size()) {
- // Add to end of table
- CTRow addedRow = ctTbl.addNewTr();
- addedRow.set(row.getCtRow());
- } else {
- // Insert before the row currently at position pos
- XWPFTableRow nextRow = tableRows.get(pos);
- CTRow nextCTRow = nextRow.getCtRow();
-
- // Find where to insert by checking the parent of the next row
- try (XmlCursor cursor = nextCTRow.newCursor()) {
- if (cursor.toParent()) {
- XmlObject parent = cursor.getObject();
- if (parent instanceof CTTbl tbl) {
- // Top-level row - insert into table
- List<CTRow> trList =
Arrays.asList(tbl.getTrArray());
- int idx = trList.indexOf(nextCTRow);
- if (idx >= 0) {
- CTRow addedRow = tbl.insertNewTr(idx);
- addedRow.set(row.getCtRow());
- } else {
- // Fallback: add to end
- CTRow addedRow = ctTbl.addNewTr();
- addedRow.set(row.getCtRow());
- }
- } else if (parent instanceof CTSdtContentRow
sdtContent) {
- // SDT-wrapped row - insert into SDT content
- List<CTRow> trList =
Arrays.asList(sdtContent.getTrArray());
- int idx = trList.indexOf(nextCTRow);
- if (idx >= 0) {
- CTRow addedRow = sdtContent.insertNewTr(idx);
- addedRow.set(row.getCtRow());
- } else {
- // Fallback: add to end of SDT content
- CTRow addedRow = sdtContent.addNewTr();
- addedRow.set(row.getCtRow());
- }
- } else {
- // Unknown parent - add to table as fallback
- CTRow addedRow = ctTbl.addNewTr();
- addedRow.set(row.getCtRow());
- }
- } else {
- // No parent - add to table as fallback
- CTRow addedRow = ctTbl.addNewTr();
- addedRow.set(row.getCtRow());
- }
- }
- }
+ CTRow addedRow = pos == tableRows.size()
+ ? ctTbl.addNewTr()
+ : insertNewTrBefore(tableRows.get(pos).getCtRow());
+ addedRow.set(row.getCtRow());
+ row.setCtRow(addedRow);
tableRows.add(pos, row);
return true;
}
@@ -1225,57 +1188,18 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
/**
* inserts a new tablerow at position pos
+ * <p>
+ * If the row currently at pos is inside an SDT row (content control)
+ * or a custom XML row, the new row is inserted inside that too.
*
* @param pos the position to insert the new row
* @return the inserted row
*/
public XWPFTableRow insertNewTableRow(int pos) {
if (pos >= 0 && pos <= tableRows.size()) {
- CTRow insertedRow;
-
- if (pos == tableRows.size() || tableRows.isEmpty()) {
- // Insert at end or first row in empty table - add to table
- insertedRow = ctTbl.addNewTr();
- } else {
- // Insert before the row currently at position pos
- XWPFTableRow nextRow = tableRows.get(pos);
- CTRow nextCTRow = nextRow.getCtRow();
-
- // Find where to insert by checking the parent of the next row
- try (XmlCursor cursor = nextCTRow.newCursor()) {
- if (cursor.toParent()) {
- XmlObject parent = cursor.getObject();
- if (parent instanceof CTTbl tbl) {
- // Top-level row - insert into table
- List<CTRow> trList =
Arrays.asList(tbl.getTrArray());
- int idx = trList.indexOf(nextCTRow);
- if (idx >= 0) {
- insertedRow = tbl.insertNewTr(idx);
- } else {
- // Fallback: add to end of table
- insertedRow = ctTbl.addNewTr();
- }
- } else if (parent instanceof CTSdtContentRow
sdtContent) {
- // SDT-wrapped row - insert into SDT content
- List<CTRow> trList =
Arrays.asList(sdtContent.getTrArray());
- int idx = trList.indexOf(nextCTRow);
- if (idx >= 0) {
- insertedRow = sdtContent.insertNewTr(idx);
- } else {
- // Fallback: add to end of SDT content
- insertedRow = sdtContent.addNewTr();
- }
- } else {
- // Unknown parent - add to table as fallback
- insertedRow = ctTbl.addNewTr();
- }
- } else {
- // No parent - add to table as fallback
- insertedRow = ctTbl.addNewTr();
- }
- }
- }
-
+ CTRow insertedRow = pos == tableRows.size()
+ ? ctTbl.addNewTr()
+ : insertNewTrBefore(tableRows.get(pos).getCtRow());
XWPFTableRow tableRow = new XWPFTableRow(insertedRow, this);
tableRows.add(pos, tableRow);
return tableRow;
@@ -1283,6 +1207,22 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
return null;
}
+ /**
+ * Insert a new w:tr just before next, in whatever holds next: the table
itself,
+ * the content of an SDT row or a custom XML row. If next is in none of
these,
+ * add the new row at the end of the table.
+ */
+ private CTRow insertNewTrBefore(CTRow next) {
+ if (!isRowContainer(getParent(next))) {
+ return ctTbl.addNewTr();
+ }
+ try (XmlCursor cursor = next.newCursor()) {
+ cursor.beginElement("tr", CTRow.type.getName().getNamespaceURI());
+ cursor.toParent();
+ return (CTRow) cursor.getObject();
+ }
+ }
+
/**
* Remove a row at position pos from the table
*
@@ -1290,40 +1230,15 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
*/
public boolean removeRow(int pos) throws IndexOutOfBoundsException {
if (pos >= 0 && pos < tableRows.size()) {
- XWPFTableRow row = tableRows.get(pos);
- CTRow ctRow = row.getCtRow();
-
- // Remove the row from its actual parent (could be CTTbl or
CTSdtContentRow)
- try (XmlCursor cursor = ctRow.newCursor()) {
- if (cursor.toParent()) {
- XmlObject parent = cursor.getObject();
- if (parent instanceof CTTbl tbl) {
- // Top-level row - find its actual position in tr array
- List<CTRow> trList = Arrays.asList(tbl.getTrArray());
- int idx = trList.indexOf(ctRow);
- if (idx >= 0) {
- tbl.removeTr(idx);
- }
- } else if (parent instanceof CTSdtContentRow sdtContent) {
- // SDT-wrapped row - remove from SDT content
- List<CTRow> trList =
Arrays.asList(sdtContent.getTrArray());
- int idx = trList.indexOf(ctRow);
- if (idx >= 0) {
- sdtContent.removeTr(idx);
- removeEmptySdtRows(sdtContent);
- }
- } else {
- // Fallback: try removing from the table directly
- List<CTRow> trList = Arrays.asList(ctTbl.getTrArray());
- int idx = trList.indexOf(ctRow);
- if (idx >= 0) {
- ctTbl.removeTr(idx);
- }
- }
+ CTRow ctRow = tableRows.get(pos).getCtRow();
+ // the row is either a direct child of a table or inside an SDT or
custom XML row
+ XmlObject parent = getParent(ctRow);
+ if (isRowContainer(parent)) {
+ try (XmlCursor cursor = ctRow.newCursor()) {
+ cursor.removeXml();
}
+ removeEmptyRowWrappers(parent);
}
-
- // Remove from our list
tableRows.remove(pos);
return true;
}
@@ -1331,32 +1246,65 @@ public class XWPFTable implements IBodyElement,
ISDTContents {
}
/**
- * Remove the SDT that holds sdtContent once it has no content left,
- * and likewise any enclosing SDT rows that end up empty.
+ * Remove the SDT row or custom XML row that container belongs to once it
holds
+ * nothing but its properties, and likewise any enclosing wrappers that
end up empty.
*/
- private static void removeEmptySdtRows(CTSdtContentRow sdtContent) {
- XmlObject content = sdtContent;
- while (content instanceof CTSdtContentRow) {
- XmlObject sdt;
- XmlObject parent;
- try (XmlCursor cursor = content.newCursor()) {
- if (cursor.toFirstChild()) {
- return;
- }
- if (!cursor.toParent()) {
- return;
- }
- sdt = cursor.getObject();
- if (!cursor.toParent()) {
- return;
- }
- parent = cursor.getObject();
+ private static void removeEmptyRowWrappers(XmlObject container) {
+ while (container instanceof CTSdtContentRow || container instanceof
CTCustomXmlRow) {
+ if (hasContent(container)) {
+ return;
+ }
+ // for an SDT row, container is the w:sdtContent, so remove the
w:sdt around it
+ XmlObject wrapper = container instanceof CTSdtContentRow ?
getParent(container) : container;
+ XmlObject parent = wrapper == null ? null : getParent(wrapper);
+ if (parent == null) {
+ return;
}
- try (XmlCursor cursor = sdt.newCursor()) {
+ try (XmlCursor cursor = wrapper.newCursor()) {
cursor.removeXml();
}
- content = parent;
+ container = parent;
+ }
+ }
+
+ private static boolean hasContent(XmlObject container) {
+ try (XmlCursor cursor = container.newCursor()) {
+ if (!cursor.toFirstChild()) {
+ return false;
+ }
+ do {
+ if (!(cursor.getObject() instanceof CTCustomXmlPr)) {
+ return true;
+ }
+ } while (cursor.toNextSibling());
+ }
+ return false;
+ }
+
+ private static boolean isRowContainer(XmlObject xmlObject) {
+ return xmlObject instanceof CTTbl || xmlObject instanceof
CTSdtContentRow
+ || xmlObject instanceof CTCustomXmlRow;
+ }
+
+ private static XmlObject getParent(XmlObject xmlObject) {
+ try (XmlCursor cursor = xmlObject.newCursor()) {
+ return cursor.toParent() ? cursor.getObject() : null;
+ }
+ }
+
+ /**
+ * Get the table that holds a row, looking through any SDT rows
+ * (w:sdt/w:sdtContent) and custom XML rows (w:customXml) between the two.
+ *
+ * @return the table, or null if the row is not in a table
+ */
+ static CTTbl getParentTable(CTRow row) {
+ XmlObject parent = getParent(row);
+ while (parent instanceof CTSdtContentRow || parent instanceof CTSdtRow
+ || parent instanceof CTCustomXmlRow) {
+ parent = getParent(parent);
}
+ return parent instanceof CTTbl tbl ? tbl : null;
}
public List<XWPFTableRow> getRows() {
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
index 6d79f156e4..98ebf87f56 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java
@@ -512,10 +512,9 @@ public class XWPFTableCell implements IBody, ICell {
return null;
}
row = ctRow;
- cursor.toParent();
- o = cursor.getObject();
}
- if (!(o instanceof CTTbl tbl)) {
+ CTTbl tbl = XWPFTable.getParentTable(row);
+ if (tbl == null) {
return null;
}
XWPFTable table = getTable(tbl);
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java
index 2717fe5661..2bac81b68d 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableRow.java
@@ -43,7 +43,7 @@ import
org.openxmlformats.schemas.wordprocessingml.x2006.main.STHeightRule;
* the child {@link XWPFTableCell}s
*/
public class XWPFTableRow {
- private final CTRow ctRow;
+ private CTRow ctRow;
private final XWPFTable table;
private List<XWPFTableCell> tableCells;
@@ -58,6 +58,16 @@ public class XWPFTableRow {
return ctRow;
}
+ /**
+ * Point this row at ctRow, the copy of its XML that a table has just
added,
+ * and rebuild its cells from it.
+ */
+ void setCtRow(CTRow ctRow) {
+ this.ctRow = ctRow;
+ this.tableCells = null;
+ getTableCells();
+ }
+
/**
* create a new XWPFTableCell and add it to the tableCell-list of this
tableRow
*
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
index 587f4c4788..eab696afbe 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFTable.java
@@ -25,6 +25,7 @@ import java.util.List;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
import org.junit.jupiter.api.Test;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTCustomXmlRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
@@ -834,6 +835,153 @@ class TestXWPFTable {
}
}
+ @Test
+ void testGetTableCellInSdtRowFromDocument() throws IOException {
+ try (XWPFDocument doc =
XWPFTestDataSamples.openSampleDocument("Bug66263-table.docx")) {
+ XWPFTableCell cell = doc.getTables().get(0).getRow(0).getCell(1);
+ assertSame(cell, doc.getTableCell(cell.getCTTc()));
+ }
+ }
+
+ @Test
+ void testGetTableCellInSdtRowOfNestedTable() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl outer = CTTbl.Factory.newInstance();
+ CTTc outerTc = outer.addNewTr().addNewTc();
+ CTTbl nested = outerTc.addNewTbl();
+ CTSdtContentRow content =
nested.addNewSdt().addNewSdtContent().addNewSdt().addNewSdtContent();
+ content.addNewTr().addNewTc().addNewP();
+ outerTc.addNewP();
+
+ XWPFTableCell outerCell = new XWPFTable(outer,
doc).getRow(0).getCell(0);
+ XWPFTableCell nestedCell =
outerCell.getTables().get(0).getRow(0).getCell(0);
+ assertSame(nestedCell,
outerCell.getTableCell(nestedCell.getCTTc()));
+ }
+ }
+
+ @Test
+ void testInsertRowsBeforeSdtRowAndTopLevelRow() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ table.addNewTr().addNewTc().addNewP();
+
table.addNewSdt().addNewSdtContent().addNewTr().addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ XWPFTableRow beforeSdtRow = xtab.insertNewTableRow(1);
+ XWPFTableRow beforeFirstRow = xtab.insertNewTableRow(0);
+
+ assertEquals(4, xtab.getNumberOfRows());
+ assertSame(beforeFirstRow, xtab.getRow(0));
+ assertSame(beforeSdtRow, xtab.getRow(2));
+ // the row inserted before the SDT row goes inside the SDT
+ assertEquals(2, table.sizeOfTrArray());
+ assertSame(table.getTrArray(0), beforeFirstRow.getCtRow());
+ assertEquals(2,
table.getSdtArray(0).getSdtContent().sizeOfTrArray());
+ assertSame(table.getSdtArray(0).getSdtContent().getTrArray(0),
beforeSdtRow.getCtRow());
+ }
+ }
+
+ @Test
+ void testTableWithCustomXmlRows() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ addRowWithText(table.addNewTr(), "A");
+ CTCustomXmlRow customXml = table.addNewCustomXml();
+ customXml.setElement("item");
+ customXml.addNewCustomXmlPr();
+ addRowWithText(customXml.addNewTr(), "B");
+
addRowWithText(table.addNewCustomXml().addNewSdt().addNewSdtContent().addNewTr(),
"C");
+
addRowWithText(table.addNewSdt().addNewSdtContent().addNewCustomXml().addNewTr(),
"D");
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+
+ assertEquals(4, xtab.getNumberOfRows());
+ assertEquals(List.of("A", "B", "C", "D"),
+ xtab.getRows().stream().map(r ->
r.getCell(0).getText()).toList());
+ assertEquals("A\nB\nC\nD\n", xtab.getText());
+ }
+ }
+
+ @Test
+ void testGetTableCellInCustomXmlRowOfNestedTable() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl outer = CTTbl.Factory.newInstance();
+ CTTc outerTc = outer.addNewTr().addNewTc();
+
outerTc.addNewTbl().addNewCustomXml().addNewTr().addNewTc().addNewP();
+ outerTc.addNewP();
+
+ XWPFTableCell outerCell = new XWPFTable(outer,
doc).getRow(0).getCell(0);
+ XWPFTableCell nestedCell =
outerCell.getTables().get(0).getRow(0).getCell(0);
+ assertSame(nestedCell,
outerCell.getTableCell(nestedCell.getCTTc()));
+ }
+ }
+
+ @Test
+ void testRemoveLastRowOfCustomXmlRemovesWrappers() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ table.addNewTr().addNewTc().addNewP();
+ CTCustomXmlRow customXml =
table.addNewSdt().addNewSdtContent().addNewCustomXml();
+ customXml.setElement("item");
+ customXml.addNewCustomXmlPr();
+ customXml.addNewTr().addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ assertEquals(2, xtab.getNumberOfRows());
+
+ assertTrue(xtab.removeRow(1));
+ assertEquals(1, xtab.getNumberOfRows());
+ assertEquals(1, table.sizeOfTrArray());
+ assertEquals(0, table.sizeOfSdtArray());
+ }
+ }
+
+ @Test
+ void testInsertRowBeforeCustomXmlRow() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ CTTbl table = CTTbl.Factory.newInstance();
+ table.addNewTr().addNewTc().addNewP();
+ CTCustomXmlRow customXml = table.addNewCustomXml();
+ customXml.addNewTr().addNewTc().addNewP();
+
+ XWPFTable xtab = new XWPFTable(table, doc);
+ XWPFTableRow inserted = xtab.insertNewTableRow(1);
+
+ assertEquals(3, xtab.getNumberOfRows());
+ assertSame(inserted, xtab.getRow(1));
+ assertEquals(1, table.sizeOfTrArray());
+ assertEquals(2, customXml.sizeOfTrArray());
+ assertSame(customXml.getTrArray(0), inserted.getCtRow());
+ }
+ }
+
+ @Test
+ void testChangesToRowAfterAddRowAreKept() throws IOException {
+ try (XWPFDocument doc = new XWPFDocument()) {
+ XWPFTable xtab = doc.createTable();
+ XWPFTableRow appended = new
XWPFTableRow(CTRow.Factory.newInstance(), xtab);
+ appended.createCell();
+ XWPFTableRow first = new XWPFTableRow(CTRow.Factory.newInstance(),
xtab);
+ first.createCell();
+
+ xtab.addRow(appended);
+ assertTrue(xtab.addRow(first, 0));
+ appended.getCell(0).setText("appended");
+ first.getCell(0).setText("first");
+
+ CTTbl ctTbl = xtab.getCTTbl();
+ assertEquals(3, ctTbl.sizeOfTrArray());
+ assertSame(ctTbl.getTrArray(0), first.getCtRow());
+ assertSame(ctTbl.getTrArray(2), appended.getCtRow());
+ assertEquals("first", new XWPFTableRow(ctTbl.getTrArray(0),
xtab).getCell(0).getText());
+ assertEquals("appended", new XWPFTableRow(ctTbl.getTrArray(2),
xtab).getCell(0).getText());
+ }
+ }
+
+ private static void addRowWithText(CTRow row, String text) {
+ row.addNewTc().addNewP().addNewR().addNewT().setStringValue(text);
+ }
+
@Test
void testAddRowToSdtTable() throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
diff --git a/src/resources/ooxml-lite-report.clazz
b/src/resources/ooxml-lite-report.clazz
index 5eb215c0b2..f316c852ac 100644
--- a/src/resources/ooxml-lite-report.clazz
+++ b/src/resources/ooxml-lite-report.clazz
@@ -2559,3 +2559,7 @@
org/openxmlformats/schemas/drawingml/x2006/chart/impl/STHPercentUShortImpl
org/openxmlformats/schemas/drawingml/x2006/chart/impl/STDepthPercentWithSymbolImpl
org/openxmlformats/schemas/drawingml/x2006/chart/impl/STHPercentWithSymbolImpl
org/openxmlformats/schemas/drawingml/x2006/main/impl/CTPath2DQuadBezierToImpl
+org/openxmlformats/schemas/wordprocessingml/x2006/main/CTCustomXmlRow
+org/openxmlformats/schemas/wordprocessingml/x2006/main/impl/CTCustomXmlRowImpl
+org/openxmlformats/schemas/wordprocessingml/x2006/main/CTCustomXmlCell
+org/openxmlformats/schemas/officeDocument/x2006/sharedTypes/STConformanceClass
diff --git a/src/resources/ooxml-lite-report.xsb
b/src/resources/ooxml-lite-report.xsb
index b987260b7b..4325381a88 100644
--- a/src/resources/ooxml-lite-report.xsb
+++ b/src/resources/ooxml-lite-report.xsb
@@ -1193,3 +1193,4 @@ stdepthpercentushorte7a3type
sthpercentwithsymbole671type
sthpercentushort0848type
ctpath2dquadbezierto3f53type
+ctcustomxmlrowedd4type
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]