Project

General

Profile

1
package eu.dnetlib.data.resultSet.cleaner;
2

    
3
import groovy.lang.Closure;
4
import groovy.lang.GroovyShell;
5

    
6
import java.util.Map;
7

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

    
11
/**
12
 * @author michele
13
 * 
14
 *         groovy example 1 : "(input =~ /X/).replaceAll('Y')" groovy example 2 : "input.toUpperCase()"
15
 */
16

    
17
public class GroovyRule extends XPathCleaningRule {
18

    
19
	private static final Log log = LogFactory.getLog(GroovyRule.class); // NOPMD by marko on 11/24/08 5:02 PM
20

    
21
	private String groovyRule;
22
	private Closure<String> closure;
23

    
24
	private GroovyShell groovyShell = new GroovyShell();
25

    
26
	@SuppressWarnings("unchecked")
27
	public GroovyRule(final String groovyRule, final String xpath, final boolean strict) {
28
		super(xpath, strict);
29
		this.groovyRule = groovyRule;
30
		this.closure = (Closure<String>) groovyShell.evaluate("{ input -> " + groovyRule + "}");
31
	}
32

    
33
	@Override
34
	protected String calculateNewValue(final String oldValue) {
35
		log.info("Executing groovy closure on value " + oldValue);
36
		return closure.call(oldValue);
37
	}
38

    
39
	@Override
40
	protected Map<String, String> verifyValue(final String value) {
41
		return null;
42
	}
43

    
44
	@Override
45
	public String toString() {
46
		return "GROOVY: " + groovyRule;
47
	}
48
}
(3-3/5)