source: vlo/trunk/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/CMDIComponentProfileNamePostProcessor.java @ 5345

Last change on this file since 5345 was 5345, checked in by teckart@informatik.uni-leipzig.de, 10 years ago

Changed PostProcessor? interface: a PostProcessor? may now return multiple values if extracted content has to be split up for a multivalued Solr field

File size: 3.4 KB
Line 
1package eu.clarin.cmdi.vlo.importer;
2
3import com.ximpleware.AutoPilot;
4import com.ximpleware.NavException;
5import com.ximpleware.VTDGen;
6import com.ximpleware.VTDNav;
7import com.ximpleware.XPathEvalException;
8import com.ximpleware.XPathParseException;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.List;
12import org.slf4j.Logger;
13import 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 */
18public 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}
Note: See TracBrowser for help on using the repository browser.