diff --git a/src/main/java/org/jabref/logic/importer/util/FileFieldParser.java b/src/main/java/org/jabref/logic/importer/util/FileFieldParser.java index cdbb1818807..9c51c7468fc 100644 --- a/src/main/java/org/jabref/logic/importer/util/FileFieldParser.java +++ b/src/main/java/org/jabref/logic/importer/util/FileFieldParser.java @@ -1,5 +1,7 @@ package org.jabref.logic.importer.util; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -65,7 +67,20 @@ private static LinkedFile convert(List entry) { while (entry.size() < 3) { entry.add(""); } - LinkedFile field = new LinkedFile(entry.get(0), Path.of(entry.get(1)), entry.get(2)); + + LinkedFile field = null; + if (LinkedFile.isOnlineLink(entry.get(1))) { + try { + field = new LinkedFile(new URL(entry.get(1)), entry.get(2)); + } catch (MalformedURLException ignored) { + // ignored + } + } + + if (field == null) { + field = new LinkedFile(entry.get(0), Path.of(entry.get(1)), entry.get(2)); + } + // link is only mandatory field if (field.getDescription().isEmpty() && field.getLink().isEmpty() && !field.getFileType().isEmpty()) { field = new LinkedFile("", Path.of(field.getFileType()), ""); diff --git a/src/main/java/org/jabref/model/entry/LinkedFile.java b/src/main/java/org/jabref/model/entry/LinkedFile.java index d06e4d956e2..ba98a8ba14e 100644 --- a/src/main/java/org/jabref/model/entry/LinkedFile.java +++ b/src/main/java/org/jabref/model/entry/LinkedFile.java @@ -133,7 +133,7 @@ private void readObject(ObjectInputStream in) throws IOException { * @param toCheck The String to check * @return true, if it starts with "http://", "https://" or contains "www."; false otherwise */ - private boolean isOnlineLink(String toCheck) { + public static boolean isOnlineLink(String toCheck) { String normalizedFilePath = toCheck.trim().toLowerCase(); return normalizedFilePath.startsWith("http://") || normalizedFilePath.startsWith("https://") || normalizedFilePath.contains("www."); } diff --git a/src/test/java/org/jabref/logic/bibtex/FileFieldWriterTest.java b/src/test/java/org/jabref/logic/bibtex/FileFieldWriterTest.java index 38fdbc465ff..7305ebbcfb3 100644 --- a/src/test/java/org/jabref/logic/bibtex/FileFieldWriterTest.java +++ b/src/test/java/org/jabref/logic/bibtex/FileFieldWriterTest.java @@ -1,8 +1,11 @@ package org.jabref.logic.bibtex; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; +import java.util.List; import org.jabref.logic.importer.util.FileFieldParser; import org.jabref.model.entry.LinkedFile; @@ -31,6 +34,24 @@ public void parseCorrectInput() { FileFieldParser.parse(input)); } + @Test + public void parseCorrectOnlineInput() throws MalformedURLException { + String input = ":http\\://arxiv.org/pdf/2010.08497v1:PDF"; + String inputURL = "http://arxiv.org/pdf/2010.08497v1"; + List expected = Collections.singletonList(new LinkedFile(new URL(inputURL), "PDF")); + + assertEquals(expected, FileFieldParser.parse(input)); + } + + @Test + public void parseFaultyOnlineInput() { + String input = ":htt\\://arxiv.org/pdf/2010.08497v1:PDF"; + String inputURL = "htt://arxiv.org/pdf/2010.08497v1"; + List expected = Collections.singletonList(new LinkedFile("", Path.of(inputURL), "PDF")); + + assertEquals(expected, FileFieldParser.parse(input)); + } + @Test public void ingoreMissingDescription() { String input = ":wei2005ahp.pdf:PDF";