source: vlo/trunk/vlo_importer/src/main/java/eu/clarin/cmdi/vlo/importer/CMDIParserVTDXML.java @ 2774

Last change on this file since 2774 was 2774, checked in by keeloo, 11 years ago

Corrected typos in config package. Added list initialisation to VloConfig?. Also fixed ticket 297, Access to packaged National projects mapping file. Finally, made provisions displaying the landing page in the web application.

File size: 6.6 KB
Line 
1package eu.clarin.cmdi.vlo.importer;
2
3import com.ximpleware.AutoPilot;
4import com.ximpleware.NavException;
5import com.ximpleware.VTDException;
6import com.ximpleware.VTDGen;
7import com.ximpleware.VTDNav;
8import com.ximpleware.XPathEvalException;
9import com.ximpleware.XPathParseException;
10import eu.clarin.cmdi.vlo.config.VloConfig;
11import java.io.File;
12import java.io.FileInputStream;
13import java.io.IOException;
14import java.util.List;
15import java.util.Map;
16import org.apache.commons.io.IOUtils;
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
19
20public class CMDIParserVTDXML implements CMDIDataProcessor {
21    private final Map<String, PostProcessor> postProcessors;
22    private final static Logger LOG = LoggerFactory.getLogger(CMDIParserVTDXML.class);
23
24    public CMDIParserVTDXML(Map<String, PostProcessor> postProcessors) {
25        this.postProcessors = postProcessors;
26    }
27
28    @Override
29        public CMDIData process(File file) throws VTDException, IOException {
30        CMDIData result = new CMDIData();
31        VTDGen vg = new VTDGen();
32        vg.setDoc(IOUtils.toByteArray(new FileInputStream(file)));
33        vg.parse(true);
34        VTDNav nav = vg.getNav();
35        setNameSpace(nav);//setting namespace once, all other instance of AutoPilot keep the setting (a bit tricky).
36        FacetMapping facetMapping = getFacetMapping(nav.cloneNav(), file.getAbsolutePath());
37        /** New nice error log to find erroneous files */
38        if(facetMapping.getFacets().size() == 0){
39            LOG.error("Problems mapping facets for file: " + file.getAbsolutePath());
40        }
41
42        nav.toElement(VTDNav.ROOT);
43        processResources(result, nav);
44        processFacets(result, nav, facetMapping);
45        return result;
46    }
47
48    static void setNameSpace(VTDNav nav) {
49        AutoPilot ap = new AutoPilot(nav);
50        ap.declareXPathNameSpace("c", "http://www.clarin.eu/cmd/");
51    }
52
53    private FacetMapping getFacetMapping(VTDNav nav, String tolog) throws VTDException {
54        String xsd = extractXsd(nav);
55        if (xsd == null) {
56            throw new RuntimeException("Cannot get xsd schema so cannot get a proper mapping. Parse failed!");
57        }
58        if (xsd.indexOf("http") != xsd.lastIndexOf("http")){
59            LOG.info("FILE WITH WEIRD HTTP THINGY! " + tolog);
60        }
61        return FacetMappingFactory.getFacetMapping(xsd);
62    }
63
64    String extractXsd(VTDNav nav) throws VTDException {
65        String xsd = getXsdFromHeader(nav);
66        if (xsd == null) {
67            xsd = getXsdFromSchemaLocation(nav);
68        }
69        return xsd;
70    }
71
72    private String getXsdFromHeader(VTDNav nav) throws XPathParseException, XPathEvalException, NavException {
73        String result = null;
74        nav.toElement(VTDNav.ROOT);
75        AutoPilot ap = new AutoPilot(nav);
76        ap.selectXPath("/c:CMD/c:Header/c:MdProfile/text()");
77        int index = ap.evalXPath();
78        if (index != -1) {
79            String profileId = nav.toString(index).trim();
80            result = VloConfig.getComponentRegistryProfileSchema(profileId);
81        }
82        return result;
83    }
84
85    private String getXsdFromSchemaLocation(VTDNav nav) throws NavException {
86        String result = null;
87        nav.toElement(VTDNav.ROOT);
88        int index = nav.getAttrValNS("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
89        if (index != -1) {
90            String schemaLocation = nav.toNormalizedString(index);
91            result = schemaLocation.split(" ")[1];
92        } else {
93            index = nav.getAttrValNS("http://www.w3.org/2001/XMLSchema-instance", "noNamespaceSchemaLocation");
94            if (index != -1) {
95                result = nav.toNormalizedString(index);
96            }
97        }
98        return result;
99    }
100   
101    /*
102     * kj: describe
103     */
104    private void processResources(CMDIData result, VTDNav nav) throws VTDException {
105       
106        AutoPilot resourceProxy = new AutoPilot(nav);
107        resourceProxy.selectXPath("/c:CMD/c:Resources/c:ResourceProxyList/c:ResourceProxy");
108       
109        AutoPilot resourceRef = new AutoPilot(nav);
110        resourceRef.selectXPath("c:ResourceRef");
111        AutoPilot resourceType = new AutoPilot(nav);
112        resourceType.selectXPath("c:ResourceType");
113        AutoPilot resourceMimeType = new AutoPilot(nav);
114        resourceMimeType.selectXPath("c:ResourceType/@mimetype");
115       
116        while (resourceProxy.evalXPath() != -1) {
117            String ref = resourceRef.evalXPathToString();
118            String type = resourceType.evalXPathToString();
119            String mimeType = resourceMimeType.evalXPathToString();
120           
121         // if (!ref.equals("") && !type.equals("")){
122            if (!ref.equals("")){
123                // mime type is allowed to be an empty string, kj: check if this is allowed in general
124                result.addResource(ref, type, mimeType);
125            }
126        }
127    }
128
129    private void processFacets(CMDIData result, VTDNav nav, FacetMapping facetMapping) throws VTDException {
130        List<FacetConfiguration> facetList = facetMapping.getFacets();
131        for (FacetConfiguration config : facetList) {
132            List<String> patterns = config.getPatterns();
133            for (String pattern : patterns) {
134                boolean matchedPattern = matchPattern(result, nav, config, pattern, config.getAllowMultipleValues());
135                if (matchedPattern && !config.getAllowMultipleValues()) {
136                    break;
137                }
138            }
139        }
140    }
141
142    private boolean matchPattern(CMDIData result, VTDNav nav, FacetConfiguration config, String pattern, Boolean allowMultipleValues) throws VTDException {
143        boolean matchedPattern = false;
144        AutoPilot ap = new AutoPilot(nav);
145        ap.selectXPath(pattern);
146        int index = ap.evalXPath();
147        while (index != -1) {
148            matchedPattern = true;
149            if (nav.getTokenType(index) == VTDNav.TOKEN_ATTR_NAME) {
150                //if it is an attribute you need to add 1 to the index to get the right value
151                index++;
152            }
153            String value = nav.toString(index);
154            value = postProcess(config.getName(), value);
155            result.addDocField(config.getName(), value, config.isCaseInsensitive());
156            index = ap.evalXPath();
157           
158            if(!allowMultipleValues)
159                break;
160        }
161        return matchedPattern;
162    }
163
164    private String postProcess(String name, String value) {
165        String result = value;
166        if (postProcessors.containsKey(name)) {
167            PostProcessor processor = postProcessors.get(name);
168            result = processor.process(value);
169        }
170        return result.trim();
171    }
172
173}
Note: See TracBrowser for help on using the repository browser.