Search in sources :

Example 1 with ParseException

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

the class V3WorkToCiteprocTranslator 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();
        String ascii = Normalizer.normalize(work.getWorkCitation().getCitation(), Normalizer.Form.NFKD);
        ascii = ascii.replaceAll("[^\\x00-\\x7F]", "");
        BibTeXDatabase db = conv.loadDatabase(IOUtils.toInputStream(ascii));
        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)

Example 2 with ParseException

use of org.jbibtex.ParseException 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();
        String ascii = Normalizer.normalize(work.getWorkCitation().getCitation(), Normalizer.Form.NFKD);
        ascii = ascii.replaceAll("[^\\x00-\\x7F]", "");
        BibTeXDatabase db = conv.loadDatabase(IOUtils.toInputStream(ascii));
        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)2 CSLItemData (de.undercouch.citeproc.csl.CSLItemData)2 CSLName (de.undercouch.citeproc.csl.CSLName)2 CSLNameBuilder (de.undercouch.citeproc.csl.CSLNameBuilder)2 IOException (java.io.IOException)2 BibTeXDatabase (org.jbibtex.BibTeXDatabase)2 ParseException (org.jbibtex.ParseException)2