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 org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
|
16
|
import eu.dnetlib.miscutils.functional.hash.Hashing;
|
17
|
import org.springframework.util.Assert;
|
18
|
|
19
|
/**
|
20
|
* This class provides some XSLT functions.
|
21
|
*
|
22
|
* <xsl:stylesheet ... xmlns:dnet="eu.dnetlib.miscutils.functional.xml.DnetXsltFunctions"> ... </xsl:stylesheet>
|
23
|
*
|
24
|
* For using in Saxon HE, methods in this class are becoming classes extending ExtensionFunctionDefinition in package eu.dnetlib.saxon.ext
|
25
|
*
|
26
|
* @author michele
|
27
|
*
|
28
|
*/
|
29
|
public class DnetXsltFunctions {
|
30
|
|
31
|
private static final Log log = LogFactory.getLog(DnetXsltFunctions.class); // NOPMD by marko on 11/24/08 5:02 PM
|
32
|
|
33
|
private static volatile long seedUniquifier = 8682522807148012L;
|
34
|
|
35
|
private static String[] dateFormats = { "yyyy-MM-dd", "yyyy/MM/dd" };
|
36
|
|
37
|
private static final String[] normalizeDateFormats = { "yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy" };
|
38
|
|
39
|
private static final String normalizeOutFormat = new String("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
40
|
|
41
|
public static String extractYear(String s) throws ParseException {
|
42
|
Calendar c = new GregorianCalendar();
|
43
|
for (String format : dateFormats) {
|
44
|
try {
|
45
|
c.setTime(new SimpleDateFormat(format).parse(s));
|
46
|
String year = String.valueOf(c.get(Calendar.YEAR));
|
47
|
return year;
|
48
|
} catch (ParseException e) {}
|
49
|
}
|
50
|
return "";
|
51
|
}
|
52
|
|
53
|
public static String normalizeDate(final String s, final boolean strict) {
|
54
|
|
55
|
final String date = s != null ? s.trim() : "";
|
56
|
|
57
|
for (String format : normalizeDateFormats) {
|
58
|
try {
|
59
|
Date parse = new SimpleDateFormat(format).parse(date);
|
60
|
String res = new SimpleDateFormat(normalizeOutFormat).format(parse);
|
61
|
return res;
|
62
|
} catch (ParseException e) {}
|
63
|
}
|
64
|
if (strict) {
|
65
|
throw new IllegalArgumentException("unable to normalize date: " + date);
|
66
|
} else {
|
67
|
//log.warn("unable to normalize date: " + s);
|
68
|
return "";
|
69
|
}
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* TODO: this function seems not to be used.
|
74
|
* @param max
|
75
|
* @return
|
76
|
*/
|
77
|
public static String randomInt(int max) {
|
78
|
return String.valueOf(new Random(++seedUniquifier + System.nanoTime()).nextInt(max));
|
79
|
}
|
80
|
|
81
|
public static String md5(String s) {
|
82
|
return Hashing.md5(s);
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* TODO: this function seems not to be used.
|
87
|
* @param s
|
88
|
* @return
|
89
|
*/
|
90
|
public static String decodeBase64(String s) {
|
91
|
return Hashing.decodeBase64(s);
|
92
|
}
|
93
|
/**
|
94
|
* TODO: this function seems not to be used.
|
95
|
* @param s
|
96
|
* @return
|
97
|
*/
|
98
|
public static String encodeBase64(String s) {
|
99
|
return Hashing.encodeBase64(s);
|
100
|
}
|
101
|
/**
|
102
|
* TODO: this function seems not to be used.
|
103
|
* @param s
|
104
|
* @return
|
105
|
*/
|
106
|
public static String lc(String s) {
|
107
|
return s.toLowerCase();
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* TODO: this function seems not to be used.
|
112
|
* @param s
|
113
|
* @return
|
114
|
*/
|
115
|
public static String uc(String s) {
|
116
|
return s.toUpperCase();
|
117
|
}
|
118
|
|
119
|
public static String decade(String s) {
|
120
|
String res = _decade(s.trim());
|
121
|
log.debug(s + "--> " + res);
|
122
|
return res;
|
123
|
}
|
124
|
|
125
|
private static String _decade(String s) {
|
126
|
Matcher m1 = Pattern.compile("(\\d\\d\\d)\\d").matcher(s);
|
127
|
if (m1.find()) {
|
128
|
String part = m1.group(1);
|
129
|
return part + "0-" + part + "9";
|
130
|
}
|
131
|
Matcher m2 = Pattern.compile("(\\d)\\d").matcher(s);
|
132
|
if (m2.find()) {
|
133
|
String part = m2.group(1);
|
134
|
return "19" + part + "0-19" + part + "9";
|
135
|
}
|
136
|
return "n/a";
|
137
|
}
|
138
|
|
139
|
// ENVELOPE(-10, 20, 15, 10) which is minX, maxX, maxY, minY order
|
140
|
public static String normalizeSolrBBox(final String box) {
|
141
|
if (StringUtils.isBlank(box)) return "";
|
142
|
final String[] split = box.trim().split(" ");
|
143
|
if (split == null) return "";
|
144
|
try {
|
145
|
Assert.isTrue(split.length == 4, "all 4 coordinates are present");
|
146
|
// Rect(minX=-180.0,maxX=180.0,minY=-90.0,maxY=90.0)
|
147
|
|
148
|
Assert.isTrue(Double.parseDouble(split[1]) >= -180.0, "minX=-180");
|
149
|
Assert.isTrue(Double.parseDouble(split[3]) <= 180.0, "maxX= 180");
|
150
|
Assert.isTrue(Double.parseDouble(split[0]) >= -90.0, "minY= -90");
|
151
|
Assert.isTrue(Double.parseDouble(split[2]) <= 90.0, "maxY= 90");
|
152
|
|
153
|
//maxY must be >= minY: 90.0 to -90.0
|
154
|
Assert.isTrue(Double.parseDouble(split[2]) >= Double.parseDouble(split[0]), "maxY must be >= minY");
|
155
|
|
156
|
//maxX must be >= minX: 180.0 to -180.0
|
157
|
Assert.isTrue(Double.parseDouble(split[3]) >= Double.parseDouble(split[1]), "maxX must be >= minX");
|
158
|
} catch (IllegalArgumentException e) {
|
159
|
//System.err.println(String.format("document %s has %s coordinates: %s", d.getDNGF().getEntity().getId(), split.length, e.getMessage()));
|
160
|
return "";
|
161
|
}
|
162
|
|
163
|
return String.format("ENVELOPE(%s, %s, %s, %s)", split[1], split[3], split[2], split[0]);
|
164
|
}
|
165
|
}
|