Skip to content

Commit

Permalink
fix xml tag matching
Browse files Browse the repository at this point in the history
* use correct regex for vertical whitespace splitting
* only match tag start + tag name
  • Loading branch information
infeo committed Jul 17, 2024
1 parent b999064 commit 5e9eacc
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public QuickAccessService.QuickAccessEntry add(Path target, String displayName)
//validate
xmlValidator.validate(new StreamSource(new StringReader(placesContent)));
// modify
int insertIndex = placesContent.lastIndexOf("</xbel>"); //cannot be -1 due to validation
int insertIndex = placesContent.lastIndexOf("</xbel"); //cannot be -1 due to validation; we do not match the end tag, since betweent tag name and closing bracket can be whitespaces
try (var writer = Files.newBufferedWriter(TMP_FILE, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
writer.write(placesContent, 0, insertIndex);
writer.newLine();
Expand Down Expand Up @@ -126,9 +126,9 @@ public void remove() throws QuickAccessServiceException {
int openingTagIndex = placesContentPart1.lastIndexOf("<bookmark href=");
var contentToWrite1 = placesContentPart1.substring(0, openingTagIndex).stripTrailing();

int closingTagIndex = placesContent.indexOf("</bookmark>", idIndex);
var part2Tmp = placesContent.substring(closingTagIndex + "</bookmark>".length()).split("\\v*", 2); //removing leading vertical whitespaces
var contentToWrite2 = part2Tmp.length == 1 ? part2Tmp[0] : part2Tmp[1];
int closingTagEndIndex = placesContent.indexOf('>', placesContent.indexOf("</bookmark", idIndex));
var part2Tmp = placesContent.substring(closingTagEndIndex + 1).split("\\v+", 2); //removing leading vertical whitespaces, but no indentation
var contentToWrite2 = part2Tmp[part2Tmp.length - 1];

try (var writer = Files.newBufferedWriter(TMP_FILE, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
writer.write(contentToWrite1);
Expand Down

0 comments on commit 5e9eacc

Please sign in to comment.