Search in sources :

Example 6 with ParseException

use of org.jabref.logic.importer.ParseException in project jabref by JabRef.

the class CrossRef method jsonItemToBibEntry.

private BibEntry jsonItemToBibEntry(JSONObject item) throws ParseException {
    try {
        BibEntry entry = new BibEntry();
        entry.setType(convertType(item.getString("type")));
        entry.setField(FieldName.TITLE, item.getJSONArray("title").optString(0));
        entry.setField(FieldName.SUBTITLE, Optional.ofNullable(item.optJSONArray("subtitle")).map(array -> array.optString(0)).orElse(""));
        entry.setField(FieldName.AUTHOR, toAuthors(item.optJSONArray("author")));
        entry.setField(FieldName.YEAR, Optional.ofNullable(item.optJSONObject("published-print")).map(array -> array.optJSONArray("date-parts")).map(array -> array.optJSONArray(0)).map(array -> array.optInt(0)).map(year -> Integer.toString(year)).orElse(""));
        entry.setField(FieldName.DOI, item.getString("DOI"));
        entry.setField(FieldName.PAGES, item.optString("page"));
        entry.setField(FieldName.VOLUME, item.optString("volume"));
        entry.setField(FieldName.ISSN, Optional.ofNullable(item.optJSONArray("ISSN")).map(array -> array.getString(0)).orElse(""));
        return entry;
    } catch (JSONException exception) {
        throw new ParseException("CrossRef API JSON format has changed", exception);
    }
}
Also used : StringSimilarity(org.jabref.logic.util.strings.StringSimilarity) EntryBasedParserFetcher(org.jabref.logic.importer.EntryBasedParserFetcher) FieldName(org.jabref.model.entry.FieldName) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) SearchBasedParserFetcher(org.jabref.logic.importer.SearchBasedParserFetcher) OptionalUtil(org.jabref.model.util.OptionalUtil) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JsonReader(org.jabref.logic.importer.util.JsonReader) FieldFormatterCleanup(org.jabref.model.cleanup.FieldFormatterCleanup) IdBasedParserFetcher(org.jabref.logic.importer.IdBasedParserFetcher) MalformedURLException(java.net.MalformedURLException) URIBuilder(org.apache.http.client.utils.URIBuilder) ClearFormatter(org.jabref.logic.formatter.bibtexfields.ClearFormatter) BiblatexEntryTypes(org.jabref.model.entry.BiblatexEntryTypes) FetcherException(org.jabref.logic.importer.FetcherException) BibEntry(org.jabref.model.entry.BibEntry) Parser(org.jabref.logic.importer.Parser) ParseException(org.jabref.logic.importer.ParseException) AuthorList(org.jabref.model.entry.AuthorList) List(java.util.List) DOI(org.jabref.model.entry.identifier.DOI) Optional(java.util.Optional) EntryType(org.jabref.model.entry.EntryType) RemoveBracesFormatter(org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter) JSONArray(org.json.JSONArray) IdParserFetcher(org.jabref.logic.importer.IdParserFetcher) BibEntry(org.jabref.model.entry.BibEntry) JSONException(org.json.JSONException) ParseException(org.jabref.logic.importer.ParseException)

Example 7 with ParseException

use of org.jabref.logic.importer.ParseException in project jabref by JabRef.

the class DoiFetcher method performSearchById.

@Override
public Optional<BibEntry> performSearchById(String identifier) throws FetcherException {
    Optional<DOI> doi = DOI.parse(identifier);
    try {
        if (doi.isPresent()) {
            URL doiURL = new URL(doi.get().getURIAsASCIIString());
            // BibTeX data
            URLDownload download = new URLDownload(doiURL);
            download.addHeader("Accept", "application/x-bibtex");
            String bibtexString = download.asString();
            // BibTeX entry
            Optional<BibEntry> fetchedEntry = BibtexParser.singleFromString(bibtexString, preferences);
            fetchedEntry.ifPresent(this::doPostCleanup);
            return fetchedEntry;
        } else {
            throw new FetcherException(Localization.lang("Invalid_DOI:_'%0'.", identifier));
        }
    } catch (IOException e) {
        throw new FetcherException(Localization.lang("Connection error"), e);
    } catch (ParseException e) {
        throw new FetcherException("Could not parse BibTeX entry", e);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) IOException(java.io.IOException) ParseException(org.jabref.logic.importer.ParseException) URLDownload(org.jabref.logic.net.URLDownload) URL(java.net.URL) DOI(org.jabref.model.entry.identifier.DOI)

Example 8 with ParseException

use of org.jabref.logic.importer.ParseException in project jabref by JabRef.

the class AstrophysicsDataSystem method performSearch.

@Override
public List<BibEntry> performSearch(String query) throws FetcherException {
    if (StringUtil.isBlank(query)) {
        return Collections.emptyList();
    }
    try {
        URLConnection connection = getURLForQuery(query).openConnection();
        connection.setRequestProperty("User-Agent", URLDownload.USER_AGENT);
        try (InputStream stream = connection.getInputStream()) {
            List<BibEntry> fetchedEntries = getParser().parseEntries(stream);
            // Post-cleanup
            fetchedEntries.forEach(this::doPostCleanup);
            return fetchedEntries;
        } catch (IOException e) {
            throw new FetcherException("An I/O exception occurred", e);
        }
    } catch (URISyntaxException | MalformedURLException e) {
        throw new FetcherException("Search URI is malformed", e);
    } catch (IOException e) {
        throw new FetcherException("An I/O exception occurred", e);
    } catch (ParseException e) {
        throw new FetcherException("Error occurred when parsing entry", Localization.lang("Error occurred when parsing entry"), e);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ParseException(org.jabref.logic.importer.ParseException) URLConnection(java.net.URLConnection)

Example 9 with ParseException

use of org.jabref.logic.importer.ParseException in project jabref by JabRef.

the class BibtexParser method parseFileContent.

private ParserResult parseFileContent() throws IOException {
    Map<String, String> meta = new HashMap<>();
    while (!eof) {
        boolean found = consumeUncritically('@');
        if (!found) {
            break;
        }
        skipWhitespace();
        // Try to read the entry type
        String entryType = parseTextToken().toLowerCase(Locale.ROOT).trim();
        if ("preamble".equals(entryType)) {
            database.setPreamble(parsePreamble());
            // Consume new line which signals end of preamble
            skipOneNewline();
            // the preamble is saved verbatim anyways, so the text read so far can be dropped
            dumpTextReadSoFarToString();
        } else if ("string".equals(entryType)) {
            parseBibtexString();
        } else if ("comment".equals(entryType)) {
            parseJabRefComment(meta);
        } else {
            // Not a comment, preamble, or string. Thus, it is an entry
            parseAndAddEntry(entryType);
        }
        skipWhitespace();
    }
    // Instantiate meta data:
    try {
        parserResult.setMetaData(MetaDataParser.parse(meta, importFormatPreferences.getKeywordSeparator()));
    } catch (ParseException exception) {
        parserResult.addException(exception);
    }
    parseRemainingContent();
    return parserResult;
}
Also used : HashMap(java.util.HashMap) BibtexString(org.jabref.model.entry.BibtexString) ParseException(org.jabref.logic.importer.ParseException)

Example 10 with ParseException

use of org.jabref.logic.importer.ParseException in project jabref by JabRef.

the class GvkParser method parseEntries.

@Override
public List<BibEntry> parseEntries(InputStream inputStream) throws ParseException {
    try {
        DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document content = dbuild.parse(inputStream);
        return this.parseEntries(content);
    } catch (ParserConfigurationException | SAXException | IOException exception) {
        throw new ParseException(exception);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) ParseException(org.jabref.logic.importer.ParseException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

ParseException (org.jabref.logic.importer.ParseException)14 IOException (java.io.IOException)8 BibEntry (org.jabref.model.entry.BibEntry)6 URL (java.net.URL)4 FetcherException (org.jabref.logic.importer.FetcherException)4 URLDownload (org.jabref.logic.net.URLDownload)4 MalformedURLException (java.net.MalformedURLException)3 BufferedReader (java.io.BufferedReader)2 URISyntaxException (java.net.URISyntaxException)2 URLConnection (java.net.URLConnection)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)2 QuotedStringTokenizer (org.jabref.logic.util.strings.QuotedStringTokenizer)2 DOI (org.jabref.model.entry.identifier.DOI)2 ExplicitGroup (org.jabref.model.groups.ExplicitGroup)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 Dimension (java.awt.Dimension)1