use of de.undercouch.citeproc.bibtex.BibTeXConverter 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;
}
}
use of de.undercouch.citeproc.bibtex.BibTeXConverter 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;
}
}
Aggregations