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