source: vlo/branches/vlo-3.3-oeaw/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/PostProcessorsWithVocabularyMap.java @ 6581

Last change on this file since 6581 was 6581, checked in by davor.ostojic@oeaw.ac.at, 9 years ago
  • Property svn:mime-type set to text/plain
File size: 2.1 KB
Line 
1package eu.clarin.cmdi.vlo.importer;
2
3import java.io.InputStream;
4import java.io.InputStreamReader;
5import java.io.Reader;
6import java.util.Map;
7import java.util.Map.Entry;
8
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11
12import eu.clarin.cmdi.vlo.pojo.VariantsMap;
13import 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
25public 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}
Note: See TracBrowser for help on using the repository browser.