Search in sources :

Example 1 with ParseException

use of org.jabref.logic.importer.ParseException 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 2 with ParseException

use of org.jabref.logic.importer.ParseException 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 3 with ParseException

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

the class BibsonomyScraper method getEntry.

/**
     * Return a BibEntry by looking up the given url from the BibSonomy scraper.
     * @param entryUrl
     * @return
     */
public static Optional<BibEntry> getEntry(String entryUrl, ImportFormatPreferences importFormatPreferences) {
    try {
        // Replace special characters by corresponding sequences:
        String cleanURL = entryUrl.replace("%", "%25").replace(":", "%3A").replace("/", "%2F").replace("?", "%3F").replace("&", "%26").replace("=", "%3D");
        URL url = new URL(BibsonomyScraper.BIBSONOMY_SCRAPER + cleanURL + BibsonomyScraper.BIBSONOMY_SCRAPER_POST);
        String bibtex = new URLDownload(url).asString();
        return BibtexParser.singleFromString(bibtex, importFormatPreferences);
    } catch (IOException ex) {
        LOGGER.warn("Could not download entry", ex);
        return Optional.empty();
    } catch (ParseException ex) {
        LOGGER.warn("Could not parse entry", ex);
        return Optional.empty();
    } catch (RuntimeException ex) {
        LOGGER.warn("Could not get entry", ex);
        return Optional.empty();
    }
}
Also used : IOException(java.io.IOException) ParseException(org.jabref.logic.importer.ParseException) URLDownload(org.jabref.logic.net.URLDownload) URL(java.net.URL)

Example 4 with ParseException

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

the class JsonReader method toJsonObject.

public static JSONObject toJsonObject(InputStreamReader input) throws ParseException {
    BufferedReader streamReader = new BufferedReader(input);
    StringBuilder responseStrBuilder = new StringBuilder();
    try {
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null) {
            responseStrBuilder.append(inputStr);
        }
        return new JSONObject(responseStrBuilder.toString());
    } catch (IOException e) {
        throw new ParseException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ParseException(org.jabref.logic.importer.ParseException)

Example 5 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)

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