use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class ClipBoardManager method extractBibEntriesFromClipboard.
public List<BibEntry> extractBibEntriesFromClipboard() {
// Get clipboard contents, and see if TransferableBibtexEntry is among the content flavors offered
Transferable content = CLIPBOARD.getContents(null);
List<BibEntry> result = new ArrayList<>();
if (content.isDataFlavorSupported(TransferableBibtexEntry.entryFlavor)) {
// We have determined that the clipboard data is a set of entries.
try {
@SuppressWarnings("unchecked") List<BibEntry> contents = (List<BibEntry>) content.getTransferData(TransferableBibtexEntry.entryFlavor);
result = contents;
} catch (UnsupportedFlavorException | ClassCastException ex) {
LOGGER.warn("Could not paste this type", ex);
} catch (IOException ex) {
LOGGER.warn("Could not paste", ex);
}
} else if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
String data = (String) content.getTransferData(DataFlavor.stringFlavor);
// fetch from doi
if (DOI.parse(data).isPresent()) {
LOGGER.info("Found DOI in clipboard");
Optional<BibEntry> entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(new DOI(data).getDOI());
entry.ifPresent(result::add);
} else {
// parse bibtex string
BibtexParser bp = new BibtexParser(Globals.prefs.getImportFormatPreferences());
BibDatabase db = bp.parse(new StringReader(data)).getDatabase();
LOGGER.info("Parsed " + db.getEntryCount() + " entries from clipboard text");
if (db.hasEntries()) {
result = db.getEntries();
}
}
} catch (UnsupportedFlavorException ex) {
LOGGER.warn("Could not parse this type", ex);
} catch (IOException ex) {
LOGGER.warn("Data is no longer available in the requested flavor", ex);
} catch (FetcherException ex) {
LOGGER.error("Error while fetching", ex);
}
}
return result;
}
use of org.jabref.logic.importer.fileformat.BibtexParser 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.fileformat.BibtexParser in project jabref by JabRef.
the class GoogleScholar method downloadEntry.
private BibEntry downloadEntry(String link) throws IOException, FetcherException {
String downloadedContent = new URLDownload(link).asString();
BibtexParser parser = new BibtexParser(importFormatPreferences);
ParserResult result = parser.parse(new StringReader(downloadedContent));
if ((result == null) || (result.getDatabase() == null)) {
throw new FetcherException("Parsing entries from Google Scholar bib file failed.");
} else {
Collection<BibEntry> entries = result.getDatabase().getEntries();
if (entries.size() != 1) {
LOGGER.debug(entries.size() + " entries found! (" + link + ")");
throw new FetcherException("Parsing entries from Google Scholar bib file failed.");
} else {
BibEntry entry = entries.iterator().next();
return entry;
}
}
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class AuxCommandLineTest method test.
@Test
public void test() throws URISyntaxException, IOException {
InputStream originalStream = AuxCommandLineTest.class.getResourceAsStream("origin.bib");
File auxFile = Paths.get(AuxCommandLineTest.class.getResource("paper.aux").toURI()).toFile();
try (InputStreamReader originalReader = new InputStreamReader(originalStream, StandardCharsets.UTF_8)) {
ParserResult result = new BibtexParser(importFormatPreferences).parse(originalReader);
AuxCommandLine auxCommandLine = new AuxCommandLine(auxFile.getAbsolutePath(), result.getDatabase());
BibDatabase newDB = auxCommandLine.perform();
Assert.assertNotNull(newDB);
Assert.assertEquals(2, newDB.getEntries().size());
}
}
use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.
the class EntryFromFileCreatorManagerTest method testAddEntrysFromFiles.
@Test
@Ignore
public void testAddEntrysFromFiles() throws FileNotFoundException, IOException {
try (FileInputStream stream = new FileInputStream(ImportDataTest.UNLINKED_FILES_TEST_BIB);
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
ParserResult result = new BibtexParser(mock(ImportFormatPreferences.class)).parse(reader);
BibDatabase database = result.getDatabase();
List<File> files = new ArrayList<>();
files.add(ImportDataTest.FILE_NOT_IN_DATABASE);
files.add(ImportDataTest.NOT_EXISTING_PDF);
EntryFromFileCreatorManager manager = new EntryFromFileCreatorManager();
List<String> errors = manager.addEntrysFromFiles(files, database, null, true);
/**
* One file doesn't exist, so adding it as an entry should lead to an error message.
*/
Assert.assertEquals(1, errors.size());
boolean file1Found = false;
boolean file2Found = false;
for (BibEntry entry : database.getEntries()) {
String filesInfo = entry.getField("file").get();
if (filesInfo.contains(files.get(0).getName())) {
file1Found = true;
}
if (filesInfo.contains(files.get(1).getName())) {
file2Found = true;
}
}
Assert.assertTrue(file1Found);
Assert.assertFalse(file2Found);
}
}
Aggregations