Re: [PR] Fix Bug 66263 — Add support for reading SDT row in tables [poi]
hostedbygnome (via GitHub) <[email protected]> Mon, 20 Jul 2026 05:34:36 -0000
| Newsgroups | gmane.comp.jakarta.poi.devel |
|---|---|
| Message-ID | <PR_kwDOAAMmIM65GrKp-88368fce-4302-4e9a-8d19-ebaaac215b6a@gitbox.apache.org> |
hostedbygnome commented on code in PR #971:
URL: https://github.com/apache/poi/pull/971#discussion_r3612206104
##########
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java:
##########
@@ -165,27 +169,70 @@ public XWPFTable(CTTbl table, IBody part, boolean initRow) {
this.ctTbl = table;
// is an empty table: I add one row and one column as default
- if (initRow && table.sizeOfTrArray() == 0) {
+ // Check if table has any row-like content (either TR or SDT-wrapped rows)
+ if (initRow && table.sizeOfTrArray() == 0 && !hasSdtRows(table)) {
createEmptyTable(table);
}
- for (CTRow row : table.getTrList()) {
- StringBuilder rowText = new StringBuilder();
- XWPFTableRow tabRow = new XWPFTableRow(row, this);
- tableRows.add(tabRow);
- for (CTTc cell : row.getTcList()) {
- for (CTP ctp : cell.getPList()) {
- XWPFParagraph p = new XWPFParagraph(ctp, part);
- if (rowText.length() > 0) {
- rowText.append('\t');
+ try (XmlCursor cursor = table.newCursor()) {
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject xmlObject = cursor.getObject();
+ if (xmlObject instanceof CTRow) {
+ processCTRow((CTRow)xmlObject);
+ }
+ else if (xmlObject instanceof CTSdtRow) {
+ List<CTRow> rows = new ArrayList<>();
+ collectCTRowsInnerSdtRow((CTSdtRow)xmlObject, rows);
+ for (CTRow row : rows)
+ {
+ processCTRow(row);
}
- rowText.append(p.getText());
}
}
- if (rowText.length() > 0) {
- this.text.append(rowText);
- this.text.append('\n');
+ }
+ }
+
+ private void processCTRow(CTRow row) {
+ StringBuilder rowText = new StringBuilder();
+ XWPFTableRow tableRow = new XWPFTableRow(row, this);
+ tableRows.add(tableRow);
+ for (CTTc cell : row.getTcList()) {
+ for (CTP ctp : cell.getPList()) {
+ XWPFParagraph p = new XWPFParagraph(ctp, part);
+ if (rowText.length() > 0) {
+ rowText.append('\t');
+ }
+ rowText.append(p.getText());
+ }
+ }
+ if (rowText.length() > 0) {
+ this.text.append(rowText);
+ this.text.append('\n');
+ }
+ }
+
+ private void collectCTRowsInnerSdtRow(CTSdtRow sdtRow, List<CTRow> rows) {
+ CTSdtContentRow sdtContent = sdtRow.getSdtContent();
+ if (sdtContent == null) {
+ return;
+ }
+
+ XmlCursor cursor = sdtContent.newCursor();
Review Comment:
Ok
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]