Search in sources :

Example 1 with MrDLibImporter

use of org.jabref.logic.importer.fileformat.MrDLibImporter in project jabref by JabRef.

the class MrDLibFetcher method performSearch.

@Override
public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
    Optional<String> title = entry.getLatexFreeField(FieldName.TITLE);
    if (title.isPresent()) {
        String response = makeServerRequest(title.get());
        MrDLibImporter importer = new MrDLibImporter();
        ParserResult parserResult = new ParserResult();
        try {
            if (importer.isRecognizedFormat(new BufferedReader(new StringReader(response)))) {
                parserResult = importer.importDatabase(new BufferedReader(new StringReader(response)));
            } else {
                // For displaying An ErrorMessage
                BibEntry errorBibEntry = new BibEntry();
                errorBibEntry.setField("html_representation", Localization.lang("Error_while_fetching_from_%0", "Mr.DLib"));
                BibDatabase errorBibDataBase = new BibDatabase();
                errorBibDataBase.insertEntry(errorBibEntry);
                parserResult = new ParserResult(errorBibDataBase);
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw new FetcherException("XML Parser IOException.");
        }
        return parserResult.getDatabase().getEntries();
    } else {
        // without a title there is no reason to ask MrDLib
        return new ArrayList<>(0);
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) MrDLibImporter(org.jabref.logic.importer.fileformat.MrDLibImporter) IOException(java.io.IOException) BibDatabase(org.jabref.model.database.BibDatabase)

Aggregations

BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 FetcherException (org.jabref.logic.importer.FetcherException)1 ParserResult (org.jabref.logic.importer.ParserResult)1 MrDLibImporter (org.jabref.logic.importer.fileformat.MrDLibImporter)1 BibDatabase (org.jabref.model.database.BibDatabase)1 BibEntry (org.jabref.model.entry.BibEntry)1