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 @@ -68,6 +68,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)

### Removed

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 @@ -46,7 +46,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;
import org.jabref.shared.prefs.SharedDatabasePreferences;
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 @@ -99,7 +99,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 @@ -119,7 +119,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.DatabaseLocation;
Expand All @@ -136,6 +135,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;
import org.jabref.shared.DBMSSynchronizer;
Expand Down Expand Up @@ -448,16 +448,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 @@ -1735,15 +1729,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
24 changes: 6 additions & 18 deletions src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import org.jabref.logic.TypedBibEntry;
import org.jabref.logic.bibtex.InvalidFieldValueException;
import org.jabref.logic.bibtex.LatexFieldFormatter;
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.EntryBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
Expand All @@ -80,6 +80,7 @@
import org.jabref.logic.util.OS;
import org.jabref.logic.util.UpdateField;
import org.jabref.model.EntryTypes;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.EntryType;
Expand Down Expand Up @@ -655,7 +656,7 @@ public void actionPerformed(ActionEvent event) {
}

// Make sure the key is legal:
String cleaned = BibtexKeyPatternUtil.checkLegalKey(newValue,
String cleaned = BibtexKeyGenerator.cleanKey(newValue,
Globals.prefs.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
if ((cleaned == null) || cleaned.equals(newValue)) {
textField.setValidBackgroundColor();
Expand Down Expand Up @@ -852,22 +853,9 @@ public void actionPerformed(ActionEvent e) {
}
}

BibtexKeyPatternUtil.makeAndSetLabel(panel.getBibDatabaseContext().getMetaData()
.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()),
panel.getDatabase(), entry,
Globals.prefs.getBibtexKeyPatternPreferences());

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();
}
Optional<FieldChange> change = new BibtexKeyGenerator(panel.getBibDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences())
.generateAndSetKey(entry);
change.ifPresent(fieldChange -> panel.getUndoManager().addEdit(new UndoableKeyChange(fieldChange)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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 @@ -453,10 +453,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 @@ -497,10 +495,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
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/undo/UndoableKeyChange.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.undo;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.strings.StringUtil;

Expand All @@ -15,6 +16,9 @@ public class UndoableKeyChange extends AbstractUndoableJabRefEdit {
private final String oldValue;
private final String newValue;

public UndoableKeyChange(FieldChange change) {
this(change.getEntry(), change.getOldValue(), change.getNewValue());
}

public UndoableKeyChange(BibEntry entry, String oldValue, String newValue) {
this.entry = entry;
Expand Down
Loading