Search in sources :

Example 16 with MetaData

use of org.jabref.model.metadata.MetaData in project jabref by JabRef.

the class ArgumentProcessor method regenerateBibtexKeys.

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"));
        }
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) MetaData(org.jabref.model.metadata.MetaData) BibDatabase(org.jabref.model.database.BibDatabase)

Example 17 with MetaData

use of org.jabref.model.metadata.MetaData in project jabref by JabRef.

the class ImportInspectionDialog method generateKeyForEntry.

/**
     * Generate key for an entry.
     */
private void generateKeyForEntry(BibEntry entry) {
    entries.getReadWriteLock().writeLock().lock();
    try {
        BibDatabase database;
        MetaData localMetaData;
        // Relate to existing database, if any:
        if (panel == null) {
            database = new BibDatabase();
            localMetaData = new MetaData();
        } else {
            database = panel.getDatabase();
            localMetaData = panel.getBibDatabaseContext().getMetaData();
        }
        entry.setId(IdGenerator.next());
        // Add the entry to the database we are working with:
        database.insertEntry(entry);
        // Generate a unique key:
        BibtexKeyPatternUtil.makeAndSetLabel(localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), database, entry, Globals.prefs.getBibtexKeyPatternPreferences());
        // Remove the entry from the database again, since we only added it in
        // order to
        // make sure the key was unique:
        database.removeEntry(entry);
    } finally {
        entries.getReadWriteLock().writeLock().unlock();
    }
    glTable.repaint();
}
Also used : MetaData(org.jabref.model.metadata.MetaData) BibDatabase(org.jabref.model.database.BibDatabase)

Example 18 with MetaData

use of org.jabref.model.metadata.MetaData in project jabref by JabRef.

the class ImportInspectionDialog method generateKeys.

/**
     * Generate keys for all entries. All keys will be unique with respect to
     * one another, and, if they are destined for an existing database, with
     * respect to existing keys in the database.
     */
private void generateKeys() {
    entries.getReadWriteLock().writeLock().lock();
    try {
        BibDatabase database;
        MetaData localMetaData;
        // Relate to existing database, if any:
        if (panel == null) {
            database = new BibDatabase();
            localMetaData = new MetaData();
        } else {
            database = panel.getDatabase();
            localMetaData = panel.getBibDatabaseContext().getMetaData();
        }
        List<Optional<String>> keys = new ArrayList<>(entries.size());
        // and generate unique keys:
        for (BibEntry entry : entries) {
            entry.setId(IdGenerator.next());
            database.insertEntry(entry);
            BibtexKeyPatternUtil.makeAndSetLabel(localMetaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), database, entry, Globals.prefs.getBibtexKeyPatternPreferences());
            // Add the generated key to our list:   -- TODO: Why??
            keys.add(entry.getCiteKeyOptional());
        }
        preview.update();
        preview.repaint();
        // control over key uniqueness.
        for (BibEntry entry : entries) {
            database.removeEntry(entry);
        }
    } finally {
        entries.getReadWriteLock().writeLock().unlock();
    }
    glTable.repaint();
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Optional(java.util.Optional) MetaData(org.jabref.model.metadata.MetaData) ArrayList(java.util.ArrayList) BibDatabase(org.jabref.model.database.BibDatabase)

Example 19 with MetaData

use of org.jabref.model.metadata.MetaData in project jabref by JabRef.

the class BibtexDatabaseWriterTest method setUp.

@Before
public void setUp() {
    // Write to a string instead of to a file
    databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new);
    database = new BibDatabase();
    metaData = new MetaData();
    bibtexContext = new BibDatabaseContext(database, metaData, new Defaults(BibDatabaseMode.BIBTEX));
    importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
}
Also used : Defaults(org.jabref.model.Defaults) MetaData(org.jabref.model.metadata.MetaData) ImportFormatPreferences(org.jabref.logic.importer.ImportFormatPreferences) BibDatabase(org.jabref.model.database.BibDatabase) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Before(org.junit.Before)

Aggregations

MetaData (org.jabref.model.metadata.MetaData)19 BibDatabase (org.jabref.model.database.BibDatabase)12 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)8 BibEntry (org.jabref.model.entry.BibEntry)7 Defaults (org.jabref.model.Defaults)6 Before (org.junit.Before)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 ParserResult (org.jabref.logic.importer.ParserResult)3 FileDirectoryPreferences (org.jabref.model.metadata.FileDirectoryPreferences)3 BibtexDatabaseWriter (org.jabref.logic.exporter.BibtexDatabaseWriter)2 SavePreferences (org.jabref.logic.exporter.SavePreferences)2 StringSaveSession (org.jabref.logic.exporter.StringSaveSession)2 ImportFormatPreferences (org.jabref.logic.importer.ImportFormatPreferences)2 LayoutFormatterPreferences (org.jabref.logic.layout.LayoutFormatterPreferences)2 FieldFormatterCleanups (org.jabref.model.cleanup.FieldFormatterCleanups)2 ContentSelector (org.jabref.model.metadata.ContentSelector)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1