Search in sources :

Example 1 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class EntryTableTransferHandler method handleDropTransfer.

private boolean handleDropTransfer(URL dropLink) throws IOException {
    File tmpfile = File.createTempFile("jabrefimport", "");
    tmpfile.deleteOnExit();
    new URLDownload(dropLink).toFile(tmpfile.toPath());
    // Import into new if entryTable==null, otherwise into current library:
    ImportMenuItem importer = new ImportMenuItem(frame, entryTable == null);
    importer.automatedImport(Collections.singletonList(tmpfile.getAbsolutePath()));
    return true;
}
Also used : URLDownload(org.jabref.logic.net.URLDownload) File(java.io.File) ImportMenuItem(org.jabref.gui.importer.ImportMenuItem)

Example 2 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class CiteSeerXFetcher method getSingleCitation.

private static BibEntry getSingleCitation(String urlString) throws IOException {
    String cont = new URLDownload(urlString).asString();
    // Find title, and create entry if we do. Otherwise assume we did not get an entry:
    Matcher m = CiteSeerXFetcher.TITLE_PATTERN.matcher(cont);
    if (m.find()) {
        BibEntry entry = new BibEntry();
        entry.setField(FieldName.TITLE, m.group(1));
        // Find authors:
        m = CiteSeerXFetcher.AUTHOR_PATTERN.matcher(cont);
        if (m.find()) {
            String authors = m.group(1);
            entry.setField(FieldName.AUTHOR, new NormalizeNamesFormatter().format(authors));
        }
        // Find year:
        m = CiteSeerXFetcher.YEAR_PATTERN.matcher(cont);
        if (m.find()) {
            entry.setField(FieldName.YEAR, m.group(1));
        }
        // Find abstract:
        m = CiteSeerXFetcher.ABSTRACT_PATTERN.matcher(cont);
        if (m.find()) {
            entry.setField(FieldName.ABSTRACT, m.group(1));
        }
        return entry;
    } else {
        return null;
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Matcher(java.util.regex.Matcher) NormalizeNamesFormatter(org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter) URLDownload(org.jabref.logic.net.URLDownload)

Example 3 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class CiteSeerXFetcher method getCitationsFromUrl.

private static String getCitationsFromUrl(String urlQuery, List<String> ids) throws IOException {
    String cont = new URLDownload(urlQuery).asString(Globals.prefs.getDefaultEncoding());
    Matcher m = CiteSeerXFetcher.CITE_LINK_PATTERN.matcher(cont);
    while (m.find()) {
        ids.add(CiteSeerXFetcher.URL_START + m.group(1));
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) URLDownload(org.jabref.logic.net.URLDownload)

Example 4 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class ArgumentProcessor method importFile.

private static Optional<ParserResult> importFile(String argument) {
    String[] data = argument.split(",");
    String address = data[0];
    Path file;
    if (address.startsWith("http://") || address.startsWith("https://") || address.startsWith("ftp://")) {
        // Download web resource to temporary file
        try {
            file = new URLDownload(address).toTemporaryFile();
        } catch (IOException e) {
            System.err.println(Localization.lang("Problem downloading from %1", address) + e.getLocalizedMessage());
            return Optional.empty();
        }
    } else {
        if (OS.WINDOWS) {
            file = Paths.get(address);
        } else {
            file = Paths.get(address.replace("~", System.getProperty("user.home")));
        }
    }
    String importFormat;
    if (data.length > 1) {
        importFormat = data[1];
    } else {
        importFormat = "*";
    }
    Optional<ParserResult> importResult = importFile(file, importFormat);
    importResult.ifPresent(result -> {
        OutputPrinter printer = new SystemOutputPrinter();
        if (result.hasWarnings()) {
            printer.showMessage(result.getErrorMessage());
        }
    });
    return importResult;
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload) OutputPrinter(org.jabref.logic.importer.OutputPrinter)

Example 5 with URLDownload

use of org.jabref.logic.net.URLDownload in project jabref by JabRef.

the class ACMPortalFetcher method processQueryGetPreview.

@Override
public boolean processQueryGetPreview(String query, FetcherPreviewDialog preview, OutputPrinter status) {
    this.terms = query;
    piv = 0;
    shouldContinue = true;
    acmOrGuide = acmButton.isSelected();
    fetchAbstract = absCheckBox.isSelected();
    String address = makeUrl();
    LinkedHashMap<String, JLabel> previews = new LinkedHashMap<>();
    try {
        URLDownload dl = new URLDownload(address);
        String page = dl.asString(Globals.prefs.getDefaultEncoding());
        int hits = getNumberOfHits(page, RESULTS_FOUND_PATTERN, ACMPortalFetcher.HITS_PATTERN);
        int index = page.indexOf(RESULTS_FOUND_PATTERN);
        if (index >= 0) {
            page = page.substring(index + RESULTS_FOUND_PATTERN.length());
        }
        if (hits == 0) {
            status.showMessage(Localization.lang("No entries found for the search string '%0'", terms), Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
            return false;
        } else if (hits > 20) {
            status.showMessage(Localization.lang("%0 entries found. To reduce server load, only %1 will be downloaded.", String.valueOf(hits), String.valueOf(PER_PAGE)), Localization.lang("Search %0", getTitle()), JOptionPane.INFORMATION_MESSAGE);
        }
        hits = getNumberOfHits(page, PAGE_RANGE_PATTERN, ACMPortalFetcher.MAX_HITS_PATTERN);
        parse(page, Math.min(hits, PER_PAGE), previews);
        for (Map.Entry<String, JLabel> entry : previews.entrySet()) {
            preview.addEntry(entry.getKey(), entry.getValue());
        }
        return true;
    } catch (IOException e) {
        LOGGER.error("Error while fetching from " + getTitle(), e);
        preview.showErrorMessage(this.getTitle(), e.getLocalizedMessage());
        return false;
    }
}
Also used : JLabel(javax.swing.JLabel) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

URLDownload (org.jabref.logic.net.URLDownload)19 IOException (java.io.IOException)11 URL (java.net.URL)7 BibEntry (org.jabref.model.entry.BibEntry)7 Matcher (java.util.regex.Matcher)5 ParseException (org.jabref.logic.importer.ParseException)4 FetcherException (org.jabref.logic.importer.FetcherException)3 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)3 DOI (org.jabref.model.entry.identifier.DOI)3 File (java.io.File)2 Path (java.nio.file.Path)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 JLabel (javax.swing.JLabel)2 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)2 OutputPrinter (org.jabref.logic.importer.OutputPrinter)2 ParserResult (org.jabref.logic.importer.ParserResult)2 Dimension (java.awt.Dimension)1 GridLayout (java.awt.GridLayout)1