Project

General

Profile

1
package eu.dnetlib.data.transformation.service;
2

    
3
import java.util.function.UnaryOperator;
4
import javax.xml.transform.TransformerConfigurationException;
5
import javax.xml.transform.TransformerFactory;
6

    
7
import eu.dnetlib.common.profile.ProfileNotFoundException;
8
import eu.dnetlib.common.profile.ResourceDao;
9
import eu.dnetlib.data.collective.transformation.VocabularyRegistry;
10
import eu.dnetlib.data.collective.transformation.engine.SimpleTransformationEngine;
11
import eu.dnetlib.data.collective.transformation.engine.core.TransformationImpl;
12
import eu.dnetlib.data.collective.transformation.utils.TransformationRulesImportTool;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.springframework.core.io.Resource;
16

    
17
public class SimpleDataTransformer implements UnaryOperator<String> {
18

    
19
	/**
20
	 * logger.
21
	 */
22
	private static final Log log = LogFactory.getLog(SimpleDataTransformer.class);
23

    
24
	/**
25
	 * Transformation rule profile
26
	 */
27
	private String ruleProfile;
28
	private SimpleTransformationEngine transformationEngine;
29
	private TransformerFactory saxonTransformerFactory;
30

    
31
	public SimpleDataTransformer(final String ruleProfile, TransformerFactory transformerFactory) {
32
		this.saxonTransformerFactory = transformerFactory;
33
		this.ruleProfile = ruleProfile;
34

    
35
		// TODO
36
		// instantiate here the xml transformer
37

    
38
		if (log.isDebugEnabled()) {
39
			log.debug("************************************************************");
40
			log.debug("New transformer created from profile " + ruleProfile);
41
			log.debug("************************************************************");
42
		}
43

    
44
	}
45

    
46
	@Override
47
	public String apply(String record) {
48
		if (log.isDebugEnabled()) {
49
			log.debug("************************************************************");
50
			log.debug("INPUT: " + record);
51
			log.debug("************************************************************");
52
		}
53

    
54
		final String output = transform(record);
55

    
56
		if (log.isDebugEnabled()) {
57
			log.debug("************************************************************");
58
			log.debug("OUTPUT: " + output);
59
			log.debug("************************************************************");
60
		}
61

    
62
		return output;
63
	}
64

    
65
	private String transform(String record) {
66
		// use here the xml transformer
67
		return transformationEngine.transform(record);
68
	}
69

    
70
	protected void setupEngine(VocabularyRegistry vocabularyRegistry, Resource transformationTemplate,
71
			Resource defaultSchema, TransformationRulesImportTool rulesProfileUtil, ResourceDao resourceDao, Resource blacklistApi)
72
			throws TransformerConfigurationException, ProfileNotFoundException {
73
		transformationEngine = new SimpleTransformationEngine(saxonTransformerFactory);
74
		transformationEngine.setVocabularyRegistry(vocabularyRegistry);
75
		TransformationImpl transformation = new TransformationImpl(saxonTransformerFactory);
76
		transformation.setSchema(defaultSchema);
77
		transformation.setTemplate(transformationTemplate);
78
		transformation.init();
79
		if (log.isDebugEnabled()) {
80
			log.debug("************************************************************");
81
			log.debug(ruleProfile);
82
			log.debug("************************************************************");
83
		}
84
		transformation.setRuleLanguageParser(rulesProfileUtil.getRuleLanguageParser(ruleProfile));
85

    
86
		transformation.configureTransformation();
87
		transformationEngine.setTransformation(transformation);
88
		transformationEngine.setResourceDao(resourceDao);
89
		transformationEngine.setBlacklistApi(blacklistApi);
90
	}
91

    
92
	public String getRuleProfile() {
93
		return ruleProfile;
94
	}
95

    
96
	public void setRuleProfile(String ruleProfile) {
97
		this.ruleProfile = ruleProfile;
98
	}
99

    
100
}
(2-2/3)