Search in sources :

Example 76 with BibDatabase

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

the class AuxCommandLine method perform.

public BibDatabase perform() {
    BibDatabase subDatabase = null;
    if (!auxFile.isEmpty() && (database != null)) {
        AuxParser auxParser = new AuxParser(auxFile, database);
        AuxParserResult result = auxParser.parse();
        subDatabase = result.getGeneratedBibDatabase();
        // print statistics
        System.out.println(result.getInformation(true));
    }
    return subDatabase;
}
Also used : AuxParser(org.jabref.logic.auxparser.AuxParser) AuxParserResult(org.jabref.logic.auxparser.AuxParserResult) BibDatabase(org.jabref.model.database.BibDatabase)

Example 77 with BibDatabase

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

the class CrossrefFetcherEvaluator method main.

public static void main(String[] args) throws IOException, InterruptedException {
    Globals.prefs = JabRefPreferences.getInstance();
    try (FileReader reader = new FileReader(args[0])) {
        BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
        ParserResult result = parser.parse(reader);
        BibDatabase db = result.getDatabase();
        List<BibEntry> entries = db.getEntries();
        AtomicInteger dois = new AtomicInteger();
        AtomicInteger doiFound = new AtomicInteger();
        AtomicInteger doiNew = new AtomicInteger();
        AtomicInteger doiIdentical = new AtomicInteger();
        int total = entries.size();
        CountDownLatch countDownLatch = new CountDownLatch(total);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (BibEntry entry : entries) {
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    Optional<DOI> origDOI = entry.getField(FieldName.DOI).flatMap(DOI::parse);
                    if (origDOI.isPresent()) {
                        dois.incrementAndGet();
                        try {
                            Optional<DOI> crossrefDOI = new CrossRef().findIdentifier(entry);
                            if (crossrefDOI.isPresent()) {
                                doiFound.incrementAndGet();
                                if (origDOI.get().getDOI().equalsIgnoreCase(crossrefDOI.get().getDOI())) {
                                    doiIdentical.incrementAndGet();
                                } else {
                                    System.out.println("DOI not identical for : " + entry);
                                }
                            } else {
                                System.out.println("DOI not found for: " + entry);
                            }
                        } catch (FetcherException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            Optional<DOI> crossrefDOI = new CrossRef().findIdentifier(entry);
                            if (crossrefDOI.isPresent()) {
                                System.out.println("New DOI found for: " + entry);
                                doiNew.incrementAndGet();
                            }
                        } catch (FetcherException e) {
                            e.printStackTrace();
                        }
                    }
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        System.out.println("---------------------------------");
        System.out.println("Total DB size: " + total);
        System.out.println("Total DOIs: " + dois);
        System.out.println("DOIs found: " + doiFound);
        System.out.println("DOIs identical: " + doiIdentical);
        System.out.println("New DOIs found: " + doiNew);
        executorService.shutdown();
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Optional(java.util.Optional) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) CountDownLatch(java.util.concurrent.CountDownLatch) ParserResult(org.jabref.logic.importer.ParserResult) FetcherException(org.jabref.logic.importer.FetcherException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) FileReader(java.io.FileReader) CrossRef(org.jabref.logic.importer.fetcher.CrossRef) BibDatabase(org.jabref.model.database.BibDatabase)

Example 78 with BibDatabase

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

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

the class FromAuxDialog method parseActionPerformed.

private void parseActionPerformed() {
    parseButton.setEnabled(false);
    BasePanel bp = (BasePanel) parentTabbedPane.getComponentAt(dbChooser.getSelectedIndex());
    notFoundList.removeAll();
    statusInfos.setText(null);
    BibDatabase refBase = bp.getDatabase();
    String auxName = auxFileField.getText();
    if ((auxName != null) && (refBase != null) && !auxName.isEmpty()) {
        auxParser = new AuxParser(auxName, refBase);
        AuxParserResult result = auxParser.parse();
        notFoundList.setListData(result.getUnresolvedKeys().toArray(new String[result.getUnresolvedKeys().size()]));
        statusInfos.append(result.getInformation(false));
        generateButton.setEnabled(true);
        // the generated database contains no entries -> no active generate-button
        if (!result.getGeneratedBibDatabase().hasEntries()) {
            statusInfos.append("\n" + Localization.lang("empty library"));
            generateButton.setEnabled(false);
        }
    } else {
        generateButton.setEnabled(false);
    }
    parseButton.setEnabled(true);
}
Also used : BasePanel(org.jabref.gui.BasePanel) AuxParser(org.jabref.logic.auxparser.AuxParser) AuxParserResult(org.jabref.logic.auxparser.AuxParserResult) BibDatabase(org.jabref.model.database.BibDatabase)

Example 80 with BibDatabase

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

the class EntryEditor method storeSource.

private boolean storeSource() {
    BibtexParser bibtexParser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
    try {
        ParserResult parserResult = bibtexParser.parse(new StringReader(source.getText()));
        BibDatabase database = parserResult.getDatabase();
        if (database.getEntryCount() > 1) {
            throw new IllegalStateException("More than one entry found.");
        }
        if (!database.hasEntries()) {
            if (parserResult.hasWarnings()) {
                // put the warning into as exception text -> it will be displayed to the user
                throw new IllegalStateException(parserResult.warnings().get(0));
            } else {
                throw new IllegalStateException("No entries found.");
            }
        }
        NamedCompound compound = new NamedCompound(Localization.lang("source edit"));
        BibEntry newEntry = database.getEntries().get(0);
        String newKey = newEntry.getCiteKeyOptional().orElse(null);
        boolean entryChanged = false;
        boolean emptyWarning = (newKey == null) || newKey.isEmpty();
        if (newKey != null) {
            entry.setCiteKey(newKey);
        } else {
            entry.clearCiteKey();
        }
        // First, remove fields that the user has removed.
        for (Entry<String, String> field : entry.getFieldMap().entrySet()) {
            String fieldName = field.getKey();
            String fieldValue = field.getValue();
            if (InternalBibtexFields.isDisplayableField(fieldName) && !newEntry.hasField(fieldName)) {
                compound.addEdit(new UndoableFieldChange(entry, fieldName, fieldValue, null));
                entry.clearField(fieldName);
                entryChanged = true;
            }
        }
        // Then set all fields that have been set by the user.
        for (Entry<String, String> field : newEntry.getFieldMap().entrySet()) {
            String fieldName = field.getKey();
            String oldValue = entry.getField(fieldName).orElse(null);
            String newValue = field.getValue();
            if (!Objects.equals(oldValue, newValue)) {
                // Test if the field is legally set.
                new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()).format(newValue, fieldName);
                compound.addEdit(new UndoableFieldChange(entry, fieldName, oldValue, newValue));
                entry.setField(fieldName, newValue);
                entryChanged = true;
            }
        }
        // See if the user has changed the entry type:
        if (!Objects.equals(newEntry.getType(), entry.getType())) {
            compound.addEdit(new UndoableChangeType(entry, entry.getType(), newEntry.getType()));
            entry.setType(newEntry.getType());
            entryChanged = true;
        }
        compound.end();
        if (!entryChanged) {
            return true;
        }
        panel.getUndoManager().addEdit(compound);
        if (panel.getDatabase().getDuplicationChecker().isDuplicateCiteKeyExisting(entry)) {
            warnDuplicateBibtexkey();
        } else if (emptyWarning) {
            warnEmptyBibtexkey();
        } else {
            panel.output(Localization.lang("Stored entry") + '.');
        }
        lastSourceStringAccepted = source.getText();
        // Update UI
        // TODO: we need to repaint the entryeditor if fields that are not displayed have been added
        panel.updateEntryEditorIfShowing();
        lastSourceAccepted = true;
        updateSource = true;
        // TODO: does updating work properly after source stored?
        panel.markBaseChanged();
        panel.highlightEntry(entry);
        return true;
    } catch (InvalidFieldValueException | IOException ex) {
        // The source couldn't be parsed, so the user is given an
        // error message, and the choice to keep or revert the contents
        // of the source text field.
        updateSource = false;
        lastSourceAccepted = false;
        tabbed.setSelectedComponent(srcPanel);
        Object[] options = { Localization.lang("Edit"), Localization.lang("Revert to original source") };
        if (!SwingUtilities.isEventDispatchThread()) {
            int answer = JOptionPane.showOptionDialog(frame, Localization.lang("Error") + ": " + ex.getMessage(), Localization.lang("Problem with parsing entry"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
            if (answer != 0) {
                updateSource = true;
                lastSourceAccepted = true;
                updateSource();
            }
        }
        LOGGER.debug("Incorrect source", ex);
        return false;
    }
}
Also used : UndoableChangeType(org.jabref.gui.undo.UndoableChangeType) TypedBibEntry(org.jabref.logic.TypedBibEntry) BibEntry(org.jabref.model.entry.BibEntry) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) InvalidFieldValueException(org.jabref.logic.bibtex.InvalidFieldValueException) IOException(java.io.IOException) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter) ParserResult(org.jabref.logic.importer.ParserResult) NamedCompound(org.jabref.gui.undo.NamedCompound) StringReader(java.io.StringReader) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) 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