-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce convienent interface for ID-based fetcher #1998
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/main/java/net/sf/jabref/logic/importer/IdBasedParserFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package net.sf.jabref.logic.importer; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import net.sf.jabref.logic.formatter.Formatter; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.jsoup.helper.StringUtil; | ||
|
||
/** | ||
* Provides a convenient interface for ID-based fetcher, which follow the usual three-step procedure: | ||
* 1. Open a URL based on the search query | ||
* 2. Parse the response to get a list of {@link BibEntry} | ||
* 3. Post-process fetched entries | ||
*/ | ||
public interface IdBasedParserFetcher extends IdBasedFetcher { | ||
|
||
Log LOGGER = LogFactory.getLog(IdBasedParserFetcher.class); | ||
|
||
/** | ||
* Constructs a URL based on the query. | ||
* @param identifier the ID | ||
*/ | ||
URL getURLForID(String identifier) throws URISyntaxException, MalformedURLException, FetcherException; | ||
|
||
/** | ||
* Returns the parser used to convert the response to a list of {@link BibEntry}. | ||
*/ | ||
Parser getParser(); | ||
|
||
/** | ||
* Performs a cleanup of the fetched entry. | ||
* | ||
* Only systematic errors of the fetcher should be corrected here | ||
* (i.e. if information is consistently contained in the wrong field or the wrong format) | ||
* but not cosmetic issues which may depend on the user's taste (for example, LateX code vs HTML in the abstract). | ||
* | ||
* Try to reuse existing {@link Formatter} for the cleanup. For example, | ||
* {@code new FieldFormatterCleanup(FieldName.TITLE, new RemoveBracesFormatter()).cleanup(entry);} | ||
* | ||
* By default, no cleanup is done. | ||
* @param entry the entry to be cleaned-up | ||
*/ | ||
default void doPostCleanup(BibEntry entry) { | ||
// Do nothing by default | ||
} | ||
|
||
@Override | ||
default Optional<BibEntry> performSearchById(String identifier) throws FetcherException { | ||
if (StringUtil.isBlank(identifier)) { | ||
return Optional.empty(); | ||
} | ||
|
||
try (InputStream stream = new BufferedInputStream(getURLForID(identifier).openStream())) { | ||
List<BibEntry> fetchedEntries = getParser().parseEntries(stream); | ||
|
||
if (fetchedEntries.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
|
||
if (fetchedEntries.size() > 1) { | ||
LOGGER.info("Fetcher " + getName() + "found more than one result for identifier " + identifier | ||
+ ". We will use the first entry."); | ||
} | ||
|
||
BibEntry entry = fetchedEntries.get(0); | ||
|
||
// Post-cleanup | ||
doPostCleanup(entry); | ||
|
||
return Optional.of(entry); | ||
} catch (URISyntaxException e) { | ||
throw new FetcherException("Search URI is malformed", e); | ||
} catch (IOException e) { | ||
// TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource | ||
throw new FetcherException("An I/O exception occurred", e); | ||
} catch (ParserException e) { | ||
throw new FetcherException("An internal parser error occurred", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2305,3 +2305,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2304,3 +2304,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2304,3 +2304,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2305,3 +2305,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2301,3 +2301,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2304,3 +2304,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2302,3 +2302,5 @@ Select_first_entry= | |
Select_last_entry= | ||
|
||
Search_in_all_open_databases= | ||
|
||
Invalid_ISBN\:_'%0'.= |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure we don't have a related error message already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't find anything related to ISBN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, we have Invalid_DOI already. It would make sense to have sth like Invalid_%0 which we can reuse for all id fetchers. (e.g. doi/isbn) so we