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");
}
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(']');
}
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());
}
}
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;
}
}
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");
}
Aggregations