Search in sources :

Example 1 with MetaData

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

the class Benchmarks method write.

@Benchmark
public String write() throws Exception {
    BibtexDatabaseWriter<StringSaveSession> databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new);
    StringSaveSession saveSession = databaseWriter.savePartOfDatabase(new BibDatabaseContext(database, new MetaData(), new Defaults()), database.getEntries(), new SavePreferences());
    return saveSession.getStringValue();
}
Also used : Defaults(org.jabref.model.Defaults) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) MetaData(org.jabref.model.metadata.MetaData) SavePreferences(org.jabref.logic.exporter.SavePreferences) StringSaveSession(org.jabref.logic.exporter.StringSaveSession) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 2 with MetaData

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

the class MetaDataSerializer method getSerializedStringMap.

/**
     * Writes all data in the format <key, serialized data>.
     */
public static Map<String, String> getSerializedStringMap(MetaData metaData, GlobalBibtexKeyPattern globalCiteKeyPattern) {
    // First write all meta data except groups
    Map<String, List<String>> stringyMetaData = new HashMap<>();
    metaData.getSaveOrderConfig().ifPresent(saveOrderConfig -> stringyMetaData.put(MetaData.SAVE_ORDER_CONFIG, saveOrderConfig.getAsStringList()));
    metaData.getSaveActions().ifPresent(saveActions -> stringyMetaData.put(MetaData.SAVE_ACTIONS, saveActions.getAsStringList(OS.NEWLINE)));
    if (metaData.isProtected()) {
        stringyMetaData.put(MetaData.PROTECTED_FLAG_META, Collections.singletonList("true"));
    }
    stringyMetaData.putAll(serializeCiteKeyPattern(metaData, globalCiteKeyPattern));
    metaData.getMode().ifPresent(mode -> stringyMetaData.put(MetaData.DATABASE_TYPE, Collections.singletonList(mode.getAsString())));
    metaData.getDefaultFileDirectory().ifPresent(path -> stringyMetaData.put(MetaData.FILE_DIRECTORY, Collections.singletonList(path.trim())));
    metaData.getUserFileDirectories().forEach((user, path) -> stringyMetaData.put(MetaData.FILE_DIRECTORY + '-' + user, Collections.singletonList(path.trim())));
    for (ContentSelector selector : metaData.getContentSelectorList()) {
        stringyMetaData.put(MetaData.SELECTOR_META_PREFIX + selector.getFieldName(), selector.getValues());
    }
    Map<String, String> serializedMetaData = serializeMetaData(stringyMetaData);
    // Write groups if present.
    // Skip this if only the root node exists (which is always the AllEntriesGroup).
    metaData.getGroups().filter(root -> root.getNumberOfChildren() > 0).ifPresent(root -> serializedMetaData.put(MetaData.GROUPSTREE, serializeGroups(root)));
    // finally add all unknown meta data items to the serialization map
    Map<String, List<String>> unknownMetaData = metaData.getUnknownMetaData();
    for (Map.Entry<String, List<String>> entry : unknownMetaData.entrySet()) {
        StringBuilder value = new StringBuilder();
        value.append(OS.NEWLINE);
        for (String line : entry.getValue()) {
            value.append(line.replaceAll(";", "\\\\;") + MetaData.SEPARATOR_STRING + OS.NEWLINE);
        }
        serializedMetaData.put(entry.getKey(), value.toString());
    }
    return serializedMetaData;
}
Also used : GlobalBibtexKeyPattern(org.jabref.model.bibtexkeypattern.GlobalBibtexKeyPattern) StringUtil(org.jabref.model.strings.StringUtil) AbstractBibtexKeyPattern(org.jabref.model.bibtexkeypattern.AbstractBibtexKeyPattern) ContentSelector(org.jabref.model.metadata.ContentSelector) HashMap(java.util.HashMap) FieldFormatterCleanups(org.jabref.model.cleanup.FieldFormatterCleanups) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) OS(org.jabref.logic.util.OS) GroupTreeNode(org.jabref.model.groups.GroupTreeNode) Map(java.util.Map) MetaData(org.jabref.model.metadata.MetaData) Collections(java.util.Collections) HashMap(java.util.HashMap) ContentSelector(org.jabref.model.metadata.ContentSelector) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 3 with MetaData

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

the class ChangeScanner method run.

@Override
public void run() {
    try {
        // Parse the temporary file.
        Path tempFile = Globals.getFileUpdateMonitor().getTempFile(panel.fileMonitorHandle());
        ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences();
        ParserResult result = OpenDatabase.loadDatabase(tempFile.toFile(), importFormatPreferences);
        databaseInTemp = result.getDatabase();
        metadataInTemp = result.getMetaData();
        // Parse the modified file.
        result = OpenDatabase.loadDatabase(file, importFormatPreferences);
        BibDatabase databaseOnDisk = result.getDatabase();
        MetaData metadataOnDisk = result.getMetaData();
        // Sort both databases according to a common sort key.
        EntryComparator comparator = new EntryComparator(false, true, SORT_BY[2]);
        comparator = new EntryComparator(false, true, SORT_BY[1], comparator);
        comparator = new EntryComparator(false, true, SORT_BY[0], comparator);
        EntrySorter sorterInTemp = databaseInTemp.getSorter(comparator);
        comparator = new EntryComparator(false, true, SORT_BY[2]);
        comparator = new EntryComparator(false, true, SORT_BY[1], comparator);
        comparator = new EntryComparator(false, true, SORT_BY[0], comparator);
        EntrySorter sorterOnDisk = databaseOnDisk.getSorter(comparator);
        comparator = new EntryComparator(false, true, SORT_BY[2]);
        comparator = new EntryComparator(false, true, SORT_BY[1], comparator);
        comparator = new EntryComparator(false, true, SORT_BY[0], comparator);
        EntrySorter sorterInMem = databaseInMemory.getSorter(comparator);
        // Start looking at changes.
        scanMetaData(metadataInMemory, metadataInTemp, metadataOnDisk);
        scanPreamble(databaseInMemory, databaseInTemp, databaseOnDisk);
        scanStrings(databaseInMemory, databaseInTemp, databaseOnDisk);
        scanEntries(sorterInMem, sorterInTemp, sorterOnDisk);
        scanGroups(metadataInTemp, metadataOnDisk);
    } catch (IOException ex) {
        LOGGER.warn("Problem running", ex);
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) EntryComparator(org.jabref.logic.bibtex.comparator.EntryComparator) MetaData(org.jabref.model.metadata.MetaData) ImportFormatPreferences(org.jabref.logic.importer.ImportFormatPreferences) IOException(java.io.IOException) EntrySorter(org.jabref.model.database.EntrySorter) BibDatabase(org.jabref.model.database.BibDatabase)

Example 4 with MetaData

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

the class AppendDatabaseAction method mergeFromBibtex.

private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, boolean importEntries, boolean importStrings, boolean importGroups, boolean importSelectorWords) throws KeyCollisionException {
    BibDatabase fromDatabase = parserResult.getDatabase();
    List<BibEntry> appendedEntries = new ArrayList<>();
    List<BibEntry> originalEntries = new ArrayList<>();
    BibDatabase database = panel.getDatabase();
    NamedCompound ce = new NamedCompound(Localization.lang("Append library"));
    MetaData meta = parserResult.getMetaData();
    if (importEntries) {
        // Add entries
        boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
        boolean overwriteTimeStamp = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP);
        for (BibEntry originalEntry : fromDatabase.getEntries()) {
            BibEntry entry = (BibEntry) originalEntry.clone();
            UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, Globals.prefs.getUpdateFieldPreferences());
            database.insertEntry(entry);
            appendedEntries.add(entry);
            originalEntries.add(originalEntry);
            ce.addEdit(new UndoableInsertEntry(database, entry, panel));
        }
    }
    if (importStrings) {
        for (BibtexString bs : fromDatabase.getStringValues()) {
            if (!database.hasStringLabel(bs.getName())) {
                database.addString(bs);
                ce.addEdit(new UndoableInsertString(panel, database, bs));
            }
        }
    }
    if (importGroups) {
        meta.getGroups().ifPresent(newGroups -> {
            if (newGroups.getGroup() instanceof AllEntriesGroup) {
                try {
                    ExplicitGroup group = new ExplicitGroup("Imported", GroupHierarchyType.INDEPENDENT, Globals.prefs.getKeywordDelimiter());
                    newGroups.setGroup(group);
                    group.add(appendedEntries);
                } catch (IllegalArgumentException e) {
                    LOGGER.error(e);
                }
            }
            addGroups(newGroups, ce);
        });
    }
    if (importSelectorWords) {
        for (ContentSelector selector : meta.getContentSelectorList()) {
            panel.getBibDatabaseContext().getMetaData().addContentSelector(selector);
        }
    }
    ce.end();
    panel.getUndoManager().addEdit(ce);
    panel.markBaseChanged();
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) BibtexString(org.jabref.model.entry.BibtexString) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) ExplicitGroup(org.jabref.model.groups.ExplicitGroup) AllEntriesGroup(org.jabref.model.groups.AllEntriesGroup) UndoableInsertString(org.jabref.gui.undo.UndoableInsertString) NamedCompound(org.jabref.gui.undo.NamedCompound) MetaData(org.jabref.model.metadata.MetaData) ContentSelector(org.jabref.model.metadata.ContentSelector) BibDatabase(org.jabref.model.database.BibDatabase)

Example 5 with MetaData

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

the class BibtexParser method initializeParserResult.

private void initializeParserResult() {
    database = new BibDatabase();
    // To store custom entry types parsed.
    entryTypes = new HashMap<>();
    parserResult = new ParserResult(database, new MetaData(), entryTypes);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) MetaData(org.jabref.model.metadata.MetaData) BibDatabase(org.jabref.model.database.BibDatabase)

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