Search in sources :

Example 1 with BibDatabase

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

the class ArgumentProcessor method generateAux.

private boolean generateAux(List<ParserResult> loaded, String[] data) {
    if (data.length == 2) {
        ParserResult pr = loaded.get(0);
        AuxCommandLine acl = new AuxCommandLine(data[0], pr.getDatabase());
        BibDatabase newBase = acl.perform();
        boolean notSavedMsg = false;
        // write an output, if something could be resolved
        if ((newBase != null) && newBase.hasEntries()) {
            String subName = StringUtil.getCorrectFileName(data[1], "bib");
            try {
                System.out.println(Localization.lang("Saving") + ": " + subName);
                SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
                BibDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
                Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
                SaveSession session = databaseWriter.saveDatabase(new BibDatabaseContext(newBase, defaults), prefs);
                // Show just a warning message if encoding did not work for all characters:
                if (!session.getWriter().couldEncodeAll()) {
                    System.err.println(Localization.lang("Warning") + ": " + Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName()) + " " + session.getWriter().getProblemCharacters());
                }
                session.commit(subName);
            } catch (SaveException ex) {
                System.err.println(Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage());
            }
            notSavedMsg = true;
        }
        if (!notSavedMsg) {
            System.out.println(Localization.lang("no library generated"));
        }
        return false;
    } else {
        return true;
    }
}
Also used : SaveException(org.jabref.logic.exporter.SaveException) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) ParserResult(org.jabref.logic.importer.ParserResult) Defaults(org.jabref.model.Defaults) SavePreferences(org.jabref.logic.exporter.SavePreferences) BibDatabase(org.jabref.model.database.BibDatabase) SaveSession(org.jabref.logic.exporter.SaveSession) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext)

Example 2 with BibDatabase

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

the class ClipBoardManager method extractBibEntriesFromClipboard.

public List<BibEntry> extractBibEntriesFromClipboard() {
    // Get clipboard contents, and see if TransferableBibtexEntry is among the content flavors offered
    Transferable content = CLIPBOARD.getContents(null);
    List<BibEntry> result = new ArrayList<>();
    if (content.isDataFlavorSupported(TransferableBibtexEntry.entryFlavor)) {
        // We have determined that the clipboard data is a set of entries.
        try {
            @SuppressWarnings("unchecked") List<BibEntry> contents = (List<BibEntry>) content.getTransferData(TransferableBibtexEntry.entryFlavor);
            result = contents;
        } catch (UnsupportedFlavorException | ClassCastException ex) {
            LOGGER.warn("Could not paste this type", ex);
        } catch (IOException ex) {
            LOGGER.warn("Could not paste", ex);
        }
    } else if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
        try {
            String data = (String) content.getTransferData(DataFlavor.stringFlavor);
            // fetch from doi
            if (DOI.parse(data).isPresent()) {
                LOGGER.info("Found DOI in clipboard");
                Optional<BibEntry> entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(new DOI(data).getDOI());
                entry.ifPresent(result::add);
            } else {
                // parse bibtex string
                BibtexParser bp = new BibtexParser(Globals.prefs.getImportFormatPreferences());
                BibDatabase db = bp.parse(new StringReader(data)).getDatabase();
                LOGGER.info("Parsed " + db.getEntryCount() + " entries from clipboard text");
                if (db.hasEntries()) {
                    result = db.getEntries();
                }
            }
        } catch (UnsupportedFlavorException ex) {
            LOGGER.warn("Could not parse this type", ex);
        } catch (IOException ex) {
            LOGGER.warn("Data is no longer available in the requested flavor", ex);
        } catch (FetcherException ex) {
            LOGGER.error("Error while fetching", ex);
        }
    }
    return result;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) DoiFetcher(org.jabref.logic.importer.fetcher.DoiFetcher) Optional(java.util.Optional) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) FetcherException(org.jabref.logic.importer.FetcherException) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) BibDatabase(org.jabref.model.database.BibDatabase) DOI(org.jabref.model.entry.identifier.DOI)

Example 3 with BibDatabase

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

the class SearchFixDuplicateLabels method run.

@Override
public void run() {
    // Find all multiple occurrences of BibTeX keys.
    dupes = new HashMap<>();
    Map<String, BibEntry> foundKeys = new HashMap<>();
    BibDatabase db = panel.getDatabase();
    for (BibEntry entry : db.getEntries()) {
        entry.getCiteKeyOptional().filter(key -> !key.isEmpty()).ifPresent(key -> {
            if (foundKeys.containsKey(key)) {
                if (dupes.containsKey(key)) {
                    dupes.get(key).add(entry);
                } else {
                    List<BibEntry> al = new ArrayList<>();
                    al.add(foundKeys.get(key));
                    al.add(entry);
                    dupes.put(key, al);
                }
            } else {
                foundKeys.put(key, entry);
            }
        });
    }
}
Also used : NamedCompound(org.jabref.gui.undo.NamedCompound) BibDatabase(org.jabref.model.database.BibDatabase) BibEntry(org.jabref.model.entry.BibEntry) AbstractWorker(org.jabref.gui.worker.AbstractWorker) HashMap(java.util.HashMap) BasePanel(org.jabref.gui.BasePanel) ArrayList(java.util.ArrayList) Globals(org.jabref.Globals) List(java.util.List) BibtexKeyPatternUtil(org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil) Map(java.util.Map) JCheckBox(javax.swing.JCheckBox) Localization(org.jabref.logic.l10n.Localization) UndoableKeyChange(org.jabref.gui.undo.UndoableKeyChange) BibEntry(org.jabref.model.entry.BibEntry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BibDatabase(org.jabref.model.database.BibDatabase)

Example 4 with BibDatabase

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

the class INSPIREFetcher method processQuery.

/*
     * @see java.lang.Runnable
     */
@Override
public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
    try {
        status.setStatus(Localization.lang("Fetching entries from Inspire"));
        /* query the archive and load the results into the BibEntry */
        BibDatabase bd = importInspireEntries(query);
        status.setStatus(Localization.lang("Adding fetched entries"));
        /* add the entry to the inspection dialog */
        bd.getEntries().forEach(dialog::addEntry);
        return true;
    } catch (Exception e) {
        LOGGER.error("Error while fetching from " + getTitle(), e);
        ((ImportInspectionDialog) dialog).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
    }
    return false;
}
Also used : BibDatabase(org.jabref.model.database.BibDatabase) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with BibDatabase

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

the class OpenOfficePanel method pushEntries.

private void pushEntries(boolean inParenthesisIn, boolean withText, boolean addPageInfo) {
    if (!ooBase.isConnectedToDocument()) {
        JOptionPane.showMessageDialog(frame, Localization.lang("Not connected to any Writer document. Please" + " make sure a document is open, and use the 'Select Writer document' button to connect to it."), Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    Boolean inParenthesis = inParenthesisIn;
    String pageInfo = null;
    if (addPageInfo) {
        AdvancedCiteDialog citeDialog = new AdvancedCiteDialog(frame);
        citeDialog.showDialog();
        if (citeDialog.canceled()) {
            return;
        }
        if (!citeDialog.getPageInfo().isEmpty()) {
            pageInfo = citeDialog.getPageInfo();
        }
        inParenthesis = citeDialog.isInParenthesisCite();
    }
    BasePanel panel = frame.getCurrentBasePanel();
    if (panel != null) {
        final BibDatabase database = panel.getDatabase();
        List<BibEntry> entries = panel.getSelectedEntries();
        if (!entries.isEmpty() && checkThatEntriesHaveKeys(entries)) {
            try {
                if (style == null) {
                    style = loader.getUsedStyle();
                }
                ooBase.insertEntry(entries, database, getBaseList(), style, inParenthesis, withText, pageInfo, preferences.syncWhenCiting());
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(frame, Localization.lang("You must select either a valid style file, or use one of the default styles."), Localization.lang("No valid style file defined"), JOptionPane.ERROR_MESSAGE);
                LOGGER.warn("Problem with style file", ex);
            } catch (ConnectionLostException ex) {
                showConnectionLostErrorMessage();
            } catch (UndefinedCharacterFormatException ex) {
                reportUndefinedCharacterFormat(ex);
            } catch (UndefinedParagraphFormatException ex) {
                reportUndefinedParagraphFormat(ex);
            } catch (com.sun.star.lang.IllegalArgumentException | UnknownPropertyException | PropertyVetoException | CreationException | NoSuchElementException | WrappedTargetException | IOException | BibEntryNotFoundException | IllegalTypeException | PropertyExistException | NotRemoveableException ex) {
                LOGGER.warn("Could not insert entry", ex);
            }
        }
    }
}
Also used : BasePanel(org.jabref.gui.BasePanel) WrappedTargetException(com.sun.star.lang.WrappedTargetException) IllegalTypeException(com.sun.star.beans.IllegalTypeException) NotRemoveableException(com.sun.star.beans.NotRemoveableException) FileNotFoundException(java.io.FileNotFoundException) PropertyExistException(com.sun.star.beans.PropertyExistException) BibEntry(org.jabref.model.entry.BibEntry) IOException(java.io.IOException) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) PropertyVetoException(com.sun.star.beans.PropertyVetoException) BibDatabase(org.jabref.model.database.BibDatabase) UndefinedParagraphFormatException(org.jabref.logic.openoffice.UndefinedParagraphFormatException) NoSuchElementException(com.sun.star.container.NoSuchElementException)

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