forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ShortScience integration (JabRef#6018)
- Loading branch information
1 parent
c0abdce
commit 7fda4cc
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
src/main/java/org/jabref/gui/maintable/SearchShortScienceAction.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,50 @@ | ||
package org.jabref.gui.maintable; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import javafx.beans.binding.BooleanExpression; | ||
|
||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.StateManager; | ||
import org.jabref.gui.actions.SimpleCommand; | ||
import org.jabref.gui.desktop.JabRefDesktop; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.ExternalLinkCreator; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
|
||
import static org.jabref.gui.actions.ActionHelper.isFieldSetForSelectedEntry; | ||
import static org.jabref.gui.actions.ActionHelper.needsEntriesSelected; | ||
|
||
public class SearchShortScienceAction extends SimpleCommand { | ||
private final DialogService dialogService; | ||
private final StateManager stateManager; | ||
|
||
public SearchShortScienceAction(DialogService dialogService, StateManager stateManager) { | ||
this.dialogService = dialogService; | ||
this.stateManager = stateManager; | ||
|
||
BooleanExpression fieldIsSet = isFieldSetForSelectedEntry(StandardField.TITLE, stateManager); | ||
this.executable.bind(needsEntriesSelected(1, stateManager).and(fieldIsSet)); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
stateManager.getActiveDatabase().ifPresent(databaseContext -> { | ||
final List<BibEntry> bibEntries = stateManager.getSelectedEntries(); | ||
|
||
if (bibEntries.size() != 1) { | ||
dialogService.notify(Localization.lang("This operation requires exactly one item to be selected.")); | ||
return; | ||
} | ||
ExternalLinkCreator.getShortScienceSearchURL(bibEntries.get(0)).ifPresent(url -> { | ||
try { | ||
JabRefDesktop.openExternalViewer(databaseContext, url, StandardField.URL); | ||
} catch (IOException ex) { | ||
dialogService.showErrorDialogAndWait(Localization.lang("Unable to open ShortScience."), ex); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/org/jabref/logic/util/ExternalLinkCreator.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,34 @@ | ||
package org.jabref.logic.util; | ||
|
||
import java.net.URISyntaxException; | ||
import java.util.Optional; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
|
||
import org.apache.http.client.utils.URIBuilder; | ||
|
||
public class ExternalLinkCreator { | ||
private static final String SHORTSCIENCE_SEARCH_URL = "https://www.shortscience.org/internalsearch"; | ||
|
||
/** | ||
* Get a URL to the search results of ShortScience for the BibEntry's title | ||
* | ||
* @param entry The entry to search for. Expects the BibEntry's title to be set for successful return. | ||
* @return The URL if it was successfully created | ||
*/ | ||
public static Optional<String> getShortScienceSearchURL(BibEntry entry) { | ||
return entry.getField(StandardField.TITLE).map(title -> { | ||
URIBuilder uriBuilder; | ||
try { | ||
uriBuilder = new URIBuilder(SHORTSCIENCE_SEARCH_URL); | ||
} catch (URISyntaxException e) { | ||
// This should never be able to happen as it would require the field to be misconfigured. | ||
throw new AssertionError("ShortScience URL is invalid.", e); | ||
} | ||
// Direct the user to the search results for the title. | ||
uriBuilder.addParameter("q", title); | ||
return uriBuilder.toString(); | ||
}); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
src/test/java/org/jabref/logic/util/ExternalLinkCreatorTest.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,56 @@ | ||
package org.jabref.logic.util; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.field.StandardField; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.jabref.logic.util.ExternalLinkCreator.getShortScienceSearchURL; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class ExternalLinkCreatorTest { | ||
|
||
/** | ||
* Validates URL conformance to RFC2396. Does not perform complex checks such as opening connections. | ||
*/ | ||
private boolean urlIsValid(String url) { | ||
try { | ||
// This will throw on non-compliance to RFC2396. | ||
new URL(url).toURI(); | ||
return true; | ||
} catch (MalformedURLException | URISyntaxException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Test | ||
void getShortScienceSearchURLEncodesSpecialCharacters() { | ||
BibEntry entry = new BibEntry(); | ||
String rfc3986ReservedCharacters = "!*'();:@&=+$,/?#[]"; | ||
entry.setField(StandardField.TITLE, rfc3986ReservedCharacters); | ||
Optional<String> url = getShortScienceSearchURL(entry); | ||
assertTrue(url.isPresent()); | ||
assertTrue(urlIsValid(url.get())); | ||
} | ||
|
||
@Test | ||
void getShortScienceSearchURLReturnsEmptyOnMissingTitle() { | ||
BibEntry entry = new BibEntry(); | ||
assertEquals(Optional.empty(), getShortScienceSearchURL(entry)); | ||
} | ||
|
||
@Test | ||
void getShortScienceSearchURLLinksToSearchResults() { | ||
// Take an arbitrary article name | ||
BibEntry entry = new BibEntry().withField(StandardField.TITLE, "JabRef bibliography management"); | ||
Optional<String> url = getShortScienceSearchURL(entry); | ||
// Expected behaviour is to link to the search results page, /internalsearch | ||
assertEquals(Optional.of("https://www.shortscience.org/internalsearch?q=JabRef+bibliography+management"), url); | ||
} | ||
} |