1
|
package eu.dnetlib.data.search.transform.config;
|
2
|
|
3
|
import eu.dnetlib.api.data.SearchService;
|
4
|
import eu.dnetlib.data.search.utils.vocabulary.ISVocabulary;
|
5
|
import eu.dnetlib.data.search.utils.vocabulary.IndexVocabulary;
|
6
|
import eu.dnetlib.data.search.utils.vocabulary.LocalVocabulary;
|
7
|
import eu.dnetlib.data.search.utils.vocabulary.Vocabulary;
|
8
|
import org.apache.commons.io.IOUtils;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.core.io.Resource;
|
11
|
import org.w3c.dom.Element;
|
12
|
import org.w3c.dom.NodeList;
|
13
|
import org.xml.sax.InputSource;
|
14
|
|
15
|
import javax.xml.xpath.*;
|
16
|
import java.io.ByteArrayOutputStream;
|
17
|
import java.io.IOException;
|
18
|
import java.io.InputStream;
|
19
|
import java.io.StringReader;
|
20
|
import java.net.URISyntaxException;
|
21
|
import java.util.ArrayList;
|
22
|
import java.util.List;
|
23
|
import java.util.Locale;
|
24
|
import java.util.StringTokenizer;
|
25
|
|
26
|
/**
|
27
|
*
|
28
|
* @author kiatrop
|
29
|
*
|
30
|
*/
|
31
|
|
32
|
/**
|
33
|
* ConfigurationFactory creates a {@link Configuration} for the {@link SearchService}
|
34
|
* based on an XML configuration file.
|
35
|
*
|
36
|
* configurationName is the name of the XML configuration file
|
37
|
* vocabulariesPath is the local folder that is the working folder for {@link SearchService}
|
38
|
* configurationXml has the content of the XML configuration file
|
39
|
*/
|
40
|
public class ConfigurationFactory {
|
41
|
|
42
|
private Resource configurationName;
|
43
|
private String vocabulariesPath;
|
44
|
private String configurationXml;
|
45
|
|
46
|
private static Logger logger = Logger.getLogger(ConfigurationFactory.class);
|
47
|
|
48
|
public void init() throws IOException, URISyntaxException {
|
49
|
logger.info("Reading configuration file " + configurationName + " from classpath");
|
50
|
|
51
|
InputStream stream = null;
|
52
|
|
53
|
try {
|
54
|
stream = configurationName.getInputStream();
|
55
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
56
|
|
57
|
IOUtils.copy(stream, baos);
|
58
|
|
59
|
this.configurationXml = baos.toString();
|
60
|
} finally {
|
61
|
IOUtils.closeQuietly(stream);
|
62
|
}
|
63
|
|
64
|
logger.debug("Configuration read.\n" + configurationXml);
|
65
|
}
|
66
|
|
67
|
public Configuration createConfiguration(String id) throws ConfigurationFactoryException {
|
68
|
Configuration configuration = new Configuration(id);
|
69
|
XPathFactory factory = XPathFactory.newInstance();
|
70
|
XPath xPath = factory.newXPath();
|
71
|
|
72
|
try {
|
73
|
readLocales(xPath, configuration);
|
74
|
readVocabularies(xPath, configuration);
|
75
|
readTransformers(xPath, configuration);
|
76
|
readFormatters(xPath, configuration);
|
77
|
|
78
|
} catch (XPathExpressionException|IOException ex) {
|
79
|
logger.error("Error parsing configuration file.", ex);
|
80
|
throw new ConfigurationFactoryException("Error parsing configuration file.", ex);
|
81
|
|
82
|
}
|
83
|
|
84
|
return configuration;
|
85
|
}
|
86
|
|
87
|
private void readLocales(XPath xPath, Configuration configuration) throws XPathExpressionException {
|
88
|
XPathExpression xPathExpression = xPath.compile("/configuration/locales/locale");
|
89
|
NodeList nodes = (NodeList) xPathExpression.evaluate(
|
90
|
new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
|
91
|
|
92
|
for (int i = 0; i < nodes.getLength(); i++) {
|
93
|
StringTokenizer tokenizer = new StringTokenizer(nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(), "_");
|
94
|
Locale locale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
|
95
|
configuration.getLocales().add(locale);
|
96
|
|
97
|
if (nodes.item(i).getAttributes().getNamedItem("default").getNodeValue().equals("true")) {
|
98
|
configuration.setDefaultLocale(locale);
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
|
103
|
private void readVocabularies(XPath xPath, Configuration configuration) throws XPathExpressionException, IOException, ConfigurationFactoryException {
|
104
|
String vocabularyName = null;
|
105
|
Vocabulary vocabulary = null;
|
106
|
|
107
|
XPathExpression xPathExpression = xPath.compile("/configuration/vocabularies/local_vocabulary");
|
108
|
NodeList nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
|
109
|
for (int i = 0; i < nodes.getLength(); i++) {
|
110
|
vocabularyName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
|
111
|
vocabulary = new LocalVocabulary(vocabularyName, nodes.item(i).getAttributes().getNamedItem("file").getNodeValue());
|
112
|
configuration.getLocalVocabularyMap().put(vocabularyName, vocabulary);
|
113
|
}
|
114
|
|
115
|
xPathExpression = xPath.compile("/configuration/vocabularies/is_vocabulary");
|
116
|
nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
|
117
|
|
118
|
for (int i = 0; i < nodes.getLength(); i++) {
|
119
|
vocabularyName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
|
120
|
String isVocabularyName = nodes.item(i).getAttributes().getNamedItem("xml_name").getNodeValue();
|
121
|
vocabulary = new ISVocabulary(vocabularyName, isVocabularyName);
|
122
|
configuration.getIsVocabularyMap().put(vocabularyName, vocabulary);
|
123
|
}
|
124
|
|
125
|
xPathExpression = xPath.compile("/configuration/vocabularies/index_vocabulary");
|
126
|
nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
|
127
|
|
128
|
for (int i = 0; i < nodes.getLength(); i++) {
|
129
|
vocabularyName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
|
130
|
String query = nodes.item(i).getAttributes().getNamedItem("query").getNodeValue();
|
131
|
String transformer = nodes.item(i).getAttributes().getNamedItem("transformer").getNodeValue();
|
132
|
vocabulary = new IndexVocabulary(vocabularyName, query, transformer);
|
133
|
configuration.getIndexVocabularyMap().put(vocabularyName, vocabulary);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
private void readTransformers(XPath xPath, Configuration configuration) throws XPathExpressionException {
|
138
|
XPathExpression xPathExpression = xPath.compile("/configuration/transformers/transformer");
|
139
|
NodeList nodes = (NodeList) xPathExpression.evaluate(
|
140
|
new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
|
141
|
|
142
|
//per transformer
|
143
|
for (int i = 0; i < nodes.getLength(); i++) {
|
144
|
NodeList transformationNodes = ((Element) nodes.item(i)).getElementsByTagName("transformation");
|
145
|
|
146
|
List<Transformation> transformations = new ArrayList<Transformation>();
|
147
|
for (int j=0; j < transformationNodes.getLength(); j++) {
|
148
|
Element transformation = (Element) transformationNodes.item(j);
|
149
|
|
150
|
//logger.debug("Tr " + transformation.getAttributes().getNamedItem("xslt"));
|
151
|
if (transformation.getAttributes().getNamedItem("xslt") != null) {
|
152
|
transformations.add(new XSLTTransformation(transformation.getAttributes().getNamedItem("xslt").getNodeValue()));
|
153
|
//logger.debug("Tr2 " + transformation.getAttributes().getNamedItem("xslt").getNodeValue());
|
154
|
} else {
|
155
|
String match = transformation.getAttributes().getNamedItem("match").getNodeValue();
|
156
|
String change = transformation.getAttributes().getNamedItem("change").getNodeValue();
|
157
|
String vocabulary = transformation.getAttributes().getNamedItem("vocabulary").getNodeValue();
|
158
|
|
159
|
transformations.add(new XPathTrasformation(match, change, vocabulary));
|
160
|
}
|
161
|
}
|
162
|
|
163
|
String transformerName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
|
164
|
configuration.getTransformationsMap().put(transformerName, transformations);
|
165
|
|
166
|
}
|
167
|
}
|
168
|
|
169
|
private void readFormatters(XPath xPath, Configuration configuration) throws XPathExpressionException {
|
170
|
XPathExpression xPathExpression = xPath.compile("/configuration/formatters/formatter");
|
171
|
NodeList nodes = (NodeList) xPathExpression.evaluate(
|
172
|
new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
|
173
|
|
174
|
//per formatter
|
175
|
for (int i = 0; i < nodes.getLength(); i++) {
|
176
|
FormatterConfiguration formatterConfiguration = new FormatterConfiguration();
|
177
|
formatterConfiguration.setXsl_file(nodes.item(i).getAttributes().getNamedItem("xslt").getNodeValue());
|
178
|
|
179
|
if (nodes.item(i).getAttributes().getNamedItem("template") != null) {
|
180
|
formatterConfiguration.setTemplate(nodes.item(i).getAttributes().getNamedItem("template").getNodeValue());
|
181
|
}
|
182
|
|
183
|
configuration.getFormattersMap().put(
|
184
|
nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(), formatterConfiguration);
|
185
|
}
|
186
|
}
|
187
|
|
188
|
public void setConfigurationName(Resource configurationName) {
|
189
|
this.configurationName = configurationName;
|
190
|
}
|
191
|
|
192
|
public void setVocabulariesPath(String vocabulariesPath) {
|
193
|
this.vocabulariesPath = vocabulariesPath;
|
194
|
}
|
195
|
|
196
|
public Resource getConfigurationName() {
|
197
|
return configurationName;
|
198
|
}
|
199
|
|
200
|
public String getVocabulariesPath() {
|
201
|
return vocabulariesPath;
|
202
|
}
|
203
|
|
204
|
public String getConfigurationXml() {
|
205
|
return configurationXml;
|
206
|
}
|
207
|
|
208
|
public static Logger getLogger() {
|
209
|
return logger;
|
210
|
}
|
211
|
}
|