diff options
author | Jacobo Aragunde Pérez <jaragunde@igalia.com> | 2025-07-23 22:33:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-23 21:33:47 +0100 |
commit | 0b9bc5d0b46a9325f783e5362f9986a00fd35bda (patch) | |
tree | 086722dc5e0fb507570164424df21f651904f138 /poi-ooxml | |
parent | e5b2fbb89b7d704d69f64825dd03d819545f28f7 (diff) | |
download | poi-trunk.tar.gz poi-trunk.zip |
* 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.
Diffstat (limited to 'poi-ooxml')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSDTCell.java | 10 | ||||
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/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; + } } |