Search in sources :

Example 61 with BibEntry

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

the class BibDatabaseWriter method applySaveActions.

private static List<FieldChange> applySaveActions(List<BibEntry> toChange, MetaData metaData) {
    List<FieldChange> changes = new ArrayList<>();
    Optional<FieldFormatterCleanups> saveActions = metaData.getSaveActions();
    saveActions.ifPresent(actions -> {
        for (BibEntry entry : toChange) {
            changes.addAll(actions.applySaveActions(entry));
        }
    });
    return changes;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange) FieldFormatterCleanups(org.jabref.model.cleanup.FieldFormatterCleanups)

Example 62 with BibEntry

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

the class IsbnViaChimboriFetcher method performSearchById.

@Override
public Optional<BibEntry> performSearchById(String identifier) throws FetcherException {
    if (StringUtil.isBlank(identifier)) {
        return Optional.empty();
    }
    this.ensureThatIsbnIsValid(identifier);
    HttpResponse<String> postResponse;
    try {
        postResponse = Unirest.post("https://bibtex.chimbori.com/isbn-bibtex").field("isbn", identifier).asString();
    } catch (UnirestException e) {
        throw new FetcherException("Could not retrieve data from chimbori.com", e);
    }
    if (postResponse.getStatus() != 200) {
        throw new FetcherException("Error while retrieving data from chimbori.com: " + postResponse.getBody());
    }
    List<BibEntry> fetchedEntries;
    try {
        fetchedEntries = getParser().parseEntries(postResponse.getRawBody());
    } catch (ParseException e) {
        throw new FetcherException("An internal parser error occurred", e);
    }
    if (fetchedEntries.isEmpty()) {
        return Optional.empty();
    } else if (fetchedEntries.size() > 1) {
        LOGGER.info("Fetcher " + getName() + "found more than one result for identifier " + identifier + ". We will use the first entry.");
    }
    BibEntry entry = fetchedEntries.get(0);
    // chimbori does not return an ISBN. Thus, we add the one searched for
    entry.setField("isbn", identifier);
    doPostCleanup(entry);
    return Optional.of(entry);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) ParseException(org.jabref.logic.importer.ParseException)

Example 63 with BibEntry

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

the class GoogleScholar method downloadEntry.

private BibEntry downloadEntry(String link) throws IOException, FetcherException {
    String downloadedContent = new URLDownload(link).asString();
    BibtexParser parser = new BibtexParser(importFormatPreferences);
    ParserResult result = parser.parse(new StringReader(downloadedContent));
    if ((result == null) || (result.getDatabase() == null)) {
        throw new FetcherException("Parsing entries from Google Scholar bib file failed.");
    } else {
        Collection<BibEntry> entries = result.getDatabase().getEntries();
        if (entries.size() != 1) {
            LOGGER.debug(entries.size() + " entries found! (" + link + ")");
            throw new FetcherException("Parsing entries from Google Scholar bib file failed.");
        } else {
            BibEntry entry = entries.iterator().next();
            return entry;
        }
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) URLDownload(org.jabref.logic.net.URLDownload)

Example 64 with BibEntry

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

the class GoogleScholar method addHitsFromQuery.

private void addHitsFromQuery(List<BibEntry> entryList, String queryURL) throws IOException, FetcherException {
    String content = new URLDownload(queryURL).asString();
    Matcher matcher = LINK_TO_BIB_PATTERN.matcher(content);
    while (matcher.find()) {
        String citationsPageURL = matcher.group().replace("&amp;", "&");
        BibEntry newEntry = downloadEntry(citationsPageURL);
        entryList.add(newEntry);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Matcher(java.util.regex.Matcher) URLDownload(org.jabref.logic.net.URLDownload)

Example 65 with BibEntry

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

the class IdParserFetcher method findIdentifier.

@Override
default default Optional<T> findIdentifier(BibEntry entry) throws FetcherException {
    Objects.requireNonNull(entry);
    try (InputStream stream = new BufferedInputStream(getURLForEntry(entry).openStream())) {
        List<BibEntry> fetchedEntries = getParser().parseEntries(stream);
        if (fetchedEntries.isEmpty()) {
            return Optional.empty();
        }
        // Post-cleanup
        fetchedEntries.forEach(this::doPostCleanup);
        return extractIdentifier(entry, fetchedEntries);
    } catch (URISyntaxException e) {
        throw new FetcherException("Search URI is malformed", e);
    } catch (FileNotFoundException e) {
        LOGGER.debug("Id not found");
        return Optional.empty();
    } catch (IOException e) {
        // TODO catch 503 service unavailable and alert user
        throw new FetcherException("An I/O exception occurred", e);
    } catch (ParseException e) {
        throw new FetcherException("An internal parser error occurred", e);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

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