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 0b9bc5d0b4 Add getters for CTSdt(Content)Cell objects. (#854)
0b9bc5d0b4 is described below

commit 0b9bc5d0b46a9325f783e5362f9986a00fd35bda
Author: Jacobo Aragunde PĂ©rez <[email protected]>
AuthorDate: Wed Jul 23 22:33:47 2025 +0200

    Add getters for CTSdt(Content)Cell objects. (#854)
    
    * Add getters for CTSdt(Content)Cell objects.
    
    Currently the XWPFSdt(Content)Cell object just produces a text
    equivalent of the content. There is no way to access the inner XML node
    for further inspection; instead, one needs to go to the parent object
    and iterate over its XML children.
    
    This would allow users to use the XWPFRow.getICells() to obtain
    XWPFTableCell or XWPFSDTCell objects, and then access the latter's inner
    XML node to get access to its full data.
    
    We also defer the parsing of the text in XWPFContentCell until the
    getter is called. A user who will work with the inner XML is unlikely to
    need the text parsing done.
    
    * Add javadoc and @since annotation.
---
 .../org/apache/poi/xwpf/usermodel/XWPFSDTCell.java | 10 +++++++++
 .../poi/xwpf/usermodel/XWPFSDTContentCell.java     | 26 ++++++++++++++++++----
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTCell.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTCell.java
index 5589287263..294831b9a7 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTCell.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTCell.java
@@ -29,9 +29,11 @@ import 
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtCell;
  */
 public class XWPFSDTCell extends XWPFAbstractSDT implements ICell {
     private final XWPFSDTContentCell cellContent;
+    private final CTSdtCell sdtCell;
 
     public XWPFSDTCell(CTSdtCell sdtCell, XWPFTableRow xwpfTableRow, IBody 
part) {
         super(sdtCell.getSdtPr(), part);
+        this.sdtCell = sdtCell;
         cellContent = new XWPFSDTContentCell(sdtCell.getSdtContent(), 
xwpfTableRow, part);
     }
 
@@ -40,4 +42,12 @@ public class XWPFSDTCell extends XWPFAbstractSDT implements 
ICell {
         return cellContent;
     }
 
+    /**
+     * Return the underlying XML bean.
+     * @return the underlying CTSdtCell bean.
+     * @since POI 5.4.2
+     */
+    public CTSdtCell getCTSdtCell() {
+        return sdtCell;
+    }
 }
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java
index d43e24fc43..576e84b064 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTContentCell.java
@@ -40,14 +40,19 @@ public class XWPFSDTContentCell implements ISDTContent {
 
     //private List<ICell> cells = new ArrayList<ICell>().
 
-    private String text = "";
+    private final CTSdtContentCell sdtContentCell;
+    private String text;
 
     public XWPFSDTContentCell(CTSdtContentCell sdtContentCell,
                               XWPFTableRow xwpfTableRow, IBody part) {
         super();
+        this.sdtContentCell = sdtContentCell;
+    }
+
+    private String extractTextFromSdtContentCell() {
         //sdtContentCell is allowed to be null:  minOccurs="0" maxOccurs="1"
         if (sdtContentCell == null) {
-            return;
+            return "";
         }
         StringBuilder sb = new StringBuilder();
         try (final XmlCursor cursor = sdtContentCell.newCursor()) {
@@ -87,7 +92,7 @@ public class XWPFSDTContentCell implements ISDTContent {
                     depth--;
                 }
             }
-            text = sb.toString();
+            return sb.toString();
         }
     }
 
@@ -103,12 +108,25 @@ public class XWPFSDTContentCell implements ISDTContent {
         return false;
     }
 
-
+    @Override
     public String getText() {
+        if (text == null) {
+            text = extractTextFromSdtContentCell();
+        }
         return text;
     }
 
+    @Override
     public String toString() {
         return getText();
     }
+
+    /**
+     * Return the underlying XML bean.
+     * @return the underlying CTSdtContentCell bean.
+     * @since POI 5.4.2
+     */
+    public CTSdtContentCell getCTSdtContentCell() {
+        return sdtContentCell;
+    }
 }


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

Reply via email to