This is an automated email from the ASF dual-hosted git repository.
fanningpj 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 6670c48c30 Add getter for body elements in XWPFSDTContent. (#991)
6670c48c30 is described below
commit 6670c48c305fca2aa30c4bdd01763c696f8f698e
Author: Jacobo Aragunde Pérez <[email protected]>
AuthorDate: Fri Jan 9 13:28:30 2026 +0100
Add getter for body elements in XWPFSDTContent. (#991)
* Add getter for body elements in XWPFSDTContent.
This getter allows to navigate the POI objects stored inside a
XWPFSDTContent object without resorting to navigating XML.
* Address comments in review.
---
.../apache/poi/xwpf/usermodel/XWPFSDTContent.java | 10 ++++++++++
.../org/apache/poi/xwpf/usermodel/TestXWPFSDT.java | 23 ++++++++++++++++++++++
2 files changed, 33 insertions(+)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java
index fc0bd3981f..ace3e0271e 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContent.java
@@ -17,6 +17,7 @@
package org.apache.poi.xwpf.usermodel;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import org.apache.poi.util.Beta;
@@ -107,6 +108,15 @@ public class XWPFSDTContent implements ISDTContent {
}
}
+ /**
+ * Get the body elements inside this SDT content element.
+ * @return Unmodifiable list containing body elements.
+ * @since 6.0.0
+ */
+ public List<ISDTContents> getBodyElements() {
+ return Collections.unmodifiableList(bodyElements);
+ }
+
@Override
public String getText() {
StringBuilder text = new StringBuilder();
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java
index 235c17d3ab..9b40eb4837 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java
@@ -95,6 +95,29 @@ public final class TestXWPFSDT {
}
}
+ @Test
+ void testGetSDTContentBodyElements() throws Exception {
+ try (XWPFDocument doc =
XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
+ IBodyElement sdtBodyElement = doc.getBodyElements().get(2);
+ assertTrue(sdtBodyElement instanceof XWPFSDT, "sdtBodyElement
instance of XWPFSDT");
+ XWPFSDTContent content = (XWPFSDTContent) ((XWPFSDT)
sdtBodyElement).getContent();
+ assertEquals(3, content.getBodyElements().size(), "elements inside
SDT");
+
+ ISDTContents c1 = content.getBodyElements().get(0);
+ assertTrue(c1 instanceof XWPFParagraph, "c1 instance of
XWPFParagraph");
+ assertEquals("Rich_text_pre_table", ((XWPFParagraph)
c1).getText());
+
+ ISDTContents c2 = content.getBodyElements().get(1);
+ assertTrue(c2 instanceof XWPFTable, "c2 instance of XWPFTable");
+ assertEquals(3, ((XWPFTable) c2).getNumberOfRows(), "rows in table
inside SDT");
+ assertEquals("Rich_text_cell1", ((XWPFTable)
c2).getRow(0).getCell(0).getText());
+
+ ISDTContents c3 = content.getBodyElements().get(2);
+ assertTrue(c3 instanceof XWPFParagraph, "c3 instance of
XWPFParagraph");
+ assertEquals("Rich_text_post_table", ((XWPFParagraph)
c3).getText());
+ }
+ }
+
/**
* POI-54771 and TIKA-1317
*/
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]