Search in sources :

Example 81 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class RepecNepImporter method importDatabase.

@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
    Objects.requireNonNull(reader);
    List<BibEntry> bibitems = new ArrayList<>();
    String paperNoStr = null;
    this.line = 0;
    try {
        // skip header and editor information
        readLine(reader);
        while (this.lastLine != null) {
            if (this.lastLine.startsWith("-----------------------------")) {
                this.inOverviewSection = this.preLine.startsWith("In this issue we have");
            }
            if (isStartOfWorkingPaper()) {
                BibEntry be = new BibEntry();
                be.setType("techreport");
                paperNoStr = this.lastLine.substring(0, this.lastLine.indexOf('.'));
                parseTitleString(be, reader);
                if (startsWithKeyword(RepecNepImporter.RECOGNIZED_FIELDS)) {
                    parseAdditionalFields(be, false, reader);
                } else {
                    // skip empty line
                    readLine(reader);
                    parseAuthors(be, reader);
                    // skip empty line
                    readLine(reader);
                }
                if (!startsWithKeyword(RepecNepImporter.RECOGNIZED_FIELDS)) {
                    parseAbstract(be, reader);
                }
                parseAdditionalFields(be, true, reader);
                bibitems.add(be);
                paperNoStr = null;
            } else {
                this.preLine = this.lastLine;
                readLine(reader);
            }
        }
    } catch (Exception e) {
        String message = "Error in REPEC-NEP import on line " + this.line;
        if (paperNoStr != null) {
            message += ", paper no. " + paperNoStr + ": ";
        }
        message += e.getLocalizedMessage();
        LOGGER.error(message, e);
        return ParserResult.fromErrorMessage(message);
    }
    return new ParserResult(bibitems);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ParserResult(org.jabref.logic.importer.ParserResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 82 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class SilverPlatterImporter method importDatabase.

@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
    List<BibEntry> bibitems = new ArrayList<>();
    boolean isChapter = false;
    String str;
    StringBuilder sb = new StringBuilder();
    while ((str = reader.readLine()) != null) {
        if (str.length() < 2) {
            sb.append("__::__").append(str);
        } else {
            sb.append("__NEWFIELD__").append(str);
        }
    }
    String[] entries = sb.toString().split("__::__");
    String type = "";
    Map<String, String> h = new HashMap<>();
    for (String entry : entries) {
        if (entry.trim().length() < 6) {
            continue;
        }
        h.clear();
        String[] fields = entry.split("__NEWFIELD__");
        for (String field : fields) {
            if (field.length() < 6) {
                continue;
            }
            String f3 = field.substring(0, 2);
            String frest = field.substring(5);
            if ("TI".equals(f3)) {
                h.put(FieldName.TITLE, frest);
            } else if ("AU".equals(f3)) {
                if (frest.trim().endsWith("(ed)")) {
                    String ed = frest.trim();
                    ed = ed.substring(0, ed.length() - 4);
                    h.put(FieldName.EDITOR, AuthorList.fixAuthorLastNameFirst(ed.replace(",-", ", ").replace(";", " and ")));
                } else {
                    h.put(FieldName.AUTHOR, AuthorList.fixAuthorLastNameFirst(frest.replace(",-", ", ").replace(";", " and ")));
                }
            } else if ("AB".equals(f3)) {
                h.put(FieldName.ABSTRACT, frest);
            } else if ("DE".equals(f3)) {
                String kw = frest.replace("-;", ",").toLowerCase(Locale.ROOT);
                h.put(FieldName.KEYWORDS, kw.substring(0, kw.length() - 1));
            } else if ("SO".equals(f3)) {
                int m = frest.indexOf('.');
                if (m >= 0) {
                    String jr = frest.substring(0, m);
                    h.put(FieldName.JOURNAL, jr.replace("-", " "));
                    frest = frest.substring(m);
                    m = frest.indexOf(';');
                    if (m >= 5) {
                        String yr = frest.substring(m - 5, m).trim();
                        h.put(FieldName.YEAR, yr);
                        frest = frest.substring(m);
                        m = frest.indexOf(':');
                        int issueIndex = frest.indexOf('(');
                        int endIssueIndex = frest.indexOf(')');
                        if (m >= 0) {
                            String pg = frest.substring(m + 1).trim();
                            h.put(FieldName.PAGES, pg);
                            h.put(FieldName.VOLUME, frest.substring(1, issueIndex).trim());
                            h.put(FieldName.ISSUE, frest.substring(issueIndex + 1, endIssueIndex).trim());
                        }
                    }
                }
            } else if ("PB".equals(f3)) {
                int m = frest.indexOf(':');
                if (m >= 0) {
                    String jr = frest.substring(0, m);
                    h.put(FieldName.PUBLISHER, jr.replace("-", " ").trim());
                    frest = frest.substring(m);
                    m = frest.indexOf(", ");
                    if ((m + 2) < frest.length()) {
                        String yr = frest.substring(m + 2).trim();
                        try {
                            Integer.parseInt(yr);
                            h.put(FieldName.YEAR, yr);
                        } catch (NumberFormatException ex) {
                        // Let's assume that this wasn't a number, since it
                        // couldn't be parsed as an integer.
                        }
                    }
                }
            } else if ("AF".equals(f3)) {
                h.put(FieldName.SCHOOL, frest.trim());
            } else if ("DT".equals(f3)) {
                frest = frest.trim();
                if ("Monograph".equals(frest)) {
                    type = "book";
                } else if (frest.startsWith("Dissertation")) {
                    type = "phdthesis";
                } else if (frest.toLowerCase(Locale.ROOT).contains(FieldName.JOURNAL)) {
                    type = "article";
                } else if ("Contribution".equals(frest) || "Chapter".equals(frest)) {
                    type = "incollection";
                    // This entry type contains page numbers and booktitle in the
                    // title field.
                    isChapter = true;
                } else {
                    type = frest.replace(" ", "");
                }
            }
        }
        if (isChapter) {
            String titleO = h.get(FieldName.TITLE);
            if (titleO != null) {
                String title = titleO.trim();
                int inPos = title.indexOf("\" in ");
                if (inPos > 1) {
                    h.put(FieldName.TITLE, title.substring(0, inPos));
                }
            }
        }
        BibEntry b = new BibEntry(type);
        // create one here
        b.setField(h);
        bibitems.add(b);
    }
    return new ParserResult(bibitems);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ParserResult(org.jabref.logic.importer.ParserResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 83 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class ModsImporter method importDatabase.

@Override
public ParserResult importDatabase(BufferedReader input) throws IOException {
    Objects.requireNonNull(input);
    List<BibEntry> bibItems = new ArrayList<>();
    try {
        if (context == null) {
            context = JAXBContext.newInstance("org.jabref.logic.importer.fileformat.mods");
        }
        Unmarshaller unmarshaller = context.createUnmarshaller();
        //The unmarshalled object is a jaxbElement.
        JAXBElement<?> unmarshalledObject = (JAXBElement<?>) unmarshaller.unmarshal(input);
        Optional<ModsCollectionDefinition> collection = getElement(unmarshalledObject.getValue(), ModsCollectionDefinition.class);
        Optional<ModsDefinition> mods = getElement(unmarshalledObject.getValue(), ModsDefinition.class);
        if (collection.isPresent()) {
            List<ModsDefinition> modsDefinitions = collection.get().getMods();
            parseModsCollection(bibItems, modsDefinitions);
        } else if (mods.isPresent()) {
            ModsDefinition modsDefinition = mods.get();
            parseMods(bibItems, modsDefinition);
        } else {
            LOGGER.warn("Not expected root element found");
        }
    } catch (JAXBException e) {
        LOGGER.debug("could not parse document", e);
        return ParserResult.fromError(e);
    }
    return new ParserResult(bibItems);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ModsCollectionDefinition(org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition) ModsDefinition(org.jabref.logic.importer.fileformat.mods.ModsDefinition) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBElement(javax.xml.bind.JAXBElement) ParserResult(org.jabref.logic.importer.ParserResult) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 84 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class JSONEntryParser method parseBibJSONtoBibtex.

/**
     * Convert a JSONObject containing a bibJSON entry to a BibEntry
     *
     * @param bibJsonEntry The JSONObject to convert
     * @return the converted BibEntry
     */
public BibEntry parseBibJSONtoBibtex(JSONObject bibJsonEntry, Character keywordSeparator) {
    // Fields that are directly accessible at the top level BibJson object
    String[] singleFieldStrings = { FieldName.YEAR, FieldName.TITLE, FieldName.ABSTRACT, FieldName.MONTH };
    // Fields that are accessible in the journal part of the BibJson object
    String[] journalSingleFieldStrings = { FieldName.PUBLISHER, FieldName.NUMBER, FieldName.VOLUME };
    BibEntry entry = new BibEntry();
    entry.setType("article");
    // Authors
    if (bibJsonEntry.has("author")) {
        JSONArray authors = bibJsonEntry.getJSONArray("author");
        List<String> authorList = new ArrayList<>();
        for (int i = 0; i < authors.length(); i++) {
            if (authors.getJSONObject(i).has("name")) {
                authorList.add(authors.getJSONObject(i).getString("name"));
            } else {
                LOGGER.info("Empty author name.");
            }
        }
        entry.setField(FieldName.AUTHOR, String.join(" and ", authorList));
    } else {
        LOGGER.info("No author found.");
    }
    // Direct accessible fields
    for (String field : singleFieldStrings) {
        if (bibJsonEntry.has(field)) {
            entry.setField(field, bibJsonEntry.getString(field));
        }
    }
    // Page numbers
    if (bibJsonEntry.has("start_page")) {
        if (bibJsonEntry.has("end_page")) {
            entry.setField(FieldName.PAGES, bibJsonEntry.getString("start_page") + "--" + bibJsonEntry.getString("end_page"));
        } else {
            entry.setField(FieldName.PAGES, bibJsonEntry.getString("start_page"));
        }
    }
    // Journal
    if (bibJsonEntry.has("journal")) {
        JSONObject journal = bibJsonEntry.getJSONObject("journal");
        // Journal title
        if (journal.has("title")) {
            entry.setField(FieldName.JOURNAL, journal.getString("title"));
        } else {
            LOGGER.info("No journal title found.");
        }
        // Other journal related fields
        for (String field : journalSingleFieldStrings) {
            if (journal.has(field)) {
                entry.setField(field, journal.getString(field));
            }
        }
    } else {
        LOGGER.info("No journal information found.");
    }
    // Keywords
    if (bibJsonEntry.has("keywords")) {
        JSONArray keywords = bibJsonEntry.getJSONArray("keywords");
        for (int i = 0; i < keywords.length(); i++) {
            if (!keywords.isNull(i)) {
                entry.addKeyword(keywords.getString(i), keywordSeparator);
            }
        }
    }
    // Identifiers
    if (bibJsonEntry.has("identifier")) {
        JSONArray identifiers = bibJsonEntry.getJSONArray("identifier");
        for (int i = 0; i < identifiers.length(); i++) {
            String type = identifiers.getJSONObject(i).getString("type");
            if ("doi".equals(type)) {
                entry.setField(FieldName.DOI, identifiers.getJSONObject(i).getString("id"));
            } else if ("pissn".equals(type)) {
                entry.setField(FieldName.ISSN, identifiers.getJSONObject(i).getString("id"));
            } else if ("eissn".equals(type)) {
                entry.setField(FieldName.ISSN, identifiers.getJSONObject(i).getString("id"));
            }
        }
    }
    // Links
    if (bibJsonEntry.has("link")) {
        JSONArray links = bibJsonEntry.getJSONArray("link");
        for (int i = 0; i < links.length(); i++) {
            if (links.getJSONObject(i).has("type")) {
                String type = links.getJSONObject(i).getString("type");
                if ("fulltext".equals(type) && links.getJSONObject(i).has("url")) {
                    entry.setField(FieldName.URL, links.getJSONObject(i).getString("url"));
                }
            }
        }
    }
    return entry;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList)

Example 85 with BibEntry

use of org.jabref.model.entry.BibEntry in project jabref by JabRef.

the class MrDLibImporter method parse.

/**
     * Parses the input from the server to a ParserResult
     * @param input A BufferedReader with a reference to a string with the servers response
     * @throws IOException
     */
private void parse(BufferedReader input) throws IOException {
    // The Bibdatabase that gets returned in the ParserResult.
    BibDatabase bibDatabase = new BibDatabase();
    // The document to parse
    String recommendations = convertToString(input);
    // The sorted BibEntries gets stored here later
    List<BibEntry> bibEntries = new ArrayList<>();
    //Parsing the response with a SAX parser
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        MrDlibImporterHandler handler = new MrDlibImporterHandler();
        try (InputStream stream = new ByteArrayInputStream(recommendations.getBytes())) {
            saxParser.parse(stream, handler);
        } catch (SAXException e) {
            LOGGER.error(e.getMessage(), e);
        }
        List<RankedBibEntry> rankedBibEntries = handler.getRankedBibEntries();
        rankedBibEntries.sort((RankedBibEntry rankedBibEntry1, RankedBibEntry rankedBibEntry2) -> rankedBibEntry1.rank.compareTo(rankedBibEntry2.rank));
        bibEntries = rankedBibEntries.stream().map(e -> e.entry).collect(Collectors.toList());
    } catch (ParserConfigurationException | SAXException e) {
        LOGGER.error(e.getMessage(), e);
    }
    for (BibEntry bibentry : bibEntries) {
        bibDatabase.insertEntry(bibentry);
    }
    parserResult = new ParserResult(bibDatabase);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) ParserResult(org.jabref.logic.importer.ParserResult) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BibDatabase(org.jabref.model.database.BibDatabase) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

BibEntry (org.jabref.model.entry.BibEntry)716 Test (org.junit.Test)466 ParserResult (org.jabref.logic.importer.ParserResult)131 StringReader (java.io.StringReader)107 ArrayList (java.util.ArrayList)75 BibDatabase (org.jabref.model.database.BibDatabase)63 Path (java.nio.file.Path)52 IOException (java.io.IOException)43 HashMap (java.util.HashMap)37 Before (org.junit.Before)36 NamedCompound (org.jabref.gui.undo.NamedCompound)30 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)28 BibtexString (org.jabref.model.entry.BibtexString)28 List (java.util.List)23 File (java.io.File)21 StringWriter (java.io.StringWriter)19 Optional (java.util.Optional)19 BasePanel (org.jabref.gui.BasePanel)19 FieldChange (org.jabref.model.FieldChange)18 InputStream (java.io.InputStream)16