41 lines
1.4 KiB
Java
41 lines
1.4 KiB
Java
package eu.dnetlib.common.mapping.xslt.functions;
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import eu.dnetlib.common.mapping.xslt.XsltTransformerFactory;
|
|
import net.sf.saxon.s9api.ExtensionFunction;
|
|
import net.sf.saxon.s9api.ItemType;
|
|
import net.sf.saxon.s9api.OccurrenceIndicator;
|
|
import net.sf.saxon.s9api.QName;
|
|
import net.sf.saxon.s9api.SaxonApiException;
|
|
import net.sf.saxon.s9api.SequenceType;
|
|
import net.sf.saxon.s9api.XdmAtomicValue;
|
|
import net.sf.saxon.s9api.XdmValue;
|
|
|
|
public class CalculateMd5 implements ExtensionFunction {
|
|
|
|
@Override
|
|
public QName getName() {
|
|
return new QName(XsltTransformerFactory.QNAME_BASE_URI + "/common", "md5");
|
|
}
|
|
|
|
@Override
|
|
public SequenceType getResultType() {
|
|
return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ZERO_OR_ONE);
|
|
}
|
|
|
|
@Override
|
|
public SequenceType[] getArgumentTypes() {
|
|
return new SequenceType[] { SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ZERO_OR_ONE) };
|
|
}
|
|
|
|
@Override
|
|
public XdmValue call(final XdmValue[] xdmValues) throws SaxonApiException {
|
|
final XdmValue r = xdmValues[0];
|
|
if (r.size() == 0) { return new XdmAtomicValue(""); }
|
|
final String currentValue = xdmValues[0].itemAt(0).getStringValue();
|
|
return new XdmAtomicValue(StringUtils.isNotBlank(currentValue) ? DigestUtils.md5Hex(currentValue) : null);
|
|
}
|
|
}
|