source: vlo/trunk/vlo_webapp/src/main/java/eu/clarin/cmdi/vlo/importer/NationalProjectPostProcessor.java @ 1886

Last change on this file since 1886 was 1886, checked in by herste, 12 years ago

Made location of NationalProjectsMapping?.xml configurable and externalized the xml file also: only relevant for the importer. Should allow updating of mapping _without_ a new deployment.

  • Property svn:mime-type set to text/plain
File size: 4.1 KB
Line 
1package eu.clarin.cmdi.vlo.importer;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.HashMap;
6import java.util.Map;
7
8import javax.xml.parsers.DocumentBuilder;
9import javax.xml.parsers.DocumentBuilderFactory;
10import javax.xml.xpath.XPath;
11import javax.xml.xpath.XPathConstants;
12import javax.xml.xpath.XPathFactory;
13
14import eu.clarin.cmdi.vlo.Configuration;
15import org.apache.commons.io.FileUtils;
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18import org.w3c.dom.Document;
19import 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 */
26public 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}
Note: See TracBrowser for help on using the repository browser.