Project

General

Profile

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
			//clean vocabularies folder
74
			FileUtils.deleteDirectory(new File(vocabulariesPath));
75
			
76
		} catch (IOException ioe) {
77
			logger.warn("Error parsing configuration file.", ioe);
78
		
79
		} finally {		
80
			new File(vocabulariesPath).mkdir();
81
		}*/
82
		
83
		try {
84
			readLocales(xPath, configuration);
85
			readVocabularies(xPath, configuration);
86
			readTransformers(xPath, configuration);
87
			readFormatters(xPath, configuration);
88
			
89
		} catch (XPathExpressionException xpee) {
90
			logger.error("Error parsing configuration file.", xpee);
91
			throw new ConfigurationFactoryException("Error parsing configuration file.", xpee);
92
			
93
		} catch (IOException ioe) {
94
			logger.warn("Error parsing configuration file.", ioe);
95
			throw new ConfigurationFactoryException("Error parsing configuration file.", ioe);
96
		}
97
		
98
		return configuration;
99
	}
100
	
101
	private void readLocales(XPath xPath, Configuration configuration) throws XPathExpressionException {
102
		XPathExpression  xPathExpression = xPath.compile("/configuration/locales/locale");
103
		NodeList nodes = (NodeList) xPathExpression.evaluate(
104
				new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
105
					
106
		for (int i = 0; i < nodes.getLength(); i++) {
107
			StringTokenizer tokenizer = new StringTokenizer(nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(), "_");
108
			Locale locale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
109
			configuration.getLocales().add(locale);
110
			
111
			if (nodes.item(i).getAttributes().getNamedItem("default").getNodeValue().equals("true")) {
112
				configuration.setDefaultLocale(locale);
113
			}
114
		}
115
	}
116
	
117
	private void readVocabularies(XPath xPath, Configuration configuration) throws XPathExpressionException, IOException, ConfigurationFactoryException {		
118
		String vocabularyName = null;
119
		Vocabulary vocabulary = null;
120
		
121
		XPathExpression  xPathExpression = xPath.compile("/configuration/vocabularies/local_vocabulary");
122
		NodeList nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);		
123
		for (int i = 0; i < nodes.getLength(); i++) {
124
			vocabularyName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
125
			vocabulary = new LocalVocabulary(vocabularyName, nodes.item(i).getAttributes().getNamedItem("file").getNodeValue());
126
			configuration.getLocalVocabularyMap().put(vocabularyName, vocabulary);
127
		}
128
		
129
		xPathExpression = xPath.compile("/configuration/vocabularies/is_vocabulary");
130
		nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
131
		
132
		for (int i = 0; i < nodes.getLength(); i++) {
133
			vocabularyName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
134
			String isVocabularyName = nodes.item(i).getAttributes().getNamedItem("xml_name").getNodeValue();
135
			vocabulary = new ISVocabulary(vocabularyName, isVocabularyName);
136
			configuration.getIsVocabularyMap().put(vocabularyName, vocabulary);
137
		}
138
		
139
		xPathExpression = xPath.compile("/configuration/vocabularies/index_vocabulary");
140
		nodes = (NodeList) xPathExpression.evaluate(new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
141
		
142
		for (int i = 0; i < nodes.getLength(); i++) {
143
			vocabularyName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
144
			String query = nodes.item(i).getAttributes().getNamedItem("query").getNodeValue();
145
			String transformer = nodes.item(i).getAttributes().getNamedItem("transformer").getNodeValue();			
146
			vocabulary = new IndexVocabulary(vocabularyName, query, transformer);
147
			configuration.getIndexVocabularyMap().put(vocabularyName, vocabulary);
148
		}
149
	}
150

    
151
	private void readTransformers(XPath xPath, Configuration configuration) throws XPathExpressionException {
152
		XPathExpression xPathExpression = xPath.compile("/configuration/transformers/transformer");
153
		NodeList nodes = (NodeList) xPathExpression.evaluate(
154
				new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
155

    
156
		//per transformer
157
		for (int i = 0; i < nodes.getLength(); i++) {
158
			NodeList transformationNodes = ((Element) nodes.item(i)).getElementsByTagName("transformation");
159
			
160
			List<Transformation> transformations = new ArrayList<Transformation>();
161
			for (int j=0; j < transformationNodes.getLength(); j++) {
162
				Element transformation = (Element) transformationNodes.item(j);
163

    
164
				logger.debug("Tr " + transformation.getAttributes().getNamedItem("xslt"));
165
				if (transformation.getAttributes().getNamedItem("xslt") != null) {
166
					transformations.add(new XSLTTransformation(transformation.getAttributes().getNamedItem("xslt").getNodeValue()));
167
					logger.debug("Tr2 " + transformation.getAttributes().getNamedItem("xslt").getNodeValue());
168
				} else {
169
					String match = transformation.getAttributes().getNamedItem("match").getNodeValue();
170
					String change = transformation.getAttributes().getNamedItem("change").getNodeValue();
171
					String vocabulary = transformation.getAttributes().getNamedItem("vocabulary").getNodeValue();
172
					
173
					transformations.add(new XPathTrasformation(match, change, vocabulary));
174
				}			
175
			}
176
			
177
			String transformerName = nodes.item(i).getAttributes().getNamedItem("name").getNodeValue();
178
			configuration.getTransformationsMap().put(transformerName, transformations);
179

    
180
		}
181
	}
182
	
183
	private void readFormatters(XPath xPath, Configuration configuration) throws XPathExpressionException {
184
		XPathExpression xPathExpression = xPath.compile("/configuration/formatters/formatter");
185
		NodeList nodes = (NodeList) xPathExpression.evaluate(
186
				new InputSource(new StringReader(configurationXml)), XPathConstants.NODESET);
187
		
188
		//per formatter
189
		for (int i = 0; i < nodes.getLength(); i++) {
190
            FormatterConfiguration formatterConfiguration = new FormatterConfiguration();
191
            formatterConfiguration.setXsl_file(nodes.item(i).getAttributes().getNamedItem("xslt").getNodeValue());
192

    
193
            if (nodes.item(i).getAttributes().getNamedItem("template") != null) {
194
                formatterConfiguration.setTemplate(nodes.item(i).getAttributes().getNamedItem("template").getNodeValue());
195
            }
196

    
197
            configuration.getFormattersMap().put(
198
                nodes.item(i).getAttributes().getNamedItem("name").getNodeValue(), formatterConfiguration);
199
			}
200
	}
201

    
202
	public void setConfigurationName(Resource configurationName) {
203
		this.configurationName = configurationName;
204
	}
205

    
206
	public void setVocabulariesPath(String vocabulariesPath) {
207
		this.vocabulariesPath = vocabulariesPath;
208
	}
209

    
210
	public Resource getConfigurationName() {
211
		return configurationName;
212
	}
213

    
214
	public String getVocabulariesPath() {
215
		return vocabulariesPath;
216
	}
217

    
218
	public String getConfigurationXml() {
219
		return configurationXml;
220
	}
221

    
222
	public static Logger getLogger() {
223
		return logger;
224
	}
225
}
(2-2/9)