Skip to content

Commit 0c439de

Browse files
Siedlerchrkoppor
andauthored
Fix online link detection in entry editor (#8514)
* Fix online link detection in entry editor * add changelog * Fix typo * update linked file * fix test * remove check for colon * fix checkstyle * fix checkstyle javadoc Co-authored-by: Oliver Kopp <[email protected]>
1 parent 3c247ff commit 0c439de

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
2121

2222
### Fixed
2323

24+
- We fixed an issue where online links in the file field were not detected correctly and could produce an exception [#8150](https://github.com/JabRef/jabref/issues/8510)
2425
- We fixed an issue where an exception could occur when saving the preferences [#7614](https://github.com/JabRef/jabref/issues/7614)
2526
- We fixed an issue where "Copy DOI url" in the right-click menu of the Entry List would just copy the DOI and not the DOI url. [#8389](https://github.com/JabRef/jabref/issues/8389)
2627
- We fixed an issue where opening the console from the drop-down menu would cause an exception. [#8466](https://github.com/JabRef/jabref/issues/8466)

src/main/java/org/jabref/logic/importer/SearchBasedParserFetcher.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* <li>Post-process fetched entries</li>
2121
* </ol>
2222
* <p>
23-
* This interface is used for web resources which do NOT provide BibTeX data (@link {@link BibEntry)}.
23+
* This interface is used for web resources which do NOT provide BibTeX data {@link BibEntry}.
2424
* JabRef's infrastructure to convert arbitrary input data to BibTeX is {@link Parser}.
2525
* </p>
2626
* <p>
@@ -30,7 +30,7 @@
3030
* <p>
3131
* Note that this interface "should" be an abstract class.
3232
* However, Java does not support multi inheritance with classes (but with interfaces).
33-
* We need multi inheritance, because a fetcher might implement multiple query types (such as id fetching {@link IdBasedFetcher)}, complete entry {@link EntryBasedFetcher}, and search-based fetcher (this class).
33+
* We need multi inheritance, because a fetcher might implement multiple query types (such as id fetching {@link IdBasedFetcher}), complete entry {@link EntryBasedFetcher}, and search-based fetcher (this class).
3434
* </p>
3535
*/
3636
public interface SearchBasedParserFetcher extends SearchBasedFetcher {

src/main/java/org/jabref/model/entry/LinkedFile.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Objects;
1313
import java.util.Optional;
14+
import java.util.regex.Pattern;
1415

1516
import javafx.beans.Observable;
1617
import javafx.beans.property.SimpleStringProperty;
@@ -26,6 +27,9 @@
2627
*/
2728
public class LinkedFile implements Serializable {
2829

30+
private static final String REGEX_URL = "^((?:https?\\:\\/\\/|www\\.)(?:[-a-z0-9]+\\.)*[-a-z0-9]+.*)";
31+
private static final Pattern URL_PATTERN = Pattern.compile(REGEX_URL);
32+
2933
private static final LinkedFile NULL_OBJECT = new LinkedFile("", Path.of(""), "");
3034

3135
// We have to mark these properties as transient because they can't be serialized directly
@@ -145,7 +149,7 @@ private void readObject(ObjectInputStream in) throws IOException {
145149
*/
146150
public static boolean isOnlineLink(String toCheck) {
147151
String normalizedFilePath = toCheck.trim().toLowerCase();
148-
return normalizedFilePath.startsWith("http://") || normalizedFilePath.startsWith("https://") || normalizedFilePath.contains("www.");
152+
return URL_PATTERN.matcher(normalizedFilePath).matches();
149153
}
150154

151155
@Override

src/test/java/org/jabref/logic/importer/util/FileFieldParserTest.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,34 @@ private static Stream<Arguments> stringsToParseTestData() throws Exception {
138138
"desc:file.pdf:PDF:asdf"
139139
),
140140

141+
// www inside filename
142+
Arguments.of(
143+
Collections.singletonList(new LinkedFile("", Path.of("/home/www.google.de.pdf"), "")),
144+
":/home/www.google.de.pdf"
145+
),
146+
141147
// url
142148
Arguments.of(
143149
Collections.singletonList(new LinkedFile(new URL("https://books.google.de/"), "")),
144150
"https://books.google.de/"
145151
),
146152

153+
// url with www
154+
Arguments.of(
155+
Collections.singletonList(new LinkedFile(new URL("https://www.google.de/"), "")),
156+
"https://www.google.de/"
157+
),
158+
147159
// url as file
148160
Arguments.of(
149161
Collections.singletonList(new LinkedFile("", new URL("http://ceur-ws.org/Vol-438"), "URL")),
150162
":http\\://ceur-ws.org/Vol-438:URL"
151-
)
163+
),
164+
// url as file with desc
165+
Arguments.of(
166+
Collections.singletonList(new LinkedFile("desc", new URL("http://ceur-ws.org/Vol-438"), "URL")),
167+
"desc:http\\://ceur-ws.org/Vol-438:URL"
168+
)
152169
);
153170
}
154171

0 commit comments

Comments
 (0)