1
|
package eu.dnetlib.data.utils;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringWriter;
|
5
|
import java.io.Writer;
|
6
|
import java.util.Collection;
|
7
|
import java.util.stream.Collectors;
|
8
|
|
9
|
import org.apache.commons.lang3.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.jbibtex.BibTeXDatabase;
|
13
|
import org.jbibtex.BibTeXEntry;
|
14
|
import org.jbibtex.BibTeXFormatter;
|
15
|
import org.jbibtex.Key;
|
16
|
import org.jbibtex.StringValue;
|
17
|
import org.jbibtex.StringValue.Style;
|
18
|
|
19
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
20
|
|
21
|
public class BibTexConverter {
|
22
|
|
23
|
private static final Log log = LogFactory.getLog(BibTexConverter.class);
|
24
|
|
25
|
public static String asBibTex(final MdRecord record) {
|
26
|
try {
|
27
|
|
28
|
final BibTeXEntry dbEntry = new BibTeXEntry(findBibTexType(record.getType()), new Key(record.getId()));
|
29
|
|
30
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_TITLE, record.getTitle());
|
31
|
addMultipleStringValue(dbEntry, BibTeXEntry.KEY_AUTHOR, record.getCreators());
|
32
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_PUBLISHER, record.getPublisher());
|
33
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_YEAR, record.getDate());
|
34
|
addMultipleStringValue(dbEntry, BibTeXEntry.KEY_DOI, record.getDois());
|
35
|
|
36
|
final BibTeXFormatter formatter = new BibTeXFormatter();
|
37
|
final Writer res = new StringWriter();
|
38
|
final BibTeXDatabase db = new BibTeXDatabase();
|
39
|
db.addObject(dbEntry);
|
40
|
formatter.format(db, res);
|
41
|
return res.toString();
|
42
|
} catch (final IOException e) {
|
43
|
log.warn("Error generating bibtex", e);
|
44
|
return "";
|
45
|
}
|
46
|
}
|
47
|
|
48
|
private static void addSimpleStringValue(final BibTeXEntry dbEntry, final Key key, final Object value) {
|
49
|
if ((value != null) && StringUtils.isNotBlank(value.toString())) {
|
50
|
dbEntry.addField(key, new StringValue(value.toString(), Style.BRACED));
|
51
|
}
|
52
|
}
|
53
|
|
54
|
private static void addMultipleStringValue(final BibTeXEntry dbEntry, final Key key, final Collection<String> list) {
|
55
|
final String val = list.stream()
|
56
|
.map(String::trim)
|
57
|
.filter(StringUtils::isNotBlank)
|
58
|
.collect(Collectors.joining(" and "));
|
59
|
|
60
|
if (StringUtils.isNotBlank(val)) {
|
61
|
dbEntry.addField(key, new StringValue(val, Style.BRACED));
|
62
|
}
|
63
|
}
|
64
|
|
65
|
private static Key findBibTexType(final String resourceType) {
|
66
|
switch (resourceType) {
|
67
|
case "Conference object":
|
68
|
return BibTeXEntry.TYPE_INPROCEEDINGS;
|
69
|
case "Report":
|
70
|
return BibTeXEntry.TYPE_TECHREPORT;
|
71
|
case "Article":
|
72
|
return BibTeXEntry.TYPE_ARTICLE;
|
73
|
case "Other":
|
74
|
return BibTeXEntry.TYPE_MISC;
|
75
|
case "Part of book or chapter of book":
|
76
|
return BibTeXEntry.TYPE_INBOOK;
|
77
|
case "Book":
|
78
|
return BibTeXEntry.TYPE_BOOK;
|
79
|
case "Bachelor thesis":
|
80
|
return BibTeXEntry.TYPE_MASTERSTHESIS;
|
81
|
case "Doctoral thesis":
|
82
|
return BibTeXEntry.TYPE_PHDTHESIS;
|
83
|
case "Patent":
|
84
|
return BibTeXEntry.TYPE_MISC;
|
85
|
case "Master thesis":
|
86
|
return BibTeXEntry.TYPE_MASTERSTHESIS;
|
87
|
default:
|
88
|
return BibTeXEntry.TYPE_MISC;
|
89
|
}
|
90
|
}
|
91
|
}
|