Skip to content

Commit

Permalink
Ignore the table of contents when creating a document
Browse files Browse the repository at this point in the history
The object containing the toc has no body, which lead
to `NullPointerExceptions` during document generation
  • Loading branch information
AntonOellerer committed Sep 26, 2022
1 parent e6b20ca commit 6524fa5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 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.1'
version = '1.5.2'

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import org.apache.poi.xwpf.usermodel.XWPFTable;

class WordGenerator {
Expand All @@ -39,7 +40,7 @@ private void generate() {
for (int i = 0; i < elements.size(); i++) {
var element = elements.get(i);

if (!WordUtilities.exists(element)) {
if (element instanceof XWPFSDT || !WordUtilities.exists(element)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,23 @@ void shouldScaleLargePicture() throws IOException, InterruptedException {
var documentWrapper = new XWPFDocumentWrapper(xwpfDocument);
assertThat(documentWrapper.bodyElement(0).asParagraph().run(0).pictures(), hasSize(1));
}

@Test
@DisplayName("Process Document With TOC")
void shouldProcessDocumentWithTOC() throws IOException, InterruptedException {
// Assemble
Template template = Template.fromClassPath("/templates/word/TOCTemplate.docx")
.orElseThrow();
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.FUTURE_PICARD);

// 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).asParagraph().runs(), hasSize(1));
}
}
Binary file not shown.

0 comments on commit 6524fa5

Please sign in to comment.