-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shorten DOI field formatter (koppor#343) (#5276)
- Loading branch information
1 parent
dde32bd
commit 164843b
Showing
10 changed files
with
372 additions
and
25 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
59 changes: 59 additions & 0 deletions
59
src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.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,59 @@ | ||
package org.jabref.logic.formatter.bibtexfields; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.util.ShortDOIService; | ||
import org.jabref.logic.importer.util.ShortDOIServiceException; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.cleanup.Formatter; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ShortenDOIFormatter extends Formatter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ShortenDOIFormatter.class); | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("Shorten DOI"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "short_doi"; | ||
} | ||
|
||
@Override | ||
public String format(String value) { | ||
Objects.requireNonNull(value); | ||
|
||
ShortDOIService shortDOIService = new ShortDOIService(); | ||
|
||
Optional<DOI> doi = Optional.empty(); | ||
|
||
try { | ||
doi = DOI.parse(value); | ||
|
||
if (doi.isPresent()) { | ||
return shortDOIService.getShortDOI(doi.get()).getDOI(); | ||
} | ||
} catch (ShortDOIServiceException e) { | ||
LOGGER.error(e.getMessage(), e); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Shortens DOI to more human readable form."); | ||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "10.1006/jmbi.1998.2354"; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/org/jabref/logic/importer/util/ShortDOIService.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,63 @@ | ||
package org.jabref.logic.importer.util; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
|
||
import org.jabref.logic.importer.ParseException; | ||
import org.jabref.logic.net.URLDownload; | ||
import org.jabref.model.entry.identifier.DOI; | ||
|
||
import org.apache.http.client.utils.URIBuilder; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* Class for obtaining shortened DOI names. | ||
* | ||
* @see http://shortdoi.org | ||
*/ | ||
public class ShortDOIService { | ||
|
||
private static final String BASIC_URL = "http://shortdoi.org/"; | ||
|
||
/** | ||
* Obtains shortened DOI name for given DOI | ||
* | ||
* @param doi DOI | ||
* @return A shortened DOI name | ||
*/ | ||
public DOI getShortDOI(DOI doi) throws ShortDOIServiceException { | ||
JSONObject responseJSON = makeRequest(doi); | ||
String shortDoi = responseJSON.getString("ShortDOI"); | ||
|
||
return new DOI(shortDoi); | ||
} | ||
|
||
private JSONObject makeRequest(DOI doi) throws ShortDOIServiceException { | ||
|
||
URIBuilder uriBuilder = null; | ||
URL url = null; | ||
|
||
try { | ||
uriBuilder = new URIBuilder(BASIC_URL); | ||
uriBuilder.setPath(uriBuilder.getPath() + doi.getDOI()); | ||
uriBuilder.addParameter("format", "json"); | ||
|
||
URI uri = uriBuilder.build(); | ||
url = uri.toURL(); | ||
} catch (URISyntaxException | MalformedURLException e) { | ||
throw new ShortDOIServiceException("Cannot get short DOI", e); | ||
} | ||
|
||
URLDownload urlDownload = new URLDownload(url); | ||
|
||
try { | ||
return JsonReader.toJsonObject(urlDownload.asInputStream()); | ||
} catch (ParseException | IOException | JSONException e) { | ||
throw new ShortDOIServiceException("Cannot get short DOI", e); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/org/jabref/logic/importer/util/ShortDOIServiceException.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,25 @@ | ||
package org.jabref.logic.importer.util; | ||
|
||
import org.jabref.JabRefException; | ||
|
||
public class ShortDOIServiceException extends JabRefException { | ||
public ShortDOIServiceException(String message) { | ||
super(message); | ||
} | ||
|
||
public ShortDOIServiceException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ShortDOIServiceException(String message, String localizedMessage) { | ||
super(message, localizedMessage); | ||
} | ||
|
||
public ShortDOIServiceException(String message, String localizedMessage, Throwable cause) { | ||
super(message, localizedMessage, cause); | ||
} | ||
|
||
public ShortDOIServiceException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
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
Oops, something went wrong.