-
-
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
New fetcher #1929
New fetcher #1929
Changes from 13 commits
a90bc21
4fc8551
443f50f
1b57e28
d39fc73
adfe299
d3b62be
9539d47
8d98ad9
9816aa7
528b197
cde8b17
0655709
f9c2ce6
db93bbb
3979146
e1002d3
6737da0
a1f98e8
61579c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import net.sf.jabref.FileDirectoryPreferences; | ||
import net.sf.jabref.logic.layout.LayoutFormatterPreferences; | ||
|
||
public class CleanupPreferences { | ||
|
||
private final String fileNamePattern; | ||
private final LayoutFormatterPreferences layoutFormatterPreferences; | ||
private final FileDirectoryPreferences fileDirectoryPreferences; | ||
|
||
public CleanupPreferences(String fileNamePattern, LayoutFormatterPreferences layoutFormatterPreferences, | ||
FileDirectoryPreferences fileDirectoryPreferences) { | ||
this.fileNamePattern = fileNamePattern; | ||
this.layoutFormatterPreferences = layoutFormatterPreferences; | ||
this.fileDirectoryPreferences = fileDirectoryPreferences; | ||
} | ||
|
||
public String getFileNamePattern() { | ||
return fileNamePattern; | ||
} | ||
|
||
public LayoutFormatterPreferences getLayoutFormatterPreferences() { | ||
return layoutFormatterPreferences; | ||
} | ||
|
||
public FileDirectoryPreferences getFileDirectoryPreferences() { | ||
return fileDirectoryPreferences; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.sf.jabref.logic.cleanup; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import net.sf.jabref.logic.util.OptionalUtil; | ||
import net.sf.jabref.model.FieldChange; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
/** | ||
* Moves the content of one field to another field. | ||
*/ | ||
public class MoveFieldCleanup implements CleanupJob { | ||
|
||
private String sourceField; | ||
private String targetField; | ||
|
||
public MoveFieldCleanup(String sourceField, String targetField) { | ||
this.sourceField = sourceField; | ||
this.targetField = targetField; | ||
} | ||
|
||
@Override | ||
public List<FieldChange> cleanup(BibEntry entry) { | ||
|
||
Optional<FieldChange> setFieldChange = entry.getField(sourceField).flatMap( | ||
value -> entry.setField(targetField, value)); | ||
Optional<FieldChange> clearFieldChange = entry.clearField(sourceField); | ||
return OptionalUtil.toList(setFieldChange, clearFieldChange); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.sf.jabref.logic.importer; | ||
|
||
import java.util.List; | ||
|
||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
/** | ||
* Searches web resources for bibliographic information based on a {@link BibEntry}. | ||
* Useful to complete an existing entry with fetched information. | ||
* May return multiple search hits. | ||
*/ | ||
public interface EntryBasedFetcher extends WebFetcher { | ||
|
||
/** | ||
* Looks for hits which are matched by the given {@link BibEntry}. | ||
* | ||
* @param entry entry to search bibliographic information for | ||
* @return a list of {@link BibEntry}, which are matched by the query (may be empty) | ||
*/ | ||
List<BibEntry> performSearch(BibEntry entry) throws FetcherException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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.Collections; | ||
import java.util.List; | ||
|
||
import net.sf.jabref.logic.formatter.Formatter; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
import org.jsoup.helper.StringUtil; | ||
|
||
/** | ||
* Provides a convenient interface for entry-based fetcher, which follow the usual three-step procedure: | ||
* 1. Open a URL based on the entry | ||
* 2. Parse the response to get a list of {@link BibEntry} | ||
* 3. Post-process fetched entries | ||
*/ | ||
public interface EntryBasedParserFetcher extends EntryBasedFetcher { | ||
|
||
/** | ||
* Constructs a URL based on the {@link BibEntry}. | ||
* @param entry the entry to look information for | ||
*/ | ||
URL getURLForEntry(BibEntry entry) 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 List<BibEntry> performSearch(BibEntry entry) throws FetcherException { | ||
try (InputStream stream = new BufferedInputStream(getURLForEntry(entry).openStream())) { | ||
List<BibEntry> fetchedEntries = getParser().parseEntries(stream); | ||
|
||
// Post-cleanup | ||
fetchedEntries.forEach(this::doPostCleanup); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment as at the other fetcher interface (#1929): Shouldn't we have a framework so that the not each implementer of a fetcher has to take care about the right calling, but just offer the cleanup method? -- Maybe, this is too much work and we have to stick with the current solution. Then, the method |
||
|
||
return fetchedEntries; | ||
} catch (URISyntaxException e) { | ||
throw new FetcherException("Search URI is malformed", e); | ||
} catch (IOException e) { | ||
throw new FetcherException("An I/O exception occurred", e); | ||
} catch (ParserException e) { | ||
throw new FetcherException("An internal parser error occurred", e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.sf.jabref.logic.importer; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
public interface Parser { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a JavaDoc comment here? |
||
|
||
List<BibEntry> parseEntries(InputStream inputStream) throws ParserException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.sf.jabref.logic.importer; | ||
|
||
public class ParserException extends Exception { | ||
|
||
|
||
public ParserException(String errorMessage, Exception cause) { | ||
super(errorMessage, cause); | ||
} | ||
|
||
public ParserException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
|
||
public ParserException(Exception cause) { | ||
super(cause); | ||
} | ||
} |
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.
Is it possible to add links to these databases?