Search in sources :

Example 1 with BibtexParser

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;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) DoiFetcher(org.jabref.logic.importer.fetcher.DoiFetcher) Optional(java.util.Optional) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) FetcherException(org.jabref.logic.importer.FetcherException) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) BibDatabase(org.jabref.model.database.BibDatabase) DOI(org.jabref.model.entry.identifier.DOI)

Example 2 with BibtexParser

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();
}
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 3 with BibtexParser

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;
        }
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) FetcherException(org.jabref.logic.importer.FetcherException) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) URLDownload(org.jabref.logic.net.URLDownload)

Example 4 with BibtexParser

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());
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) File(java.io.File) BibDatabase(org.jabref.model.database.BibDatabase) Test(org.junit.Test)

Example 5 with BibtexParser

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);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) InputStreamReader(java.io.InputStreamReader) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) ParserResult(org.jabref.logic.importer.ParserResult) BibDatabase(org.jabref.model.database.BibDatabase) File(java.io.File) Ignore(org.junit.Ignore) ImportDataTest(org.jabref.logic.importer.ImportDataTest) Test(org.junit.Test)

Aggregations

BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)43 ParserResult (org.jabref.logic.importer.ParserResult)38 Test (org.junit.Test)28 BibEntry (org.jabref.model.entry.BibEntry)27 StringReader (java.io.StringReader)20 BibDatabase (org.jabref.model.database.BibDatabase)13 InputStreamReader (java.io.InputStreamReader)12 StringWriter (java.io.StringWriter)12 File (java.io.File)8 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 Charset (java.nio.charset.Charset)6 Defaults (org.jabref.model.Defaults)6 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)6 Scanner (java.util.Scanner)5 IOException (java.io.IOException)4 FileInputStream (java.io.FileInputStream)3 Collection (java.util.Collection)3 Optional (java.util.Optional)3 FetcherException (org.jabref.logic.importer.FetcherException)3