Search in sources :

Example 46 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class DroppedFileHandler method tryXmpImport.

// Done by MrDlib
private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCompound edits) {
    if (!"pdf".equals(fileType.getExtension())) {
        return false;
    }
    List<BibEntry> xmpEntriesInFile;
    try {
        xmpEntriesInFile = XMPUtil.readXMP(fileName, Globals.prefs.getXMPPreferences());
    } catch (IOException e) {
        LOGGER.warn("Problem reading XMP", e);
        return false;
    }
    if ((xmpEntriesInFile == null) || xmpEntriesInFile.isEmpty()) {
        return false;
    }
    JLabel confirmationMessage = new JLabel(Localization.lang("The PDF contains one or several BibTeX-records.") + "\n" + Localization.lang("Do you want to import these as new entries into the current library?"));
    JPanel entriesPanel = new JPanel();
    entriesPanel.setLayout(new BoxLayout(entriesPanel, BoxLayout.Y_AXIS));
    xmpEntriesInFile.forEach(entry -> {
        JTextArea entryArea = new JTextArea(entry.toString());
        entryArea.setEditable(false);
        entriesPanel.add(entryArea);
    });
    JPanel contentPanel = new JPanel(new BorderLayout());
    contentPanel.add(confirmationMessage, BorderLayout.NORTH);
    contentPanel.add(entriesPanel, BorderLayout.CENTER);
    int reply = JOptionPane.showConfirmDialog(frame, contentPanel, Localization.lang("XMP-metadata found in PDF: %0", fileName), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (reply == JOptionPane.CANCEL_OPTION) {
        // The user canceled thus that we are done.
        return true;
    }
    if (reply == JOptionPane.NO_OPTION) {
        return false;
    }
    // reply == JOptionPane.YES_OPTION)
    /*
         * TODO Extract Import functionality from ImportMenuItem then we could
         * do:
         *
         * ImportMenuItem importer = new ImportMenuItem(frame, (mainTable ==
         * null), new PdfXmpImporter());
         *
         * importer.automatedImport(new String[] { fileName });
         */
    boolean isSingle = xmpEntriesInFile.size() == 1;
    BibEntry single = isSingle ? xmpEntriesInFile.get(0) : null;
    boolean success = true;
    String destFilename;
    if (linkInPlace.isSelected()) {
        destFilename = FileUtil.shortenFileName(Paths.get(fileName), panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences())).toString();
    } else {
        if (renameCheckBox.isSelected() || (single == null)) {
            destFilename = fileName;
        } else {
            destFilename = single.getCiteKey() + "." + fileType.getExtension();
        }
        if (copyRadioButton.isSelected()) {
            success = doCopy(fileName, destFilename, edits);
        } else if (moveRadioButton.isSelected()) {
            success = doMove(fileName, destFilename, edits);
        }
    }
    if (success) {
        for (BibEntry aXmpEntriesInFile : xmpEntriesInFile) {
            aXmpEntriesInFile.setId(IdGenerator.next());
            edits.addEdit(new UndoableInsertEntry(panel.getDatabase(), aXmpEntriesInFile, panel));
            panel.getDatabase().insertEntry(aXmpEntriesInFile);
            doLink(aXmpEntriesInFile, fileType, destFilename, true, edits);
        }
        panel.markBaseChanged();
        panel.updateEntryEditorIfShowing();
    }
    return true;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JPanel(javax.swing.JPanel) JTextArea(javax.swing.JTextArea) BorderLayout(java.awt.BorderLayout) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) IOException(java.io.IOException) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Example 47 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class FindFullTextAction method update.

@Override
public void update() {
    List<Optional<URL>> remove = new ArrayList<>();
    for (Entry<Optional<URL>, BibEntry> download : downloads.entrySet()) {
        BibEntry entry = download.getValue();
        Optional<URL> result = download.getKey();
        if (result.isPresent()) {
            List<String> dirs = basePanel.getBibDatabaseContext().getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
            if (dirs.isEmpty()) {
                JOptionPane.showMessageDialog(basePanel.frame(), Localization.lang("Main file directory not set!") + " " + Localization.lang("Preferences") + " -> " + Localization.lang("File"), Localization.lang("Directory not found"), JOptionPane.ERROR_MESSAGE);
                return;
            }
            DownloadExternalFile def = new DownloadExternalFile(basePanel.frame(), basePanel.getBibDatabaseContext(), entry);
            try {
                def.download(result.get(), file -> {
                    FileListTableModel fileLinkModel = new FileListTableModel();
                    entry.getField(FieldName.FILE).ifPresent(fileLinkModel::setContent);
                    fileLinkModel.addEntry(0, file);
                    String newValue = fileLinkModel.getStringRepresentation();
                    UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE, entry.getField(FieldName.FILE).orElse(null), newValue);
                    entry.setField(FieldName.FILE, newValue);
                    basePanel.getUndoManager().addEdit(edit);
                    basePanel.markBaseChanged();
                });
            } catch (IOException e) {
                LOGGER.warn("Problem downloading file", e);
            }
            basePanel.output(Localization.lang("Finished downloading full text document for entry %0.", entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))));
        } else {
            String title = Localization.lang("Full text document download failed");
            String message = Localization.lang("Full text document download failed for entry %0.", entry.getCiteKeyOptional().orElse(Localization.lang("undefined")));
            basePanel.output(message);
            JOptionPane.showMessageDialog(basePanel.frame(), message, title, JOptionPane.ERROR_MESSAGE);
        }
        remove.add(result);
    }
    for (Optional<URL> result : remove) {
        downloads.remove(result);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) Optional(java.util.Optional) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange)

Example 48 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class ACMPortalFetcher method downloadEntryBibTeX.

private static Optional<BibEntry> downloadEntryBibTeX(String id, boolean downloadAbstract) {
    try {
        URL url = new URL(ACMPortalFetcher.START_URL + ACMPortalFetcher.BIBTEX_URL + id + ACMPortalFetcher.BIBTEX_URL_END);
        URLConnection connection = url.openConnection();
        // set user-agent to avoid being blocked as a crawler
        connection.addRequestProperty("User-Agent", URLDownload.USER_AGENT);
        Collection<BibEntry> items = null;
        try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
            String htmlCode = in.lines().filter(s -> !s.isEmpty()).collect(Collectors.joining());
            String bibtexString = htmlCode.substring(htmlCode.indexOf(START_BIBTEX_ENTRY), htmlCode.indexOf(END_BIBTEX_ENTRY_HTML));
            items = new BibtexParser(Globals.prefs.getImportFormatPreferences()).parseEntries(bibtexString);
        } catch (IOException | ParseException e) {
            LOGGER.info("Download of BibTeX information from ACM Portal failed.", e);
        }
        if ((items == null) || items.isEmpty()) {
            return Optional.empty();
        }
        BibEntry entry = items.iterator().next();
        //wait between requests or you will be blocked by ACM
        Thread.sleep(ACMPortalFetcher.WAIT_TIME);
        // get abstract
        if (downloadAbstract) {
            URLDownload dl = new URLDownload(ACMPortalFetcher.START_URL + ACMPortalFetcher.ABSTRACT_URL + id);
            String page = dl.asString(Globals.prefs.getDefaultEncoding());
            Matcher absM = ACMPortalFetcher.ABSTRACT_PATTERN.matcher(page);
            if (absM.find()) {
                entry.setField(FieldName.ABSTRACT, absM.group(1).trim());
            }
            //wait between requests or you will be blocked by ACM
            Thread.sleep(ACMPortalFetcher.WAIT_TIME);
        }
        return Optional.of(entry);
    } catch (NoSuchElementException e) {
        LOGGER.info("Bad BibTeX record read at: " + ACMPortalFetcher.BIBTEX_URL + id + ACMPortalFetcher.BIBTEX_URL_END, e);
    } catch (MalformedURLException e) {
        LOGGER.info("Malformed URL.", e);
    } catch (IOException e) {
        LOGGER.info("Cannot connect.", e);
    } catch (InterruptedException ignored) {
    // Ignored
    }
    return Optional.empty();
}
Also used : FetcherPreviewDialog(org.jabref.gui.importer.FetcherPreviewDialog) FieldName(org.jabref.model.entry.FieldName) URL(java.net.URL) HtmlToLatexFormatter(org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter) OutputPrinter(org.jabref.logic.importer.OutputPrinter) JabRefPreferences(org.jabref.preferences.JabRefPreferences) URLDownload(org.jabref.logic.net.URLDownload) GridLayout(java.awt.GridLayout) LinkedHashMap(java.util.LinkedHashMap) Matcher(java.util.regex.Matcher) URLConnection(java.net.URLConnection) Map(java.util.Map) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Localization(org.jabref.logic.l10n.Localization) NoSuchElementException(java.util.NoSuchElementException) ProtectedTermsLoader(org.jabref.logic.protectedterms.ProtectedTermsLoader) ProtectTermsFormatter(org.jabref.logic.formatter.casechanger.ProtectTermsFormatter) HelpFile(org.jabref.logic.help.HelpFile) MalformedURLException(java.net.MalformedURLException) ButtonGroup(javax.swing.ButtonGroup) Collection(java.util.Collection) BibEntry(org.jabref.model.entry.BibEntry) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) JRadioButton(javax.swing.JRadioButton) Globals(org.jabref.Globals) ParseException(org.jabref.logic.importer.ParseException) Dimension(java.awt.Dimension) JLabel(javax.swing.JLabel) JCheckBox(javax.swing.JCheckBox) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) UnitsToLatexFormatter(org.jabref.logic.formatter.bibtexfields.UnitsToLatexFormatter) ImportInspector(org.jabref.logic.importer.ImportInspector) LogFactory(org.apache.commons.logging.LogFactory) JPanel(javax.swing.JPanel) BibEntry(org.jabref.model.entry.BibEntry) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload) URL(java.net.URL) URLConnection(java.net.URLConnection) BufferedReader(java.io.BufferedReader) ParseException(org.jabref.logic.importer.ParseException) NoSuchElementException(java.util.NoSuchElementException)

Example 49 with BibEntry

use of org.jabref.model.entry.BibEntry 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 50 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class EntryFromFileCreatorManager method addEntriesFromFiles.

/**
     * Tries to add a entry for each file in the List.
     *
     * @param files
     * @param database
     * @param panel
     * @param entryType
     * @param generateKeywordsFromPathToFile
     * @param changeListener
     * @param importGUIMessages list of unexpected import event - Messages including
     *         failures
     * @return Returns The number of entries added
     */
public int addEntriesFromFiles(List<File> files, BibDatabase database, BasePanel panel, EntryType entryType, boolean generateKeywordsFromPathToFile, ChangeListener changeListener, List<String> importGUIMessages) {
    int count = 0;
    CompoundEdit ce = new CompoundEdit();
    for (File f : files) {
        EntryFromFileCreator creator = getEntryCreator(f);
        if (creator == null) {
            importGUIMessages.add("Problem importing " + f.getPath() + ": Unknown filetype.");
        } else {
            Optional<BibEntry> entry = creator.createEntry(f, generateKeywordsFromPathToFile);
            if (!entry.isPresent()) {
                importGUIMessages.add("Problem importing " + f.getPath() + ": Entry could not be created.");
                continue;
            }
            if (entryType != null) {
                entry.get().setType(entryType);
            }
            if (entry.get().getId() == null) {
                entry.get().setId(IdGenerator.next());
            }
            /*
                 * TODO: database.insertEntry(BibEntry) is not sensible. Why
                 * does 'true' mean "There were duplicates", while 'false' means
                 * "Everything alright"?
                 */
            if (!database.containsEntryWithId(entry.get().getId())) {
                // Therefore, we only insert the entry if it is not already present
                if (database.insertEntry(entry.get())) {
                    importGUIMessages.add("Problem importing " + f.getPath() + ": Insert into BibDatabase failed.");
                } else {
                    count++;
                    if (panel != null) {
                        ce.addEdit(new UndoableInsertEntry(database, entry.get(), panel));
                    }
                }
            }
        }
        if (changeListener != null) {
            changeListener.stateChanged(new ChangeEvent(this));
        }
    }
    if ((count > 0) && (panel != null)) {
        ce.end();
        panel.getUndoManager().addEdit(ce);
    }
    return count;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ChangeEvent(javax.swing.event.ChangeEvent) CompoundEdit(javax.swing.undo.CompoundEdit) File(java.io.File) UndoableInsertEntry(org.jabref.gui.undo.UndoableInsertEntry)

Aggregations

BibEntry (org.jabref.model.entry.BibEntry)716 Test (org.junit.Test)466 ParserResult (org.jabref.logic.importer.ParserResult)131 StringReader (java.io.StringReader)107 ArrayList (java.util.ArrayList)75 BibDatabase (org.jabref.model.database.BibDatabase)63 Path (java.nio.file.Path)52 IOException (java.io.IOException)43 HashMap (java.util.HashMap)37 Before (org.junit.Before)36 NamedCompound (org.jabref.gui.undo.NamedCompound)30 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)28 BibtexString (org.jabref.model.entry.BibtexString)28 List (java.util.List)23 File (java.io.File)21 StringWriter (java.io.StringWriter)19 Optional (java.util.Optional)19 BasePanel (org.jabref.gui.BasePanel)19 FieldChange (org.jabref.model.FieldChange)18 InputStream (java.io.InputStream)16