Project

General

Profile

1
package eu.dnetlib.data.collective.transformation.utils;
2

    
3
import java.io.StringReader;
4
import java.util.List;
5

    
6
import org.apache.commons.text.StringEscapeUtils;
7

    
8
import eu.dnetlib.common.profile.ProfileNotFoundException;
9
import eu.dnetlib.data.collective.transformation.rulelanguage.RuleLanguageParser;
10
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
11
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
12
import eu.dnetlib.enabling.tools.ServiceLocator;
13

    
14
/**
15
 * 
16
 * @author jochen
17
 * @since 1.2
18
 */
19
public class TransformationRulesImportTool {
20
	private ServiceLocator<ISLookUpService> lookupServiceLocator;
21
	
22
	/**
23
	 * retrieves the transformation rule script of a transformation rule profile identified by a profile id
24
	 * @param aProfileId
25
	 * @return list of the transformation rule script and optionally profile id's of subscripts
26
	 * @throws ProfileNotFoundException
27
	 */
28
	protected List<String> getScript(String aProfileId) throws ProfileNotFoundException{
29
 		String xquery =  "collection('/db/DRIVER/TransformationRuleDSResources')//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value ='" +
30
 			aProfileId + "']//CODE/child::node(), " +
31
 			"for $id in (collection('/db/DRIVER/TransformationRuleDSResources')//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value ='" +
32
 			aProfileId + "']//IMPORTED/SCRIPT_REFERENCE/@id) return string($id)";
33
		List<String> queryResult;
34
		try {
35
			queryResult = lookupServiceLocator.getService().quickSearchProfile(xquery);
36
			if (!queryResult.isEmpty()){
37
				return queryResult;
38
			}else{
39
				throw new ProfileNotFoundException("no script found in profile for profileId: " + aProfileId);
40
			}
41
		} catch (ISLookUpException e) {
42
			throw new ProfileNotFoundException(e);
43
		}
44
	}
45
	
46
	protected void importRules(RuleLanguageParser aParser, String aProfileId) throws ProfileNotFoundException{
47
		List<String> profileQueryResult = getScript(aProfileId);
48
		String script = StringEscapeUtils.unescapeXml(profileQueryResult.get(0)); // the first entry contains the script
49
		if (script.trim().startsWith("<xsl:stylesheet")){
50
			aParser.setXslStylesheet(script.trim());
51
		}else{
52
			StringReader reader = new StringReader(script);
53
			aParser.parse(reader);
54
			if (aParser.getImportedScripts().size() != (profileQueryResult.size() - 1) ) throw new IllegalStateException(
55
					"invalid number of scripts to import: " + aParser.getImportedScripts().size() + " != " + profileQueryResult.size());
56
			// recursiv
57
			if (!aParser.getImportedScripts().isEmpty()){
58
				for (String importScriptProfileId: profileQueryResult.subList(1, profileQueryResult.size())){
59
					RuleLanguageParser childParser = new RuleLanguageParser();
60
					importRules(childParser, importScriptProfileId);
61
					aParser.addRulesFromParser(childParser);
62
				}
63
			}
64
		}
65
	}
66
	
67
	/**
68
	 * gets the rule language parser, which creates the mapping of rules which is defined in a transformation rule script identified by the transformation rule profile id
69
	 * @param aProfileId - id of the transformation rules profile
70
	 * @return the rule language parser
71
	 * @throws ProfileNotFoundException
72
	 */
73
	public RuleLanguageParser getRuleLanguageParser(String aProfileId) throws ProfileNotFoundException{
74
		RuleLanguageParser parser = new RuleLanguageParser();
75
		importRules(parser, aProfileId);
76
		return parser;
77
	}
78

    
79
	public ServiceLocator<ISLookUpService> getLookupServiceLocator() {
80
		return lookupServiceLocator;
81
	}
82

    
83
	public void setLookupServiceLocator(
84
			ServiceLocator<ISLookUpService> lookupServiceLocator) {
85
		this.lookupServiceLocator = lookupServiceLocator;
86
	}
87
	
88
}
(3-3/3)