Search in sources :

Example 1 with CSLItemData

use of de.undercouch.citeproc.csl.CSLItemData in project ORCID-Source by ORCID.

the class WorkToCiteprocTranslatorTest method testBibtexWorkTranslationHyperAuthorLiteralAndMissingDOI.

@Test
public void testBibtexWorkTranslationHyperAuthorLiteralAndMissingDOI() {
    Work w = makeWork(bibtexHyperLiteral);
    ExternalIDs wei = new ExternalIDs();
    ExternalID eid = new ExternalID();
    eid.setType(WorkExternalIdentifierType.DOI.name());
    //WorkExternalIdentifierId id = new WorkExternalIdentifierId();
    eid.setValue("10.1234/1234");
    wei.getExternalIdentifier().add(eid);
    w.setWorkExternalIdentifiers(wei);
    WorkToCiteprocTranslator t = new WorkToCiteprocTranslator();
    CSLItemData d = t.toCiteproc(w, null, true);
    Assert.assertEquals(d.getAuthor().length, 1);
    Assert.assertEquals(d.getAuthor()[0].getLiteral(), "Altshuler, D.M. and Durbin, R.M. and Abecasis, G.R. and Bentley, D.R. and Chakravarti, A. and Clark, A.G. and Donnelly, P. and Eichler, E.E. and Flicek, P. and Gabriel, S.B. and Gibbs, R.A. and Gre...");
    Assert.assertEquals(d.getDOI(), "10.1234/1234");
}
Also used : ExternalIDs(org.orcid.jaxb.model.record_v2.ExternalIDs) CSLItemData(de.undercouch.citeproc.csl.CSLItemData) ExternalID(org.orcid.jaxb.model.record_v2.ExternalID) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Example 2 with CSLItemData

use of de.undercouch.citeproc.csl.CSLItemData in project ORCID-Source by ORCID.

the class CSLItemDataListMBWriter method writeTo.

@Override
public void writeTo(CSLItemDataList data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    final PrintStream printStream = new PrintStream(entityStream);
    printStream.println('[');
    boolean first = true;
    for (CSLItemData item : data.getData()) {
        if (item != null) {
            if (!first)
                printStream.print(',');
            else
                first = false;
            printStream.println(item.toJson(jsonFactory.createJsonBuilder()).toString());
        }
    }
    printStream.println(']');
}
Also used : PrintStream(java.io.PrintStream) CSLItemData(de.undercouch.citeproc.csl.CSLItemData)

Example 3 with CSLItemData

use of de.undercouch.citeproc.csl.CSLItemData in project jabref by JabRef.

the class CitationStyleGenerator method generateCitations.

/**
     * WARNING: the citation is generated using JavaScript which may take some time, better call it from outside the main Thread
     * Generates the citation for multiple entries at once. This is useful when the Citation Style has an increasing number
     */
public static List<String> generateCitations(List<BibEntry> bibEntries, String style, CitationStyleOutputFormat outputFormat) {
    try {
        CSLItemData[] cslItemData = new CSLItemData[bibEntries.size()];
        for (int i = 0; i < bibEntries.size(); i++) {
            cslItemData[i] = bibEntryToCSLItemData(bibEntries.get(i));
        }
        Bibliography bibliography = CSL.makeAdhocBibliography(style, outputFormat.getFormat(), cslItemData);
        return Arrays.asList(bibliography.getEntries());
    } catch (IOException | ArrayIndexOutOfBoundsException e) {
        LOGGER.error("Could not generate BibEntry citation", e);
        return Collections.singletonList(Localization.lang("Cannot generate preview based on selected citation style."));
    } catch (TokenMgrException e) {
        LOGGER.error("Bad character inside BibEntry", e);
        // sadly one cannot easily retrieve the bad char from the TokenMgrError
        return Collections.singletonList(new StringBuilder().append(Localization.lang("Cannot generate preview based on selected citation style.")).append(outputFormat.getLineSeparator()).append(Localization.lang("Bad character inside entry")).append(outputFormat.getLineSeparator()).append(e.getLocalizedMessage()).toString());
    }
}
Also used : CSLItemData(de.undercouch.citeproc.csl.CSLItemData) TokenMgrException(org.jbibtex.TokenMgrException) IOException(java.io.IOException) Bibliography(de.undercouch.citeproc.output.Bibliography)

Example 4 with CSLItemData

use of de.undercouch.citeproc.csl.CSLItemData 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)

Example 5 with CSLItemData

use of de.undercouch.citeproc.csl.CSLItemData in project ORCID-Source by ORCID.

the class WorkToCiteprocTranslatorTest method testBibtexWorkTranslationHyperAuthor.

@Test
public void testBibtexWorkTranslationHyperAuthor() {
    Work w = makeWork(bibtexHyper);
    WorkToCiteprocTranslator t = new WorkToCiteprocTranslator();
    CSLItemData d = t.toCiteproc(w, null, true);
    Assert.assertEquals(d.getAuthor().length, 1);
    Assert.assertEquals(d.getAuthor()[0].getLiteral(), "G. Aad et all.");
    Assert.assertEquals(d.getTitle(), "Observation of a new particle in the search for the Standard Model Higgs boson with the {ATLAS} detector at the {LHC}");
    Assert.assertEquals(d.getDOI(), "http://dx.doi.org/10.1016/j.physletb.2012.08.020");
}
Also used : CSLItemData(de.undercouch.citeproc.csl.CSLItemData) Work(org.orcid.jaxb.model.record_v2.Work) Test(org.junit.Test)

Aggregations

CSLItemData (de.undercouch.citeproc.csl.CSLItemData)7 Work (org.orcid.jaxb.model.record_v2.Work)4 Test (org.junit.Test)3 IOException (java.io.IOException)2 BibTeXConverter (de.undercouch.citeproc.bibtex.BibTeXConverter)1 CSLName (de.undercouch.citeproc.csl.CSLName)1 CSLNameBuilder (de.undercouch.citeproc.csl.CSLNameBuilder)1 Bibliography (de.undercouch.citeproc.output.Bibliography)1 PrintStream (java.io.PrintStream)1 BibTeXDatabase (org.jbibtex.BibTeXDatabase)1 ParseException (org.jbibtex.ParseException)1 TokenMgrException (org.jbibtex.TokenMgrException)1 WorkToCiteprocTranslator (org.orcid.api.common.writer.citeproc.WorkToCiteprocTranslator)1 ExternalID (org.orcid.jaxb.model.record_v2.ExternalID)1 ExternalIDs (org.orcid.jaxb.model.record_v2.ExternalIDs)1 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)1 RecordNameEntity (org.orcid.persistence.jpa.entities.RecordNameEntity)1