]> source.dussan.org Git - poi.git/commitdiff
[bug-64561] XWPFSDTContent.getText() is empty for nested SDT elements
authorPJ Fanning <fanningpj@apache.org>
Fri, 26 Jun 2020 10:15:53 +0000 (10:15 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 26 Jun 2020 10:15:53 +0000 (10:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879223 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java
test-data/spreadsheet/Bug64561.docx [new file with mode: 0644]

index f39c5cf482a733bb8b36b1474c74d17270a08834..eaaf94d873e1409293d427f8e9aaf513c31f4604 100644 (file)
@@ -21,12 +21,7 @@ import java.util.List;
 
 import org.apache.xmlbeans.XmlCursor;
 import org.apache.xmlbeans.XmlObject;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentBlock;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRun;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
 
 /**
  * Experimental class to offer rudimentary read-only processing of
@@ -50,11 +45,19 @@ public class XWPFSDTContent implements ISDTContent {
         if (sdtRun == null) {
             return;
         }
-        for (CTR ctr : sdtRun.getRArray()) {
-            XWPFRun run = new XWPFRun(ctr, parent);
-            // runs.add(run);
-            bodyElements.add(run);
+        XmlCursor cursor = sdtRun.newCursor();
+        cursor.selectPath("./*");
+        while (cursor.toNextSelection()) {
+            XmlObject o = cursor.getObject();
+            if (o instanceof CTR) {
+                XWPFRun run = new XWPFRun((CTR) o, parent);
+                bodyElements.add(run);
+            } else if (o instanceof CTSdtRun) {
+                XWPFSDT c = new XWPFSDT(((CTSdtRun) o), part);
+                bodyElements.add(c);
+            }
         }
+        cursor.dispose();
     }
 
     public XWPFSDTContent(CTSdtContentBlock block, IBody part, IRunBody parent) {
index 4bf87fcbb0f1420210337cd1c0000fa7adc7c776..c1ca100be59b846c33d930b2e75d04700a4b2e17 100644 (file)
@@ -30,6 +30,17 @@ import org.junit.Test;
 
 public final class TestXWPFSDT {
 
+    /**
+     * Test text extraction from nested SDTs
+     */
+    @Test
+    public void testNestedSDTs() throws Exception {
+        try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug64561.docx")) {
+            XWPFAbstractSDT sdt = extractAllSDTs(doc).get(0);
+            assertEquals("extracted text", "Subject", sdt.getContent().getText());
+        }
+    }
+
     /**
      * Test simple tag and title extraction from SDT
      */
diff --git a/test-data/spreadsheet/Bug64561.docx b/test-data/spreadsheet/Bug64561.docx
new file mode 100644 (file)
index 0000000..f34fe70
Binary files /dev/null and b/test-data/spreadsheet/Bug64561.docx differ