1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import com.ximpleware.AutoPilot; |
---|
4 | import com.ximpleware.NavException; |
---|
5 | import com.ximpleware.VTDGen; |
---|
6 | import com.ximpleware.VTDNav; |
---|
7 | import com.ximpleware.XPathEvalException; |
---|
8 | import com.ximpleware.XPathParseException; |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.HashMap; |
---|
11 | import java.util.List; |
---|
12 | import org.slf4j.Logger; |
---|
13 | import org.slf4j.LoggerFactory; |
---|
14 | |
---|
15 | /** |
---|
16 | * Takes the value of the componentprofileid and uses the componentregistry REST service to transform this to the name of the componentprofile. |
---|
17 | */ |
---|
18 | public class CMDIComponentProfileNamePostProcessor implements PostProcessor { |
---|
19 | private static final String XPATH = "/CMD_ComponentSpec/Header/Name/text()"; |
---|
20 | private String BASE_URL = null; |
---|
21 | private AutoPilot ap = null; |
---|
22 | private VTDGen vg = null; |
---|
23 | private VTDNav vn = null; |
---|
24 | |
---|
25 | private static final String _EMPTY_STRING = ""; |
---|
26 | private final static Logger LOG = LoggerFactory.getLogger(CMDIComponentProfileNamePostProcessor.class); |
---|
27 | private final HashMap<String, String> cache = new HashMap<String, String>(); |
---|
28 | |
---|
29 | @Override |
---|
30 | public List<String> process(String profileId) { |
---|
31 | String result = _EMPTY_STRING; |
---|
32 | if(profileId != null){ |
---|
33 | if(cache.containsKey(profileId)){ |
---|
34 | result = cache.get(profileId); |
---|
35 | } |
---|
36 | else { |
---|
37 | setup(); |
---|
38 | // get the name of the profile from the expanded xml in the component registry |
---|
39 | if(vg.parseHttpUrl(BASE_URL + profileId + "/xml", true)){ |
---|
40 | LOG.debug("PARSED: "+BASE_URL+profileId); |
---|
41 | vn = vg.getNav(); |
---|
42 | ap.bind(vn); |
---|
43 | int idx; |
---|
44 | try { |
---|
45 | idx = ap.evalXPath(); |
---|
46 | LOG.debug("EVALUATED XPATH: "+XPATH+ " found idx: "+idx); |
---|
47 | if(idx == -1){ // idx represent the nodeId in the xml file, if -1 the xpath evaluates to nothing. |
---|
48 | List<String> resultList = new ArrayList<String>(); |
---|
49 | resultList.add(result); |
---|
50 | return resultList; |
---|
51 | } |
---|
52 | result = vn.toString(idx); |
---|
53 | cache.put(profileId, result); |
---|
54 | } catch (NavException e) { |
---|
55 | LOG.error(e.getLocalizedMessage()); |
---|
56 | List<String> resultList = new ArrayList<String>(); |
---|
57 | resultList.add(result); |
---|
58 | return resultList; |
---|
59 | } catch (XPathEvalException e) { |
---|
60 | LOG.error(e.getLocalizedMessage()); |
---|
61 | List<String> resultList = new ArrayList<String>(); |
---|
62 | resultList.add(result); |
---|
63 | return resultList; |
---|
64 | } |
---|
65 | } |
---|
66 | else { |
---|
67 | LOG.error("Cannot open and/or parse XML Schema: {}.", BASE_URL + profileId); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | List<String> resultList = new ArrayList<String>(); |
---|
72 | resultList.add(result); |
---|
73 | return resultList; |
---|
74 | } |
---|
75 | |
---|
76 | private void setup() { |
---|
77 | ap = new AutoPilot(); |
---|
78 | try { |
---|
79 | ap.selectXPath(XPATH); |
---|
80 | } catch (XPathParseException e) { |
---|
81 | LOG.error(e.getLocalizedMessage()); |
---|
82 | ap = null; |
---|
83 | } |
---|
84 | vg = new VTDGen(); |
---|
85 | BASE_URL = MetadataImporter.config.getComponentRegistryRESTURL(); |
---|
86 | } |
---|
87 | } |
---|