Project

General

Profile

Download (4.24 KB) Statistics
| Branch: | Revision:
1 91904a96 Andreas Müller
/**
2
* Copyright (C) 2009 EDIT
3 7d882578 Andreas Müller
* European Distributed Institute of Taxonomy
4 91904a96 Andreas Müller
* http://www.e-taxonomy.eu
5 7d882578 Andreas Müller
*
6 91904a96 Andreas Müller
* 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
10
package eu.etaxonomy.cdm.io.wp6;
11
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
18
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
22 7d882578 Andreas Müller
import eu.etaxonomy.cdm.model.name.IBotanicalName;
23 91904a96 Andreas Müller
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
24
import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
25
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
26
27
/**
28
 * @author a.babadshanjan
29
 * @created 13.01.2009
30
 * @version 1.0
31
 */
32
public class CommonNameRow {
33
	private static final Logger logger = Logger.getLogger(CommonNameRow.class);
34
35
36
	private String species;
37
	private String reference;
38
	private String area;
39 218a4795 Andreas Müller
	private String nameUsedInSource;
40 7d882578 Andreas Müller
41 91904a96 Andreas Müller
	private Map<String, List<String>> commonNames = new HashMap<String, List<String>>();
42 7d882578 Andreas Müller
43
44 91904a96 Andreas Müller
	public CommonNameRow() {
45
		this.species = "";
46
		this.reference =  "";
47
		this.area =  "";
48
		commonNames = new HashMap<String, List<String>>();
49
	}
50 7d882578 Andreas Müller
51
// **************************** GETTER / SETTER *********************************/
52
53 91904a96 Andreas Müller
	public void setCommonNames(String commonNamesString){
54 218a4795 Andreas Müller
		commonNamesString = makeNameUsedInSource(commonNamesString);
55 91904a96 Andreas Müller
		String[] split = commonNamesString.split(";");
56
		for (String oneLanguage : split){
57
			oneLanguage = oneLanguage.trim();
58 34b56211 Andreas Müller
			String reLangPattern = "\\((\\*|[a-z]{2,3}|.{2,},\\sno\\sISO-Code)\\)$";
59 91904a96 Andreas Müller
			String pattern = ".+" + reLangPattern;
60
			if (! oneLanguage.matches(pattern)){
61
				logger.warn("Common name does not match: "  + oneLanguage);
62
			}else{
63
				Pattern langPattern = Pattern.compile(reLangPattern);
64
				Matcher matcher = langPattern.matcher(oneLanguage);
65
				if (matcher.find()){
66
					String lang = matcher.group().substring(1);
67
					lang = lang.substring(0, lang.length()-1);
68
					String names = "";
69
					try {
70
						names = oneLanguage.substring(0,matcher.start()-1);
71
					} catch (Exception e) {
72
						e.printStackTrace();
73
					}
74
					String[] splitNames = names.split(",");
75
					List<String> nameList = new ArrayList<String>();
76
					for (String singleName : splitNames){
77
						if (StringUtils.isNotBlank(singleName)){
78
							nameList.add(singleName.trim());
79
						}
80
					}
81
					if (!nameList.isEmpty()){
82
						this.commonNames.put(lang, nameList);
83
					}
84
				}else{
85
					logger.warn("Common name does not match: "  + oneLanguage);
86
				}
87
			}
88
		}
89
	}
90 7d882578 Andreas Müller
91 218a4795 Andreas Müller
	private String makeNameUsedInSource(String commonNamesString) {
92
		String[] split = commonNamesString.split(":");
93
		if (split.length > 1){
94 001bef66 Andreas Müller
			logger.debug("NameUsedInSource: " + split[0]);
95 218a4795 Andreas Müller
			this.nameUsedInSource = split[0].trim();
96
			if (split.length > 2){
97
				logger.warn("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:" + commonNamesString);
98
			}
99
			return split[1].trim();
100
		}else{
101
			return split[0].trim();
102
		}
103
	}
104
105 91904a96 Andreas Müller
	public Map<String, List<String>> getCommonNames() {
106
		return commonNames;
107
	}
108 7d882578 Andreas Müller
109 91904a96 Andreas Müller
	public void parseSpecies(String species){
110 7d882578 Andreas Müller
		INonViralNameParser<?> parser = NonViralNameParserImpl.NewInstance();
111
		IBotanicalName name = (IBotanicalName)parser.parseFullName(species, NomenclaturalCode.ICNAFP, null);
112 91904a96 Andreas Müller
		if (name.isProtectedTitleCache()){
113
			logger.warn("Name could not be parsed: " + species);
114
		}
115 c273c734 Andreas Müller
		this.species = species;
116 91904a96 Andreas Müller
	}
117
118
	public String getSpecies() {
119
		return species;
120
	}
121 7d882578 Andreas Müller
122 91904a96 Andreas Müller
	public void setSpecies(String species) {
123
		this.species = species;
124
	}
125
126
	public void setReference(String reference) {
127
		reference = reference.replace(".", "");
128
		if (! reference.matches("\\d{7}")){
129
			logger.warn("Unexpected reference");
130
		}
131 218a4795 Andreas Müller
		this.reference = reference.substring(0,6);
132 91904a96 Andreas Müller
	}
133
134
	public String getReference() {
135
		return reference;
136
	}
137
138
	public void setArea(String area) {
139
		this.area = area;
140
	}
141 7d882578 Andreas Müller
142 91904a96 Andreas Müller
	public String getArea() {
143
		return area;
144
	}
145
146 218a4795 Andreas Müller
	public void setNameUsedInSource(String nameUsedInSource) {
147
		this.nameUsedInSource = nameUsedInSource;
148
	}
149
150
	public String getNameUsedInSource() {
151
		return nameUsedInSource;
152
	}
153
154 91904a96 Andreas Müller
155 7d882578 Andreas Müller
156 91904a96 Andreas Müller
}