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 for issue 2811: bibtexkey generator does not use crossref information #3248

Merged
merged 9 commits into from
Oct 6, 2017
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We added support for '[entrytype]' bracketed expression.
- Updated French translation
- We added support for pasting entries in different formats [#3143](https://github.com/JabRef/jabref/issues/3143)
- Crossreferenced entries are now used when a BibTex key is generated for an entry with empty fields. [#2811](https://github.com/JabRef/jabref/issues/2811)

### Fixed
- We fixed an issue where JabRef would not terminated after asking to collect anonymous statistics [#2955 comment](https://github.com/JabRef/jabref/issues/2955#issuecomment-334591123)
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,17 @@ public void actionPerformed(ActionEvent e) {
panel.getDatabase(), entry,
Globals.prefs.getBibtexKeyPatternPreferences());

// Store undo information:
panel.getUndoManager().addEdit(
new UndoableKeyChange(entry, oldValue.orElse(null),
entry.getCiteKeyOptional().get())); // Cite key always set here

// here we update the field
String bibtexKeyData = entry.getCiteKeyOptional().get();
entry.setField(BibEntry.KEY_FIELD, bibtexKeyData);
panel.markBaseChanged();
if (entry.hasCiteKey()) {
// Store undo information:
panel.getUndoManager().addEdit(
new UndoableKeyChange(entry, oldValue.orElse(null),
entry.getCiteKeyOptional().get())); // Cite key always set here

// here we update the field
String bibtexKeyData = entry.getCiteKeyOptional().get();
entry.setField(BibEntry.KEY_FIELD, bibtexKeyData);
panel.markBaseChanged();
}
}
}

Expand Down
60 changes: 30 additions & 30 deletions src/main/java/org/jabref/logic/util/BracketedPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ public static String getFieldValue(BibEntry entry, String value, Character keywo
*/
String authString;
if (database != null) {
authString = entry.getField(FieldName.AUTHOR)
authString = entry.getResolvedFieldOrAlias(FieldName.AUTHOR, database)
.map(authorString -> normalize(database.resolveForStrings(authorString))).orElse("");
} else {
authString = entry.getField(FieldName.AUTHOR).orElse("");
authString = entry.getResolvedFieldOrAlias(FieldName.AUTHOR, database).orElse("");
}

if (val.startsWith("pure")) {
Expand All @@ -151,10 +151,10 @@ public static String getFieldValue(BibEntry entry, String value, Character keywo

if (authString.isEmpty()) {
if (database != null) {
authString = entry.getField(FieldName.EDITOR)
authString = entry.getResolvedFieldOrAlias(FieldName.EDITOR, database)
.map(authorString -> normalize(database.resolveForStrings(authorString))).orElse("");
} else {
authString = entry.getField(FieldName.EDITOR).orElse("");
authString = entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse("");
}
}

Expand Down Expand Up @@ -208,41 +208,41 @@ else if ("authorLast".equals(val)) {
} else {
// This "auth" business was a dead end, so just
// use it literally:
return entry.getFieldOrAlias(val).orElse("");
return entry.getResolvedFieldOrAlias(val, database).orElse("");
}
} else if (val.startsWith("ed")) {
// Gather all markers starting with "ed" here, so we
// don't have to check all the time.
if ("edtr".equals(val)) {
return firstAuthor(entry.getField(FieldName.EDITOR).orElse(""));
return firstAuthor(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
} else if ("edtrForeIni".equals(val)) {
return firstAuthorForenameInitials(entry.getField(FieldName.EDITOR).orElse(""));
return firstAuthorForenameInitials(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
} else if ("editors".equals(val)) {
return allAuthors(entry.getField(FieldName.EDITOR).orElse(""));
return allAuthors(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
// Last author's last name
} else if ("editorLast".equals(val)) {
return lastAuthor(entry.getField(FieldName.EDITOR).orElse(""));
return lastAuthor(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
} else if ("editorLastForeIni".equals(val)) {
return lastAuthorForenameInitials(entry.getField(FieldName.EDITOR).orElse(""));
return lastAuthorForenameInitials(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
} else if ("editorIni".equals(val)) {
return oneAuthorPlusIni(entry.getField(FieldName.EDITOR).orElse(""));
return oneAuthorPlusIni(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
} else if (val.matches("edtrIni[\\d]+")) {
int num = Integer.parseInt(val.substring(7));
return authIniN(entry.getField(FieldName.EDITOR).orElse(""), num);
return authIniN(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""), num);
} else if (val.matches("edtr[\\d]+_[\\d]+")) {
String[] nums = val.substring(4).split("_");
return authNofMth(entry.getField(FieldName.EDITOR).orElse(""),
return authNofMth(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""),
Integer.parseInt(nums[0]),
Integer.parseInt(nums[1]) - 1);
} else if ("edtr.edtr.ea".equals(val)) {
return authAuthEa(entry.getField(FieldName.EDITOR).orElse(""));
return authAuthEa(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
} else if ("edtrshort".equals(val)) {
return authshort(entry.getField(FieldName.EDITOR).orElse(""));
return authshort(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
}
// authN. First N chars of the first author's last
// name.
else if (val.matches("edtr\\d+")) {
String fa = firstAuthor(entry.getField(FieldName.EDITOR).orElse(""));
String fa = firstAuthor(entry.getResolvedFieldOrAlias(FieldName.EDITOR, database).orElse(""));
int num = Integer.parseInt(val.substring(4));
if (num > fa.length()) {
num = fa.length();
Expand All @@ -251,31 +251,31 @@ else if (val.matches("edtr\\d+")) {
} else {
// This "ed" business was a dead end, so just
// use it literally:
return entry.getFieldOrAlias(val).orElse("");
return entry.getResolvedFieldOrAlias(val, database).orElse("");
}
} else if ("firstpage".equals(val)) {
return firstPage(entry.getField(FieldName.PAGES).orElse(""));
return firstPage(entry.getResolvedFieldOrAlias(FieldName.PAGES, database).orElse(""));
} else if ("pageprefix".equals(val)) {
return pagePrefix(entry.getField(FieldName.PAGES).orElse(""));
return pagePrefix(entry.getResolvedFieldOrAlias(FieldName.PAGES, database).orElse(""));
} else if ("lastpage".equals(val)) {
return lastPage(entry.getField(FieldName.PAGES).orElse(""));
return lastPage(entry.getResolvedFieldOrAlias(FieldName.PAGES, database).orElse(""));
} else if ("title".equals(val)) {
return camelizeSignificantWordsInTitle(entry.getField(FieldName.TITLE).orElse(""));
return camelizeSignificantWordsInTitle(entry.getResolvedFieldOrAlias(FieldName.TITLE, database).orElse(""));
} else if ("fulltitle".equals(val)) {
return entry.getField(FieldName.TITLE).orElse("");
return entry.getResolvedFieldOrAlias(FieldName.TITLE, database).orElse("");
} else if ("shorttitle".equals(val)) {
return getTitleWords(3, entry.getField(FieldName.TITLE).orElse(""));
return getTitleWords(3, entry.getResolvedFieldOrAlias(FieldName.TITLE, database).orElse(""));
} else if ("shorttitleINI".equals(val)) {
return keepLettersAndDigitsOnly(
applyModifiers(getTitleWordsWithSpaces(3, entry.getField(FieldName.TITLE).orElse("")),
applyModifiers(getTitleWordsWithSpaces(3, entry.getResolvedFieldOrAlias(FieldName.TITLE, database).orElse("")),
Collections.singletonList("abbr"), 0));
} else if ("veryshorttitle".equals(val)) {
return getTitleWords(1,
removeSmallWords(entry.getField(FieldName.TITLE).orElse("")));
removeSmallWords(entry.getResolvedFieldOrAlias(FieldName.TITLE, database).orElse("")));
} else if ("camel".equals(val)) {
return getCamelizedTitle(entry.getField(FieldName.TITLE).orElse(""));
return getCamelizedTitle(entry.getResolvedFieldOrAlias(FieldName.TITLE, database).orElse(""));
} else if ("shortyear".equals(val)) {
String yearString = entry.getFieldOrAlias(FieldName.YEAR).orElse("");
String yearString = entry.getResolvedFieldOrAlias(FieldName.YEAR, database).orElse("");
if (yearString.isEmpty()) {
return yearString;
// In press/in preparation/submitted
Expand All @@ -291,7 +291,7 @@ else if (val.matches("edtr\\d+")) {
} else if (val.matches("keyword\\d+")) {
// according to LabelPattern.php, it returns keyword number n
int num = Integer.parseInt(val.substring(7));
KeywordList separatedKeywords = entry.getKeywords(keywordDelimiter);
KeywordList separatedKeywords = entry.getResolvedKeywords(keywordDelimiter, database);
if (separatedKeywords.size() < num) {
// not enough keywords
return "";
Expand All @@ -307,7 +307,7 @@ else if (val.matches("edtr\\d+")) {
} else {
num = Integer.MAX_VALUE;
}
KeywordList separatedKeywords = entry.getKeywords(keywordDelimiter);
KeywordList separatedKeywords = entry.getResolvedKeywords(keywordDelimiter, database);
StringBuilder sb = new StringBuilder();
int i = 0;
for (Keyword keyword : separatedKeywords) {
Expand All @@ -322,7 +322,7 @@ else if (val.matches("edtr\\d+")) {
return sb.toString();
} else {
// we haven't seen any special demands
return entry.getFieldOrAlias(val).orElse("");
return entry.getResolvedFieldOrAlias(val, database).orElse("");
}
}
catch (NullPointerException ex) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,12 @@ public void addKeywords(Collection<String> keywords, Character delimiter) {

public KeywordList getKeywords(Character delimiter) {
Optional<String> keywordsContent = getField(FieldName.KEYWORDS);
if (keywordsContent.isPresent()) {
return KeywordList.parse(keywordsContent.get(), delimiter);
} else {
return new KeywordList();
}
return KeywordList.parse(keywordsContent, delimiter);
}

public KeywordList getResolvedKeywords(Character delimiter, BibDatabase database) {
Optional<String> keywordsContent = getResolvedFieldOrAlias(FieldName.KEYWORDS, database);
return KeywordList.parse(keywordsContent, delimiter);
}

public Collection<String> getFieldValues() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/jabref/model/entry/KeywordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -66,6 +67,16 @@ public static KeywordList parse(String keywordString, Character delimiter) {
return parse(keywordString, delimiter, Keyword.DEFAULT_HIERARCHICAL_DELIMITER);
}

/**
* parses a KeywordList from an optional String of keywordChains
*
* @param keywordString a optional String of keywordChains
* @return an parsed list containing the keywordChains
*/
public static KeywordList parse(Optional<String> keywordString, Character delimiter) {
return keywordString.map(keyword -> KeywordList.parse(keyword, delimiter)).orElse(new KeywordList());
}

public KeywordList createClone() {
return new KeywordList(this.keywordChains);
}
Expand Down
Loading