1
|
package eu.dnetlib.data.search.transform.config;
|
2
|
|
3
|
|
4
|
import eu.dnetlib.data.search.transform.*;
|
5
|
import eu.dnetlib.data.search.transform.formatter.Formatter;
|
6
|
import eu.dnetlib.data.search.transform.formatter.XSLTFormatter;
|
7
|
import eu.dnetlib.data.search.transform.utils.VelocityUtil;
|
8
|
import eu.dnetlib.data.search.utils.vocabulary.VocabularyManager;
|
9
|
import org.apache.commons.io.IOUtils;
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.apache.velocity.Template;
|
12
|
import org.apache.velocity.VelocityContext;
|
13
|
import org.apache.velocity.app.VelocityEngine;
|
14
|
|
15
|
import java.io.InputStream;
|
16
|
import java.io.StringWriter;
|
17
|
import java.util.*;
|
18
|
|
19
|
public class SearchRegistry implements TransformerRegistry, FormatterRegistry {
|
20
|
private Logger logger = Logger.getLogger(this.getClass());
|
21
|
|
22
|
private Configuration config = null;
|
23
|
private VocabularyManager vocabularyManager = null;
|
24
|
|
25
|
private Map<Locale,HashMap<String,Transformer>> transformers = new HashMap<Locale, HashMap<String,Transformer>>();
|
26
|
private Map<String, Formatter> formatters = new HashMap<String, Formatter>();
|
27
|
|
28
|
@Override
|
29
|
public Transformer getTransformer(String name, Locale locale) {
|
30
|
logger.debug("Requested transformer " + name + " locale " + locale);
|
31
|
Transformer tx = null;
|
32
|
|
33
|
if(name == null) {
|
34
|
return null;
|
35
|
}
|
36
|
|
37
|
if (locale == null) {
|
38
|
locale = config.getDefaultLocale();
|
39
|
}
|
40
|
|
41
|
try {
|
42
|
if (this.transformers.get(locale)!= null) {
|
43
|
tx = this.transformers.get(locale).get(name);
|
44
|
|
45
|
} else { //not map for locale
|
46
|
logger.debug("Transofrmation map " + config.getTransformationsMap().keySet());
|
47
|
tx = createChainTransformer(name, locale, config.getTransformationsMap().get(name));
|
48
|
Map<String, Transformer> map = new HashMap<String, Transformer>();
|
49
|
map.put(name, tx);
|
50
|
transformers.put(locale, (HashMap<String, Transformer>) map);
|
51
|
}
|
52
|
|
53
|
if (tx == null) { // there is the local map but not the transformer
|
54
|
tx = createChainTransformer(name, locale, config.getTransformationsMap().get(name));
|
55
|
transformers.get(locale).put(name, tx);
|
56
|
}
|
57
|
|
58
|
} catch (Exception e) {
|
59
|
logger.error("Cannot create transformer " + name, e);
|
60
|
throw new IllegalArgumentException("Cannot create transformer " + name, e);
|
61
|
}
|
62
|
|
63
|
logger.debug("Returned in get transformer" + tx);
|
64
|
return tx;
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public Formatter getFormatter(String name) {
|
69
|
Formatter fm = this.formatters.get(name);
|
70
|
|
71
|
try {
|
72
|
if (fm == null) {
|
73
|
FormatterConfiguration formatterConfiguration = config.getFormattersMap().get(name);
|
74
|
String formatterXslt = formatterConfiguration.getXsl_file();
|
75
|
String template = formatterConfiguration.getTemplate();
|
76
|
|
77
|
logger.debug("Configuration for formatter: " + formatterXslt + ", " + template);
|
78
|
|
79
|
if (formatterXslt == null) {
|
80
|
logger.error("Cannot create formatter " + name);
|
81
|
throw new IllegalArgumentException("Cannot create formatter " + name);
|
82
|
}
|
83
|
|
84
|
if (template == null) {
|
85
|
fm = createFormatter(formatterXslt);
|
86
|
} else {
|
87
|
fm = createFormatter(formatterXslt, template);
|
88
|
}
|
89
|
|
90
|
formatters.put(name, fm);
|
91
|
}
|
92
|
|
93
|
} catch (Exception e) {
|
94
|
logger.error("Cannot create formatter " + name);
|
95
|
throw new IllegalArgumentException("Cannot create formatter " + name, e);
|
96
|
}
|
97
|
|
98
|
return fm;
|
99
|
}
|
100
|
|
101
|
private Transformer createChainTransformer(String name, Locale locale, List<Transformation> transformations) throws SearchFactoryException {
|
102
|
List<XPathTrasformation> temp = new ArrayList<XPathTrasformation>();
|
103
|
List<Transformer> forChaining = new ArrayList<Transformer>();
|
104
|
for (Transformation tr: transformations){
|
105
|
if (tr instanceof XSLTTransformation) {
|
106
|
try {
|
107
|
if (!temp.isEmpty()){
|
108
|
forChaining.add(createTransformer(locale, temp));
|
109
|
}
|
110
|
temp.clear();
|
111
|
forChaining.add(createTransformer((XSLTTransformation)tr));
|
112
|
} catch (Exception e) {
|
113
|
logger.warn("Error creating a transformer with name: "+name+" and locale: "+locale.getDisplayName(), e);
|
114
|
throw new SearchFactoryException("Error creating a transformer with name: "+name+" and locale: "+locale.getDisplayName(), e);
|
115
|
|
116
|
}
|
117
|
}
|
118
|
else {
|
119
|
temp.add((XPathTrasformation)tr);
|
120
|
}
|
121
|
}
|
122
|
if (!temp.isEmpty()){
|
123
|
try {
|
124
|
forChaining.add(createTransformer(locale, temp));
|
125
|
} catch (Exception e) {
|
126
|
logger.warn("Error creating a transformer with name: " + name + " and locale: "+locale.getDisplayName(), e);
|
127
|
throw new SearchFactoryException("Error creating a transformer with name: " + name + " and locale: "+locale.getDisplayName(), e);
|
128
|
}
|
129
|
}
|
130
|
return new ChainTransformer(name, forChaining);
|
131
|
}
|
132
|
|
133
|
private Transformer createTransformer(XSLTTransformation transformation) throws Exception {
|
134
|
String xsl_file = transformation.getXslt();
|
135
|
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xsl_file);
|
136
|
StringWriter writer = new StringWriter();
|
137
|
IOUtils.copy(inputStream, writer);
|
138
|
String xsl = writer.toString();
|
139
|
|
140
|
XsltTransformer OptimusPrime = new XsltTransformer(xsl);
|
141
|
|
142
|
writer.close();
|
143
|
IOUtils.closeQuietly(inputStream);
|
144
|
|
145
|
return OptimusPrime;
|
146
|
}
|
147
|
|
148
|
private Transformer createTransformer(Locale locale, List<XPathTrasformation> transformations) throws Exception {
|
149
|
VelocityEngine ve = VelocityUtil.getEngine();
|
150
|
|
151
|
Template t = ve.getTemplate("/eu/dnetlib/data/search/transform/config/velocity.vm", "UTF-8");
|
152
|
VelocityContext context = new VelocityContext();
|
153
|
|
154
|
context.put("locale", locale);
|
155
|
context.put("defLocale", config.getDefaultLocale());
|
156
|
context.put("transformations", transformations);
|
157
|
StringWriter writer = new StringWriter();
|
158
|
t.merge(context, writer);
|
159
|
XsltTransformer Megatron = new XsltTransformer(writer.toString());
|
160
|
|
161
|
//logger.debug("Transformer xsl " + Megatron.getXslt());
|
162
|
//logger.debug("Vocab paths are: " + loadVocabulariesPathMap(transformations, locale).toString());
|
163
|
|
164
|
return Megatron;
|
165
|
}
|
166
|
|
167
|
private Formatter createFormatter(String xsl_file) throws Exception {
|
168
|
logger.debug("Loading xslt " + xsl_file);
|
169
|
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xsl_file);
|
170
|
|
171
|
StringWriter writer = new StringWriter();
|
172
|
IOUtils.copy(inputStream, writer);
|
173
|
String xsl = writer.toString();
|
174
|
|
175
|
XSLTFormatter Cybertron = new XSLTFormatter(xsl);
|
176
|
writer.close();
|
177
|
IOUtils.closeQuietly(inputStream);
|
178
|
|
179
|
return Cybertron;
|
180
|
}
|
181
|
|
182
|
private Formatter createFormatter(String xsl_file, String template) throws Exception {
|
183
|
logger.debug("Loading xslt " + xsl_file);
|
184
|
logger.debug("Loading template " + template);
|
185
|
|
186
|
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(xsl_file);
|
187
|
|
188
|
StringWriter writer = new StringWriter();
|
189
|
IOUtils.copy(inputStream, writer);
|
190
|
String xsl = writer.toString();
|
191
|
|
192
|
XSLTFormatter Cybertron = new XSLTFormatter(xsl, template);
|
193
|
writer.close();
|
194
|
IOUtils.closeQuietly(inputStream);
|
195
|
|
196
|
return Cybertron;
|
197
|
}
|
198
|
|
199
|
public Configuration getConfig() {
|
200
|
return config;
|
201
|
}
|
202
|
|
203
|
public void setConfig(Configuration config) {
|
204
|
this.config = config;
|
205
|
}
|
206
|
|
207
|
public void setVocabularyManager(VocabularyManager vocabularyManager) {
|
208
|
this.vocabularyManager = vocabularyManager;
|
209
|
}
|
210
|
|
211
|
public VocabularyManager getVocabularyManager() {
|
212
|
return vocabularyManager;
|
213
|
}
|
214
|
|
215
|
}
|