Skip to content

Commit

Permalink
Apply review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
subhramit committed Mar 20, 2024
1 parent a04b364 commit e1e1daa
Show file tree
Hide file tree
Showing 3 changed files with 1,357 additions and 44 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- Clicking on the crossref and related tags in the entry editor jumps to the linked entry. [#5484](https://github.com/JabRef/jabref/issues/5484) [#9369](https://github.com/JabRef/jabref/issues/9369)
- We fixed an issue where JabRef could not parse absolute file paths from Zotero exports. [#10959](https://github.com/JabRef/jabref/issues/10959)
- We fixed an issue where an exception occured when toggling between "Live" or "Locked" in the internal Document Viewer. [#10935](https://github.com/JabRef/jabref/issues/10935)
- Fixed an issue on Windows where the browser extension reported failure to send an entry to JabRef even though it was sent properly. [JabRef-Browser-Extension#493](https://github.com/JabRef/JabRef-Browser-Extension/issues/493)
- We fixed an issue with where JabRef would throw an error when using MathSciNet search, as it was unable to parse the fetched JSON coreectly. [10996](https://github.com/JabRef/jabref/issues/10996)

### Removed
Expand Down Expand Up @@ -1285,4 +1286,4 @@ The changelog of JabRef 2.11 and all previous versions is available as [text fil
[5.0]: https://github.com/JabRef/jabref/compare/v5.0-beta...v5.0
[5.0-beta]: https://github.com/JabRef/jabref/compare/v5.0-alpha...v5.0-beta
[5.0-alpha]: https://github.com/JabRef/jabref/compare/v4.3...v5.0-alpha
<!-- markdownlint-disable-file MD012 MD024 MD033 MD053 -->
<!-- markdownlint-disable-file MD012 MD024 MD033 MD053 -->
45 changes: 20 additions & 25 deletions src/main/java/org/jabref/logic/importer/fetcher/MathSciNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@
public class MathSciNet implements SearchBasedParserFetcher, EntryBasedParserFetcher, IdBasedParserFetcher {
private static final Logger LOGGER = LoggerFactory.getLogger(MathSciNet.class);
private final ImportFormatPreferences preferences;

// Define the field mappings
private final Map<StandardField, List<String>> fieldMappings = Map.ofEntries(
Map.entry(StandardField.TITLE, List.of("titles", "title")),
Map.entry(StandardField.AUTHOR, List.of("authors")),
Map.entry(StandardField.YEAR, List.of("issue", "issue", "pubYear")),
Map.entry(StandardField.JOURNAL, List.of("issue", "issue", "journal", "shortTitle")),
Map.entry(StandardField.VOLUME, List.of("issue", "issue", "volume")),
Map.entry(StandardField.NUMBER, List.of("issue", "issue", "number")),
Map.entry(StandardField.PAGES, List.of("paging", "paging", "text")),
Map.entry(StandardField.KEYWORDS, List.of("primaryClass")),
Map.entry(StandardField.ISSN, List.of("issue", "issue", "journal", "issn"))
);
public MathSciNet(ImportFormatPreferences preferences) {
this.preferences = Objects.requireNonNull(preferences);
}
Expand Down Expand Up @@ -147,39 +158,24 @@ public Parser getParser() {
private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
try {
BibEntry entry = new BibEntry(StandardEntryType.Article);

// Define the field mappings
Map<StandardField, String[]> fieldMappings = Map.ofEntries(
Map.entry(StandardField.TITLE, new String[]{"titles", "title"}),
Map.entry(StandardField.AUTHOR, new String[]{"authors"}),
Map.entry(StandardField.YEAR, new String[]{"issue", "issue", "pubYear"}),
Map.entry(StandardField.JOURNAL, new String[]{"issue", "issue", "journal", "shortTitle"}),
Map.entry(StandardField.VOLUME, new String[]{"issue", "issue", "volume"}),
Map.entry(StandardField.NUMBER, new String[]{"issue", "issue", "number"}),
Map.entry(StandardField.PAGES, new String[]{"paging", "paging", "text"}),
Map.entry(StandardField.KEYWORDS, new String[]{"primaryClass"}),
Map.entry(StandardField.ISSN, new String[]{"issue", "issue", "journal", "issn"})
);

// Set fields based on the mappings
for (Map.Entry<StandardField, String[]> mapEntry : fieldMappings.entrySet()) {
for (Map.Entry<StandardField, List<String>> mapEntry : fieldMappings.entrySet()) {
StandardField field = mapEntry.getKey();
String[] path = mapEntry.getValue();
List<String> path = mapEntry.getValue();

String value;
if (field == StandardField.AUTHOR) {
value = toAuthors(item.optJSONArray(path[0]));
value = toAuthors(item.optJSONArray(path.getFirst()));
} else if (field == StandardField.KEYWORDS) {
value = getKeywords(item.optJSONObject(path[0]));
value = getKeywords(item.optJSONObject(path.getFirst()));
} else {
value = getOrNull(item, path);
value = getOrNull(item, path).orElse(null);
}

if (value != null) {
entry.setField(field, value);
}
}

// Handle articleUrl and mrnumber fields separately
String doi = item.optString("articleUrl", "");
if (!doi.isEmpty()) {
Expand All @@ -190,14 +186,13 @@ private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
if (!mrNumber.isEmpty()) {
entry.setField(StandardField.MR_NUMBER, mrNumber);
}

return entry;
} catch (JSONException exception) {
throw new ParseException("MathSciNet API JSON format has changed", exception);
}
}

private String getOrNull(JSONObject item, String... keys) {
private Optional<String> getOrNull(JSONObject item, List<String> keys) {
Object value = item;
for (String key : keys) {
if (value instanceof JSONObject) {
Expand All @@ -210,10 +205,10 @@ private String getOrNull(JSONObject item, String... keys) {
}

if (value instanceof String stringValue) {
return new String(stringValue.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
return Optional.of(new String(stringValue.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}

return null;
return Optional.empty();
}

private String toAuthors(JSONArray authors) {
Expand Down
Loading

0 comments on commit e1e1daa

Please sign in to comment.