Project

General

Profile

1
package eu.dnetlib.data.collective.transformation.rulelanguage.util;
2

    
3
import java.util.LinkedList;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.UUID;
7

    
8
import org.apache.commons.text.StringEscapeUtils;
9

    
10
import eu.dnetlib.data.collective.transformation.core.xsl.XsltConstants;
11
import eu.dnetlib.data.collective.transformation.engine.functions.Convert;
12
import eu.dnetlib.data.collective.transformation.engine.functions.IdentifierExtract;
13
import eu.dnetlib.data.collective.transformation.engine.functions.Lookup;
14
import eu.dnetlib.data.collective.transformation.engine.functions.RegularExpression;
15
import eu.dnetlib.data.collective.transformation.engine.functions.Split;
16
import eu.dnetlib.data.collective.transformation.rulelanguage.Argument;
17

    
18
/**
19
 * TODO: make this class abstract and function classes (getValue, regexpr, ...) extending this class
20
 * @author jochen
21
 *
22
 */
23
public class FunctionCall {
24

    
25
	private String externalFunctionName;
26
	private Map<String, String> paramMap;
27
	private List<String> paramList;
28
	private String uuid;
29
	private List<Argument> argList = new LinkedList<Argument>();
30
	private boolean isStatic = false;
31
	private boolean doPreprocess = true;
32
	
33
	public FunctionCall(boolean aIsStatic){
34
		uuid = UUID.randomUUID().toString();
35
		this.isStatic = aIsStatic;
36
	}
37
	
38
	public FunctionCall(boolean aIsStatic, boolean aDoPreprocess){
39
		this(aIsStatic);
40
		this.doPreprocess = aDoPreprocess;
41
	}
42
	
43
	public boolean doPreprocess(){
44
		return this.doPreprocess;
45
	}
46
	
47
	public String getXSLpreparatedFunctionCall(){
48
		 return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + uuid + "', $index" + ")";
49
	}
50
	
51
	public String getXSLpositionFunctionCall(){
52
		 return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + uuid + "', $index" + ", $posVar" + ")";
53
	}
54
	
55
    public String getXSLdirectFunctionCall(String aCallId){
56
    	if (externalFunctionName.equals("regExpr")){
57
        	return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(RegularExpression.paramExpr1) + ", " + this.paramMap.get(RegularExpression.paramExpr2) + ", '" + this.paramMap.get(RegularExpression.paramRegularExpr) + "')";
58
    	}else if (externalFunctionName.equals("convert")){
59
    		if (this.paramMap.containsKey(Convert.paramDefaultPattern) && this.paramMap.containsKey(Convert.paramFunction))
60
    			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '" + this.paramMap.get(Convert.paramVocabularyName) + "', '" + this.paramMap.get(Convert.paramDefaultPattern) + "', '" + this.paramMap.get(Convert.paramFunction) + "')";
61
   			else
62
   				return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '" + this.paramMap.get(Convert.paramVocabularyName) + "')";
63
    	}else if (externalFunctionName.equals("convertString")){
64
    		if (this.paramMap.containsKey(Convert.paramDefaultPattern) && this.paramMap.containsKey(Convert.paramFunction))
65
    			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '" + this.paramMap.get(Convert.paramVocabularyName) + "', '" + this.paramMap.get(Convert.paramDefaultPattern) + "', '" + this.paramMap.get(Convert.paramFunction) + "')";
66
   			else
67
   				return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '" + this.paramMap.get(Convert.paramVocabularyName) + "')";
68
    	}else if (externalFunctionName.equals("split")){
69
    		return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Split.paramInputExpr) + ", '" + this.paramMap.get(Split.paramRegExpr) + "', '" + aCallId + "')";
70
    	}else if (externalFunctionName.equals("lookup")){
71
    		return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Lookup.paramExprIdentifier) + ", '" + this.paramMap.get(Lookup.paramExprProperty) + "')";
72
    	}else if (externalFunctionName.equals("identifierExtract")){
73
    		return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + StringEscapeUtils.escapeXml10(this.paramMap.get(IdentifierExtract.paramXpathExprJson)) + "', " + this.paramMap.get(IdentifierExtract.paramXpathExprInSource) + ", '" + StringEscapeUtils.escapeXml10(this.paramMap.get(IdentifierExtract.paramRegExpr)) + "')";
74
    	}else{
75
    		throw new IllegalStateException("unsupported function call: " + externalFunctionName);
76
    	}
77
    }	
78
    
79
    public String getXSLdirectFunctionCallById(String aCallId){
80
    	if (externalFunctionName.equals("split")){
81
    		return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + aCallId + "')";
82
    	}else{
83
    		throw new IllegalStateException("unsupported function call: " + externalFunctionName);    		
84
    	}   	
85
    }
86

    
87
	public void setExternalFunctionName(String externalFunctionName) {
88
		this.externalFunctionName = externalFunctionName;
89
	}
90

    
91
	public String getExternalFunctionName() {
92
		return externalFunctionName;
93
	}
94
	
95
	public void addArgument(Argument arg){
96
		this.argList.add(arg);
97
	}
98

    
99
	public void setArguments(List<Argument> aArgList){
100
		this.argList = aArgList;
101
	}
102
	
103
	public List<Argument> getArguments(){
104
		return this.argList;
105
	}
106
	
107
	public void setParameters(Map<String, String> parameters) {
108
		this.paramMap = parameters;
109
	}
110

    
111
	public Map<String, String> getParameters() {
112
		return paramMap;
113
	}
114

    
115
	public String getUuid() {
116
		return uuid;
117
	}
118

    
119
	/**
120
	 * @param isStatic the isStatic to set
121
	 */
122
	public void setStatic(boolean isStatic) {
123
		this.isStatic = isStatic;
124
	}
125

    
126
	/**
127
	 * @return the isStatic
128
	 */
129
	public boolean isStatic() {
130
		return isStatic;
131
	}
132

    
133
	/**
134
	 * @return the paramList
135
	 */
136
	public List<String> getParamList() {
137
		return paramList;
138
	}
139

    
140
	/**
141
	 * @param paramList the paramList to set
142
	 */
143
	public void setParamList(List<String> paramList) {
144
		this.paramList = paramList;
145
	}
146
}
(2-2/2)