Skip to content

Commit

Permalink
improve entry removal tolerance against reformatted document
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Jul 17, 2024
1 parent 5e9eacc commit 66c29cc
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/org/cryptomator/linux/quickaccess/DolphinPlaces.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -122,9 +123,8 @@ public void remove() throws QuickAccessServiceException {
//validate
xmlValidator.validate(new StreamSource(new StringReader(placesContent)));
//modify
var placesContentPart1 = placesContent.substring(0, idIndex);
int openingTagIndex = placesContentPart1.lastIndexOf("<bookmark href=");
var contentToWrite1 = placesContentPart1.substring(0, openingTagIndex).stripTrailing();
int openingTagIndex = indexOfEntryOpeningTag(placesContent, idIndex);
var contentToWrite1 = placesContent.substring(0, openingTagIndex).stripTrailing();

int closingTagEndIndex = placesContent.indexOf('>', placesContent.indexOf("</bookmark", idIndex));
var part2Tmp = placesContent.substring(closingTagEndIndex + 1).split("\\v+", 2); //removing leading vertical whitespaces, but no indentation
Expand All @@ -146,6 +146,17 @@ public void remove() throws QuickAccessServiceException {
}
}

private static int indexOfEntryOpeningTag(String placesContent, int idIndex) {
var xmlWhitespaceChars = List.of(' ', '\t', '\n');
for (char c : xmlWhitespaceChars) {
int idx = placesContent.lastIndexOf("<bookmark" + c, idIndex);
if (idx != -1) {
return idx;
}
}
throw new IllegalStateException("File " + PLACES_FILE + " is valid xbel file, but does not contain opening bookmark tag.");
}

@CheckAvailability
public static boolean isSupported() {
try {
Expand Down

0 comments on commit 66c29cc

Please sign in to comment.