diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDT.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDT.java index 82aa024623..645653a8cb 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDT.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDT.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun; /** @@ -39,6 +40,11 @@ public class XWPFSDT extends XWPFAbstractSDT this.content = new XWPFSDTContent(block.getSdtContent(), part, this); } + public XWPFSDT(CTSdtRow row, IBody part) { + super(row.getSdtPr(), part); + this.content = new XWPFSDTContent(row.getSdtContent(), part, this); + } + public ISDTContent getContent() { return content; } 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 da4fc7de35..8223435efe 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 @@ -87,6 +87,26 @@ public class XWPFSDTContent implements ISDTContent { } } + public XWPFSDTContent(CTSdtContentRow sdtContentRow, IBody part, IRunBody parent) { + if (sdtContentRow == null) { + return; + } + try (final XmlCursor cursor = sdtContentRow.newCursor()) { + cursor.selectPath("./*"); + while (cursor.toNextSelection()) { + XmlObject o = cursor.getObject(); + if (o instanceof CTSdtRow) { + XWPFSDT c = new XWPFSDT(((CTSdtRow) o), part); + bodyElements.add(c); + // contentControls.add(c); + } else if (o instanceof CTRow) { + //can only create XWPFTableRow if you have an XWPFTable instance + //XWPFTableRow tableRow = new XWPFTableRow((CTRow) o, parent); + } + } + } + } + @Override public String getText() { StringBuilder text = new StringBuilder(); diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java index 71120bd7b3..90f4c817a5 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/extractor/TestXWPFWordExtractor.java @@ -27,14 +27,25 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.poi.util.StringUtil; +import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFSDT; +import org.apache.xmlbeans.XmlCursor; +import org.apache.xmlbeans.XmlObject; import org.junit.jupiter.api.Test; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRow; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun; + +import javax.xml.namespace.QName; /** * Tests for HXFWordExtractor @@ -503,6 +514,35 @@ class TestXWPFWordExtractor { XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) { String txt = extractor.getText(); assertContains(txt, "Note\tDetails"); + List sdts = extractSDTsFromBody(doc); + assertEquals(3, sdts.size()); } } + + private static List extractSDTsFromBody(XWPFDocument document) { + XWPFSDT sdt; + XmlCursor xmlcursor = document.getDocument().getBody().newCursor(); + QName qnameSdt = new QName(XSSFRelation.NS_WORDPROCESSINGML, "sdt"); + List allsdts = new ArrayList<>(); + while (xmlcursor.hasNextToken()) { + XmlCursor.TokenType tokentype = xmlcursor.toNextToken(); + if (tokentype.isStart()) { + if (qnameSdt.equals(xmlcursor.getName())) { + XmlObject xo = xmlcursor.getObject(); + if (xo instanceof CTSdtRun) { + sdt = new XWPFSDT((CTSdtRun) xo, document); + allsdts.add(sdt); + } else if (xo instanceof CTSdtBlock) { + sdt = new XWPFSDT((CTSdtBlock) xo, document); + allsdts.add(sdt); + } else if (xo instanceof CTSdtRow) { + sdt = new XWPFSDT((CTSdtRow) xo, document); + allsdts.add(sdt); + } + } + } + } + return allsdts; + } + }