Search in sources :

Example 31 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class CrossrefFetcherEvaluator method main.

public static void main(String[] args) throws IOException, InterruptedException {
    Globals.prefs = JabRefPreferences.getInstance();
    try (FileReader reader = new FileReader(args[0])) {
        BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
        ParserResult result = parser.parse(reader);
        BibDatabase db = result.getDatabase();
        List<BibEntry> entries = db.getEntries();
        AtomicInteger dois = new AtomicInteger();
        AtomicInteger doiFound = new AtomicInteger();
        AtomicInteger doiNew = new AtomicInteger();
        AtomicInteger doiIdentical = new AtomicInteger();
        int total = entries.size();
        CountDownLatch countDownLatch = new CountDownLatch(total);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (BibEntry entry : entries) {
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    Optional<DOI> origDOI = entry.getField(FieldName.DOI).flatMap(DOI::parse);
                    if (origDOI.isPresent()) {
                        dois.incrementAndGet();
                        try {
                            Optional<DOI> crossrefDOI = new CrossRef().findIdentifier(entry);
                            if (crossrefDOI.isPresent()) {
                                doiFound.incrementAndGet();
                                if (origDOI.get().getDOI().equalsIgnoreCase(crossrefDOI.get().getDOI())) {
                                    doiIdentical.incrementAndGet();
                                } else {
                                    System.out.println("DOI not identical for : " + entry);
                                }
                            } else {
                                System.out.println("DOI not found for: " + entry);
                            }
                        } catch (FetcherException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            Optional<DOI> crossrefDOI = new CrossRef().findIdentifier(entry);
                            if (crossrefDOI.isPresent()) {
                                System.out.println("New DOI found for: " + entry);
                                doiNew.incrementAndGet();
                            }
                        } catch (FetcherException e) {
                            e.printStackTrace();
                        }
                    }
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        System.out.println("---------------------------------");
        System.out.println("Total DB size: " + total);
        System.out.println("Total DOIs: " + dois);
        System.out.println("DOIs found: " + doiFound);
        System.out.println("DOIs identical: " + doiIdentical);
        System.out.println("New DOIs found: " + doiNew);
        executorService.shutdown();
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Optional(java.util.Optional) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) CountDownLatch(java.util.concurrent.CountDownLatch) ParserResult(org.jabref.logic.importer.ParserResult) FetcherException(org.jabref.logic.importer.FetcherException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) FileReader(java.io.FileReader) CrossRef(org.jabref.logic.importer.fetcher.CrossRef) BibDatabase(org.jabref.model.database.BibDatabase)

Example 32 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class EntryEditor method storeSource.

private boolean storeSource() {
    BibtexParser bibtexParser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
    try {
        ParserResult parserResult = bibtexParser.parse(new StringReader(source.getText()));
        BibDatabase database = parserResult.getDatabase();
        if (database.getEntryCount() > 1) {
            throw new IllegalStateException("More than one entry found.");
        }
        if (!database.hasEntries()) {
            if (parserResult.hasWarnings()) {
                // put the warning into as exception text -> it will be displayed to the user
                throw new IllegalStateException(parserResult.warnings().get(0));
            } else {
                throw new IllegalStateException("No entries found.");
            }
        }
        NamedCompound compound = new NamedCompound(Localization.lang("source edit"));
        BibEntry newEntry = database.getEntries().get(0);
        String newKey = newEntry.getCiteKeyOptional().orElse(null);
        boolean entryChanged = false;
        boolean emptyWarning = (newKey == null) || newKey.isEmpty();
        if (newKey != null) {
            entry.setCiteKey(newKey);
        } else {
            entry.clearCiteKey();
        }
        // First, remove fields that the user has removed.
        for (Entry<String, String> field : entry.getFieldMap().entrySet()) {
            String fieldName = field.getKey();
            String fieldValue = field.getValue();
            if (InternalBibtexFields.isDisplayableField(fieldName) && !newEntry.hasField(fieldName)) {
                compound.addEdit(new UndoableFieldChange(entry, fieldName, fieldValue, null));
                entry.clearField(fieldName);
                entryChanged = true;
            }
        }
        // Then set all fields that have been set by the user.
        for (Entry<String, String> field : newEntry.getFieldMap().entrySet()) {
            String fieldName = field.getKey();
            String oldValue = entry.getField(fieldName).orElse(null);
            String newValue = field.getValue();
            if (!Objects.equals(oldValue, newValue)) {
                // Test if the field is legally set.
                new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()).format(newValue, fieldName);
                compound.addEdit(new UndoableFieldChange(entry, fieldName, oldValue, newValue));
                entry.setField(fieldName, newValue);
                entryChanged = true;
            }
        }
        // See if the user has changed the entry type:
        if (!Objects.equals(newEntry.getType(), entry.getType())) {
            compound.addEdit(new UndoableChangeType(entry, entry.getType(), newEntry.getType()));
            entry.setType(newEntry.getType());
            entryChanged = true;
        }
        compound.end();
        if (!entryChanged) {
            return true;
        }
        panel.getUndoManager().addEdit(compound);
        if (panel.getDatabase().getDuplicationChecker().isDuplicateCiteKeyExisting(entry)) {
            warnDuplicateBibtexkey();
        } else if (emptyWarning) {
            warnEmptyBibtexkey();
        } else {
            panel.output(Localization.lang("Stored entry") + '.');
        }
        lastSourceStringAccepted = source.getText();
        // Update UI
        // TODO: we need to repaint the entryeditor if fields that are not displayed have been added
        panel.updateEntryEditorIfShowing();
        lastSourceAccepted = true;
        updateSource = true;
        // TODO: does updating work properly after source stored?
        panel.markBaseChanged();
        panel.highlightEntry(entry);
        return true;
    } catch (InvalidFieldValueException | IOException ex) {
        // The source couldn't be parsed, so the user is given an
        // error message, and the choice to keep or revert the contents
        // of the source text field.
        updateSource = false;
        lastSourceAccepted = false;
        tabbed.setSelectedComponent(srcPanel);
        Object[] options = { Localization.lang("Edit"), Localization.lang("Revert to original source") };
        if (!SwingUtilities.isEventDispatchThread()) {
            int answer = JOptionPane.showOptionDialog(frame, Localization.lang("Error") + ": " + ex.getMessage(), Localization.lang("Problem with parsing entry"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
            if (answer != 0) {
                updateSource = true;
                lastSourceAccepted = true;
                updateSource();
            }
        }
        LOGGER.debug("Incorrect source", ex);
        return false;
    }
}
Also used : UndoableChangeType(org.jabref.gui.undo.UndoableChangeType) TypedBibEntry(org.jabref.logic.TypedBibEntry) BibEntry(org.jabref.model.entry.BibEntry) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) InvalidFieldValueException(org.jabref.logic.bibtex.InvalidFieldValueException) IOException(java.io.IOException) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter) ParserResult(org.jabref.logic.importer.ParserResult) NamedCompound(org.jabref.gui.undo.NamedCompound) StringReader(java.io.StringReader) UndoableFieldChange(org.jabref.gui.undo.UndoableFieldChange) BibDatabase(org.jabref.model.database.BibDatabase)

Example 33 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class INSPIREFetcher method importInspireEntries.

/**
     * Constructs a INSPIRE query url from slaccitation field
     *
     * @param slaccitation
     * @return query string
     *
     *         public static String constructUrlFromSlaccitation(String slaccitation) { String cmd = "j"; String key =
     *         slaccitation.replaceAll("^%%CITATION = ", "").replaceAll( ";%%$", ""); if (key.matches("^\\w*-\\w*[ /].*"
     *         )) cmd = "eprint"; try { key = URLEncoder.encode(key, "UTF-8"); } catch (UnsupportedEncodingException e)
     *         { } StringBuffer sb = new StringBuffer("http://").append(INSPIRE_HOST) .append("/");
     *         sb.append("spires/find/hep/www").append("?"); sb.append("rawcmd=find+").append(cmd).append("+");
     *         sb.append(key); return sb.toString(); }
     *
     *         /** Construct an INSPIRE query url from eprint field
     *
     * @param eprint
     * @return query string
     *
     *         public static String constructUrlFromEprint(String eprint) { String key = eprint.replaceAll(" [.*]$",
     *         ""); try { key = URLEncoder.encode(key, "UTF-8"); } catch (UnsupportedEncodingException e) { return ""; }
     *         StringBuffer sb = new StringBuffer("http://").append(INSPIRE_HOST) .append("/");
     *         sb.append("spires/find/hep/www").append("?"); sb.append("rawcmd=find+eprint+"); sb.append(key); return
     *         sb.toString(); }
     */
/**
     * Import an entry from an OAI2 archive. The BibEntry provided has to have the field OAI2_IDENTIFIER_FIELD set to
     * the search string.
     *
     * @param key The OAI2 key to fetch from ArXiv.
     * @return The imported BibEntry or null if none.
     */
private BibDatabase importInspireEntries(String key) throws IOException {
    String url = constructUrl(key);
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", "JabRef");
    InputStream inputStream = conn.getInputStream();
    try (INSPIREBibtexFilterReader reader = new INSPIREBibtexFilterReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
        ParserResult pr = new BibtexParser(Globals.prefs.getImportFormatPreferences()).parse(reader);
        return pr.getDatabase();
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) INSPIREBibtexFilterReader(org.jabref.logic.importer.util.INSPIREBibtexFilterReader) URL(java.net.URL)

Example 34 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class IEEEXploreFetcher method processQuery.

@Override
public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
    //IEEE API seems to use .QT. as a marker for the quotes for exact phrase searching
    String terms = query.replaceAll("\"", "\\.QT\\.");
    shouldContinue = true;
    int parsed = 0;
    int pageNumber = 1;
    String postData = makeSearchPostRequestPayload(pageNumber, terms);
    try {
        //open the search URL
        URLDownload dl = new URLDownload(IEEEXploreFetcher.URL_SEARCH);
        //add request header
        dl.addHeader("Accept", "application/json");
        dl.addHeader("Content-Type", "application/json");
        dl.addHeader("Referer", "http://ieeexplore.ieee.org/search/searchresult.jsp");
        // set post data
        dl.setPostData(postData);
        //retrieve the search results
        String page = dl.asString();
        //the page can be blank if the search did not work (not sure the exact conditions that lead to this, but declaring it an invalid search for now)
        if (page.isEmpty()) {
            status.showMessage(Localization.lang("You have entered an invalid search '%0'.", query), DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        //parses the JSON data returned by the query
        //TODO: a faster way would be to parse the JSON tokens one at a time just to extract the article number, but this seems to be fast enough...
        JSONObject searchResultsJson = new JSONObject(page);
        int hits = searchResultsJson.getInt("totalRecords");
        //if no search results were found
        if (hits == 0) {
            status.showMessage(Localization.lang("No entries found for the search string '%0'", query), DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        //if max hits were exceeded, display the warning
        if (hits > IEEEXploreFetcher.MAX_FETCH) {
            status.showMessage(Localization.lang("%0 entries found. To reduce server load, only %1 will be downloaded.", String.valueOf(hits), String.valueOf(IEEEXploreFetcher.MAX_FETCH)), DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
        }
        //fetch the raw Bibtex results from IEEEXplore
        String bibtexPage = new URLDownload(createBibtexQueryURL(searchResultsJson)).asString(Globals.prefs.getDefaultEncoding());
        //preprocess the result (eg. convert HTML escaped characters to latex and do other formatting not performed by BibtexParser)
        bibtexPage = preprocessBibtexResultsPage(bibtexPage);
        //parse the page into Bibtex entries
        Collection<BibEntry> parsedBibtexCollection = new BibtexParser(Globals.prefs.getImportFormatPreferences()).parseEntries(bibtexPage);
        int nEntries = parsedBibtexCollection.size();
        Iterator<BibEntry> parsedBibtexCollectionIterator = parsedBibtexCollection.iterator();
        while (parsedBibtexCollectionIterator.hasNext() && shouldContinue) {
            dialog.addEntry(cleanup(parsedBibtexCollectionIterator.next()));
            dialog.setProgress(parsed, nEntries);
            parsed++;
        }
        return true;
    } catch (ParseException | IOException | JSONException e) {
        LOGGER.error("Error while fetching from " + getTitle(), e);
        ((ImportInspectionDialog) dialog).showErrorMessage(this.getTitle(), e.getLocalizedMessage());
    }
    return false;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(org.json.JSONObject) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) JSONException(org.json.JSONException) ParseException(org.jabref.logic.importer.ParseException) IOException(java.io.IOException) URLDownload(org.jabref.logic.net.URLDownload)

Example 35 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class AuxParserTest method testNotAllFound.

@Test
public void testNotAllFound() throws URISyntaxException, IOException {
    InputStream originalStream = AuxParserTest.class.getResourceAsStream("origin.bib");
    File auxFile = Paths.get(AuxParserTest.class.getResource("badpaper.aux").toURI()).toFile();
    try (InputStreamReader originalReader = new InputStreamReader(originalStream, StandardCharsets.UTF_8)) {
        ParserResult result = new BibtexParser(importFormatPreferences).parse(originalReader);
        AuxParser auxParser = new AuxParser(auxFile.getAbsolutePath(), result.getDatabase());
        AuxParserResult auxResult = auxParser.parse();
        assertTrue(auxResult.getGeneratedBibDatabase().hasEntries());
        assertEquals(1, auxResult.getUnresolvedKeysCount());
        BibDatabase newDB = auxResult.getGeneratedBibDatabase();
        assertEquals(2, newDB.getEntries().size());
        assertEquals(2, auxResult.getResolvedKeysCount());
        assertEquals(3, auxResult.getFoundKeysInAux());
        assertEquals(auxResult.getFoundKeysInAux() + auxResult.getCrossRefEntriesCount(), auxResult.getResolvedKeysCount() + auxResult.getUnresolvedKeysCount());
        assertEquals(0, auxResult.getCrossRefEntriesCount());
    }
}
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)

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