Skip to content

Commit

Permalink
Resolve custom placeholders in tables
Browse files Browse the repository at this point in the history
To be able to resolve custom placeholders in tables,
this commit applies the full transformation
process to IBodyElements found in tables
  • Loading branch information
AntonOellerer committed Oct 5, 2022
1 parent d0651c5 commit 89694d3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.5.7-alpha.1'
version = '1.5.8-alpha.1'

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ private void transform(IBodyElement element, List<IBodyElement> remaining) {
} else if (element instanceof XWPFParagraph xwpfParagraph) {
transform(xwpfParagraph);
} else if (element instanceof XWPFTable xwpfTable) {
transform(xwpfTable);
transform(xwpfTable, remaining);
} else {
logger.info("Failed to transform element {}", element);
}
}

private void transform(XWPFTable table) {
private void transform(XWPFTable table, List<IBodyElement> remaining) {
table.getRows()
.stream()
.flatMap(xwpfTableRow -> xwpfTableRow.getTableCells().stream())
.flatMap(xwpfTableCell -> xwpfTableCell.getParagraphs().stream())
.filter(xwpfParagraph -> !xwpfParagraph.isEmpty())
.forEach(this::transform);
.forEach(xwpfParagraph -> this.transform(xwpfParagraph, remaining));
logger.debug("Transformed table {}", table);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ private static OptionalInt findPositionInFooter(XWPFTable xwpfTable, List<XWPFFo
public static Optional<XmlCursor> openCursor(IBodyElement element) {
if (element instanceof XWPFParagraph xwpfParagraph) {
logger.debug("Opening cursor to paragraph {}", xwpfParagraph);
return Optional.of((xwpfParagraph).getCTP().newCursor());
return Optional.of(xwpfParagraph.getCTP().newCursor());
} else if (element instanceof XWPFTable xwpfTable) {
logger.debug("Opening cursor to table {}", xwpfTable);
return Optional.of((xwpfTable).getCTTbl().newCursor());
return Optional.of(xwpfTable.getCTTbl().newCursor());
} else {
logger.warn("Failed to open cursor to element {}", element);
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ void shouldApplyForeignCustomWordPlaceholder() throws InterruptedException, IOEx
equalTo("Live your life not celebrating victories, but overcoming defeats."));
}

@Test
@DisplayName("Apply custom word placeholder wrapped in table.")
void shouldApplyCustomPlaceholderInTable() throws InterruptedException, IOException {
// Arrange
Template template = Template.fromClassPath("/templates/word/CustomPlaceholderInTableTemplate.docx")
.orElseThrow();
CustomPlaceholderRegistry customPlaceholderRegistry = new CustomPlaceholderRegistryImpl();
customPlaceholderRegistry.addHandler("quote", QuotePlaceholder.class);
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PICARD, customPlaceholderRegistry);

// Act
Document document = template.startGeneration(resolver);
document.blockUntilCompletion(60000L); // 1 minute

// Assert
assertThat(document.completed(), is(true));
xwpfDocument = TestUtils.getXWPFDocumentFromDocument(document);
var documentWrapper = new XWPFDocumentWrapper(xwpfDocument);
assertThat(documentWrapper.bodyElement(0).asTable().row(0).cell(0).text(),
equalTo("Live your life not celebrating victories, but overcoming defeats."));
}

@Test
@DisplayName("Resolve legacy placeholder")
void shouldResolveLegacy() throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import java.util.Locale;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

public class QuotePlaceholder extends CustomWordPlaceholderData {
@Override
protected void transform(IBodyElement placeholder, XWPFDocument document, Locale locale, GenerationOptions options) {
var paragraph = document.insertNewParagraph(WordUtilities.openCursor(placeholder).orElseThrow());
paragraph.createRun().setText("Live your life not celebrating victories, but overcoming defeats.");
WordUtilities.removeIfExists(placeholder);
if (placeholder instanceof XWPFParagraph xwpfParagraph) {
WordUtilities.replaceText(xwpfParagraph, "Live your life not celebrating victories, but overcoming defeats.");
}
}
}
Binary file not shown.

0 comments on commit 89694d3

Please sign in to comment.