Revision 51857
Added by Michele Artini almost 6 years ago
BibTexConverter.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.utils; |
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
|
5 | 4 |
import java.io.StringWriter; |
6 | 5 |
import java.io.Writer; |
7 |
import java.util.List;
|
|
6 |
import java.util.Collection;
|
|
8 | 7 |
import java.util.stream.Collectors; |
9 | 8 |
|
10 | 9 |
import org.apache.commons.lang3.StringUtils; |
11 | 10 |
import org.apache.commons.logging.Log; |
12 | 11 |
import org.apache.commons.logging.LogFactory; |
13 |
import org.dom4j.Document; |
|
14 |
import org.dom4j.DocumentException; |
|
15 |
import org.dom4j.Node; |
|
16 |
import org.dom4j.io.SAXReader; |
|
17 | 12 |
import org.jbibtex.BibTeXDatabase; |
18 | 13 |
import org.jbibtex.BibTeXEntry; |
19 | 14 |
import org.jbibtex.BibTeXFormatter; |
... | ... | |
21 | 16 |
import org.jbibtex.StringValue; |
22 | 17 |
import org.jbibtex.StringValue.Style; |
23 | 18 |
|
19 |
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord; |
|
20 |
|
|
24 | 21 |
public class BibTexConverter { |
25 | 22 |
|
26 | 23 |
private static final Log log = LogFactory.getLog(BibTexConverter.class); |
27 | 24 |
|
28 |
public static String asBibTex(final String xml) {
|
|
25 |
public static String asBibTex(final MdRecord record) {
|
|
29 | 26 |
try { |
30 |
final Document doc = new SAXReader().read(new StringReader(xml)); |
|
31 | 27 |
|
32 |
final BibTeXEntry dbEntry = new BibTeXEntry( |
|
33 |
findBibTexType(doc.valueOf("//*[local-name() = 'resourceType']")), |
|
34 |
new Key(doc.valueOf("//*[local-name() = 'identifier']"))); |
|
28 |
final BibTeXEntry dbEntry = new BibTeXEntry(findBibTexType(record.getType()), new Key(record.getId())); |
|
35 | 29 |
|
36 |
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_TITLE, doc, "//*[local-name() = 'title']"); |
|
37 |
addMultipleStringValue(dbEntry, BibTeXEntry.KEY_AUTHOR, doc, "//*[local-name() = 'creator']"); |
|
38 |
addSimpleStringValue(dbEntry, BibTeXEntry.KEY_YEAR, doc, "//*[local-name() = 'date']"); |
|
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()); |
|
39 | 35 |
|
40 | 36 |
final BibTeXFormatter formatter = new BibTeXFormatter(); |
41 | 37 |
final Writer res = new StringWriter(); |
... | ... | |
43 | 39 |
db.addObject(dbEntry); |
44 | 40 |
formatter.format(db, res); |
45 | 41 |
return res.toString(); |
46 |
} catch (final DocumentException | IOException e) {
|
|
42 |
} catch (final IOException e) { |
|
47 | 43 |
log.warn("Error generating bibtex", e); |
48 | 44 |
return ""; |
49 | 45 |
} |
50 | 46 |
} |
51 | 47 |
|
52 |
private static void addSimpleStringValue(final BibTeXEntry dbEntry, final Key key, final Document doc, final String xpath) { |
|
53 |
final String val = doc.valueOf(xpath).trim(); |
|
54 |
if (StringUtils.isNotBlank(val)) { |
|
55 |
dbEntry.addField(key, new StringValue(val, Style.BRACED)); |
|
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)); |
|
56 | 51 |
} |
57 | 52 |
} |
58 | 53 |
|
59 |
@SuppressWarnings("unchecked") |
|
60 |
private static void addMultipleStringValue(final BibTeXEntry dbEntry, final Key key, final Document doc, final String xpath) { |
|
61 |
final String val = ((List<Node>) doc.selectNodes(xpath)) |
|
62 |
.stream() |
|
63 |
.map(Node::getText) |
|
54 |
private static void addMultipleStringValue(final BibTeXEntry dbEntry, final Key key, final Collection<String> list) { |
|
55 |
final String val = list.stream() |
|
64 | 56 |
.map(String::trim) |
65 |
.filter(StringUtils::isNotEmpty)
|
|
57 |
.filter(StringUtils::isNotBlank)
|
|
66 | 58 |
.collect(Collectors.joining(" and ")); |
67 | 59 |
|
68 | 60 |
if (StringUtils.isNotBlank(val)) { |
Also available in: Unified diff