Skip to content

Commit

Permalink
Merge pull request #37 from airslateinc/ASP-18332-sdt-elements-clone
Browse files Browse the repository at this point in the history
[ASP-18332] changed ISDTContentRun and ISDTContent cloneExisting metod impl.
  • Loading branch information
alekseytatarynov authored May 18, 2022
2 parents 7139ec2 + 02956f4 commit 50d23a0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ subprojects {
apply plugin: 'de.thetaphi.forbiddenapis'
apply plugin: 'com.github.spotbugs'

version = '5.2.3-AIRSLATE-2'
version = '5.2.3-AIRSLATE-4'
ext {
bouncyCastleVersion = '1.70'
commonsCodecVersion = '1.15'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ public IBodyElement cloneExistingIBodyElement(IBodyElement elem) {
tables.add(tbl);
bodyElements.add(tbl);
return tbl;
} else if (elem instanceof XWPFSDTBlock) {
CTSdtBlock ctSdtBlock = ctSdtContentBlock.addNewSdt();
ctSdtBlock.set(((XWPFSDTBlock) elem).getCtSdtBlock());
XWPFSDTBlock sdtBlock = new XWPFSDTBlock(ctSdtBlock, parent);
contentControls.add(sdtBlock);
bodyElements.add(sdtBlock);
return sdtBlock;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public IRunElement cloneExistingIRunElement(IRunElement elem) {
runs.add(r);
iruns.add(r);
return r;
} else if (elem instanceof XWPFSDTRun) {
CTSdtRun ctSdtRun = ctContentRun.addNewSdt();
ctSdtRun.set(((XWPFSDTRun) elem).getCtSdtRun());
XWPFSDTRun sdtRun = new XWPFSDTRun(ctSdtRun, parent);
iruns.add(sdtRun);
return sdtRun;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,56 @@ public void testInsertExistingTblToSdtContentBlock() {
Assertions.assertEquals(0, sdtBlockContent.getSdtBlocks().size());
}

@Test
public void testInsertSdtIntoSdt() {
XWPFDocument document = new XWPFDocument();
XWPFSDTBlock dstSdtBlock = document.createSdt();
XWPFSDTContentBlock dstSdtBlockContent = dstSdtBlock.createSdtContent();
dstSdtBlockContent.createParagraph().createRun().setText("t1");
dstSdtBlockContent.createTable().getRow(0).getCell(0).setText("t2");
dstSdtBlockContent.createParagraph().createRun().setText("t3");
dstSdtBlockContent.createSdt().createSdtContent().createParagraph().createRun().setText("t4");

XWPFSDTBlock srcSdtBlock = document.createSdt();
XWPFSDTContentBlock srcSdtBlockContent = srcSdtBlock.createSdtContent();
srcSdtBlockContent.createTable().getRow(0).getCell(0).setText("t5");
srcSdtBlockContent.createParagraph().createRun().setText("t6");
srcSdtBlockContent.createSdt().createSdtContent().createParagraph().createRun().setText("t7");

dstSdtBlockContent.cloneExistingIBodyElement(srcSdtBlock);

Assertions.assertEquals(2, document.getBodyElements().size());
Assertions.assertEquals(2, document.getSdtBlocks().size());

XWPFSDTContentBlock sdtBlock1Content = document.getSdtBlocks().get(0).getContent();
Assertions.assertEquals(5, sdtBlock1Content.getBodyElements().size());
Assertions.assertEquals(2, sdtBlock1Content.getParagraphs().size());
Assertions.assertEquals(1, sdtBlock1Content.getTables().size());
Assertions.assertEquals(2, sdtBlock1Content.getSdtBlocks().size());
Assertions.assertEquals("t1", ((XWPFParagraph) sdtBlock1Content.getBodyElements().get(0)).getText());
Assertions.assertEquals("t2", ((XWPFTable) sdtBlock1Content.getBodyElements().get(1)).getRow(0).getCell(0).getText());
Assertions.assertEquals("t3", ((XWPFParagraph) sdtBlock1Content.getBodyElements().get(2)).getText());
Assertions.assertEquals("t4", ((XWPFSDTBlock) sdtBlock1Content.getBodyElements().get(3)).getContent().getText());

XWPFSDTContentBlock innerSdtBlockContent = ((XWPFSDTBlock) sdtBlock1Content.getBodyElements().get(4)).getContent();
Assertions.assertEquals(3, innerSdtBlockContent.getBodyElements().size());
Assertions.assertEquals(1, innerSdtBlockContent.getParagraphs().size());
Assertions.assertEquals(1, innerSdtBlockContent.getTables().size());
Assertions.assertEquals(1, innerSdtBlockContent.getSdtBlocks().size());
Assertions.assertEquals("t5", ((XWPFTable) innerSdtBlockContent.getBodyElements().get(0)).getRow(0).getCell(0).getText());
Assertions.assertEquals("t6", ((XWPFParagraph) innerSdtBlockContent.getBodyElements().get(1)).getText());
Assertions.assertEquals("t7", ((XWPFSDTBlock) innerSdtBlockContent.getBodyElements().get(2)).getContent().getText());

XWPFSDTContentBlock sdtBlock2Content = document.getSdtBlocks().get(1).getContent();
Assertions.assertEquals(3, sdtBlock2Content.getBodyElements().size());
Assertions.assertEquals(1, sdtBlock2Content.getParagraphs().size());
Assertions.assertEquals(1, sdtBlock2Content.getTables().size());
Assertions.assertEquals(1, sdtBlock2Content.getSdtBlocks().size());
Assertions.assertEquals("t5", ((XWPFTable) sdtBlock2Content.getBodyElements().get(0)).getRow(0).getCell(0).getText());
Assertions.assertEquals("t6", ((XWPFParagraph) sdtBlock2Content.getBodyElements().get(1)).getText());
Assertions.assertEquals("t7", ((XWPFSDTBlock) sdtBlock2Content.getBodyElements().get(2)).getContent().getText());
}

@Test
public void testInsertNewTblToSdtBlockContent() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("blockAndInlineSdtTags.docx");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,47 @@ public void testInsertSDTRunBetweenRuns() {
Assertions.assertEquals(XWPFSDTRun.class, p.getIRuns().get(3).getClass());
}

@Test
public void testInsertSDTIntoSdt() {
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();

XWPFSDTRun dstSdtRun = paragraph.createSdtRun();
XWPFSDTContentRun dstSdtContent = dstSdtRun.createSdtContent();
dstSdtContent.createRun().setText("t1");
dstSdtContent.createRun().setText("t2");

XWPFSDTRun srcSdtRun = paragraph.createSdtRun();
XWPFSDTPr srcSdtPr = srcSdtRun.createSdtPr();
srcSdtPr.setTag("tag");
XWPFSDTContentRun srcSdtContent = srcSdtRun.createSdtContent();
srcSdtContent.createRun().setText("t3");
srcSdtContent.createRun().setText("t4");

dstSdtContent.cloneExistingIRunElement(srcSdtRun);

Assertions.assertEquals(1, document.getParagraphs().size());
paragraph = document.getParagraphs().get(0);

Assertions.assertEquals(2, paragraph.getIRuns().size());
XWPFSDTRun sdtRun1 = (XWPFSDTRun) paragraph.getIRuns().get(0);
Assertions.assertEquals(3, sdtRun1.getContent().getIRuns().size());
Assertions.assertEquals("t1", ((XWPFRun) sdtRun1.getContent().getIRuns().get(0)).text());
Assertions.assertEquals("t2", ((XWPFRun) sdtRun1.getContent().getIRuns().get(1)).text());

XWPFSDTRun innerSdtRun = (XWPFSDTRun) sdtRun1.getContent().getIRuns().get(2);
Assertions.assertEquals("tag", innerSdtRun.getSdtPr().getTag());
Assertions.assertEquals(2, innerSdtRun.getContent().getIRuns().size());
Assertions.assertEquals("t3", ((XWPFRun) innerSdtRun.getContent().getIRuns().get(0)).text());
Assertions.assertEquals("t4", ((XWPFRun) innerSdtRun.getContent().getIRuns().get(1)).text());

XWPFSDTRun sdtRun2 = (XWPFSDTRun) paragraph.getIRuns().get(1);
Assertions.assertEquals("tag", sdtRun2.getSdtPr().getTag());
Assertions.assertEquals(2, sdtRun2.getContent().getIRuns().size());
Assertions.assertEquals("t3", ((XWPFRun) sdtRun2.getContent().getIRuns().get(0)).text());
Assertions.assertEquals("t4", ((XWPFRun) sdtRun2.getContent().getIRuns().get(1)).text());
}

/**
* Verify that existing Content Control in document is correctly
* unmarshalled & we can proceed with modifying its content
Expand Down

0 comments on commit 50d23a0

Please sign in to comment.