1
|
package eu.dnetlib.miscutils.functional.xml;
|
2
|
|
3
|
import java.text.ParseException;
|
4
|
import java.text.SimpleDateFormat;
|
5
|
import java.util.Calendar;
|
6
|
import java.util.Date;
|
7
|
import java.util.GregorianCalendar;
|
8
|
import java.util.Random;
|
9
|
import java.util.regex.Matcher;
|
10
|
import java.util.regex.Pattern;
|
11
|
|
12
|
import com.google.common.base.Joiner;
|
13
|
import eu.dnetlib.miscutils.functional.hash.Hashing;
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
|
18
|
/**
|
19
|
* This class provides some XSLT functions.
|
20
|
*
|
21
|
* <xsl:stylesheet ... xmlns:dnet="eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions"> ... </xsl:stylesheet>
|
22
|
*
|
23
|
* @author michele
|
24
|
*
|
25
|
*/
|
26
|
public class DnetXsltFunctions {
|
27
|
|
28
|
private static final Log log = LogFactory.getLog(DnetXsltFunctions.class); // NOPMD by marko on 11/24/08 5:02 PM
|
29
|
|
30
|
private static volatile long seedUniquifier = 8682522807148012L;
|
31
|
|
32
|
private static String[] dateFormats = { "yyyy-MM-dd", "yyyy/MM/dd" };
|
33
|
|
34
|
private static final String[] normalizeDateFormats = { "yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy" };
|
35
|
|
36
|
private static final String normalizeOutFormat = new String("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
37
|
|
38
|
public static String extractYear(String s) throws ParseException {
|
39
|
Calendar c = new GregorianCalendar();
|
40
|
for (String format : dateFormats) {
|
41
|
try {
|
42
|
c.setTime(new SimpleDateFormat(format).parse(s));
|
43
|
String year = String.valueOf(c.get(Calendar.YEAR));
|
44
|
return year;
|
45
|
} catch (ParseException e) {}
|
46
|
}
|
47
|
return "";
|
48
|
}
|
49
|
|
50
|
public static String normalizeDate(final String s, final boolean strict) {
|
51
|
|
52
|
final String date = s != null ? s.trim() : "";
|
53
|
|
54
|
for (String format : normalizeDateFormats) {
|
55
|
try {
|
56
|
Date parse = new SimpleDateFormat(format).parse(date);
|
57
|
String res = new SimpleDateFormat(normalizeOutFormat).format(parse);
|
58
|
return res;
|
59
|
} catch (ParseException e) {}
|
60
|
}
|
61
|
if (strict) {
|
62
|
throw new IllegalArgumentException("unable to normalize date: " + date);
|
63
|
} else {
|
64
|
//log.warn("unable to normalize date: " + s);
|
65
|
return "";
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public static String randomInt(int max) {
|
70
|
return String.valueOf(new Random(++seedUniquifier + System.nanoTime()).nextInt(max));
|
71
|
}
|
72
|
|
73
|
public static String md5(String s) {
|
74
|
return Hashing.md5(s);
|
75
|
}
|
76
|
|
77
|
public static String decodeBase64(String s) {
|
78
|
return Hashing.decodeBase64(s);
|
79
|
}
|
80
|
|
81
|
public static String encodeBase64(String s) {
|
82
|
return Hashing.encodeBase64(s);
|
83
|
}
|
84
|
|
85
|
public static String lc(String s) {
|
86
|
return s.toLowerCase();
|
87
|
}
|
88
|
|
89
|
public static String uc(String s) {
|
90
|
return s.toUpperCase();
|
91
|
}
|
92
|
|
93
|
public static String decade(String s) {
|
94
|
String res = _decade(s.trim());
|
95
|
log.debug(s + "--> " + res);
|
96
|
return res;
|
97
|
}
|
98
|
|
99
|
private static String _decade(String s) {
|
100
|
Matcher m1 = Pattern.compile("(\\d\\d\\d)\\d").matcher(s);
|
101
|
if (m1.find()) {
|
102
|
String part = m1.group(1);
|
103
|
return part + "0-" + part + "9";
|
104
|
}
|
105
|
Matcher m2 = Pattern.compile("(\\d)\\d").matcher(s);
|
106
|
if (m2.find()) {
|
107
|
String part = m2.group(1);
|
108
|
return "19" + part + "0-19" + part + "9";
|
109
|
}
|
110
|
return "n/a";
|
111
|
}
|
112
|
|
113
|
public static String join(final String s1, final String separator, final String s2) {
|
114
|
if(StringUtils.isBlank(s1) || StringUtils.isBlank(s2)){
|
115
|
return "";
|
116
|
}
|
117
|
return Joiner.on(separator).join(s1, s2);
|
118
|
}
|
119
|
|
120
|
public static String pickFirst(final String s1, final String s2) {
|
121
|
return StringUtils.isNotBlank(s1) ? s1 : StringUtils.isNotBlank(s2) ? s2 : "";
|
122
|
}
|
123
|
}
|