|
| 1 | +package org.jabref.logic.importer.fetcher; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.net.*; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +import org.jabref.logic.help.HelpFile; |
| 9 | +import org.jabref.logic.importer.*; |
| 10 | +import org.jabref.logic.importer.fileformat.ACMPortalParser; |
| 11 | +import org.jabref.logic.importer.fileformat.BibtexParser; |
| 12 | +import org.jabref.model.entry.BibEntry; |
| 13 | +import org.jabref.model.strings.StringUtil; |
| 14 | +import org.jabref.model.util.DummyFileUpdateMonitor; |
| 15 | + |
| 16 | +import org.apache.http.client.utils.URIBuilder; |
| 17 | + |
| 18 | +public class ACMPortalFetcher implements SearchBasedParserFetcher { |
| 19 | + |
| 20 | + public static final String SEARCH_URL = "https://dl.acm.org/action/doSearch"; |
| 21 | + |
| 22 | + public final ImportFormatPreferences preferences; |
| 23 | + |
| 24 | + public ACMPortalFetcher(ImportFormatPreferences preferences) { |
| 25 | + this.preferences = Objects.requireNonNull(preferences); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getName() { |
| 30 | + return "ACM Portal"; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Optional<HelpFile> getHelpPage() { |
| 35 | + return Optional.of(HelpFile.FETCHER_ACM); |
| 36 | + } |
| 37 | + |
| 38 | + /* |
| 39 | + * This method changes the given query from the search bar in this form. >"words" "are" "split"<. |
| 40 | + * For the Url we need >words+are+split< |
| 41 | + * So we delete the spaces and change the quotation marks to plus where needed. |
| 42 | + */ |
| 43 | + public static String createQueryString(String query) { |
| 44 | + // deletes all spaces |
| 45 | + query = query.trim().replaceAll("\\s+", ""); |
| 46 | + |
| 47 | + // changes the quotation marks between search results to a + (used with complex search) |
| 48 | + String newQuery = ""; |
| 49 | + char lastChar = '\"'; |
| 50 | + for (int i = 0; i < query.length(); i++) { |
| 51 | + // check for quotations |
| 52 | + if (query.charAt(i) == '\"') { |
| 53 | + // check if the char before was also a quotation mark (because we only need one + between words) |
| 54 | + // check if you are at the last char of the query (so there is no plus needed) |
| 55 | + if (lastChar != '\"' && i + 1 != query.length()) { |
| 56 | + newQuery += "+"; |
| 57 | + } |
| 58 | + } else { |
| 59 | + newQuery += query.charAt(i); |
| 60 | + } |
| 61 | + lastChar = query.charAt(i); |
| 62 | + } |
| 63 | + return newQuery; |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * Constructing the url for the searchpage. |
| 68 | + * The query is the input from the search bar. |
| 69 | + * |
| 70 | + * Good to know: |
| 71 | + * This method is used at SearchBasedParserFetcher line 49. |
| 72 | + * |
| 73 | + */ |
| 74 | + public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException { |
| 75 | + URIBuilder uriBuilder = new URIBuilder(SEARCH_URL); |
| 76 | + uriBuilder.addParameter("AllField", createQueryString(query)); |
| 77 | + return uriBuilder.build().toURL(); |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * Gives back an instance of our ACMPortalParser. |
| 82 | + * |
| 83 | + * What happens next in the program: |
| 84 | + * At line 77 in the SearchBasedParserFetcher an InputStream is created by the given URL |
| 85 | + * (see getURLForQuery). Then in line 78 this method is used as followed: |
| 86 | + * List<BibEntry> fetchedEntries = getParser().parseEntries(stream); |
| 87 | + */ |
| 88 | + public Parser getParser() { |
| 89 | + return new ACMPortalParser(); |
| 90 | + } |
| 91 | +} |
0 commit comments