|
| 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.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +import org.jabref.logic.formatter.bibtexfields.ClearFormatter; |
| 15 | +import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter; |
| 16 | +import org.jabref.logic.help.HelpFile; |
| 17 | +import org.jabref.logic.importer.FetcherException; |
| 18 | +import org.jabref.logic.importer.ImportFormatPreferences; |
| 19 | +import org.jabref.logic.importer.Parser; |
| 20 | +import org.jabref.logic.importer.SearchBasedParserFetcher; |
| 21 | +import org.jabref.logic.importer.fileformat.BibtexParser; |
| 22 | +import org.jabref.logic.util.OS; |
| 23 | +import org.jabref.model.cleanup.FieldFormatterCleanup; |
| 24 | +import org.jabref.model.entry.BibEntry; |
| 25 | +import org.jabref.model.entry.FieldName; |
| 26 | +import org.jabref.model.util.DummyFileUpdateMonitor; |
| 27 | + |
| 28 | +import org.apache.http.client.utils.URIBuilder; |
| 29 | + |
| 30 | +/** |
| 31 | + * Fetches data from the INSPIRE database. |
| 32 | + * |
| 33 | + * @implNote We just use the normal search interface since it provides direct BibTeX export while the API (http://inspirehep.net/info/hep/api) currently only supports JSON and XML |
| 34 | + */ |
| 35 | +public class INSPIREFetcher implements SearchBasedParserFetcher { |
| 36 | + |
| 37 | + private static final String INSPIRE_HOST = "https://inspirehep.net/search"; |
| 38 | + |
| 39 | + private final ImportFormatPreferences preferences; |
| 40 | + |
| 41 | + public INSPIREFetcher(ImportFormatPreferences preferences) { |
| 42 | + this.preferences = preferences; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getName() { |
| 47 | + return "INSPIRE"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public HelpFile getHelpPage() { |
| 52 | + return HelpFile.FETCHER_INSPIRE; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException { |
| 57 | + URIBuilder uriBuilder = new URIBuilder(INSPIRE_HOST); |
| 58 | + uriBuilder.addParameter("p", query); // Query |
| 59 | + //uriBuilder.addParameter("jrec", "1"); // Start index (not needed at the moment) |
| 60 | + uriBuilder.addParameter("rg", "100"); // Should return up to 100 items (instead of default 25) |
| 61 | + uriBuilder.addParameter("of", "hx"); // BibTeX format |
| 62 | + return uriBuilder.build().toURL(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Parser getParser() { |
| 67 | + // Inspire returns the BibTeX result embedded in HTML |
| 68 | + // So we extract the BibTeX string from the <pre>bibtex</pre> tags and pass the content to the BibTeX parser |
| 69 | + return inputStream -> { |
| 70 | + String response = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining(OS.NEWLINE)); |
| 71 | + |
| 72 | + List<BibEntry> entries = new ArrayList<>(); |
| 73 | + BibtexParser bibtexParser = new BibtexParser(preferences, new DummyFileUpdateMonitor()); |
| 74 | + Pattern pattern = Pattern.compile("<pre>(?s)(.*)</pre>"); |
| 75 | + Matcher matcher = pattern.matcher(response); |
| 76 | + while (matcher.find()) { |
| 77 | + String bibtexEntryString = matcher.group(1); |
| 78 | + entries.addAll(bibtexParser.parseEntries(bibtexEntryString)); |
| 79 | + } |
| 80 | + return entries; |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void doPostCleanup(BibEntry entry) { |
| 86 | + // Remove strange "SLACcitation" field |
| 87 | + new FieldFormatterCleanup("SLACcitation", new ClearFormatter()).cleanup(entry); |
| 88 | + |
| 89 | + // Remove braces around content of "title" field |
| 90 | + new FieldFormatterCleanup(FieldName.TITLE, new RemoveBracesFormatter()).cleanup(entry); |
| 91 | + } |
| 92 | +} |
0 commit comments