Search in sources :

Example 11 with BibDatabase

use of org.jabref.model.database.BibDatabase 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 12 with BibDatabase

use of org.jabref.model.database.BibDatabase in project jabref by JabRef.

the class ArgumentProcessor method automaticallySetFileLinks.

private void automaticallySetFileLinks(List<ParserResult> loaded) {
    for (ParserResult parserResult : loaded) {
        BibDatabase database = parserResult.getDatabase();
        LOGGER.info(Localization.lang("Automatically setting file links"));
        AutoSetLinks.autoSetLinks(database.getEntries(), parserResult.getDatabaseContext());
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibDatabase(org.jabref.model.database.BibDatabase)

Example 13 with BibDatabase

use of org.jabref.model.database.BibDatabase in project jabref by JabRef.

the class DroppedFileHandler method showLinkMoveCopyRenameDialog.

//
// @return true if user pushed "OK", false otherwise
//
private boolean showLinkMoveCopyRenameDialog(String linkFileName, ExternalFileType fileType, BibEntry entry, BibDatabase database) {
    String dialogTitle = Localization.lang("Link to file %0", linkFileName);
    Optional<Path> dir = panel.getBibDatabaseContext().getFirstExistingFileDir(Globals.prefs.getFileDirectoryPreferences());
    if (!dir.isPresent()) {
        destDirLabel.setText(Localization.lang("File directory is not set or does not exist!"));
        copyRadioButton.setEnabled(false);
        moveRadioButton.setEnabled(false);
        renameToTextBox.setEnabled(false);
        renameCheckBox.setEnabled(false);
        linkInPlace.setSelected(true);
    } else {
        destDirLabel.setText(Localization.lang("File directory is '%0':", dir.get().toString()));
        copyRadioButton.setEnabled(true);
        moveRadioButton.setEnabled(true);
        renameToTextBox.setEnabled(true);
        renameCheckBox.setEnabled(true);
    }
    ChangeListener cl = arg0 -> {
        renameCheckBox.setEnabled(!linkInPlace.isSelected());
        renameToTextBox.setEnabled(!linkInPlace.isSelected());
    };
    linkInPlace.setText(Localization.lang("Leave file in its current directory"));
    copyRadioButton.setText(Localization.lang("Copy file to file directory"));
    moveRadioButton.setText(Localization.lang("Move file to file directory"));
    renameCheckBox.setText(Localization.lang("Rename file to").concat(": "));
    LayoutFormatterPreferences layoutPrefs = Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader);
    // Determine which name to suggest:
    String targetName = FileUtil.createFileNameFromPattern(database, entry, Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), layoutPrefs);
    String fileDirPattern = Globals.prefs.get(JabRefPreferences.IMPORT_FILEDIRPATTERN);
    String targetDirName = "";
    if (!fileDirPattern.isEmpty()) {
        targetDirName = FileUtil.createFileNameFromPattern(database, entry, fileDirPattern, layoutPrefs);
    }
    if (targetDirName.isEmpty()) {
        renameToTextBox.setText(targetName.concat(".").concat(fileType.getExtension()));
    } else {
        renameToTextBox.setText(targetDirName.concat("/").concat(targetName.concat(".").concat(fileType.getExtension())));
    }
    linkInPlace.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_LEAVE));
    copyRadioButton.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_COPY));
    moveRadioButton.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_MOVE));
    renameCheckBox.setSelected(frame.prefs().getBoolean(JabRefPreferences.DROPPEDFILEHANDLER_RENAME));
    linkInPlace.addChangeListener(cl);
    cl.stateChanged(new ChangeEvent(linkInPlace));
    try {
        Object[] messages = { Localization.lang("How would you like to link to '%0'?", linkFileName), optionsPanel };
        int reply = JOptionPane.showConfirmDialog(frame, messages, dialogTitle, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (reply == JOptionPane.OK_OPTION) {
            // store user's choice
            frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_LEAVE, linkInPlace.isSelected());
            frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_COPY, copyRadioButton.isSelected());
            frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_MOVE, moveRadioButton.isSelected());
            frame.prefs().putBoolean(JabRefPreferences.DROPPEDFILEHANDLER_RENAME, renameCheckBox.isSelected());
            return true;
        } else {
            return false;
        }
    } finally {
        linkInPlace.removeChangeListener(cl);
    }
}
Also used : Path(java.nio.file.Path) NamedCompound(org.jabref.gui.undo.NamedCompound) JTextField(javax.swing.JTextField) FieldName(org.jabref.model.entry.FieldName) BibDatabase(org.jabref.model.database.BibDatabase) JabRefPreferences(org.jabref.preferences.JabRefPreferences) BasePanel(org.jabref.gui.BasePanel) JabRefFrame(org.jabref.gui.JabRefFrame) Localization(org.jabref.logic.l10n.Localization) ChangeListener(javax.swing.event.ChangeListener) BorderLayout(java.awt.BorderLayout) Path(java.nio.file.Path) BoxLayout(javax.swing.BoxLayout) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) ChangeEvent(javax.swing.event.ChangeEvent) FormBuilder(com.jgoodies.forms.builder.FormBuilder) FileUtil(org.jabref.logic.util.io.FileUtil) Files(java.nio.file.Files) ButtonGroup(javax.swing.ButtonGroup) BibEntry(org.jabref.model.entry.BibEntry) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) IOException(java.io.IOException) LayoutFormatterPreferences(org.jabref.logic.layout.LayoutFormatterPreferences) JOptionPane(javax.swing.JOptionPane) MainTable(org.jabref.gui.maintable.MainTable) IdGenerator(org.jabref.model.entry.IdGenerator) File(java.io.File) JRadioButton(javax.swing.JRadioButton) Globals(org.jabref.Globals) FileListEntry(org.jabref.gui.filelist.FileListEntry) FileHelper(org.jabref.model.util.FileHelper) List(java.util.List) Paths(java.nio.file.Paths) FormLayout(com.jgoodies.forms.layout.FormLayout) JLabel(javax.swing.JLabel) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) JCheckBox(javax.swing.JCheckBox) Optional(java.util.Optional) XMPUtil(org.jabref.logic.xmp.XMPUtil) Log(org.apache.commons.logging.Log) JTextArea(javax.swing.JTextArea) ExternalFileTypes(org.jabref.gui.externalfiletype.ExternalFileTypes) LogFactory(org.apache.commons.logging.LogFactory) JPanel(javax.swing.JPanel) LayoutFormatterPreferences(org.jabref.logic.layout.LayoutFormatterPreferences) ChangeEvent(javax.swing.event.ChangeEvent) ChangeListener(javax.swing.event.ChangeListener)

Example 14 with BibDatabase

use of org.jabref.model.database.BibDatabase 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 15 with BibDatabase

use of org.jabref.model.database.BibDatabase in project jabref by JabRef.

the class MrDLibFetcher method performSearch.

@Override
public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
    Optional<String> title = entry.getLatexFreeField(FieldName.TITLE);
    if (title.isPresent()) {
        String response = makeServerRequest(title.get());
        MrDLibImporter importer = new MrDLibImporter();
        ParserResult parserResult = new ParserResult();
        try {
            if (importer.isRecognizedFormat(new BufferedReader(new StringReader(response)))) {
                parserResult = importer.importDatabase(new BufferedReader(new StringReader(response)));
            } else {
                // For displaying An ErrorMessage
                BibEntry errorBibEntry = new BibEntry();
                errorBibEntry.setField("html_representation", Localization.lang("Error_while_fetching_from_%0", "Mr.DLib"));
                BibDatabase errorBibDataBase = new BibDatabase();
                errorBibDataBase.insertEntry(errorBibEntry);
                parserResult = new ParserResult(errorBibDataBase);
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw new FetcherException("XML Parser IOException.");
        }
        return parserResult.getDatabase().getEntries();
    } else {
        // without a title there is no reason to ask MrDLib
        return new ArrayList<>(0);
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) MrDLibImporter(org.jabref.logic.importer.fileformat.MrDLibImporter) IOException(java.io.IOException) BibDatabase(org.jabref.model.database.BibDatabase)

Aggregations

BibDatabase (org.jabref.model.database.BibDatabase)88 BibEntry (org.jabref.model.entry.BibEntry)60 Test (org.junit.Test)44 ParserResult (org.jabref.logic.importer.ParserResult)20 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)15 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)15 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)13 MetaData (org.jabref.model.metadata.MetaData)12 IOException (java.io.IOException)10 Defaults (org.jabref.model.Defaults)9 Before (org.junit.Before)9 File (java.io.File)8 InputStreamReader (java.io.InputStreamReader)8 InputStream (java.io.InputStream)7 PropertyVetoException (com.sun.star.beans.PropertyVetoException)6 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)6 WrappedTargetException (com.sun.star.lang.WrappedTargetException)6 LinkedHashMap (java.util.LinkedHashMap)5 BasePanel (org.jabref.gui.BasePanel)5