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 #3359: Automatically remove colon and apostrophe from key pattern #3506

Merged
merged 9 commits into from
Jan 2, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an issue where fetched entries from the ACM fetcher could not be imported. [#3500](https://github.com/JabRef/jabref/issues/3500)
- We fixed an issue where custom data in combobox fields in the entry editor was not saved. [#3538](https://github.com/JabRef/jabref/issues/3538)
- We fixed an issue where automatically found files were not added with a relative paths when the bib file is in the same directory as the files. [#3476](https://github.com/JabRef/jabref/issues/3476)
- We improved the key generator to remove certain illegal characters such as colons or apostrophes. [#3359](https://github.com/JabRef/jabref/issues/3359)


## [4.0] - 2017-10-04
Expand Down
19 changes: 6 additions & 13 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.jabref.gui.externalfiles.AutoSetLinks;
import org.jabref.gui.importer.fetcher.EntryFetcher;
import org.jabref.gui.importer.fetcher.EntryFetchers;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.exporter.BibDatabaseWriter;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
import org.jabref.logic.exporter.ExportFormat;
Expand Down Expand Up @@ -47,7 +47,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.SearchPreferences;

Expand Down Expand Up @@ -509,17 +508,11 @@ private void regenerateBibtexKeys(List<ParserResult> loaded) {
for (ParserResult parserResult : loaded) {
BibDatabase database = parserResult.getDatabase();

MetaData metaData = parserResult.getMetaData();
if (metaData != null) {
LOGGER.info(Localization.lang("Regenerating BibTeX keys according to metadata"));
for (BibEntry entry : database.getEntries()) {
// try to make a new label
BibtexKeyPatternUtil.makeAndSetLabel(
metaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
database, entry, Globals.prefs.getBibtexKeyPatternPreferences());
}
} else {
LOGGER.info(Localization.lang("No meta data present in BIB file. Cannot regenerate BibTeX keys"));
LOGGER.info(Localization.lang("Regenerating BibTeX keys according to metadata"));

BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(parserResult.getDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences());
for (BibEntry entry : database.getEntries()) {
keyGenerator.generateAndSetKey(entry);
}
}
}
Expand Down
27 changes: 9 additions & 18 deletions src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
import org.jabref.gui.worker.CitationStyleToClipboardWorker;
import org.jabref.gui.worker.MarkEntriesAction;
import org.jabref.gui.worker.SendAsEMailAction;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.citationstyle.CitationStyleCache;
import org.jabref.logic.citationstyle.CitationStyleOutputFormat;
import org.jabref.logic.exporter.BibtexDatabaseWriter;
Expand All @@ -122,7 +122,6 @@
import org.jabref.logic.util.io.FileFinders;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.FieldChange;
import org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.KeyCollisionException;
Expand All @@ -140,6 +139,7 @@
import org.jabref.model.entry.event.EntryEventSource;
import org.jabref.model.entry.specialfields.SpecialField;
import org.jabref.model.entry.specialfields.SpecialFieldValue;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreviewPreferences;

Expand Down Expand Up @@ -489,16 +489,10 @@ public void run() {

// generate the new cite keys for each entry
final NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys"));
AbstractBibtexKeyPattern citeKeyPattern = bibDatabaseContext.getMetaData()
.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern());
BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibDatabaseContext, Globals.prefs.getBibtexKeyPatternPreferences());
for (BibEntry entry : entries) {
String oldCiteKey = entry.getCiteKeyOptional().orElse("");
BibtexKeyPatternUtil.makeAndSetLabel(citeKeyPattern, bibDatabaseContext.getDatabase(),
entry, Globals.prefs.getBibtexKeyPatternPreferences());
String newCiteKey = entry.getCiteKeyOptional().orElse("");
if (!oldCiteKey.equals(newCiteKey)) {
ce.addEdit(new UndoableKeyChange(entry, oldCiteKey, newCiteKey));
}
Optional<FieldChange> change = keyGenerator.generateAndSetKey(entry);
change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange)));
}
ce.end();

Expand Down Expand Up @@ -1754,15 +1748,12 @@ public void autoGenerateKeysBeforeSaving() {
if (Globals.prefs.getBoolean(JabRefPreferences.GENERATE_KEYS_BEFORE_SAVING)) {
NamedCompound ce = new NamedCompound(Localization.lang("Autogenerate BibTeX keys"));

BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(bibDatabaseContext, Globals.prefs.getBibtexKeyPatternPreferences());
for (BibEntry bes : bibDatabaseContext.getDatabase().getEntries()) {
Optional<String> oldKey = bes.getCiteKeyOptional();
if (!(oldKey.isPresent()) || oldKey.get().isEmpty()) {
BibtexKeyPatternUtil.makeAndSetLabel(bibDatabaseContext.getMetaData()
.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
bibDatabaseContext.getDatabase(),
bes, Globals.prefs.getBibtexKeyPatternPreferences());
bes.getCiteKeyOptional().ifPresent(
newKey -> ce.addEdit(new UndoableKeyChange(bes, oldKey.orElse(""), newKey)));
if (StringUtil.isBlank(oldKey)) {
Optional<FieldChange> change = keyGenerator.generateAndSetKey(bes);
change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/EntryTypeDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.jabref.gui.importer.ImportInspectionDialog;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.logic.bibtex.DuplicateCheck;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
Expand Down Expand Up @@ -319,7 +319,7 @@ protected void done() {
diag.toFront();
} else {
// Regenerate CiteKey of imported BibEntry
BibtexKeyPatternUtil.makeAndSetLabel(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern(), frame.getCurrentBasePanel().getDatabase(), bibEntry, Globals.prefs.getBibtexKeyPatternPreferences());
new BibtexKeyGenerator(frame.getCurrentBasePanel().getBibDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences()).generateAndSetKey(bibEntry);
// Update Timestamps
if (Globals.prefs.getTimestampPreferences().includeCreatedTimestamp()) {
bibEntry.setField(Globals.prefs.getTimestampPreferences().getTimestampField(), Globals.prefs.getTimestampPreferences().now());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/GenFieldsCustomizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.jabref.gui.entryeditor.EntryEditorTabList;
import org.jabref.gui.help.HelpAction;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;
Expand Down Expand Up @@ -119,7 +119,7 @@ private void okActionPerformed() {
Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
return;
}
String testString = BibtexKeyPatternUtil.checkLegalKey(parts[1],
String testString = BibtexKeyGenerator.cleanKey(parts[1],
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if (!testString.equals(parts[1]) || (parts[1].indexOf('&') >= 0)) {
// Report error and exit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.swing.JCheckBox;

Expand All @@ -12,8 +13,9 @@
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableKeyChange;
import org.jabref.gui.worker.AbstractWorker;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;

Expand Down Expand Up @@ -91,13 +93,10 @@ public void update() {
// Do the actual generation:
if (!toGenerateFor.isEmpty()) {
NamedCompound ce = new NamedCompound(Localization.lang("Resolve duplicate BibTeX keys"));
BibtexKeyGenerator keyGenerator = new BibtexKeyGenerator(panel.getBibDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences());
for (BibEntry entry : toGenerateFor) {
String oldKey = entry.getCiteKeyOptional().orElse(null);
BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData()
.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
panel.getDatabase(), entry,
Globals.prefs.getBibtexKeyPatternPreferences());
ce.addEdit(new UndoableKeyChange(entry, oldKey, entry.getCiteKeyOptional().get()));
Optional<FieldChange> change = keyGenerator.generateAndSetKey(entry);
change.ifPresent(fieldChange -> ce.addEdit(new UndoableKeyChange(fieldChange)));
}
ce.end();
panel.getUndoManager().addEdit(ce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javax.swing.event.ListSelectionListener;

import org.jabref.Globals;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.EntryTypes;
import org.jabref.model.database.BibDatabaseMode;
Expand Down Expand Up @@ -57,7 +57,7 @@ protected void addField(String str) {
return;
}

String testString = BibtexKeyPatternUtil.checkLegalKey(s,
String testString = BibtexKeyGenerator.cleanKey(s,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if (!testString.equals(s) || (s.indexOf('&') >= 0)) {
// Report error and exit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import org.jabref.Globals;
import org.jabref.gui.IconTheme;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;

Expand Down Expand Up @@ -242,7 +242,7 @@ protected void addField(String str) {
return;
}

String testString = BibtexKeyPatternUtil.checkLegalKey(s,
String testString = BibtexKeyGenerator.cleanKey(s,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if (!testString.equals(s) || (s.indexOf('&') >= 0)) {
// Report error and exit.
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ public class EntryEditor extends BorderPane {

private final BibDatabaseContext bibDatabaseContext;
private final CountingUndoManager undoManager;

private final BasePanel panel;
private final List<SearchQueryHighlightListener> searchListeners = new ArrayList<>();
private final List<EntryEditorTab> tabs;
/**
* A reference to the entry this editor works on.
*/
private BibEntry entry;
@FXML private TabPane tabbed;
@FXML private Button typeChangeButton;
@FXML private Button fetcherButton;
private final BasePanel panel;
private final List<SearchQueryHighlightListener> searchListeners = new ArrayList<>();
private final List<EntryEditorTab> tabs;
private SourceTab sourceTab;
@FXML private Label typeLabel;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.jabref.gui.fieldeditors;

import java.util.Optional;

import javax.swing.undo.UndoManager;

import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
import org.jabref.gui.undo.UndoableKeyChange;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabaseContext;

public class BibtexKeyEditorViewModel extends AbstractEditorViewModel {
Expand All @@ -25,11 +22,8 @@ public BibtexKeyEditorViewModel(String fieldName, AutoCompleteSuggestionProvider
}

public void generateKey() {
Optional<FieldChange> fieldChange = BibtexKeyPatternUtil.makeAndSetLabel(
bibDatabaseContext.getMetaData().getCiteKeyPattern(keyPatternPreferences.getKeyPattern()),
bibDatabaseContext.getDatabase(),
entry,
keyPatternPreferences);
fieldChange.ifPresent(change -> undoManager.addEdit(new UndoableKeyChange(change)));
new BibtexKeyGenerator(bibDatabaseContext, keyPatternPreferences)
.generateAndSetKey(entry)
.ifPresent(change -> undoManager.addEdit(new UndoableKeyChange(change)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
import org.jabref.gui.util.component.CheckBoxMessage;
import org.jabref.logic.bibtex.DuplicateCheck;
import org.jabref.logic.bibtex.comparator.FieldComparator;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.ImportInspector;
import org.jabref.logic.importer.OutputPrinter;
Expand Down Expand Up @@ -456,10 +456,8 @@ private void generateKeyForEntry(BibEntry entry) {
database.insertEntry(entry);

// Generate a unique key:
BibtexKeyPatternUtil.makeAndSetLabel(
localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
database, entry,
Globals.prefs.getBibtexKeyPatternPreferences());
new BibtexKeyGenerator(localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
database, Globals.prefs.getBibtexKeyPatternPreferences()).generateAndSetKey(entry);
// Remove the entry from the database again, since we only added it in
// order to
// make sure the key was unique:
Expand Down Expand Up @@ -500,10 +498,8 @@ private void generateKeys() {
entry.setId(IdGenerator.next());
database.insertEntry(entry);

BibtexKeyPatternUtil.makeAndSetLabel(
localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
database, entry,
Globals.prefs.getBibtexKeyPatternPreferences());
new BibtexKeyGenerator(localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
database, Globals.prefs.getBibtexKeyPatternPreferences()).generateAndSetKey(entry);
// Add the generated key to our list: -- TODO: Why??
keys.add(entry.getCiteKeyOptional());
}
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/org/jabref/gui/openoffice/OpenOfficePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.gui.worker.AbstractWorker;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.openoffice.OOBibStyle;
Expand Down Expand Up @@ -660,14 +660,9 @@ private boolean checkThatEntriesHaveKeys(List<BibEntry> entries) {
for (BibEntry entry : entries) {
if (!entry.getCiteKeyOptional().isPresent()) {
// Generate key
BibtexKeyPatternUtil
.makeAndSetLabel(
panel.getBibDatabaseContext().getMetaData().getCiteKeyPattern(prefs.getKeyPattern()),
panel.getDatabase(), entry,
prefs);
// Add undo change
undoCompound.addEdit(
new UndoableKeyChange(entry, null, entry.getCiteKeyOptional().get()));
new BibtexKeyGenerator(panel.getBibDatabaseContext(), prefs)
.generateAndSetKey(entry)
.ifPresent(change -> undoCompound.addEdit(new UndoableKeyChange(change)));
}
}
undoCompound.end();
Expand Down
Loading