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

Last change on this file since 6721 was 6721, checked in by davor.ostojic@oeaw.ac.at, 9 years ago
  • Property svn:mime-type set to text/plain
File size: 3.3 KB
Line 
1package eu.clarin.cmdi.vlo.importer;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.InputStream;
6import java.util.List;
7import java.util.Map;
8import java.util.Map.Entry;
9
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import eu.clarin.cmdi.vlo.pojo.VariantsMap;
14import eu.clarin.cmdi.vlo.pojo.VocabularyEntry;
15import eu.clarin.cmdi.vlo.transformers.VariantsMapMarshaller;
16
17/*
18 * abstract class that encapsulates common map creation from mapping files
19 * for some postprocessors like LanguageCodePostProcessor*
20 *
21 * brings one more level in class hierarchy between interface PostPorcessor and concrete implementations
22 *
23 * @author dostojic
24 *
25 */
26
27public abstract class PostProcessorsWithVocabularyMap implements PostProcessor{
28       
29         private final static Logger _logger = LoggerFactory.getLogger(PostProcessorsWithVocabularyMap.class);
30         
31         private List<VocabularyEntry> map = null;
32                 
33       
34         /*
35          * This method is used for normalization of facet values based on normalization maps.
36          * In case that value is not in the map, the input value is returned.
37          *
38          * @param value - original value from record
39          * @return normalized value if there is a match otherwise original value
40          *
41          */
42         public String normalize(String value){
43                 return normalize(value, value);
44         }
45         
46         
47         /*
48          * This method is used for normalization of facet values based on normalization maps.
49          * With second parameter user can specify what to return in case when value is not in normalization map (special case for NationalProject facet).
50          * 
51          * @param value - original value from record
52          * @param fallBackValue - value to be returned in case of no match
53          * @return normalized value if there is a match otherwise returns what is specified with 2nd parameter
54          *
55          */
56         public String normalize(String value, String fallBackValue){
57                 //init
58                 if(map == null)
59                         createMap();
60                 
61                 int ind = map.indexOf(value);
62                 return (ind != -1)? map.get(ind).getNormalizedValue() : fallBackValue;
63         }
64         
65         public  Map<String, String> getCrossMappings(String value){
66                 
67                 //init
68                 if(map == null)
69                         createMap();
70                 
71                 int ind = map.indexOf(value);
72                 return (ind != -1)? map.get(ind).getCrossMap() : null;
73         }
74         
75         public abstract String getNormalizationMapURL();
76         
77         private void createMap(){
78                 VariantsMap varinatsRawMap = getMappingFromFile(getNormalizationMapURL());
79                 map = varinatsRawMap.getMap();
80         }
81         
82         protected VariantsMap getMappingFromFile(String mapUrl){
83                 try {
84                       
85                        _logger.info("Reading vocabulary file from: {}", mapUrl);
86                    // load records from file
87                        // in the future this should be loaded from CLAVAS directly and the file only used as fallback
88                       
89                        //InputStream is = PostProcessorsWithVocabularyMap.class.getClassLoader().getResourceAsStream(mapUrl);
90                        InputStream is = new FileInputStream(new File(mapUrl));
91
92                       
93                        return VariantsMapMarshaller.unmarshal(is);                     
94             } catch (Exception e) {
95            throw new RuntimeException("Cannot instantiate postProcessor:", e);
96         }
97         }
98         
99         
100         // for debug
101         public void printMap(){
102                 _logger.info("map contains {} entries", map.size());
103                 for(VocabularyEntry entry: map)
104                         _logger.info(entry.toString());
105               
106         }
107}
Note: See TracBrowser for help on using the repository browser.