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