1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.util.HashMap; |
---|
6 | import java.util.Map; |
---|
7 | |
---|
8 | import javax.xml.parsers.DocumentBuilder; |
---|
9 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
10 | import javax.xml.xpath.XPath; |
---|
11 | import javax.xml.xpath.XPathConstants; |
---|
12 | import javax.xml.xpath.XPathFactory; |
---|
13 | |
---|
14 | import eu.clarin.cmdi.vlo.Configuration; |
---|
15 | import org.apache.commons.io.FileUtils; |
---|
16 | import org.slf4j.Logger; |
---|
17 | import org.slf4j.LoggerFactory; |
---|
18 | import org.w3c.dom.Document; |
---|
19 | import org.w3c.dom.NodeList; |
---|
20 | |
---|
21 | /** |
---|
22 | * Adds information about the affiliation of a metadata file to a national project (like CLARIN-X etc.) into facet nationalProject |
---|
23 | * @author Thomas Eckart |
---|
24 | * |
---|
25 | */ |
---|
26 | public class NationalProjectPostProcessor extends LanguageCodePostProcessor { |
---|
27 | private final static Logger LOG = LoggerFactory.getLogger(NationalProjectPostProcessor.class); |
---|
28 | |
---|
29 | //private static String mappingFileName = "nationalProjectsMapping.xml"; |
---|
30 | private static Map<String, String> nationalProjectMap = null; |
---|
31 | |
---|
32 | /** |
---|
33 | * Returns the national project based on the mapping in Configuration.getNationalProjectMapUrl() |
---|
34 | * If no mapping was found empty String is returned |
---|
35 | * @return |
---|
36 | */ |
---|
37 | @Override |
---|
38 | public String process(String value) { |
---|
39 | String result = value.trim(); |
---|
40 | if (result != null && getMapping().containsKey(result)) { |
---|
41 | result = getMapping().get(result); |
---|
42 | } else { |
---|
43 | result = ""; |
---|
44 | } |
---|
45 | return result; |
---|
46 | } |
---|
47 | |
---|
48 | private Map<String, String> getMapping() { |
---|
49 | if(nationalProjectMap == null) |
---|
50 | nationalProjectMap = getNationalProjectMapping(); |
---|
51 | return nationalProjectMap; |
---|
52 | } |
---|
53 | |
---|
54 | private Map<String, String> getNationalProjectMapping() { |
---|
55 | String mappingFileName = Configuration.getInstance().getNationalProjectMapping(); |
---|
56 | LOG.debug("Creating national project map."); |
---|
57 | try { |
---|
58 | Map<String, String> result = new HashMap<String, String>(); |
---|
59 | DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); |
---|
60 | domFactory.setNamespaceAware(true); |
---|
61 | |
---|
62 | File mappingFile = new File(mappingFileName); |
---|
63 | if(!mappingFile.exists()) { // mapping file not accessible? |
---|
64 | LOG.info("National project mapping file does not exist - using minimal test file."); |
---|
65 | mappingFile = createMinimalMappingFile(); |
---|
66 | } |
---|
67 | DocumentBuilder builder = domFactory.newDocumentBuilder(); |
---|
68 | Document doc = builder.parse(mappingFile); |
---|
69 | XPath xpath = XPathFactory.newInstance().newXPath(); |
---|
70 | NodeList nodeList = (NodeList) xpath.evaluate("//nationalProjectMapping", doc, XPathConstants.NODESET); |
---|
71 | for (int i = 1; i <= nodeList.getLength(); i++) { |
---|
72 | String mdCollectionDisplayName = xpath.evaluate("//nationalProjectMapping["+i+"]/MdCollectionDisplayName", doc).trim(); |
---|
73 | String nationalProject = xpath.evaluate("//nationalProjectMapping["+i+"]/NationalProject", doc).trim(); |
---|
74 | result.put(mdCollectionDisplayName, nationalProject); |
---|
75 | } |
---|
76 | return result; |
---|
77 | } catch (Exception e) { |
---|
78 | throw new RuntimeException("Cannot instantiate postProcessor:", e); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Create temporary and minimal mapping file for testing purposes and as backup solution |
---|
84 | * @return minimal file for national projects mapping (e.g. ANDES -> CLARIN-EU) |
---|
85 | */ |
---|
86 | private File createMinimalMappingFile() { |
---|
87 | String content = ""; |
---|
88 | content += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
89 | content += "<nationalProjects>\n"; |
---|
90 | content += " <nationalProjectMapping><MdCollectionDisplayName>ANDES</MdCollectionDisplayName><NationalProject>CLARIN-EU</NationalProject></nationalProjectMapping>\n"; |
---|
91 | content += "</nationalProjects>\n"; |
---|
92 | |
---|
93 | File file = null; |
---|
94 | try { |
---|
95 | file = File.createTempFile("vlo.nationalTestMapping", ".map"); |
---|
96 | FileUtils.writeStringToFile(file, content, "UTF-8"); |
---|
97 | } catch (IOException ioe) { |
---|
98 | ioe.printStackTrace(); |
---|
99 | LOG.error("Could not create temporary national project mapping file"); |
---|
100 | } |
---|
101 | |
---|
102 | return file; |
---|
103 | } |
---|
104 | } |
---|