-
Notifications
You must be signed in to change notification settings - Fork 833
Fix Bug 66263 — Add support for reading SDT row in tables #971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
85f4837
e0f3da1
5c75ebe
9ba7239
34f3fa6
fe8e05f
d992752
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -138,10 +138,26 @@ private void buildRunsInOrderFromXml(XmlObject object) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| if (o instanceof CTSdtBlock) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFSDT cc = new XWPFSDT((CTSdtBlock) o, part); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| iruns.add(cc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| CTSdtContentBlock content = ((CTSdtBlock)o).getSdtContent(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (content != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTP ctp : content.getPList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| processCTRs(ctp.getRList()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (o instanceof CTSdtRun) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFSDT cc = new XWPFSDT((CTSdtRun) o, part); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| iruns.add(cc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (o instanceof CTSdtRun) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFSDT cc = new XWPFSDT((CTSdtRun)o, part); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
pjfanning marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| iruns.add(cc); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| CTSdtContentRun sdtContent = ((CTSdtRun)o).getSdtContent(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sdtContent != null) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| processCTRs(sdtContent.getRList()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| processSdtRuns(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (o instanceof CTRunTrackChange) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| final CTRunTrackChange parentRecord = (CTRunTrackChange) o; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -164,6 +180,33 @@ private void buildRunsInOrderFromXml(XmlObject object) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private void processCTRs(List<CTR> ctrs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ctrs == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTR ctr : ctrs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (ctr.getRPr() != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+179
to
+184
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private void processCTRs(List<CTR> ctrs) { | |
| if (ctrs == null) { | |
| return; | |
| } | |
| for (CTR ctr : ctrs) { | |
| if (ctr.getRPr() != null) { | |
| private boolean isRunInParagraphEditContainer(CTR ctr) { | |
| try (XmlCursor cursor = ctr.newCursor()) { | |
| if (!cursor.toParent()) { | |
| return false; | |
| } | |
| XmlObject parent = cursor.getObject(); | |
| return parent instanceof CTP | |
| || parent instanceof CTHyperlink | |
| || parent instanceof CTSimpleField; | |
| } | |
| } | |
| private void processCTRs(List<CTR> ctrs) { | |
| if (ctrs == null) { | |
| return; | |
| } | |
| for (CTR ctr : ctrs) { | |
| if (ctr.getRPr() != null && isRunInParagraphEditContainer(ctr)) { |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more | |||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.ooxml.util.POIXMLUnits; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.util.Internal; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.poi.util.Units; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.xmlbeans.XmlCursor; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.xmlbeans.XmlObject; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJcTable; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -168,24 +170,59 @@ public XWPFTable(CTTbl table, IBody part, boolean initRow) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| createEmptyTable(table); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
170
to
173
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTRow row : table.getTrList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| StringBuilder rowText = new StringBuilder(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFTableRow tabRow = new XWPFTableRow(row, this); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| tableRows.add(tabRow); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTTc cell : row.getTcList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTP ctp : cell.getPList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFParagraph p = new XWPFParagraph(ctp, part); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rowText.length() > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rowText.append('\t'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try (XmlCursor cursor = table.newCursor()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| cursor.selectPath("./*"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| while (cursor.toNextSelection()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XmlObject xmlObject = cursor.getObject(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (xmlObject instanceof CTRow) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| processCTRow((CTRow)xmlObject); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| else if (xmlObject instanceof CTSdtRow) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| List<CTRow> rows = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| collectCTRowsInnerSdtRow((CTSdtRow)xmlObject, rows); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTRow row : rows) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| processCTRow(row); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rowText.append(p.getText()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+175
to
190
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rowText.length() > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| this.text.append(rowText); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| this.text.append('\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private void processCTRow(CTRow row) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| StringBuilder rowText = new StringBuilder(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFTableRow tableRow = new XWPFTableRow(row, this); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| tableRows.add(tableRow); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTTc cell : row.getTcList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTP ctp : cell.getPList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| XWPFParagraph p = new XWPFParagraph(ctp, part); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rowText.length() > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rowText.append('\t'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rowText.append(p.getText()); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rowText.length() > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| this.text.append(rowText); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| this.text.append('\n'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private void collectCTRowsInnerSdtRow(CTSdtRow sdtRow, List<CTRow> rows) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| CTSdtContentRow sdtContent = sdtRow.getSdtContent(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sdtContent == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| List<CTRow> rowsInnerSdtContent = sdtContent.getTrList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!rowsInnerSdtContent.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| rows.addAll(rowsInnerSdtContent); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| for (CTSdtRow innerSdt : sdtContent.getSdtList()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| collectCTRowsInnerSdtRow(innerSdt, rows); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+219
to
+226
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| List<CTRow> rowsInnerSdtContent = sdtContent.getTrList(); | |
| if (!rowsInnerSdtContent.isEmpty()) { | |
| rows.addAll(rowsInnerSdtContent); | |
| return; | |
| } | |
| for (CTSdtRow innerSdt : sdtContent.getSdtList()) { | |
| collectCTRowsInnerSdtRow(innerSdt, rows); | |
| XmlCursor cursor = sdtContent.newCursor(); | |
| try { | |
| if (!cursor.toFirstChild()) { | |
| return; | |
| } | |
| do { | |
| XmlObject child = cursor.getObject(); | |
| if (child instanceof CTRow) { | |
| rows.add((CTRow) child); | |
| } else if (child instanceof CTSdtRow) { | |
| collectCTRowsInnerSdtRow((CTSdtRow) child, rows); | |
| } | |
| } while (cursor.toNextSibling()); | |
| } finally { | |
| cursor.dispose(); |
Uh oh!
There was an error while loading. Please reload this page.