1
|
package eu.dnetlib.data.utils;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
import java.util.Collection;
|
5
|
import java.util.stream.Collectors;
|
6
|
|
7
|
import org.apache.commons.lang3.StringUtils;
|
8
|
|
9
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
10
|
|
11
|
public class CsvConverter {
|
12
|
|
13
|
public static String asCsv(final MdRecord record) {
|
14
|
|
15
|
final StringWriter sw = new StringWriter();
|
16
|
sw.append(prepareValue(record.getId()));
|
17
|
sw.append("\t");
|
18
|
sw.append(prepareValue(record.getTitle()));
|
19
|
sw.append("\t");
|
20
|
sw.append(prepareValue(record.getCreators()));
|
21
|
sw.append("\t");
|
22
|
sw.append(prepareValue(record.getPublisher()));
|
23
|
sw.append("\t");
|
24
|
sw.append(prepareValue(record.getType()));
|
25
|
sw.append("\t");
|
26
|
sw.append(prepareValue("" + record.getDate()));
|
27
|
sw.append("\t");
|
28
|
sw.append(prepareValue(record.getDois()));
|
29
|
sw.append("\t");
|
30
|
sw.append(prepareValue(record.getSource()));
|
31
|
|
32
|
return sw.toString();
|
33
|
}
|
34
|
|
35
|
private static String prepareValue(final Collection<String> list) {
|
36
|
return list.stream()
|
37
|
.map(CsvConverter::prepareValue)
|
38
|
.filter(StringUtils::isNotBlank)
|
39
|
.collect(Collectors.joining(", "));
|
40
|
}
|
41
|
|
42
|
private static String prepareValue(final String s) {
|
43
|
return s.replaceAll("\\n", " ")
|
44
|
.replaceAll("\\t", " ")
|
45
|
.replaceAll("\\s+", " ")
|
46
|
.replaceAll("\\p{C}", "?") // Unprintable chars
|
47
|
.trim();
|
48
|
}
|
49
|
}
|