[bug-66263] add test case to try to get extra classes into poi-ooxml-lite

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1904081 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-09-14 19:32:25 +00:00
parent af6bc9660f
commit cf22264d70
3 changed files with 66 additions and 0 deletions

View File

@ -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;
}

View File

@ -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();

View File

@ -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<XWPFSDT> sdts = extractSDTsFromBody(doc);
assertEquals(3, sdts.size());
}
}
private static List<XWPFSDT> extractSDTsFromBody(XWPFDocument document) {
XWPFSDT sdt;
XmlCursor xmlcursor = document.getDocument().getBody().newCursor();
QName qnameSdt = new QName(XSSFRelation.NS_WORDPROCESSINGML, "sdt");
List<XWPFSDT> 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;
}
}