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 eu.dnetlib.data.collective.transformation.core.xsl.XsltConstants;
9
import eu.dnetlib.data.collective.transformation.engine.functions.*;
10
import eu.dnetlib.data.collective.transformation.rulelanguage.Argument;
11
import org.apache.commons.lang3.StringEscapeUtils;
12

    
13
/**
14
 * TODO: make this class abstract and function classes (getValue, regexpr, ...) extending this class
15
 *
16
 * @author jochen
17
 */
18
public class FunctionCall {
19

    
20
	private String externalFunctionName;
21
	private Map<String, String> paramMap;
22
	private List<String> paramList;
23
	private String uuid;
24
	private List<Argument> argList = new LinkedList<Argument>();
25
	private boolean isStatic = false;
26
	private boolean doPreprocess = true;
27

    
28
	public FunctionCall(boolean aIsStatic) {
29
		uuid = UUID.randomUUID().toString();
30
		this.isStatic = aIsStatic;
31
	}
32

    
33
	public FunctionCall(boolean aIsStatic, boolean aDoPreprocess) {
34
		this(aIsStatic);
35
		this.doPreprocess = aDoPreprocess;
36
	}
37

    
38
	public boolean doPreprocess() {
39
		return this.doPreprocess;
40
	}
41

    
42
	public String getXSLpreparatedFunctionCall() {
43
		return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + uuid + "', $index" + ")";
44
	}
45

    
46
	public String getXSLpositionFunctionCall() {
47
		return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + uuid + "', $index" + ", $posVar" + ")";
48
	}
49

    
50
	public String getXSLdirectFunctionCall(String aCallId) {
51
		if (externalFunctionName.equals("regExpr")) {
52
			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(RegularExpression.paramExpr1) + ", "
53
					+ this.paramMap.get(RegularExpression.paramExpr2) + ", '" + this.paramMap.get(RegularExpression.paramRegularExpr) + "')";
54
		} else if (externalFunctionName.equals("convert")) {
55
			if (this.paramMap.containsKey(Convert.paramDefaultPattern) && this.paramMap.containsKey(Convert.paramFunction))
56
				return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '"
57
						+ this.paramMap.get(Convert.paramVocabularyName) + "', '" + this.paramMap.get(Convert.paramDefaultPattern) + "', '" + this.paramMap
58
						.get(Convert.paramFunction) + "')";
59
			else
60
				return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '"
61
						+ this.paramMap.get(Convert.paramVocabularyName) + "')";
62
		} else if (externalFunctionName.equals("convertString")) {
63
			if (this.paramMap.containsKey(Convert.paramDefaultPattern) && this.paramMap.containsKey(Convert.paramFunction))
64
				return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '"
65
						+ this.paramMap.get(Convert.paramVocabularyName) + "', '" + this.paramMap.get(Convert.paramDefaultPattern) + "', '" + this.paramMap
66
						.get(Convert.paramFunction) + "')";
67
			else
68
				return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Convert.paramFieldValue) + ", '"
69
						+ this.paramMap.get(Convert.paramVocabularyName) + "')";
70
		} else if (externalFunctionName.equals("split")) {
71
			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Split.paramInputExpr) + ", '" + this.paramMap
72
					.get(Split.paramRegExpr) + "', '" + aCallId + "')";
73
		} else if (externalFunctionName.equals("lookup")) {
74
			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", " + this.paramMap.get(Lookup.paramExprIdentifier) + ", '"
75
					+ this.paramMap.get(Lookup.paramExprProperty) + "')";
76
		} else if (externalFunctionName.equals("identifierExtract")) {
77
			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + StringEscapeUtils
78
					.escapeXml10(this.paramMap.get(IdentifierExtract.paramXpathExprJson)) + "', " + this.paramMap.get(IdentifierExtract.paramXpathExprInSource)
79
					+ ", '" + StringEscapeUtils.escapeXml10(this.paramMap.get(IdentifierExtract.paramRegExpr)) + "')";
80
		} else {
81
			throw new IllegalStateException("unsupported function call: " + externalFunctionName);
82
		}
83
	}
84

    
85
	public String getXSLdirectFunctionCallById(String aCallId) {
86
		if (externalFunctionName.equals("split")) {
87
			return XsltConstants.extFuncNS + ":" + externalFunctionName + "(" + "$tf" + ", '" + aCallId + "')";
88
		} else {
89
			throw new IllegalStateException("unsupported function call: " + externalFunctionName);
90
		}
91
	}
92

    
93
	public String getExternalFunctionName() {
94
		return externalFunctionName;
95
	}
96

    
97
	public void setExternalFunctionName(String externalFunctionName) {
98
		this.externalFunctionName = externalFunctionName;
99
	}
100

    
101
	public void addArgument(Argument arg) {
102
		this.argList.add(arg);
103
	}
104

    
105
	public List<Argument> getArguments() {
106
		return this.argList;
107
	}
108

    
109
	public void setArguments(List<Argument> aArgList) {
110
		this.argList = aArgList;
111
	}
112

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

    
117
	public void setParameters(Map<String, String> parameters) {
118
		this.paramMap = parameters;
119
	}
120

    
121
	public String getUuid() {
122
		return uuid;
123
	}
124

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

    
132
	/**
133
	 * @param isStatic the isStatic to set
134
	 */
135
	public void setStatic(boolean isStatic) {
136
		this.isStatic = isStatic;
137
	}
138

    
139
	/**
140
	 * @return the paramList
141
	 */
142
	public List<String> getParamList() {
143
		return paramList;
144
	}
145

    
146
	/**
147
	 * @param paramList the paramList to set
148
	 */
149
	public void setParamList(List<String> paramList) {
150
		this.paramList = paramList;
151
	}
152
}
(2-2/2)