From 6670c48c305fca2aa30c4bdd01763c696f8f698e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Fri, 9 Jan 2026 13:28:30 +0100 Subject: [PATCH] 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. --- .../poi/xwpf/usermodel/XWPFSDTContent.java | 10 ++++++++ .../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 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 */