source: vlo/trunk/vlo-importer/src/main/java/eu/clarin/cmdi/vlo/importer/PostProcessorsWithControlledVocabulary.java @ 6811

Last change on this file since 6811 was 6811, checked in by davor.ostojic@oeaw.ac.at, 9 years ago

#773 Building uniform mapping file format for PostProcessing?

  • Property svn:mime-type set to text/plain
File size: 3.0 KB
Line 
1package eu.clarin.cmdi.vlo.importer;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.InputStream;
6import java.nio.file.Path;
7import java.nio.file.Paths;
8import java.util.Map;
9
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import eu.clarin.cmdi.vlo.normalization.pojo.VariantsMap;
14import eu.clarin.cmdi.vlo.normalization.service.NormalizationService;
15import eu.clarin.cmdi.vlo.normalization.service.NormalizationVocabulary;
16import eu.clarin.cmdi.vlo.normalization.service.VariantsMapMarshaller;
17
18/*
19 * abstract class that encapsulates common map creation from mapping files
20 * for some postprocessors like LanguageCodePostProcessor*
21 *
22 * brings one more level in class hierarchy between interface PostPorcessor and concrete implementations
23 *
24 * @author dostojic
25 *
26 */
27
28public abstract class PostProcessorsWithControlledVocabulary implements PostProcessor, NormalizationService {
29
30        private final static Logger _logger = LoggerFactory.getLogger(PostProcessorsWithControlledVocabulary.class);
31
32        private NormalizationVocabulary vocabulary;
33
34        public String normalize(String value) {
35                return normalize(value, value);
36        }
37
38        public String normalize(String value, String fallBackValue) {
39                if (vocabulary == null)
40                        initVocabulary();
41
42                return vocabulary.normalize(value, fallBackValue);
43        }
44
45        public Map<String, String> getCrossMappings(String value) {
46                if (vocabulary == null)
47                        initVocabulary();
48
49                return vocabulary.getCrossMappings(value);
50        }
51
52        public abstract String getNormalizationMapURL();
53
54        private void initVocabulary() {
55                VariantsMap varinatsRawMap = getMappingFromFile(getNormalizationMapURL());
56                vocabulary = varinatsRawMap.getMap();
57
58                //printMap();
59        }
60
61        protected VariantsMap getMappingFromFile(String mapUrl) {
62               
63                InputStream is = null;
64                File mapUrlFile = new File(mapUrl);
65                _logger.info("Reading vocabulary file from: {}", mapUrl);
66                // load records from file
67                // in the future this should be loaded from CLAVAS directly and the
68                // file only used as fallback
69               
70               
71                //try from file and if not exists fetch it from classpath (root of the vlo-vocabularies project)
72                Path p = Paths.get(mapUrl);
73               
74                try {
75                        is = new FileInputStream(mapUrlFile);
76                } catch (Exception e) {
77                        _logger.warn("File {} not found, trying to fetch it from classpath ...", mapUrl);
78                       
79                        is = PostProcessorsWithControlledVocabulary.class.getClassLoader().getResourceAsStream(mapUrlFile.getName());
80                        if(is == null)
81                                throw new RuntimeException("Cannot instantiate postProcessor, " + mapUrl + " is not on the classpath");
82                }
83               
84                try{
85                        return VariantsMapMarshaller.unmarshal(is);
86                } catch (Exception e) {
87                        throw new RuntimeException("Cannot instantiate postProcessor: ", e);
88                }
89                       
90               
91        }
92
93        // for debug
94        public void printMap() {
95                _logger.info("map contains {} entries", vocabulary.getEntries().length);
96                for(int i = 0; i < vocabulary.getEntries().length; i++)
97                        _logger.info(vocabulary.getEntries()[i].toString());
98                       
99
100        }
101}
Note: See TracBrowser for help on using the repository browser.