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