1
|
package eu.dnetlib.saxon.ext;
|
2
|
|
3
|
import java.util.regex.Matcher;
|
4
|
import java.util.regex.Pattern;
|
5
|
|
6
|
import net.sf.saxon.expr.XPathContext;
|
7
|
import net.sf.saxon.om.Sequence;
|
8
|
import net.sf.saxon.trans.XPathException;
|
9
|
import net.sf.saxon.value.SequenceType;
|
10
|
import net.sf.saxon.value.StringValue;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.springframework.stereotype.Component;
|
14
|
|
15
|
/**
|
16
|
* Created by alessia on 24/02/17.
|
17
|
*/
|
18
|
@Component
|
19
|
public class ConvertFunction extends AbstractExtensionFunction {
|
20
|
|
21
|
private static final Log log = LogFactory.getLog(ConvertFunction.class);
|
22
|
|
23
|
|
24
|
@Override
|
25
|
public String getName() {
|
26
|
return "decade";
|
27
|
}
|
28
|
|
29
|
@Override
|
30
|
public Sequence doCall(XPathContext context, Sequence[] arguments) throws XPathException {
|
31
|
String expr = arguments[0].head().getStringValue();
|
32
|
String decadeString = _decade(expr.trim());
|
33
|
log.debug(expr + "--> " + decadeString);
|
34
|
return new StringValue(decadeString);
|
35
|
}
|
36
|
|
37
|
@Override
|
38
|
public int getMinimumNumberOfArguments() {
|
39
|
return 1;
|
40
|
}
|
41
|
|
42
|
@Override
|
43
|
public int getMaximumNumberOfArguments() {
|
44
|
return 1;
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public SequenceType[] getArgumentTypes() {
|
49
|
return new SequenceType[] { SequenceType.SINGLE_STRING };
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
|
54
|
return SequenceType.SINGLE_STRING;
|
55
|
}
|
56
|
|
57
|
private String _decade(String s) {
|
58
|
Matcher m1 = Pattern.compile("(\\d\\d\\d)\\d").matcher(s);
|
59
|
if (m1.find()) {
|
60
|
String part = m1.group(1);
|
61
|
return part + "0-" + part + "9";
|
62
|
}
|
63
|
Matcher m2 = Pattern.compile("(\\d)\\d").matcher(s);
|
64
|
if (m2.find()) {
|
65
|
String part = m2.group(1);
|
66
|
return "19" + part + "0-19" + part + "9";
|
67
|
}
|
68
|
return "n/a";
|
69
|
}
|
70
|
}
|