Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/field inheritance #5282

Merged
merged 7 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#

### Fixed

- Inherit fields from cross-referenced entries as specified by biblatex [#5045](https://github.com/JabRef/jabref/issues/5045)

### Removed


Expand Down
126 changes: 113 additions & 13 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.IEEETranEntryType;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.strings.LatexToUnicodeAdapter;
import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -112,6 +113,110 @@ public Optional<String> getResolvedFieldOrAlias(OrFields fields, BibDatabase dat
return Optional.empty();
}

/**
* Map an (empty) field of a BibEntry to a field of a cross-referenced entry.
*
* @param targetField field name of the BibEntry
* @param targetEntry type of the BibEntry
* @param sourceEntry type of the cross-referenced BibEntry
*
* @return the mapped field or null if there is no valid mapping available
*/
private Optional<Field> getSourceField(Field targetField, EntryType targetEntry, EntryType sourceEntry) {
//// 1. Sort out forbidden fields
if ((targetField == StandardField.IDS) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can simply the code a bit:
Create an EnumSet of the fields and check with contains if the field is in the set.
https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion, but I am not sure, if this fits here. First, I have to list the fields anyway, so I don't think, it would simplify the code. Second, the set is accessed only in one single place, so making it a static class member would extend the scope unnecessarily and clutter the class member list. Creating it locally would improve neither code nor performance, I guess. What do you think?

(targetField == StandardField.CROSSREF) ||
(targetField == StandardField.XREF) ||
(targetField == StandardField.ENTRYSET) ||
(targetField == StandardField.RELATED) ||
(targetField == StandardField.SORTKEY)) {
return Optional.empty();
}

//// 2. Handle special field mappings
if ((sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.InBook) ||
(sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.BookInBook) ||
(sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.SuppBook) ||
(sourceEntry == StandardEntryType.Book && targetEntry == StandardEntryType.InBook) ||
(sourceEntry == StandardEntryType.Book && targetEntry == StandardEntryType.BookInBook) ||
(sourceEntry == StandardEntryType.Book && targetEntry == StandardEntryType.SuppBook)) {
if (targetField == StandardField.AUTHOR) { return Optional.of(StandardField.AUTHOR); }
if (targetField == StandardField.BOOKAUTHOR) { return Optional.of(StandardField.AUTHOR); }
}

if ((sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.Book) ||
(sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.InBook) ||
(sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.BookInBook) ||
(sourceEntry == StandardEntryType.MvBook && targetEntry == StandardEntryType.SuppBook) ||
(sourceEntry == StandardEntryType.MvCollection && targetEntry == StandardEntryType.Collection) ||
(sourceEntry == StandardEntryType.MvCollection && targetEntry == StandardEntryType.InCollection) ||
(sourceEntry == StandardEntryType.MvCollection && targetEntry == StandardEntryType.SuppCollection) ||
(sourceEntry == StandardEntryType.MvProceedings && targetEntry == StandardEntryType.Proceedings) ||
(sourceEntry == StandardEntryType.MvProceedings && targetEntry == StandardEntryType.InProceedings) ||
(sourceEntry == StandardEntryType.MvReference && targetEntry == StandardEntryType.Reference) ||
(sourceEntry == StandardEntryType.MvReference && targetEntry == StandardEntryType.InReference)) {
if (targetField == StandardField.MAINTITLE) { return Optional.of(StandardField.TITLE); }
if (targetField == StandardField.MAINSUBTITLE) { return Optional.of(StandardField.SUBTITLE); }
if (targetField == StandardField.MAINTITLEADDON) { return Optional.of(StandardField.TITLEADDON); }

// those fields are no more available for the same-name inheritance strategy
if ((targetField == StandardField.TITLE) ||
(targetField == StandardField.SUBTITLE) ||
(targetField == StandardField.TITLEADDON)) {
return Optional.empty();
}

// for these fields, inheritance is not allowed for the specified entry types
if ((targetField == StandardField.SHORTTITLE)) {
return Optional.empty();
}
}

if ((sourceEntry == StandardEntryType.Book && targetEntry == StandardEntryType.InBook) ||
(sourceEntry == StandardEntryType.Book && targetEntry == StandardEntryType.BookInBook) ||
(sourceEntry == StandardEntryType.Book && targetEntry == StandardEntryType.SuppBook) ||
(sourceEntry == StandardEntryType.Collection && targetEntry == StandardEntryType.InCollection) ||
(sourceEntry == StandardEntryType.Collection && targetEntry == StandardEntryType.SuppCollection) ||
(sourceEntry == StandardEntryType.Reference && targetEntry == StandardEntryType.InReference) ||
(sourceEntry == StandardEntryType.Proceedings && targetEntry == StandardEntryType.InProceedings)) {
if (targetField == StandardField.BOOKTITLE) { return Optional.of(StandardField.TITLE); }
if (targetField == StandardField.BOOKSUBTITLE) { return Optional.of(StandardField.SUBTITLE); }
if (targetField == StandardField.BOOKTITLEADDON) { return Optional.of(StandardField.TITLEADDON); }

// those fields are no more available for the same-name inheritance strategy
if ((targetField == StandardField.TITLE) ||
(targetField == StandardField.SUBTITLE) ||
(targetField == StandardField.TITLEADDON)) {
return Optional.empty();
}

// for these fields, inheritance is not allowed for the specified entry types
if ((targetField == StandardField.SHORTTITLE)) {
return Optional.empty();
}
}

if ((sourceEntry == IEEETranEntryType.Periodical && targetEntry == StandardEntryType.Article) ||
(sourceEntry == IEEETranEntryType.Periodical && targetEntry == StandardEntryType.SuppPeriodical)) {
if (targetField == StandardField.JOURNALTITLE) { return Optional.of(StandardField.TITLE); }
if (targetField == StandardField.JOURNALSUBTITLE) { return Optional.of(StandardField.SUBTITLE); }

// those fields are no more available for the same-name inheritance strategy
if ((targetField == StandardField.TITLE) ||
(targetField == StandardField.SUBTITLE)) {
return Optional.empty();
}

// for these fields, inheritance is not allowed for the specified entry types
if ((targetField == StandardField.SHORTTITLE)) {
return Optional.empty();
}
}

//// 3. Fallback to inherit the field with the same name.
return Optional.ofNullable(targetField);
}

/**
* Returns the text stored in the given field of the given bibtex entry
* which belongs to the given database.
Expand All @@ -137,21 +242,16 @@ public Optional<String> getResolvedFieldOrAlias(Field field, BibDatabase databas

Optional<String> result = getFieldOrAlias(field);
// If this field is not set, and the entry has a crossref, try to look up the
// field in the referred entry: Do not do this for the bibtex key.
if (!result.isPresent() && (database != null)) {
// field in the referred entry, following the biblatex rules
if (result.isEmpty() && (database != null)) {
Optional<BibEntry> referred = database.getReferencedEntry(this);
if (referred.isPresent()) {
result = referred.get().getFieldOrAlias(field);
if (!result.isPresent() && type.equals(StandardEntryType.InProceedings)) {
if (field == StandardField.BOOKTITLE) {
result = referred.get().getFieldOrAlias(StandardField.TITLE);
}
else if (field == StandardField.BOOKSUBTITLE) {
result = referred.get().getFieldOrAlias(StandardField.SUBTITLE);
}
else if (field == StandardField.BOOKAUTHOR) {
result = referred.get().getFieldOrAlias(StandardField.AUTHOR);
}
EntryType sourceEntry = referred.get().type.get();
EntryType targetEntry = type.get();
Optional<Field> sourceField = getSourceField(field, targetEntry, sourceEntry);

if (sourceField.isPresent()) {
result = referred.get().getFieldOrAlias(sourceField.get());
}
}
}
Expand Down
Loading