Search in sources :

Example 1 with BibTeXDatabase

use of org.jbibtex.BibTeXDatabase in project ORCID-Source by ORCID.

the class WorkToCiteprocTranslator method translateFromBibtexCitation.

/**
     * Extract the bibtex and turn into CSL Adds in DOI and URL from metadata if
     * missing Horrible use of reflection to shorten hyperauthorship. It will
     * strip anything above 20 authors down to the primary author and 'et all'.
     * 
     * @param work
     * @param abreviate
     * @return
     */
private CSLItemData translateFromBibtexCitation(Work work, boolean abreviate) {
    try {
        BibTeXConverter conv = new BibTeXConverter();
        BibTeXDatabase db = conv.loadDatabase(IOUtils.toInputStream(StringUtils.stripAccents(work.getWorkCitation().getCitation())));
        Map<String, CSLItemData> cids = conv.toItemData(db);
        if (cids.size() == 1) {
            CSLItemData item = cids.values().iterator().next();
            // multiple authors not a literal.
            if (abreviate) {
                if (item.getAuthor().length > 20) {
                    CSLName[] abrev = Arrays.copyOf(item.getAuthor(), 1);
                    abrev[0] = new CSLNameBuilder().literal(abrev[0].getGiven() + " " + abrev[0].getFamily() + " " + "et all.").build();
                    ReflectionUtils.makeAccessible(authorField);
                    ReflectionUtils.setField(authorField, item, abrev);
                }
                for (int i = 0; i < item.getAuthor().length; i++) {
                    if (item.getAuthor()[i].getLiteral() != null && item.getAuthor()[i].getLiteral().length() > 200) {
                        ReflectionUtils.makeAccessible(literalField);
                        ReflectionUtils.setField(literalField, item.getAuthor()[i], StringUtils.abbreviate(item.getAuthor()[i].getLiteral(), 200));
                    }
                }
            }
            if (item.getDOI() == null) {
                String doi = extractID(work, WorkExternalIdentifierType.DOI);
                if (doi != null) {
                    ReflectionUtils.makeAccessible(doiField);
                    ReflectionUtils.setField(doiField, item, doi);
                }
            }
            if (item.getURL() == null) {
                if (extractID(work, WorkExternalIdentifierType.URI) != null) {
                    ReflectionUtils.makeAccessible(urlField);
                    ReflectionUtils.setField(urlField, item, extractID(work, WorkExternalIdentifierType.URI));
                } else if (item.getDOI() != null) {
                    ReflectionUtils.makeAccessible(urlField);
                    ReflectionUtils.setField(urlField, item, item.getDOI());
                } else if (extractID(work, WorkExternalIdentifierType.HANDLE) != null) {
                    ReflectionUtils.makeAccessible(urlField);
                    ReflectionUtils.setField(urlField, item, extractID(work, WorkExternalIdentifierType.HANDLE));
                }
            }
            return item;
        } else
            throw new ParseException("Invalid Citation count");
    } catch (IOException | ParseException e) {
        return null;
    }
}
Also used : CSLName(de.undercouch.citeproc.csl.CSLName) CSLItemData(de.undercouch.citeproc.csl.CSLItemData) CSLNameBuilder(de.undercouch.citeproc.csl.CSLNameBuilder) BibTeXConverter(de.undercouch.citeproc.bibtex.BibTeXConverter) ParseException(org.jbibtex.ParseException) IOException(java.io.IOException) BibTeXDatabase(org.jbibtex.BibTeXDatabase)

Aggregations

BibTeXConverter (de.undercouch.citeproc.bibtex.BibTeXConverter)1 CSLItemData (de.undercouch.citeproc.csl.CSLItemData)1 CSLName (de.undercouch.citeproc.csl.CSLName)1 CSLNameBuilder (de.undercouch.citeproc.csl.CSLNameBuilder)1 IOException (java.io.IOException)1 BibTeXDatabase (org.jbibtex.BibTeXDatabase)1 ParseException (org.jbibtex.ParseException)1