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();
}
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);
}
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();
}
}
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);
}
}
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);
}
}
Aggregations