Skip to content

Commit

Permalink
Identifier: Rename "getNormalized" to "asString" (JabRef#12054)
Browse files Browse the repository at this point in the history
* Identifier: Rename "getNormalized" to "asString" (and replace getDOI by asString)

* Fix method name

* Fix checkstyle

* Fix method name

* Fix methodname ... agian ...

* Adapt to new method names

* Fix method name ... again ...
  • Loading branch information
koppor authored Oct 23, 2024
1 parent c3e5621 commit d94b47f
Show file tree
Hide file tree
Showing 45 changed files with 181 additions and 177 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/edit/CopyDoiUrlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void execute() {
if (action == StandardActions.COPY_DOI_URL) {
copy(DOI.parse(identifier).map(DOI::getURIAsASCIIString), identifier);
} else {
copy(DOI.parse(identifier).map(DOI::getDOI), identifier);
copy(DOI.parse(identifier).map(DOI::asString), identifier);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/edit/CopyMoreAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void copyDoi() {
} else {
copyDoiList(entries.stream()
.filter(entry -> entry.getDOI().isPresent())
.map(entry -> entry.getDOI().get().getDOI())
.map(entry -> entry.getDOI().get().asString())
.collect(Collectors.toList()), entries.size());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void cancelSearch() {
public SciteTallyModel fetchTallies(DOI doi) throws FetcherException {
URL url;
try {
url = new URI(BASE_URL + "tallies/" + doi.getDOI()).toURL();
url = new URI(BASE_URL + "tallies/" + doi.asString()).toURL();
} catch (MalformedURLException | URISyntaxException ex) {
throw new FetcherException("Malformed URL for DOI", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ public class BibEntryRelationsCache {
private static final Map<String, List<BibEntry>> REFERENCES_MAP = new LRUMap<>(MAX_CACHED_ENTRIES, MAX_CACHED_ENTRIES);

public List<BibEntry> getCitations(BibEntry entry) {
return CITATIONS_MAP.getOrDefault(entry.getDOI().map(DOI::getDOI).orElse(""), Collections.emptyList());
return CITATIONS_MAP.getOrDefault(entry.getDOI().map(DOI::asString).orElse(""), Collections.emptyList());
}

public List<BibEntry> getReferences(BibEntry entry) {
return REFERENCES_MAP.getOrDefault(entry.getDOI().map(DOI::getDOI).orElse(""), Collections.emptyList());
return REFERENCES_MAP.getOrDefault(entry.getDOI().map(DOI::asString).orElse(""), Collections.emptyList());
}

public void cacheOrMergeCitations(BibEntry entry, List<BibEntry> citations) {
entry.getDOI().ifPresent(doi -> CITATIONS_MAP.put(doi.getDOI(), citations));
entry.getDOI().ifPresent(doi -> CITATIONS_MAP.put(doi.asString(), citations));
}

public void cacheOrMergeReferences(BibEntry entry, List<BibEntry> references) {
entry.getDOI().ifPresent(doi -> REFERENCES_MAP.putIfAbsent(doi.getDOI(), references));
entry.getDOI().ifPresent(doi -> REFERENCES_MAP.putIfAbsent(doi.asString(), references));
}

public boolean citationsCached(BibEntry entry) {
return CITATIONS_MAP.containsKey(entry.getDOI().map(DOI::getDOI).orElse(""));
return CITATIONS_MAP.containsKey(entry.getDOI().map(DOI::asString).orElse(""));
}

public boolean referencesCached(BibEntry entry) {
return REFERENCES_MAP.containsKey(entry.getDOI().map(DOI::getDOI).orElse(""));
return REFERENCES_MAP.containsKey(entry.getDOI().map(DOI::asString).orElse(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SemanticScholarFetcher(ImporterPreferences importerPreferences) {
}

public String getAPIUrl(String entry_point, BibEntry entry) {
return SEMANTIC_SCHOLAR_API + "paper/" + "DOI:" + entry.getDOI().orElseThrow().getDOI() + "/" + entry_point
return SEMANTIC_SCHOLAR_API + "paper/" + "DOI:" + entry.getDOI().orElseThrow().asString() + "/" + entry_point
+ "?fields=" + "title,authors,year,citationCount,referenceCount,externalIds,publicationTypes,abstract,url"
+ "&limit=1000";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void lookupIdentifier(BibEntry bibEntry) {
.onFinished(() -> identifierLookupInProgress.setValue(false))
.onSuccess(identifier -> {
if (identifier.isPresent()) {
entry.setField(field, identifier.get().getNormalized());
entry.setField(field, identifier.get().asString());
} else {
dialogService.notify(Localization.lang("No %0 found", field.getDisplayName()));
}
Expand All @@ -66,7 +66,7 @@ public void fetchBibliographyInformation(BibEntry bibEntry) {

@Override
public void openExternalLink() {
identifier.get().map(DOI::getDOI)
identifier.get().map(DOI::asString)
.ifPresent(s -> NativeDesktop.openCustomDoi(s, preferences, dialogService));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private String lookupIdentifiers(List<BibEntry> bibEntries) {
LOGGER.error("Could not fetch {}", fetcher.getIdentifierName(), e);
}
if (identifier.isPresent() && !bibEntry.hasField(identifier.get().getDefaultField())) {
Optional<FieldChange> fieldChange = bibEntry.setField(identifier.get().getDefaultField(), identifier.get().getNormalized());
Optional<FieldChange> fieldChange = bibEntry.setField(identifier.get().getDefaultField(), identifier.get().asString());
if (fieldChange.isPresent()) {
namedCompound.addEdit(new UndoableFieldChange(fieldChange.get()));
foundCount++;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/logic/cleanup/DoiCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<FieldChange> cleanup(BibEntry entry) {
Optional<DOI> doi = DOI.parse(doiFieldValue);

if (doi.isPresent()) {
String newValue = doi.get().getDOI();
String newValue = doi.get().asString();
if (!doiFieldValue.equals(newValue)) {
entry.setField(StandardField.DOI, newValue);

Expand All @@ -65,7 +65,7 @@ public List<FieldChange> cleanup(BibEntry entry) {

if (doi.isPresent()) {
// Update Doi
Optional<FieldChange> change = entry.setField(StandardField.DOI, doi.get().getDOI());
Optional<FieldChange> change = entry.setField(StandardField.DOI, doi.get().asString());
change.ifPresent(changes::add);
removeFieldValue(entry, field, changes);
}
Expand All @@ -74,7 +74,7 @@ public List<FieldChange> cleanup(BibEntry entry) {
fieldContentOpt.flatMap(ArXivIdentifier::parse)
.flatMap(ArXivIdentifier::inferDOI)
.ifPresent(inferredDoi -> {
Optional<FieldChange> change = entry.setField(StandardField.DOI, inferredDoi.getDOI());
Optional<FieldChange> change = entry.setField(StandardField.DOI, inferredDoi.asString());
change.ifPresent(changes::add);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/cleanup/EprintCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<FieldChange> cleanup(BibEntry entry) {
Optional<ArXivIdentifier> arXivIdentifier = entry.getField(field).flatMap(ArXivIdentifier::parse);

if (arXivIdentifier.isPresent()) {
String normalizedEprint = arXivIdentifier.get().getNormalized();
String normalizedEprint = arXivIdentifier.get().asString();

if (version.isPresent() && !normalizedEprint.contains("v" + version.get())) {
normalizedEprint += "v" + version.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String format(String value) {
return SHORT_DOI_FORMAT.test(value) ? value : DOI.parse(value)
.map(doi -> {
try {
return new ShortDOIService().getShortDOI(doi).getDOI();
return new ShortDOIService().getShortDOI(doi).asString();
} catch (ShortDOIServiceException e) {
LOGGER.error(e.getMessage(), e);
return value;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/logic/importer/CompositeIdFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc

Optional<DOI> doi = DOI.findInText(identifier);
if (doi.isPresent()) {
return new DoiFetcher(importFormatPreferences).performSearchById(doi.get().getNormalized());
return new DoiFetcher(importFormatPreferences).performSearchById(doi.get().asString());
}
Optional<ArXivIdentifier> arXivIdentifier = ArXivIdentifier.parse(identifier);
if (arXivIdentifier.isPresent()) {
return new ArXivFetcher(importFormatPreferences).performSearchById(arXivIdentifier.get().getNormalized());
return new ArXivFetcher(importFormatPreferences).performSearchById(arXivIdentifier.get().asString());
}
Optional<ISBN> isbn = ISBN.parse(identifier);
if (isbn.isPresent()) {
return new IsbnFetcher(importFormatPreferences)
// .addRetryFetcher(new EbookDeIsbnFetcher(importFormatPreferences))
// .addRetryFetcher(new DoiToBibtexConverterComIsbnFetcher(importFormatPreferences))
.performSearchById(isbn.get().getNormalized());
.performSearchById(isbn.get().asString());
}
/* TODO: IACR is currently disabled, because it needs to be reworked: https://github.com/JabRef/jabref/issues/8876
Optional<IacrEprint> iacrEprint = IacrEprint.parse(identifier);
Expand All @@ -48,12 +48,12 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc

Optional<SSRN> ssrn = SSRN.parse(identifier);
if (ssrn.isPresent()) {
return new DoiFetcher(importFormatPreferences).performSearchById(ssrn.get().toDoi().getNormalized());
return new DoiFetcher(importFormatPreferences).performSearchById(ssrn.get().toDoi().asString());
}

Optional<RFC> rfcId = RFC.parse(identifier);
if (rfcId.isPresent()) {
return new RfcFetcher(importFormatPreferences).performSearchById(rfcId.get().getNormalized());
return new RfcFetcher(importFormatPreferences).performSearchById(rfcId.get().asString());
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void findDoiForEntry(BibEntry clonedEntry) {
try {
WebFetchers.getIdFetcherForIdentifier(DOI.class)
.findIdentifier(clonedEntry)
.ifPresent(e -> clonedEntry.setField(StandardField.DOI, e.getDOI()));
.ifPresent(e -> clonedEntry.setField(StandardField.DOI, e.asString()));
} catch (FetcherException e) {
LOGGER.debug("Failed to find DOI", e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/importer/fetcher/ACS.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
return Optional.empty();
}

String source = SOURCE.formatted(doi.get().getDOI());
String source = SOURCE.formatted(doi.get().asString());
// Retrieve PDF link
Document html = Jsoup.connect(source).ignoreHttpErrors(true).get();
Element link = html.select("a.button_primary").first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
return Optional.empty();
}

Optional<String> id = getId(doi.get().getDOI());
Optional<String> id = getId(doi.get().asString());

if (id.isPresent()) {
String pdfRequestUrl = PDF_URL + id.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private static Optional<String> getAutomaticDoi(BibEntry arXivBibEntry) {
* @return ArXiv-issued DOI
*/
private static String getAutomaticDoi(ArXivIdentifier arXivId) {
return getAutomaticDoi(arXivId.getNormalizedWithoutVersion());
return getAutomaticDoi(arXivId.asStringWithoutVersion());
}

/**
Expand Down Expand Up @@ -473,7 +473,7 @@ private List<ArXivEntry> searchForEntries(BibEntry originalEntry) throws Fetcher
String query;
Optional<String> doiString = entry.getField(StandardField.DOI)
.flatMap(DOI::parse)
.map(DOI::getNormalized);
.map(DOI::asString);

// ArXiv-issued DOIs seem to be unsearchable from ArXiv API's "query string", so ignore it
if (doiString.isPresent() && ArXivFetcher.isManualDoi(doiString.get())) {
Expand Down Expand Up @@ -543,7 +543,7 @@ private Document callApi(String searchQuery, List<ArXivIdentifier> ids, int star
}
if (!ids.isEmpty()) {
uriBuilder.addParameter("id_list",
ids.stream().map(ArXivIdentifier::getNormalized).collect(Collectors.joining(",")));
ids.stream().map(ArXivIdentifier::asString).collect(Collectors.joining(",")));
}
uriBuilder.addParameter("start", String.valueOf(start));
uriBuilder.addParameter("max_results", String.valueOf(maxResults));
Expand Down Expand Up @@ -748,7 +748,7 @@ public Optional<URL> getPdfUrl() {
* Returns the arXiv identifier
*/
public Optional<String> getIdString() {
return urlAbstractPage.flatMap(ArXivIdentifier::parse).map(ArXivIdentifier::getNormalizedWithoutVersion);
return urlAbstractPage.flatMap(ArXivIdentifier::parse).map(ArXivIdentifier::asStringWithoutVersion);
}

public Optional<ArXivIdentifier> getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
public Optional<String> getAgency(DOI doi) throws FetcherException, MalformedURLException {
Optional<String> agency = Optional.empty();
try {
URLDownload download = getUrlDownload(URI.create(DOI.AGENCY_RESOLVER + "/" + doi.getDOI()).toURL());
URLDownload download = getUrlDownload(URI.create(DOI.AGENCY_RESOLVER + "/" + doi.asString()).toURL());
JSONObject response = new JSONArray(download.asString()).getJSONObject(0);
if (response != null) {
agency = Optional.ofNullable(response.optString("RA"));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/importer/fetcher/IEEE.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Optional<URL> findFullText(BibEntry entry) throws FetcherException {
// If not, try DOI
if (stampString.isEmpty()) {
Optional<DOI> doi = entry.getField(StandardField.DOI).flatMap(DOI::parse);
if (doi.isPresent() && doi.get().getDOI().startsWith(IEEE_DOI) && doi.get().getExternalURI().isPresent()) {
if (doi.isPresent() && doi.get().asString().startsWith(IEEE_DOI) && doi.get().getExternalURI().isPresent()) {
// Download the HTML page from IEEE
URLDownload urlDownload = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
String doi = item.optString("articleUrl");
if (!doi.isEmpty()) {
try {
DOI.parse(doi).ifPresent(validDoi -> entry.setField(StandardField.DOI, validDoi.getNormalized()));
DOI.parse(doi).ifPresent(validDoi -> entry.setField(StandardField.DOI, validDoi.asString()));
} catch (IllegalArgumentException e) {
// If DOI parsing fails, use the original DOI string
entry.setField(StandardField.DOI, doi);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public TrustLevel getTrustLevel() {
}

public Optional<URL> findFullText(DOI doi) throws UnirestException {
HttpResponse<JsonNode> request = Unirest.get(API_URL + doi.getDOI() + "[email protected]")
HttpResponse<JsonNode> request = Unirest.get(API_URL + doi.asString() + "[email protected]")
.header("accept", "application/json")
.asJson();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ Optional<String> getURLByDoi(DOI doi) throws IOException, NullPointerException {
try {
URIBuilder source = new URIBuilder(SEARCH);
source.addParameter("type", "publication");
source.addParameter("query", doi.getDOI());
source.addParameter("query", doi.asString());

source = new URIBuilder(GOOGLE_SEARCH + doi.getDOI() + GOOGLE_SITE);
source = new URIBuilder(GOOGLE_SEARCH + doi.asString() + GOOGLE_SITE);
Connection connection = Jsoup.connect(source.toString());
Document html = connection
.cookieStore(connection.cookieStore())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
return Optional.empty();
}

String urlFromDoi = getUrlByDoi(doi.get().getDOI());
String urlFromDoi = getUrlByDoi(doi.get().asString());
if (urlFromDoi.isEmpty()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException, FetcherExc
if (doi.isPresent()) {
try {
// Retrieve PDF link
String source = SOURCE_ID_SEARCH + doi.get().getDOI();
String source = SOURCE_ID_SEARCH + doi.get().asString();
var jsoupRequest = Jsoup.connect(getURLBySource(source))
.userAgent(URLDownload.USER_AGENT)
.header("Accept", "text/html; charset=utf-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
try {
HttpResponse<JsonNode> jsonResponse = Unirest.get(API_URL)
.queryString("api_key", importerPreferences.getApiKey(getName()).orElse(""))
.queryString("q", "doi:%s".formatted(doi.get().getDOI()))
.queryString("q", "doi:%s".formatted(doi.get().asString()))
.asJson();
if (jsonResponse.getBody() != null) {
JSONObject json = jsonResponse.getBody().getObject();
int results = json.getJSONArray("result").getJSONObject(0).getInt("total");

if (results > 0) {
LOGGER.info("Fulltext PDF found @ Springer.");
return Optional.of(new URI("http", null, CONTENT_HOST, -1, "/content/pdf/%s.pdf".formatted(doi.get().getDOI()), null, null).toURL());
return Optional.of(new URI("http", null, CONTENT_HOST, -1, "/content/pdf/%s.pdf".formatted(doi.get().asString()), null, null).toURL());
}
}
} catch (UnirestException | URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc
}

DoiFetcher doiFetcher = new DoiFetcher(this.preferences);
return doiFetcher.performSearchById(doi.get().getDOI());
return doiFetcher.performSearchById(doi.get().asString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc
identifier = removeNewlinesAndSpacesFromIdentifier(identifier);
Optional<ISBN> isbn = ISBN.parse(identifier);
if (isbn.isPresent()) {
bibEntry = gvkIsbnFetcher.performSearchById(isbn.get().getNormalized());
bibEntry = gvkIsbnFetcher.performSearchById(isbn.get().asString());
}
} catch (FetcherException ex) {
LOGGER.debug("Got a fetcher exception for IBSN search", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ private String getDoi(String doi) {
pos = curString.indexOf(StandardField.DOI.getName());
}
if (pos >= 0) {
return DOI.findInText(curString).map(DOI::getDOI).orElse(null);
return DOI.findInText(curString).map(DOI::asString).orElse(null);
}
}
return doi;
Expand Down
Loading

0 comments on commit d94b47f

Please sign in to comment.