ref #9359 upgrade cdmlib to log4j 2
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / dwca / out / TermMapping.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.io.dwca.out;
10
11 import java.io.IOException;
12 import java.io.InputStreamReader;
13 import eu.etaxonomy.cdm.common.URI;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.UUID;
17
18 import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
19
20 import au.com.bytecode.opencsv.CSVReader;
21 import eu.etaxonomy.cdm.common.CdmUtils;
22
23 /**
24 * @author a.mueller
25 * @since 03.05.2011
26 *
27 */
28 public class TermMapping {
29 @SuppressWarnings("unused")
30 private static Logger logger = LogManager.getLogger(TermMapping.class);
31
32 private static final String COMMENT = "//";
33
34 private UUID uuidCdmVoc;
35 private URI externalVoc;
36 private char separator = '\t'; //'\u0009'; //horizontal tab
37
38 private Map<UUID, String> mappingMap = new HashMap<>();
39
40
41 public TermMapping(String filename) throws IOException{
42 readMapping(filename);
43 }
44
45
46 private void readMapping(String filename) throws IOException {
47 String strResourceFileName = "mapping/" + filename;
48 InputStreamReader isr = CdmUtils.getUtf8ResourceReader(strResourceFileName);
49 CSVReader reader = new CSVReader(isr, separator);
50
51 String [] nextLine = reader.readNext();
52 uuidCdmVoc = UUID.fromString(nextLine[0]);
53 externalVoc = URI.create(nextLine[1]);
54
55 while ((nextLine = reader.readNext()) != null) {
56 // nextLine[] is an array of values from the line
57 if (nextLine.length == 0){
58 continue;
59 }
60 readMappingLine(nextLine);
61
62 }
63 }
64
65 private void readMappingLine(String[] mappingLine) {
66 if (! mappingLine[0].startsWith(COMMENT)){
67 UUID uuidCdm = UUID.fromString(mappingLine[0]);
68 String externalTerm = mappingLine[1].trim();
69 mappingMap.put(uuidCdm, externalTerm);
70 }
71 }
72
73 public String getTerm(UUID key){
74 return mappingMap.get(key);
75 }
76
77 public URI getExternalVoc(){
78 return externalVoc;
79 }
80
81 public UUID getCdmVoc(){
82 return uuidCdmVoc;
83 }
84 }