1 | package eu.clarin.cmdi.vlo.importer; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import java.io.InputStreamReader; |
---|
5 | import java.io.Reader; |
---|
6 | import java.util.Map; |
---|
7 | import java.util.Map.Entry; |
---|
8 | |
---|
9 | import org.slf4j.Logger; |
---|
10 | import org.slf4j.LoggerFactory; |
---|
11 | |
---|
12 | import eu.clarin.cmdi.vlo.pojo.VariantsMap; |
---|
13 | import eu.clarin.cmdi.vlo.transformers.VariantsMapMarshaller; |
---|
14 | |
---|
15 | /* |
---|
16 | * abstract class that encapsulates common map creation from mapping files |
---|
17 | * for some postprocessors like LanguageCodePostProcessor* |
---|
18 | * |
---|
19 | * brings one more level in class hierarchy between interface PostPorcessor and concrete implementations |
---|
20 | * |
---|
21 | * @author dostojic |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | public abstract class PostProcessorsWithVocabularyMap implements PostProcessor{ |
---|
26 | |
---|
27 | private final static Logger LOG = LoggerFactory.getLogger(PostProcessorsWithVocabularyMap.class); |
---|
28 | |
---|
29 | |
---|
30 | /* |
---|
31 | * returns specific static map and should call createControlledVocabularyMap |
---|
32 | */ |
---|
33 | protected abstract Map<String, String> getVocabularyMap(); |
---|
34 | |
---|
35 | |
---|
36 | protected Map<String, String> createControlledVocabularyMap(String mapUrl) { |
---|
37 | VariantsMap map = getMappingFromFile(mapUrl); |
---|
38 | return map.getInvertedMap(); |
---|
39 | } |
---|
40 | |
---|
41 | protected VariantsMap getMappingFromFile(String mapUrl){ |
---|
42 | try { |
---|
43 | |
---|
44 | LOG.info("Reading vocabulary file from: {}", mapUrl); |
---|
45 | // load records from file |
---|
46 | // in the future this should be loaded from CLAVAS directly and the file only used as fallback |
---|
47 | InputStream is = PostProcessorsWithVocabularyMap.class.getClassLoader().getResourceAsStream(mapUrl); |
---|
48 | Reader reader = new InputStreamReader(is, "UTF-8"); |
---|
49 | return VariantsMapMarshaller.unmarshal(reader); |
---|
50 | } catch (Exception e) { |
---|
51 | throw new RuntimeException("Cannot instantiate postProcessor:", e); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | // for debug purposes |
---|
56 | public void printMap(){ |
---|
57 | LOG.info("map contains {} entries", getVocabularyMap().size()); |
---|
58 | for(Entry<String, String> e: getVocabularyMap().entrySet()){ |
---|
59 | LOG.info("Key <{}> will be mapped to <{}>", e.getKey(), e.getValue()); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | } |
---|