Skip to content

Commit

Permalink
Add online-link detection to FileFieldParser (#7043)
Browse files Browse the repository at this point in the history
* Add online-link detection to FileFieldParser

* Remove double space

* Remove try-catch block from control structure

* Add missing space

* Fix checkstyle violations
  • Loading branch information
Niffler authored Oct 28, 2020
1 parent 3fe34a0 commit 47fd562
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -65,7 +67,20 @@ private static LinkedFile convert(List<String> 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()), "");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/model/entry/LinkedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void readObject(ObjectInputStream in) throws IOException {
* @param toCheck The String to check
* @return <code>true</code>, if it starts with "http://", "https://" or contains "www."; <code>false</code> 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.");
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/jabref/logic/bibtex/FileFieldWriterTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<LinkedFile> 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<LinkedFile> expected = Collections.singletonList(new LinkedFile("", Path.of(inputURL), "PDF"));

assertEquals(expected, FileFieldParser.parse(input));
}

@Test
public void ingoreMissingDescription() {
String input = ":wei2005ahp.pdf:PDF";
Expand Down

0 comments on commit 47fd562

Please sign in to comment.