pjfanning commented on code in PR #971:
URL: https://github.com/apache/poi/pull/971#discussion_r3594927267


##########
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:
   cursors support close now - so can you use try with resources?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to