Skip to content

Commit

Permalink
Fix detection of loop tags
Browse files Browse the repository at this point in the history
* detect loop start tags via regex
* match loop end tag case-ignoring
  • Loading branch information
AntonOellerer committed Jul 16, 2024
1 parent 6f04be8 commit 9418f07
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 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.1.1'
version = '4.1.2'

java {
toolchain {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/docutools/jocument/impl/DocumentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public abstract class DocumentImpl extends Thread implements Document {
public static final String GERMAN_SPECIAL_CHARACTERS = "ÄäÖöÜüß";
public static final Pattern TAG_PATTERN = Pattern.compile("\\{\\{([A-Za-z\\d" + GERMAN_SPECIAL_CHARACTERS + "@\\-/#.]+)}}");
public static final Pattern LOOP_END_PATTERN = Pattern.compile("\\{\\{/([A-Za-z\\d" + GERMAN_SPECIAL_CHARACTERS + "@\\-/#.]+)}}");
public static final Pattern TAG_PATTERN = Pattern.compile("\\{\\{([A-Za-z\\d" + GERMAN_SPECIAL_CHARACTERS + "@\\-/#.]+\\??)}}");
public static final Pattern LOOP_END_PATTERN = Pattern.compile("\\{\\{/([A-Za-z\\d" + GERMAN_SPECIAL_CHARACTERS + "@\\-/#.]+\\??)}}");
private static final Logger logger = LogManager.getLogger();
protected final Template template;
protected final PlaceholderResolver resolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static String stripBrackets(String value) {
}

public static List<String> getMatchingLoopEnds(String placeholder) {
return List.of("{{/%s}}".formatted(placeholder), "{{end-%s}}".formatted(placeholder));
return List.of("{{/%s}}".formatted(placeholder).toLowerCase(), "{{end-%s}}".formatted(placeholder).toLowerCase());
}

public static Matcher matchPlaceholders(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private boolean isLoopStart(Row row) {
.map(nextRow -> nextRow.getCell(nextRow.getFirstCellNum()))
.filter(nextCell -> nextCell.getCellType() == CellType.STRING)
.map(Cell::getStringCellValue)
.anyMatch(text -> endLoopMarkers.contains(text.strip()));
.anyMatch(text -> endLoopMarkers.contains(text.strip().toLowerCase()));
}).orElse(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static boolean isMatchingLoopEnd(Row row, String placeholder) {
if (getNumberOfNonEmptyCells(row) == 1) {
var cell = row.getCell(row.getFirstCellNum());
if (cell.getCellType() == CellType.STRING) {
return endPlaceholders.stream().anyMatch(endPlaceholder -> cell.getStringCellValue().equals(endPlaceholder));
return endPlaceholders.stream().anyMatch(endPlaceholder -> cell.getStringCellValue().strip().toLowerCase().equals(endPlaceholder));
}
}
return false;
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/com/docutools/jocument/impl/word/WordGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,26 @@ private List<IBodyElement> getLoopBody(String placeholderName, List<IBodyElement
return remaining.stream()
//Could be written nice with `takeUntil(element -> (element instanceof XP xp && eLM.equals(WU.toString(xp)))
.takeWhile(element -> !(element instanceof XWPFParagraph xwpfParagraph
&& endLoopMarkers.stream().anyMatch(endLoopMarker -> endLoopMarker.equals(WordUtilities.toString(xwpfParagraph).strip()))))
&& endLoopMarkers.stream().anyMatch(endLoopMarker -> endLoopMarker.equals(WordUtilities.toString(xwpfParagraph).strip().toLowerCase()))))
.toList();
}

private boolean isLoopStart(IBodyElement element, List<IBodyElement> remaining) {
if (element instanceof XWPFParagraph xwpfParagraph) {
var placeholderName = ParsingUtils.stripBrackets(
WordUtilities.toString(xwpfParagraph)
);
return resolver.resolve(placeholderName)
.filter(placeholderData -> placeholderData.getType() == PlaceholderType.SET)
.map(placeholderData -> {
var endLoopMarkers = ParsingUtils.getMatchingLoopEnds(placeholderName);
return remaining.stream()
.filter(XWPFParagraph.class::isInstance)
.map(XWPFParagraph.class::cast)
.map(XWPFParagraph::getText)
.anyMatch(text -> endLoopMarkers.contains(text.strip()));
}).orElse(false);
Matcher matcher = TAG_PATTERN.matcher(WordUtilities.toString(xwpfParagraph));
if (matcher.find()) {
var placeholderName = matcher.group(1);
return resolver.resolve(placeholderName)
.filter(placeholderData -> placeholderData.getType() == PlaceholderType.SET)
.map(placeholderData -> {
var endLoopMarkers = ParsingUtils.getMatchingLoopEnds(placeholderName);
return remaining.stream()
.filter(XWPFParagraph.class::isInstance)
.map(XWPFParagraph.class::cast)
.map(XWPFParagraph::getText)
.anyMatch(text -> endLoopMarkers.contains(text.strip().toLowerCase()));
}).orElse(false);
}
}
return false;
}
Expand Down

0 comments on commit 9418f07

Please sign in to comment.