1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.HashMap; |
---|
5 | import java.util.List; |
---|
6 | import java.util.Map; |
---|
7 | import java.util.Map.Entry; |
---|
8 | import java.util.TreeMap; |
---|
9 | import java.util.regex.Matcher; |
---|
10 | import java.util.regex.Pattern; |
---|
11 | |
---|
12 | import org.slf4j.Logger; |
---|
13 | import org.slf4j.LoggerFactory; |
---|
14 | |
---|
15 | import eu.clarin.cmdi.vlo.pojo.Variant; |
---|
16 | import eu.clarin.cmdi.vlo.pojo.VariantsMap; |
---|
17 | |
---|
18 | /** |
---|
19 | * Adds information about the affiliation of a metadata file to a national |
---|
20 | * project (like CLARIN-X etc.) into facet nationalProject |
---|
21 | * |
---|
22 | * @author Thomas Eckart |
---|
23 | * |
---|
24 | */ |
---|
25 | public class NationalProjectPostProcessor extends PostProcessorsWithVocabularyMap { |
---|
26 | |
---|
27 | private final static Logger LOG = LoggerFactory.getLogger(NationalProjectPostProcessor.class); |
---|
28 | |
---|
29 | private static Map<String, String> nationalProjectMap = null; |
---|
30 | private static Map<Pattern, String> nationalProjectRegExpMap = null; |
---|
31 | |
---|
32 | /** |
---|
33 | * Returns the national project based on the mapping in |
---|
34 | * Configuration.getNationalProjectMapUrl() If no mapping was found empty |
---|
35 | * String is returned |
---|
36 | * |
---|
37 | * @return |
---|
38 | */ |
---|
39 | @Override |
---|
40 | public List<String> process(String value) { |
---|
41 | String input = value.trim(); |
---|
42 | List<String> resultList = new ArrayList<String>(); |
---|
43 | |
---|
44 | if (input != null && getVocabularyMap().containsKey(input)) { |
---|
45 | resultList.add(getVocabularyMap().get(input)); |
---|
46 | return resultList; |
---|
47 | } |
---|
48 | |
---|
49 | for (Pattern pattern : getRegExpMapping().keySet()) { |
---|
50 | Matcher matcher = pattern.matcher(input); |
---|
51 | if (matcher.find()) { |
---|
52 | resultList.add(getRegExpMapping().get(pattern)); |
---|
53 | return resultList; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | resultList.add(""); |
---|
58 | return resultList; |
---|
59 | } |
---|
60 | |
---|
61 | @Override |
---|
62 | protected Map<String, String> getVocabularyMap() { |
---|
63 | if(nationalProjectMap == null){ |
---|
64 | createControlledVocabularyMap(getMappingFileUrl()); |
---|
65 | } |
---|
66 | |
---|
67 | return nationalProjectMap; |
---|
68 | } |
---|
69 | |
---|
70 | private Map<Pattern, String> getRegExpMapping() { |
---|
71 | if (nationalProjectRegExpMap == null) { |
---|
72 | createControlledVocabularyMap(getMappingFileUrl()); |
---|
73 | } |
---|
74 | return nationalProjectRegExpMap; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | protected String getMappingFileUrl() { |
---|
79 | String projectsMappingFile = MetadataImporter.config.getNationalProjectMapping(); |
---|
80 | |
---|
81 | if (projectsMappingFile.length() == 0) { |
---|
82 | // use the packaged project mapping file |
---|
83 | projectsMappingFile = "/nationalProjectsMapping.xml"; |
---|
84 | } |
---|
85 | |
---|
86 | return projectsMappingFile; |
---|
87 | } |
---|
88 | |
---|
89 | @Override |
---|
90 | protected Map<String, String> createControlledVocabularyMap(String mapUrl) { |
---|
91 | if(nationalProjectMap == null){ |
---|
92 | nationalProjectMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); |
---|
93 | } |
---|
94 | |
---|
95 | if(nationalProjectRegExpMap == null){ |
---|
96 | nationalProjectRegExpMap = new HashMap<Pattern, String>(); |
---|
97 | } |
---|
98 | |
---|
99 | VariantsMap map = getMappingFromFile(mapUrl); |
---|
100 | |
---|
101 | for(Entry<String, List<Variant>> entry: map.getMap().entrySet()){ |
---|
102 | for(Variant variant: entry.getValue()){ |
---|
103 | if(variant.isRegExp()){ |
---|
104 | nationalProjectRegExpMap.put(Pattern.compile(variant.getValue()), entry.getKey()); |
---|
105 | }else{ |
---|
106 | nationalProjectMap.put(variant.getValue(), entry.getKey()); |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | return nationalProjectMap; |
---|
112 | } |
---|
113 | |
---|
114 | } |
---|