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.regex.Matcher;
|
8
|
import java.util.regex.Pattern;
|
9
|
import java.util.stream.Collectors;
|
10
|
|
11
|
import org.apache.commons.lang3.StringUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.jbibtex.BibTeXDatabase;
|
15
|
import org.jbibtex.BibTeXEntry;
|
16
|
import org.jbibtex.BibTeXFormatter;
|
17
|
import org.jbibtex.Key;
|
18
|
import org.jbibtex.StringValue;
|
19
|
import org.jbibtex.StringValue.Style;
|
20
|
|
21
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
22
|
|
23
|
public class BibTexConverter {
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(BibTexConverter.class);
|
26
|
|
27
|
public static String asBibTex(final MdRecord record) {
|
28
|
try {
|
29
|
|
30
|
final BibTeXEntry dbEntry = new BibTeXEntry(findBibTexType(record.getType()), new Key(record.getId()));
|
31
|
|
32
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_TITLE, record.getTitle());
|
33
|
addMultipleStringValue(dbEntry, BibTeXEntry.KEY_AUTHOR, record.getCreators());
|
34
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_PUBLISHER, record.getPublisher());
|
35
|
addMultipleStringValue(dbEntry, BibTeXEntry.KEY_DOI, record.getDois());
|
36
|
|
37
|
addSourceAndDate(dbEntry, record);
|
38
|
|
39
|
final BibTeXFormatter formatter = new BibTeXFormatter();
|
40
|
final Writer res = new StringWriter();
|
41
|
final BibTeXDatabase db = new BibTeXDatabase();
|
42
|
db.addObject(dbEntry);
|
43
|
formatter.format(db, res);
|
44
|
return res.toString();
|
45
|
} catch (final IOException e) {
|
46
|
log.warn("Error generating bibtex", e);
|
47
|
return "";
|
48
|
}
|
49
|
}
|
50
|
|
51
|
private static void addSimpleStringValue(final BibTeXEntry dbEntry, final Key key, final Object value) {
|
52
|
if ((value != null) && StringUtils.isNotBlank(value.toString())) {
|
53
|
dbEntry.addField(key, new StringValue(value.toString(), Style.BRACED));
|
54
|
}
|
55
|
}
|
56
|
|
57
|
private static void addMultipleStringValue(final BibTeXEntry dbEntry, final Key key, final Collection<String> list) {
|
58
|
final String val = list.stream()
|
59
|
.map(String::trim)
|
60
|
.filter(StringUtils::isNotBlank)
|
61
|
.collect(Collectors.joining(" and "));
|
62
|
|
63
|
if (StringUtils.isNotBlank(val)) {
|
64
|
dbEntry.addField(key, new StringValue(val, Style.BRACED));
|
65
|
}
|
66
|
}
|
67
|
|
68
|
private static void addSourceAndDate(final BibTeXEntry dbEntry, final MdRecord record) {
|
69
|
String year = "" + record.getDate();
|
70
|
|
71
|
switch (record.getType().toLowerCase()) {
|
72
|
case "conference object":
|
73
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_BOOKTITLE, record.getSource());
|
74
|
break;
|
75
|
case "part of book or chapter of book":
|
76
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_BOOKTITLE, record.getSource());
|
77
|
break;
|
78
|
case "report":
|
79
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_INSTITUTION, record.getSource());
|
80
|
break;
|
81
|
case "article":
|
82
|
final String regex = "^(.+) (\\d*) \\((\\d{4})\\)(: (\\d+\\–?\\d*))?";
|
83
|
final Pattern pattern = Pattern.compile(regex);
|
84
|
final Matcher matcher = pattern.matcher(record.getSource());
|
85
|
|
86
|
if (matcher.find()) {
|
87
|
if (matcher.groupCount() >= 1) {
|
88
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_JOURNAL, matcher.group(1));
|
89
|
}
|
90
|
if (matcher.groupCount() >= 2) {
|
91
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_VOLUME, matcher.group(2));
|
92
|
}
|
93
|
if (matcher.groupCount() >= 3) {
|
94
|
year = matcher.group(3);
|
95
|
}
|
96
|
if (matcher.groupCount() >= 5) {
|
97
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_PAGES, matcher.group(5));
|
98
|
}
|
99
|
}
|
100
|
|
101
|
break;
|
102
|
case "patent":
|
103
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_HOWPUBLISHED, record.getSource());
|
104
|
break;
|
105
|
}
|
106
|
|
107
|
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_YEAR, year);
|
108
|
}
|
109
|
|
110
|
private static Key findBibTexType(final String resourceType) {
|
111
|
switch (resourceType.toLowerCase()) {
|
112
|
case "conference object":
|
113
|
return BibTeXEntry.TYPE_INPROCEEDINGS;
|
114
|
case "report":
|
115
|
return BibTeXEntry.TYPE_TECHREPORT;
|
116
|
case "article":
|
117
|
return BibTeXEntry.TYPE_ARTICLE;
|
118
|
case "other":
|
119
|
return BibTeXEntry.TYPE_MISC;
|
120
|
case "part of book or chapter of book":
|
121
|
return BibTeXEntry.TYPE_INBOOK;
|
122
|
case "book":
|
123
|
return BibTeXEntry.TYPE_BOOK;
|
124
|
case "bachelor thesis":
|
125
|
return BibTeXEntry.TYPE_MASTERSTHESIS;
|
126
|
case "doctoral thesis":
|
127
|
return BibTeXEntry.TYPE_PHDTHESIS;
|
128
|
case "patent":
|
129
|
return BibTeXEntry.TYPE_MISC;
|
130
|
case "master thesis":
|
131
|
return BibTeXEntry.TYPE_MASTERSTHESIS;
|
132
|
default:
|
133
|
return BibTeXEntry.TYPE_MISC;
|
134
|
}
|
135
|
}
|
136
|
}
|