Skip to content

Commit fffb57b

Browse files
authored
Convert DOAJ fetcher to new interface (#4139)
* Convert DOAJ fetcher to new interface * Add doc url * Move to logic * Increase limit to 30
1 parent b411cfb commit fffb57b

File tree

11 files changed

+199
-146
lines changed

11 files changed

+199
-146
lines changed

src/main/java/org/jabref/gui/importer/fetcher/DOAJFetcher.java

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/main/java/org/jabref/gui/importer/fetcher/EntryFetchers.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public EntryFetchers(JournalAbbreviationLoader abbreviationLoader) {
1818
entryFetchers.add(new INSPIREFetcher());
1919
// entryFetchers.add(new OAI2Fetcher()); - new arXiv fetcher in place, see below
2020
entryFetchers.add(new ACMPortalFetcher());
21-
entryFetchers.add(new DOAJFetcher());
2221

2322
WebFetchers.getSearchBasedFetchers(Globals.prefs.getImportFormatPreferences()).stream()
2423
.map(SearchBasedEntryFetcher::new)

src/main/java/org/jabref/gui/importer/fetcher/SearchBasedEntryFetcher.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public SearchBasedEntryFetcher(SearchBasedFetcher fetcher) {
3131

3232
@Override
3333
public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
34-
3534
status.setStatus(Localization.lang("Processing %0", query));
3635
try {
3736
List<BibEntry> matches = fetcher.performSearch(query);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.jabref.logic.importer.fetcher.AstrophysicsDataSystem;
1111
import org.jabref.logic.importer.fetcher.CrossRef;
1212
import org.jabref.logic.importer.fetcher.DBLPFetcher;
13+
import org.jabref.logic.importer.fetcher.DOAJFetcher;
1314
import org.jabref.logic.importer.fetcher.DiVA;
1415
import org.jabref.logic.importer.fetcher.DoiFetcher;
1516
import org.jabref.logic.importer.fetcher.DoiResolution;
@@ -84,6 +85,7 @@ public static List<SearchBasedFetcher> getSearchBasedFetchers(ImportFormatPrefer
8485
list.add(new DBLPFetcher(importFormatPreferences));
8586
list.add(new SpringerFetcher());
8687
list.add(new CrossRef());
88+
list.add(new DOAJFetcher(importFormatPreferences));
8789
list.sort(Comparator.comparing(WebFetcher::getName));
8890
return list;
8991
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.jabref.logic.importer.fetcher;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.MalformedURLException;
6+
import java.net.URISyntaxException;
7+
import java.net.URL;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Objects;
11+
import java.util.stream.Collectors;
12+
13+
import org.jabref.logic.help.HelpFile;
14+
import org.jabref.logic.importer.FetcherException;
15+
import org.jabref.logic.importer.ImportFormatPreferences;
16+
import org.jabref.logic.importer.Parser;
17+
import org.jabref.logic.importer.SearchBasedParserFetcher;
18+
import org.jabref.logic.importer.util.JSONEntryParser;
19+
import org.jabref.logic.net.URLUtil;
20+
import org.jabref.logic.util.OS;
21+
import org.jabref.model.entry.BibEntry;
22+
23+
import org.apache.http.client.utils.URIBuilder;
24+
import org.json.JSONArray;
25+
import org.json.JSONObject;
26+
27+
/**
28+
* Fetches data from the Directory of Open Access Journals (DOAJ)
29+
*
30+
* @implNote <a href="https://doaj.org/api/v1/docs">API documentation</a>
31+
*/
32+
public class DOAJFetcher implements SearchBasedParserFetcher {
33+
34+
private static final String SEARCH_URL = "https://doaj.org/api/v1/search/articles/";
35+
private final ImportFormatPreferences preferences;
36+
37+
public DOAJFetcher(ImportFormatPreferences preferences) {
38+
this.preferences = Objects.requireNonNull(preferences);
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return "DOAJ";
44+
}
45+
46+
@Override
47+
public HelpFile getHelpPage() {
48+
return HelpFile.FETCHER_DOAJ;
49+
}
50+
51+
@Override
52+
public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException {
53+
URIBuilder uriBuilder = new URIBuilder(SEARCH_URL);
54+
URLUtil.addPath(uriBuilder, query);
55+
uriBuilder.addParameter("pageSize", "30"); // Number of results
56+
//uriBuilder.addParameter("page", "1"); // Page (not needed so far)
57+
return uriBuilder.build().toURL();
58+
}
59+
60+
@Override
61+
public Parser getParser() {
62+
return inputStream -> {
63+
JSONEntryParser jsonConverter = new JSONEntryParser();
64+
String response = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(OS.NEWLINE));
65+
JSONObject jsonObject = new JSONObject(response);
66+
67+
List<BibEntry> entries = new ArrayList<>();
68+
if (jsonObject.has("results")) {
69+
JSONArray results = jsonObject.getJSONArray("results");
70+
for (int i = 0; i < results.length(); i++) {
71+
JSONObject bibJsonEntry = results.getJSONObject(i).getJSONObject("bibjson");
72+
BibEntry entry = jsonConverter.parseBibJSONtoBibtex(bibJsonEntry, preferences.getKeywordSeparator());
73+
entries.add(entry);
74+
}
75+
}
76+
return entries;
77+
};
78+
}
79+
}

src/main/java/org/jabref/logic/importer/fetcher/MathSciNet.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ public URL getURLForID(String identifier) throws URISyntaxException, MalformedUR
8585

8686
@Override
8787
public Parser getParser() {
88-
8988
// MathSciNet returns the BibTeX result embedded in HTML
9089
// So we extract the BibTeX string from the <pre>bibtex</pre> tags and pass the content to the BibTeX parser
9190
return inputStream -> {
92-
String response = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(
93-
Collectors.joining(OS.NEWLINE));
91+
String response = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(OS.NEWLINE));
9492

9593
List<BibEntry> entries = new ArrayList<>();
9694
BibtexParser bibtexParser = new BibtexParser(preferences, new DummyFileUpdateMonitor());

src/main/java/org/jabref/logic/importer/util/JSONEntryParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public BibEntry parseBibJSONtoBibtex(JSONObject bibJsonEntry, Character keywordS
7272
JSONObject journal = bibJsonEntry.getJSONObject("journal");
7373
// Journal title
7474
if (journal.has("title")) {
75-
entry.setField(FieldName.JOURNAL, journal.getString("title"));
75+
entry.setField(FieldName.JOURNAL, journal.getString("title").trim());
7676
} else {
7777
LOGGER.info("No journal title found.");
7878
}
@@ -91,7 +91,7 @@ public BibEntry parseBibJSONtoBibtex(JSONObject bibJsonEntry, Character keywordS
9191
JSONArray keywords = bibJsonEntry.getJSONArray("keywords");
9292
for (int i = 0; i < keywords.length(); i++) {
9393
if (!keywords.isNull(i)) {
94-
entry.addKeyword(keywords.getString(i), keywordSeparator);
94+
entry.addKeyword(keywords.getString(i).trim(), keywordSeparator);
9595
}
9696
}
9797
}

src/main/java/org/jabref/logic/net/URLUtil.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import java.nio.charset.StandardCharsets;
88
import java.util.Objects;
99

10+
import org.jabref.model.strings.StringUtil;
11+
12+
import org.apache.http.client.utils.URIBuilder;
13+
1014
public class URLUtil {
1115
private static final String URL_EXP = "^(https?|ftp)://.+";
1216

@@ -74,4 +78,28 @@ public static boolean isURL(String url) {
7478
return url.contains("://");
7579
}
7680

81+
/**
82+
* @implNote slightly altered version based on https://gist.github.com/enginer/230e2dc2f1d213a825d5
83+
*/
84+
public static URIBuilder addPath(URIBuilder base, String subPath) {
85+
if (StringUtil.isBlank(subPath) || "/".equals(subPath)) {
86+
return base;
87+
} else {
88+
base.setPath(appendSegmentToPath(base.getPath(), subPath));
89+
return base;
90+
}
91+
}
92+
93+
private static String appendSegmentToPath(String path, String segment) {
94+
if (StringUtil.isBlank(path)) {
95+
path = "/";
96+
}
97+
98+
if (path.charAt(path.length() - 1) == '/' || segment.startsWith("/")) {
99+
return path + segment;
100+
}
101+
102+
return path + "/" + segment;
103+
}
104+
77105
}

src/test/java/org/jabref/logic/importer/WebFetchersTest.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@
33
import java.util.List;
44
import java.util.Set;
55
import java.util.stream.Collectors;
6-
import java.util.stream.Stream;
76

8-
import org.jabref.logic.importer.fetcher.ACS;
97
import org.jabref.logic.importer.fetcher.AbstractIsbnFetcher;
10-
import org.jabref.logic.importer.fetcher.ArXiv;
11-
import org.jabref.logic.importer.fetcher.DoiResolution;
12-
import org.jabref.logic.importer.fetcher.GoogleScholar;
13-
import org.jabref.logic.importer.fetcher.IEEE;
148
import org.jabref.logic.importer.fetcher.IsbnViaChimboriFetcher;
159
import org.jabref.logic.importer.fetcher.IsbnViaEbookDeFetcher;
1610
import org.jabref.logic.importer.fetcher.MrDLibFetcher;
17-
import org.jabref.logic.importer.fetcher.OpenAccessDoi;
18-
import org.jabref.logic.importer.fetcher.ScienceDirect;
19-
import org.jabref.logic.importer.fetcher.SpringerLink;
2011

2112
import org.junit.jupiter.api.BeforeEach;
2213
import org.junit.jupiter.api.Test;
@@ -25,18 +16,18 @@
2516
import static org.junit.jupiter.api.Assertions.assertEquals;
2617
import static org.mockito.Mockito.mock;
2718

28-
public class WebFetchersTest {
19+
class WebFetchersTest {
2920

3021
Reflections reflections = new Reflections("org.jabref");
3122
ImportFormatPreferences importFormatPreferences;
3223

3324
@BeforeEach
34-
public void setUp() throws Exception {
25+
void setUp() throws Exception {
3526
importFormatPreferences = mock(ImportFormatPreferences.class);
3627
}
3728

3829
@Test
39-
public void getIdBasedFetchersReturnsAllFetcherDerivingFromIdBasedFetcher() throws Exception {
30+
void getIdBasedFetchersReturnsAllFetcherDerivingFromIdBasedFetcher() throws Exception {
4031
List<IdBasedFetcher> idFetchers = WebFetchers.getIdBasedFetchers(importFormatPreferences);
4132

4233
Set<Class<? extends IdBasedFetcher>> expected = reflections.getSubTypesOf(IdBasedFetcher.class);
@@ -49,7 +40,7 @@ public void getIdBasedFetchersReturnsAllFetcherDerivingFromIdBasedFetcher() thro
4940
}
5041

5142
@Test
52-
public void getEntryBasedFetchersReturnsAllFetcherDerivingFromEntryBasedFetcher() throws Exception {
43+
void getEntryBasedFetchersReturnsAllFetcherDerivingFromEntryBasedFetcher() throws Exception {
5344
List<EntryBasedFetcher> idFetchers = WebFetchers.getEntryBasedFetchers(importFormatPreferences);
5445

5546
Set<Class<? extends EntryBasedFetcher>> expected = reflections.getSubTypesOf(EntryBasedFetcher.class);
@@ -59,7 +50,7 @@ public void getEntryBasedFetchersReturnsAllFetcherDerivingFromEntryBasedFetcher(
5950
}
6051

6152
@Test
62-
public void getSearchBasedFetchersReturnsAllFetcherDerivingFromSearchBasedFetcher() throws Exception {
53+
void getSearchBasedFetchersReturnsAllFetcherDerivingFromSearchBasedFetcher() throws Exception {
6354
List<SearchBasedFetcher> searchBasedFetchers = WebFetchers.getSearchBasedFetchers(importFormatPreferences);
6455

6556
Set<Class<? extends SearchBasedFetcher>> expected = reflections.getSubTypesOf(SearchBasedFetcher.class);
@@ -68,15 +59,15 @@ public void getSearchBasedFetchersReturnsAllFetcherDerivingFromSearchBasedFetche
6859
}
6960

7061
@Test
71-
public void getFullTextFetchersReturnsAllFetcherDerivingFromFullTextFetcher() throws Exception {
62+
void getFullTextFetchersReturnsAllFetcherDerivingFromFullTextFetcher() throws Exception {
7263
List<FulltextFetcher> fullTextFetchers = WebFetchers.getFullTextFetchers(importFormatPreferences);
7364

7465
Set<Class<? extends FulltextFetcher>> expected = reflections.getSubTypesOf(FulltextFetcher.class);
7566
assertEquals(expected, getClasses(fullTextFetchers));
7667
}
7768

7869
@Test
79-
public void getIdFetchersReturnsAllFetcherDerivingFromIdFetcher() throws Exception {
70+
void getIdFetchersReturnsAllFetcherDerivingFromIdFetcher() throws Exception {
8071
List<IdFetcher> idFetchers = WebFetchers.getIdFetchers(importFormatPreferences);
8172

8273
Set<Class<? extends IdFetcher>> expected = reflections.getSubTypesOf(IdFetcher.class);

0 commit comments

Comments
 (0)