Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into moveFileDir
Browse files Browse the repository at this point in the history
* upstream/master:
  Use LaTeX free title for CrossRef search #1424
  Check more results returned by CrossRef API for matching (#2531)
  Rework File links logic (#2529)
  Remove MIT string from lang files
  Translate new strings (#2528)
  Make DOIFetcher the default fetcher in new entry dialog (#2530)
  • Loading branch information
Siedlerchr committed Feb 9, 2017
2 parents d888684 + 5053a32 commit ed8addf
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 114 deletions.
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.sf.jabref.gui.keyboard.KeyBinding;
import net.sf.jabref.logic.importer.FetcherException;
import net.sf.jabref.logic.importer.IdBasedFetcher;
import net.sf.jabref.logic.importer.fetcher.DoiFetcher;
import net.sf.jabref.logic.l10n.Localization;
import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.database.BibDatabaseMode;
Expand Down Expand Up @@ -186,6 +187,8 @@ private JPanel createIdFetcherPanel() {
comboBox = new JComboBox<>();

EntryFetchers.getIdFetchers(Globals.prefs.getImportFormatPreferences()).forEach(fetcher -> comboBox.addItem(fetcher.getName()));
// set DOI as default
comboBox.setSelectedItem(DoiFetcher.name);

generateButton.addActionListener(action -> {
fetcherWorker.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public void update() {
basePanel.getBibDatabaseContext(), entry);
try {
def.download(result.get(), file -> {
FileListTableModel tm = new FileListTableModel();
entry.getField(FieldName.FILE).ifPresent(tm::setContent);
tm.addEntry(tm.getRowCount(), file);
String newValue = tm.getStringRepresentation();
FileListTableModel fileLinkModel = new FileListTableModel();
entry.getField(FieldName.FILE).ifPresent(fileLinkModel::setContent);
// add full text file link at first position
fileLinkModel.addEntry(0, file);
String newValue = fileLinkModel.getStringRepresentation();
UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE,
entry.getField(FieldName.FILE).orElse(null), newValue);
entry.setField(FieldName.FILE, newValue);
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/net/sf/jabref/gui/fieldeditors/FileListEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,26 +339,24 @@ public String getSelectedText() {
return null;
}

private void addEntry(String initialLink) {
int row = getSelectedRow();
if (row == -1) {
row = 0;
}
FileListEntry entry = new FileListEntry("", initialLink);
if (editListEntry(entry, true)) {
tableModel.addEntry(row, entry);
}
entryEditor.updateField(this);
adjustColumnWidth();
}

/**
* Will append a new file link at the last position
*/
private void addEntry() {
List<String> defaultDirectory = databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());

FileListEntry entry;
if (defaultDirectory.isEmpty() || (defaultDirectory.get(0) == null)) {
addEntry("");
entry = new FileListEntry("", "");
} else {
addEntry(defaultDirectory.get(0));
entry = new FileListEntry("", defaultDirectory.get(0));
}

if (editListEntry(entry, true)) {
tableModel.addEntry(tableModel.getRowCount(), entry);
}
entryEditor.updateField(this);
adjustColumnWidth();
}

private void removeEntries() {
Expand Down Expand Up @@ -475,14 +473,12 @@ private void downloadFile() {
*/
@Override
public void downloadComplete(FileListEntry file) {
tableModel.addEntry(tableModel.getRowCount(), file);
tableModel.addEntry(0, file);
entryEditor.updateField(this);
adjustColumnWidth();
}


class TableClickListener extends MouseAdapter {

@Override
public void mouseClicked(MouseEvent e) {
if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 2)) {
Expand Down
71 changes: 41 additions & 30 deletions src/main/java/net/sf/jabref/logic/importer/fetcher/CrossRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@
*/
public class CrossRef {
private static final Log LOGGER = LogFactory.getLog(CrossRef.class);
private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();

private static final String API_URL = "http://api.crossref.org";
// number of results to lookup from crossref API
private static final int API_RESULTS = 5;

private static final Levenshtein METRIC_DISTANCE = new Levenshtein();
// edit distance threshold for entry title comnparison
private static final int METRIC_THRESHOLD = 4;

private static final RemoveBracesFormatter REMOVE_BRACES_FORMATTER = new RemoveBracesFormatter();

public static Optional<DOI> findDOI(BibEntry entry) {
Objects.requireNonNull(entry);
Optional<DOI> doi = Optional.empty();

// title is minimum requirement
Optional<String> title = entry.getField(FieldName.TITLE);
Optional<String> title = entry.getLatexFreeField(FieldName.TITLE);

if (!title.isPresent() || title.get().isEmpty()) {
return doi;
Expand All @@ -49,15 +54,16 @@ public static Optional<DOI> findDOI(BibEntry entry) {
try {
HttpResponse<JsonNode> response = Unirest.get(API_URL + "/works")
.queryString("query", query)
.queryString("rows", "1")
.queryString("rows", API_RESULTS)
.asJson();

JSONArray items = response.getBody().getObject().getJSONObject("message").getJSONArray("items");
// quality check
if (checkValidity(entry, items)) {
String dataDOI = items.getJSONObject(0).getString("DOI");
LOGGER.debug("DOI " + dataDOI + " for " + title.get() + " found.");
return DOI.build(dataDOI);
Optional<String> dataDoi = findMatchingEntry(entry, items);

if (dataDoi.isPresent()) {
LOGGER.debug("DOI " + dataDoi.get() + " for " + title.get() + " found.");
return DOI.build(dataDoi.get());
}
} catch (UnirestException e) {
LOGGER.warn("Unable to query CrossRef API: " + e.getMessage(), e);
Expand All @@ -84,33 +90,38 @@ private static String enhanceQuery(String query, BibEntry entry) {
return enhancedQuery.toString();
}

private static boolean checkValidity(BibEntry entry, JSONArray result) {
private static Optional<String> findMatchingEntry(BibEntry entry, JSONArray results) {
final String entryTitle = REMOVE_BRACES_FORMATTER.format(entry.getLatexFreeField(FieldName.TITLE).orElse(""));

// currently only title-based
// title: [ "How the Mind Hurts and Heals the Body." ]
// subtitle: [ "" ]
try {
// title
JSONObject data = result.getJSONObject(0);
String dataTitle = data.getJSONArray("title").getString(0);

if (editDistanceIgnoreCase(entryTitle, dataTitle) <= METRIC_THRESHOLD) {
return true;
}

// subtitle
// additional check, as sometimes subtitle is needed but sometimes only duplicates the title
if (data.getJSONArray("subtitle").length() > 0) {
String dataWithSubTitle = dataTitle + " " + data.getJSONArray("subtitle").getString(0);

return editDistanceIgnoreCase(entryTitle, dataWithSubTitle) <= METRIC_THRESHOLD;
for (int i = 0; i < results.length(); i++) {
// currently only title-based
// title: [ "How the Mind Hurts and Heals the Body." ]
// subtitle: [ "" ]
try {
// title
JSONObject data = results.getJSONObject(i);
String dataTitle = data.getJSONArray("title").getString(0);

if (editDistanceIgnoreCase(entryTitle, dataTitle) <= METRIC_THRESHOLD) {
return Optional.of(data.getString("DOI"));
}

// subtitle
// additional check, as sometimes subtitle is needed but sometimes only duplicates the title
if (data.getJSONArray("subtitle").length() > 0) {
String dataWithSubTitle = dataTitle + " " + data.getJSONArray("subtitle").getString(0);

if (editDistanceIgnoreCase(entryTitle, dataWithSubTitle) <= METRIC_THRESHOLD) {
return Optional.of(data.getString("DOI"));
}
}
} catch(JSONException ex) {
LOGGER.warn("CrossRef API JSON format has changed: " + ex.getMessage());
return Optional.empty();
}

return false;
} catch(JSONException ex) {
return false;
}

return Optional.empty();
}

private static double editDistanceIgnoreCase(String a, String b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.sf.jabref.model.entry.FieldName;

public class DoiFetcher implements IdBasedFetcher, EntryBasedFetcher {
public static final String name = "DOI";

private final ImportFormatPreferences preferences;

Expand All @@ -33,7 +34,7 @@ public DoiFetcher(ImportFormatPreferences preferences) {

@Override
public String getName() {
return "DOI";
return DoiFetcher.name;
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=
Make_a_donation=
get_involved=
Used_libraries=
MIT=
Existing_file=Eksisterende_fil

ID=
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=
Make_a_donation=
get_involved=
Used_libraries=
MIT=
Existing_file=

ID=ID
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=Want_to_help?
Make_a_donation=Make_a_donation
get_involved=get_involved
Used_libraries=Used_libraries
MIT=MIT
Existing_file=Existing_file

ID=ID
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=
Make_a_donation=
get_involved=
Used_libraries=
MIT=
Existing_file=Fichero_existente

ID=ID
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=
Make_a_donation=
get_involved=
Used_libraries=
MIT=
Existing_file=

ID=
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=Vous_voulez_aider_?
Make_a_donation=Faire_un_don
get_involved=S'impliquer
Used_libraries=Bibliothèques_utilisées
MIT=MIT
Existing_file=Fichier_existant

ID=Identifiant
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Want_to_help?=
Make_a_donation=
get_involved=
Used_libraries=
MIT=
Existing_file=Berkas_yang_ada

ID=
Expand Down
Loading

0 comments on commit ed8addf

Please sign in to comment.