1
|
package eu.dnetlib.data.utils;
|
2
|
|
3
|
import java.net.MalformedURLException;
|
4
|
import java.net.URL;
|
5
|
import java.util.Arrays;
|
6
|
import java.util.regex.Matcher;
|
7
|
import java.util.regex.Pattern;
|
8
|
import java.util.stream.Collectors;
|
9
|
|
10
|
import org.apache.commons.codec.digest.DigestUtils;
|
11
|
import org.apache.commons.lang3.StringUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
|
15
|
import eu.dnetlib.data.mdstore.plugins.objects.Project;
|
16
|
|
17
|
/**
|
18
|
* Created by sandro on 12/6/16.
|
19
|
*/
|
20
|
public class XsltFunctions {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(XsltFunctions.class);
|
23
|
|
24
|
public static String cleanNames(final String s) {
|
25
|
if (StringUtils.isBlank(s)) { return ""; }
|
26
|
return cleanNames(s, StringUtils.countMatches(s, ",") > 1 ? "," : ";");
|
27
|
}
|
28
|
|
29
|
private static String cleanNames(final String s, final String sep) {
|
30
|
return Arrays.stream(s.split(sep))
|
31
|
.map(String::trim)
|
32
|
.map(XsltFunctions::clean)
|
33
|
.map(XsltFunctions::capitalize)
|
34
|
.filter(StringUtils::isNotBlank)
|
35
|
.collect(Collectors.joining("#"));
|
36
|
}
|
37
|
|
38
|
private static String clean(final String s) {
|
39
|
return s.replaceAll("\\(.*\\)", "")
|
40
|
.replaceAll("\\[.*\\]", "")
|
41
|
.replaceAll("(?i)^et\\.? al(\\.|\\s)*$", "")
|
42
|
.replaceAll("(?i)\\s*et\\.? al(\\.|\\s)*$", "")
|
43
|
.replaceAll("\\d|\\*", "")
|
44
|
.replaceAll("^(\\s|\\-|\\.)+", "")
|
45
|
.replaceAll("\\,", "")
|
46
|
.replaceAll("\\.", ". ")
|
47
|
.trim();
|
48
|
}
|
49
|
|
50
|
public static String cleanDoi(final String doi) {
|
51
|
final String x = doi.replaceAll("\\?", "")
|
52
|
.replaceAll("\\s*\\/\\s*", "/")
|
53
|
.replaceAll("\\s*\\-\\s*", "-")
|
54
|
.trim()
|
55
|
.replaceAll("\\s", "_");
|
56
|
|
57
|
if (!x.equals(doi)) {
|
58
|
log.info("Cleaning doi: " + doi + " -> " + x);
|
59
|
}
|
60
|
|
61
|
return x;
|
62
|
}
|
63
|
|
64
|
public static String capitalize(final String s) {
|
65
|
return Arrays.stream(s.split(" "))
|
66
|
.map(String::toLowerCase)
|
67
|
.map(StringUtils::capitalize)
|
68
|
.map(XsltFunctions::fixApostrophes)
|
69
|
.collect(Collectors.joining(" "));
|
70
|
}
|
71
|
|
72
|
public static String fixApostrophes(final String s) {
|
73
|
return s.replaceAll("'a", "'A").replaceAll("'e", "'E").replaceAll("'i", "'I").replaceAll("'o", "'O").replaceAll("'u", "'U")
|
74
|
.replaceAll("a'", "à").replaceAll("e'", "è").replaceAll("i'", "ì").replaceAll("o'", "ò").replaceAll("u'", "ù");
|
75
|
}
|
76
|
|
77
|
public static boolean isValidProject(final String id) {
|
78
|
return Project.isValid(id);
|
79
|
}
|
80
|
|
81
|
public static String projectLongId(final String funder,
|
82
|
final String program,
|
83
|
final String code,
|
84
|
final String jurisdiction,
|
85
|
final String name,
|
86
|
final String acronym) {
|
87
|
|
88
|
return String.format("info:eu-repo/grantAgreement/%s/%s/%s/%s/%s/%s",
|
89
|
funder.replaceAll("/", "%2F"),
|
90
|
program.replaceAll("/", "%2F"),
|
91
|
code.replaceAll("/", "%2F"),
|
92
|
jurisdiction.replaceAll("/", "%2F"),
|
93
|
name.replaceAll("/", "%2F"),
|
94
|
acronym.replaceAll("/", "%2F"));
|
95
|
|
96
|
}
|
97
|
|
98
|
public static String calculatePersonName(final String s) {
|
99
|
final Pattern pattern = Pattern.compile("info:cnr-pdr\\/author\\/(.+):(.+)\\/(.+)\\/(.+)");
|
100
|
final Matcher matcher = pattern.matcher(s);
|
101
|
return matcher.find() ? capitalize(String.format("%s, %s", matcher.group(3), matcher.group(4))) : "";
|
102
|
}
|
103
|
|
104
|
public static String serverName(final String s) {
|
105
|
try {
|
106
|
return new URL(s).getHost();
|
107
|
} catch (final MalformedURLException e) {
|
108
|
return "Unknown";
|
109
|
}
|
110
|
}
|
111
|
|
112
|
public static String md5(final String s) {
|
113
|
return DigestUtils.md5Hex(s);
|
114
|
}
|
115
|
|
116
|
// <xsl:for-each select="tokenize(istiFunction:cleanName(.), ';')">
|
117
|
// <xsl:choose>
|
118
|
// <xsl:when test="matches(normalize-space(.), ',(\s*[a-zA-Z]\.)+$')">
|
119
|
// <creator>
|
120
|
// <creatorName>
|
121
|
// <xsl:value-of select="normalize-space(translate(.,',',' '))"/>
|
122
|
// </creatorName>
|
123
|
// </creator>
|
124
|
// </xsl:when>
|
125
|
//
|
126
|
// <xsl:when test="matches(normalize-space(.), '(^[a-zA-Z\.]+,\s?[a-zA-Z\.\s]+$)|(^[a-zA-Z\.\s]+,\s?[a-zA-Z\.]+$)')">
|
127
|
// <creator>
|
128
|
// <creatorName>
|
129
|
// <xsl:value-of select="normalize-space(translate(.,',',' '))"/>
|
130
|
// </creatorName>
|
131
|
// </creator>
|
132
|
// </xsl:when>
|
133
|
//
|
134
|
// <xsl:otherwise>
|
135
|
// <xsl:for-each select="tokenize(., ',')">
|
136
|
// <creator>
|
137
|
// <creatorName>
|
138
|
// <xsl:value-of select="normalize-space(.)"/>
|
139
|
// </creatorName>
|
140
|
// </creator>
|
141
|
// </xsl:for-each>
|
142
|
// </xsl:otherwise>
|
143
|
// </xsl:choose>
|
144
|
// </xsl:for-each>
|
145
|
//
|
146
|
|
147
|
}
|