Skip to content

Commit

Permalink
Fix online link detection in entry editor (#8514)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Siedlerchr and koppor authored Mar 7, 2022
1 parent 3c247ff commit 0c439de
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- 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)
- We fixed an issue where an exception could occur when saving the preferences [#7614](https://github.com/JabRef/jabref/issues/7614)
- 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)
- 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* <li>Post-process fetched entries</li>
* </ol>
* <p>
* This interface is used for web resources which do NOT provide BibTeX data (@link {@link BibEntry)}.
* This interface is used for web resources which do NOT provide BibTeX data {@link BibEntry}.
* JabRef's infrastructure to convert arbitrary input data to BibTeX is {@link Parser}.
* </p>
* <p>
Expand All @@ -30,7 +30,7 @@
* <p>
* Note that this interface "should" be an abstract class.
* However, Java does not support multi inheritance with classes (but with interfaces).
* 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).
* 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).
* </p>
*/
public interface SearchBasedParserFetcher extends SearchBasedFetcher {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/model/entry/LinkedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import javafx.beans.Observable;
import javafx.beans.property.SimpleStringProperty;
Expand All @@ -26,6 +27,9 @@
*/
public class LinkedFile implements Serializable {

private static final String REGEX_URL = "^((?:https?\\:\\/\\/|www\\.)(?:[-a-z0-9]+\\.)*[-a-z0-9]+.*)";
private static final Pattern URL_PATTERN = Pattern.compile(REGEX_URL);

private static final LinkedFile NULL_OBJECT = new LinkedFile("", Path.of(""), "");

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,34 @@ private static Stream<Arguments> stringsToParseTestData() throws Exception {
"desc:file.pdf:PDF:asdf"
),

// www inside filename
Arguments.of(
Collections.singletonList(new LinkedFile("", Path.of("/home/www.google.de.pdf"), "")),
":/home/www.google.de.pdf"
),

// url
Arguments.of(
Collections.singletonList(new LinkedFile(new URL("https://books.google.de/"), "")),
"https://books.google.de/"
),

// url with www
Arguments.of(
Collections.singletonList(new LinkedFile(new URL("https://www.google.de/"), "")),
"https://www.google.de/"
),

// url as file
Arguments.of(
Collections.singletonList(new LinkedFile("", new URL("http://ceur-ws.org/Vol-438"), "URL")),
":http\\://ceur-ws.org/Vol-438:URL"
)
),
// url as file with desc
Arguments.of(
Collections.singletonList(new LinkedFile("desc", new URL("http://ceur-ws.org/Vol-438"), "URL")),
"desc:http\\://ceur-ws.org/Vol-438:URL"
)
);
}

Expand Down

0 comments on commit 0c439de

Please sign in to comment.