Skip to content

Commit

Permalink
Don't resolve to custom placeholder without brackets (#256)
Browse files Browse the repository at this point in the history
Due to a missing check for brackets, word paragraphs which
contained the name of a custom placeholder without brackets were
resolved to the placeholder.
This commit adds the check
  • Loading branch information
AntonOellerer authored Aug 30, 2024
1 parent 32dca6c commit 6e54df9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 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 = '4.2.1'
version = '4.2.2'

java {
toolchain {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/docutools/jocument/impl/ParsingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public static List<String> getMatchingLoopEnds(String placeholder) {
public static Matcher matchPlaceholders(String value) {
return PLACEHOLDER_PATTERN.matcher(value);
}

public static boolean containsPlaceholder(String text) {
return PLACEHOLDER_PATTERN.matcher(text).find();
}
}
20 changes: 13 additions & 7 deletions src/main/java/com/docutools/jocument/impl/word/WordGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,19 @@ private boolean isLoopStart(IBodyElement element, List<IBodyElement> remaining)
}

private boolean isCustomPlaceholder(IBodyElement element) {
return element instanceof XWPFParagraph xwpfParagraph
&& resolver.resolve(
ParsingUtils.stripBrackets(
WordUtilities.toString(xwpfParagraph)
)).map(PlaceholderData::getType)
.map(type -> type == PlaceholderType.CUSTOM)
.orElse(false);
if (!(element instanceof XWPFParagraph xwpfParagraph)) {
return false;
}
String paragraphText = WordUtilities.toString(xwpfParagraph);
if (!ParsingUtils.containsPlaceholder(paragraphText)) {
return false;
}
return resolver.resolve(
ParsingUtils.stripBrackets(
paragraphText
)).map(PlaceholderData::getType)
.map(type -> type == PlaceholderType.CUSTOM)
.orElse(false);
}

private String fillPlaceholder(MatchResult result, Locale locale) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,27 @@ void keepsPageNumbering() throws InterruptedException, IOException {
.map(String::strip)
.toList();
assertThat(instructions, contains("PAGE", "NUMPAGES"));
}

@Test
void doesNotResolveCustomPlaceholderWithoutBrackets() throws InterruptedException, IOException {
// assemble
Template template = Template.fromClassPath("/templates/word/NoCustomPlaceholder.docx")
.orElseThrow();
CustomPlaceholderRegistry customPlaceholderRegistry = new CustomPlaceholderRegistryImpl();
customPlaceholderRegistry.addHandler("quote", QuotePlaceholder.class);
PlaceholderResolver resolver = new ReflectionResolver(SampleModelData.PLANET, 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).asParagraph().run(0).text(),
equalTo("quote"));
}

}
Binary file not shown.

0 comments on commit 6e54df9

Please sign in to comment.