Project

General

Profile

1
package eu.dnetlib.data.utils;
2

    
3
import java.util.Arrays;
4
import java.util.regex.Matcher;
5
import java.util.regex.Pattern;
6
import java.util.stream.Collectors;
7

    
8
import org.apache.commons.lang3.StringUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11

    
12
import eu.dnetlib.data.mdstore.plugins.objects.Project;
13

    
14
/**
15
 * Created by sandro on 12/6/16.
16
 */
17
public class XsltFunctions {
18

    
19
	private static final Log log = LogFactory.getLog(XsltFunctions.class);
20

    
21
	public static String cleanNames(final String s) {
22
		if (StringUtils.isBlank(s)) { return ""; }
23
		return cleanNames(s, StringUtils.countMatches(s, ",") > 1 ? "," : ";");
24
	}
25

    
26
	private static String cleanNames(final String s, final String sep) {
27
		return Arrays.stream(s.split(sep))
28
				.map(String::trim)
29
				.map(XsltFunctions::clean)
30
				.map(XsltFunctions::capitalize)
31
				.filter(StringUtils::isNotBlank)
32
				.collect(Collectors.joining("#"));
33
	}
34

    
35
	private static String clean(final String s) {
36
		return s.replaceAll("\\(.*\\)", "")
37
				.replaceAll("\\[.*\\]", "")
38
				.replaceAll("(?i)^et\\.? al(\\.|\\s)*$", "")
39
				.replaceAll("(?i)\\s*et\\.? al(\\.|\\s)*$", "")
40
				.replaceAll("\\d|\\*", "")
41
				.replaceAll("^(\\s|\\-|\\.)+", "")
42
				.replaceAll("\\,", "")
43
				.replaceAll("\\.", ". ")
44
				.trim();
45
	}
46

    
47
	public static String cleanDoi(final String doi) {
48
		final String x = doi.replaceAll("\\?", "")
49
				.replaceAll("\\s*\\/\\s*", "/")
50
				.replaceAll("\\s*\\-\\s*", "-")
51
				.trim()
52
				.replaceAll("\\s", "_");
53

    
54
		if (!x.equals(doi)) {
55
			log.info("Cleaning doi: " + doi + " -> " + x);
56
		}
57

    
58
		return x;
59
	}
60

    
61
	public static String capitalize(final String s) {
62
		return Arrays.stream(s.split(" "))
63
				.map(String::toLowerCase)
64
				.map(StringUtils::capitalize)
65
				.map(XsltFunctions::fixApostrophes)
66
				.collect(Collectors.joining(" "));
67
	}
68

    
69
	public static String fixApostrophes(final String s) {
70
		return s.replaceAll("'a", "'A").replaceAll("'e", "'E").replaceAll("'i", "'I").replaceAll("'o", "'O").replaceAll("'u", "'U")
71
				.replaceAll("a'", "à").replaceAll("e'", "è").replaceAll("i'", "ì").replaceAll("o'", "ò").replaceAll("u'", "ù");
72
	}
73

    
74
	public static boolean isValidProject(final String id) {
75
		return Project.isValid(id);
76
	}
77

    
78
	public static String calculatePersonName(final String s) {
79
		final Pattern pattern = Pattern.compile("info:cnr-pdr\\/author\\/(.+):(.+)\\/(.+)\\/(.+)");
80
		final Matcher matcher = pattern.matcher(s);
81
		return matcher.find() ? capitalize(String.format("%s %s", matcher.group(4), matcher.group(3))) : "";
82
	}
83

    
84
	// <xsl:for-each select="tokenize(istiFunction:cleanName(.), ';')">
85
	// <xsl:choose>
86
	// <xsl:when test="matches(normalize-space(.), ',(\s*[a-zA-Z]\.)+$')">
87
	// <creator>
88
	// <creatorName>
89
	// <xsl:value-of select="normalize-space(translate(.,',',' '))"/>
90
	// </creatorName>
91
	// </creator>
92
	// </xsl:when>
93
	//
94
	// <xsl:when test="matches(normalize-space(.), '(^[a-zA-Z\.]+,\s?[a-zA-Z\.\s]+$)|(^[a-zA-Z\.\s]+,\s?[a-zA-Z\.]+$)')">
95
	// <creator>
96
	// <creatorName>
97
	// <xsl:value-of select="normalize-space(translate(.,',',' '))"/>
98
	// </creatorName>
99
	// </creator>
100
	// </xsl:when>
101
	//
102
	// <xsl:otherwise>
103
	// <xsl:for-each select="tokenize(., ',')">
104
	// <creator>
105
	// <creatorName>
106
	// <xsl:value-of select="normalize-space(.)"/>
107
	// </creatorName>
108
	// </creator>
109
	// </xsl:for-each>
110
	// </xsl:otherwise>
111
	// </xsl:choose>
112
	// </xsl:for-each>
113
	//
114

    
115
}
(4-4/4)