Search in sources :

Example 16 with BibEntry

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

the class LinkedFilesEditorViewModel method findAssociatedNotLinkedFiles.

/**
     * Find files that are probably associated  to the given entry but not yet linked.
     */
private List<LinkedFileViewModel> findAssociatedNotLinkedFiles(BibEntry entry) {
    final List<Path> dirs = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    final List<String> extensions = ExternalFileTypes.getInstance().getExternalFileTypeSelection().stream().map(ExternalFileType::getExtension).collect(Collectors.toList());
    // Run the search operation:
    FileFinder fileFinder = FileFinders.constructFromConfiguration(Globals.prefs.getAutoLinkPreferences());
    List<Path> newFiles = fileFinder.findAssociatedFiles(entry, dirs, extensions);
    List<LinkedFileViewModel> result = new ArrayList<>();
    for (Path newFile : newFiles) {
        boolean alreadyLinked = files.get().stream().map(file -> file.findIn(dirs)).anyMatch(file -> file.isPresent() && file.get().equals(newFile));
        if (!alreadyLinked) {
            LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(fromFile(newFile, dirs));
            newLinkedFile.markAsAutomaticallyFound();
            result.add(newLinkedFile);
        }
    }
    return result;
}
Also used : Path(java.nio.file.Path) URL(java.net.URL) BindingsHelper(org.jabref.gui.util.BindingsHelper) FXCollections(javafx.collections.FXCollections) DialogService(org.jabref.gui.DialogService) SimpleListProperty(javafx.beans.property.SimpleListProperty) JabRefPreferences(org.jabref.preferences.JabRefPreferences) FileDownloadTask(org.jabref.gui.externalfiles.FileDownloadTask) LinkedFile(org.jabref.model.entry.LinkedFile) ArrayList(java.util.ArrayList) URLDownload(org.jabref.logic.net.URLDownload) FulltextFetchers(org.jabref.logic.importer.FulltextFetchers) UnknownExternalFileType(org.jabref.gui.externalfiletype.UnknownExternalFileType) Localization(org.jabref.logic.l10n.Localization) FileFieldWriter(org.jabref.model.entry.FileFieldWriter) ListProperty(javafx.beans.property.ListProperty) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Path(java.nio.file.Path) ExternalFileType(org.jabref.gui.externalfiletype.ExternalFileType) FileFinders(org.jabref.logic.util.io.FileFinders) FileUtil(org.jabref.logic.util.io.FileUtil) FileDialogConfiguration(org.jabref.gui.util.FileDialogConfiguration) MalformedURLException(java.net.MalformedURLException) BibEntry(org.jabref.model.entry.BibEntry) DownloadExternalFile(org.jabref.gui.externalfiles.DownloadExternalFile) BackgroundTask(org.jabref.gui.util.BackgroundTask) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Globals(org.jabref.Globals) FileFinder(org.jabref.logic.util.io.FileFinder) FileHelper(org.jabref.model.util.FileHelper) TaskExecutor(org.jabref.gui.util.TaskExecutor) FileFieldParser(org.jabref.model.entry.FileFieldParser) List(java.util.List) BooleanProperty(javafx.beans.property.BooleanProperty) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) Paths(java.nio.file.Paths) OS(org.jabref.logic.util.OS) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) ObservableList(javafx.collections.ObservableList) ExternalFileTypes(org.jabref.gui.externalfiletype.ExternalFileTypes) LogFactory(org.apache.commons.logging.LogFactory) ArrayList(java.util.ArrayList) FileFinder(org.jabref.logic.util.io.FileFinder)

Example 17 with BibEntry

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

the class GroupTreeNodeViewModel method changeEntriesTo.

public void changeEntriesTo(List<BibEntry> entries, UndoManager undoManager) {
    AbstractGroup group = node.getGroup();
    List<FieldChange> changesRemove = new ArrayList<>();
    List<FieldChange> changesAdd = new ArrayList<>();
    // Sort entries into current members and non-members of the group
    // Current members will be removed
    // Current non-members will be added
    List<BibEntry> toRemove = new ArrayList<>(entries.size());
    List<BibEntry> toAdd = new ArrayList<>(entries.size());
    for (BibEntry entry : entries) {
        // Sort according to current state of the entries
        if (group.contains(entry)) {
            toRemove.add(entry);
        } else {
            toAdd.add(entry);
        }
    }
    // If there are entries to remove
    if (!toRemove.isEmpty()) {
        changesRemove = removeEntriesFromGroup(toRemove);
    }
    // If there are entries to add
    if (!toAdd.isEmpty()) {
        changesAdd = addEntriesToGroup(toAdd);
    }
    // Remember undo information
    if (!changesRemove.isEmpty()) {
        AbstractUndoableEdit undoRemove = UndoableChangeEntriesOfGroup.getUndoableEdit(this, changesRemove);
        if (!changesAdd.isEmpty() && (undoRemove != null)) {
            // we removed and added entries
            undoRemove.addEdit(UndoableChangeEntriesOfGroup.getUndoableEdit(this, changesAdd));
        }
        undoManager.addEdit(undoRemove);
    } else if (!changesAdd.isEmpty()) {
        undoManager.addEdit(UndoableChangeEntriesOfGroup.getUndoableEdit(this, changesAdd));
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) AbstractUndoableEdit(javax.swing.undo.AbstractUndoableEdit) AbstractGroup(org.jabref.model.groups.AbstractGroup) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange)

Example 18 with BibEntry

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

the class CiteSeerXFetcher method getSingleCitation.

private static BibEntry getSingleCitation(String urlString) throws IOException {
    String cont = new URLDownload(urlString).asString();
    // Find title, and create entry if we do. Otherwise assume we did not get an entry:
    Matcher m = CiteSeerXFetcher.TITLE_PATTERN.matcher(cont);
    if (m.find()) {
        BibEntry entry = new BibEntry();
        entry.setField(FieldName.TITLE, m.group(1));
        // Find authors:
        m = CiteSeerXFetcher.AUTHOR_PATTERN.matcher(cont);
        if (m.find()) {
            String authors = m.group(1);
            entry.setField(FieldName.AUTHOR, new NormalizeNamesFormatter().format(authors));
        }
        // Find year:
        m = CiteSeerXFetcher.YEAR_PATTERN.matcher(cont);
        if (m.find()) {
            entry.setField(FieldName.YEAR, m.group(1));
        }
        // Find abstract:
        m = CiteSeerXFetcher.ABSTRACT_PATTERN.matcher(cont);
        if (m.find()) {
            entry.setField(FieldName.ABSTRACT, m.group(1));
        }
        return entry;
    } else {
        return null;
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Matcher(java.util.regex.Matcher) NormalizeNamesFormatter(org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter) URLDownload(org.jabref.logic.net.URLDownload)

Example 19 with BibEntry

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

the class DOAJFetcher method processQuery.

@Override
public boolean processQuery(String query, ImportInspector inspector, OutputPrinter status) {
    shouldContinue = true;
    try {
        status.setStatus(Localization.lang("Searching..."));
        HttpResponse<JsonNode> jsonResponse;
        jsonResponse = Unirest.get(SEARCH_URL + query + "?pageSize=1").header("accept", "application/json").asJson();
        JSONObject jo = jsonResponse.getBody().getObject();
        int numberToFetch = jo.getInt("total");
        if (numberToFetch > 0) {
            if (numberToFetch > MAX_PER_PAGE) {
                boolean numberEntered = false;
                do {
                    String strCount = JOptionPane.showInputDialog(Localization.lang("%0 references found. Number of references to fetch?", String.valueOf(numberToFetch)));
                    if (strCount == null) {
                        status.setStatus(Localization.lang("%0 import canceled", getTitle()));
                        return false;
                    }
                    try {
                        numberToFetch = Integer.parseInt(strCount.trim());
                        numberEntered = true;
                    } catch (NumberFormatException ex) {
                        status.showMessage(Localization.lang("Please enter a valid number"));
                    }
                } while (!numberEntered);
            }
            // Keep track of number of items fetched for the progress bar
            int fetched = 0;
            for (int page = 1; ((page - 1) * MAX_PER_PAGE) <= numberToFetch; page++) {
                if (!shouldContinue) {
                    break;
                }
                int noToFetch = Math.min(MAX_PER_PAGE, numberToFetch - ((page - 1) * MAX_PER_PAGE));
                jsonResponse = Unirest.get(SEARCH_URL + query + "?page=" + page + "&pageSize=" + noToFetch).header("accept", "application/json").asJson();
                jo = jsonResponse.getBody().getObject();
                if (jo.has("results")) {
                    JSONArray results = jo.getJSONArray("results");
                    for (int i = 0; i < results.length(); i++) {
                        JSONObject bibJsonEntry = results.getJSONObject(i).getJSONObject("bibjson");
                        BibEntry entry = jsonConverter.parseBibJSONtoBibtex(bibJsonEntry, Globals.prefs.getKeywordDelimiter());
                        inspector.addEntry(entry);
                        fetched++;
                        inspector.setProgress(fetched, numberToFetch);
                    }
                }
            }
            return true;
        } else {
            status.showMessage(Localization.lang("No entries found for the search string '%0'", query), Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
    } catch (UnirestException e) {
        LOGGER.error("Error while fetching from " + getTitle(), e);
        ((ImportInspectionDialog) inspector).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
        return false;
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JsonNode(com.mashape.unirest.http.JsonNode)

Example 20 with BibEntry

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

the class AbbreviateAction method run.

@Override
public void run() {
    List<BibEntry> entries = panel.getSelectedEntries();
    if (entries == null) {
        return;
    }
    UndoableAbbreviator undoableAbbreviator = new UndoableAbbreviator(Globals.journalAbbreviationLoader.getRepository(Globals.prefs.getJournalAbbreviationPreferences()), iso);
    NamedCompound ce = new NamedCompound(Localization.lang("Abbreviate journal names"));
    int count = 0;
    for (BibEntry entry : entries) {
        for (String journalField : InternalBibtexFields.getJournalNameFields()) {
            if (undoableAbbreviator.abbreviate(panel.getDatabase(), entry, journalField, ce)) {
                count++;
            }
        }
    }
    if (count > 0) {
        ce.end();
        panel.getUndoManager().addEdit(ce);
        panel.markBaseChanged();
        message = Localization.lang("Abbreviated %0 journal names.", String.valueOf(count));
    } else {
        message = Localization.lang("No journal names could be abbreviated.");
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) NamedCompound(org.jabref.gui.undo.NamedCompound)

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