Skip to content

Commit

Permalink
Retrieve placeholders from string via regex
Browse files Browse the repository at this point in the history
To avoid problems related to non-breaking whitespace,
placeholders are now retrieved from strings via a regex
matcher.
AntonOellerer committed Jul 15, 2024

Verified

This commit was signed with the committer’s verified signature.
dtolnay David Tolnay
1 parent 51d13c7 commit 6f04be8
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '4.1.0'
version = '4.1.1'

java {
toolchain {
11 changes: 10 additions & 1 deletion src/main/java/com/docutools/jocument/impl/ParsingUtils.java
Original file line number Diff line number Diff line change
@@ -3,10 +3,13 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ParsingUtils {

private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{\\{([^{}]+)?}}");
private static final Logger logger = LogManager.getLogger();

private ParsingUtils() {
}
@@ -21,7 +24,13 @@ public static String stripBrackets(String value) {
if (value.length() < 4) {
return value;
}
return value.substring(2, value.length() - 2);
Matcher matcher = PLACEHOLDER_PATTERN.matcher(value);
if (matcher.find()) {
return matcher.group(1);
} else {
logger.debug("String {} did not contain a placeholder", value);
return value;
}
}

public static List<String> getMatchingLoopEnds(String placeholder) {
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ private boolean isCustomPlaceholder(IBodyElement element) {
return element instanceof XWPFParagraph xwpfParagraph
&& resolver.resolve(
ParsingUtils.stripBrackets(
WordUtilities.toString(xwpfParagraph).trim()
WordUtilities.toString(xwpfParagraph)
)).map(PlaceholderData::getType)
.map(type -> type == PlaceholderType.CUSTOM)
.orElse(false);

0 comments on commit 6f04be8

Please sign in to comment.