Skip to content

Commit a5dca5b

Browse files
committed
improve entry removal tolerance against reformatted document
1 parent 5e9eacc commit a5dca5b

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java/org/cryptomator/linux/quickaccess/DolphinPlaces.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.file.Path;
2121
import java.nio.file.StandardCopyOption;
2222
import java.nio.file.StandardOpenOption;
23+
import java.util.List;
2324
import java.util.UUID;
2425
import java.util.concurrent.TimeUnit;
2526
import java.util.concurrent.locks.Lock;
@@ -122,9 +123,8 @@ public void remove() throws QuickAccessServiceException {
122123
//validate
123124
xmlValidator.validate(new StreamSource(new StringReader(placesContent)));
124125
//modify
125-
var placesContentPart1 = placesContent.substring(0, idIndex);
126-
int openingTagIndex = placesContentPart1.lastIndexOf("<bookmark href=");
127-
var contentToWrite1 = placesContentPart1.substring(0, openingTagIndex).stripTrailing();
126+
int openingTagIndex = indexOfEntryOpeningTag(placesContent, idIndex);
127+
var contentToWrite1 = placesContent.substring(0, openingTagIndex).stripTrailing();
128128

129129
int closingTagEndIndex = placesContent.indexOf('>', placesContent.indexOf("</bookmark", idIndex));
130130
var part2Tmp = placesContent.substring(closingTagEndIndex + 1).split("\\v+", 2); //removing leading vertical whitespaces, but no indentation
@@ -144,6 +144,17 @@ public void remove() throws QuickAccessServiceException {
144144
MODIFY_LOCK.unlock();
145145
}
146146
}
147+
148+
private int indexOfEntryOpeningTag(String placesContent, int idIndex) {
149+
var xmlWhitespaceChars = List.of(' ', '\t', '\n');
150+
for (char c : xmlWhitespaceChars) {
151+
int idx = placesContent.lastIndexOf("<bookmark" + c, idIndex);
152+
if (idx != -1) {
153+
return idx;
154+
}
155+
}
156+
throw new IllegalStateException("File " + PLACES_FILE + " is valid xbel file, but does not contain opening bookmark tag.");
157+
}
147158
}
148159

149160
@CheckAvailability

0 commit comments

Comments
 (0)