2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.eflora
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
19 import java
.util
.regex
.Matcher
;
20 import java
.util
.regex
.Pattern
;
22 import org
.apache
.commons
.lang
.CharUtils
;
23 import org
.apache
.commons
.lang
.StringUtils
;
24 import org
.apache
.log4j
.Logger
;
25 import org
.jdom
.Attribute
;
26 import org
.jdom
.Element
;
27 import org
.springframework
.stereotype
.Component
;
28 import org
.springframework
.transaction
.TransactionStatus
;
30 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
31 import eu
.etaxonomy
.cdm
.common
.ResultWrapper
;
32 import eu
.etaxonomy
.cdm
.common
.XmlHelp
;
33 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
34 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.UndefinedTransformerMethodException
;
35 import eu
.etaxonomy
.cdm
.io
.eflora
.UnmatchedLeads
.UnmatchedLeadsKey
;
36 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
37 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
38 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
40 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
41 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
42 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Credit
;
44 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
45 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
46 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
47 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
48 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
49 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
50 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
51 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
52 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
53 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
54 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
55 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
56 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
57 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
58 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
59 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
60 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
61 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
62 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
63 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
64 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
65 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
66 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
67 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
68 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
69 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
70 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
71 import eu
.etaxonomy
.cdm
.model
.reference
.IBook
;
72 import eu
.etaxonomy
.cdm
.model
.reference
.IJournal
;
73 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
74 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
75 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
76 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
77 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymType
;
78 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
79 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
80 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
81 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
82 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
83 import eu
.etaxonomy
.cdm
.strategy
.parser
.TimePeriodParser
;
91 public class EfloraTaxonImport
extends EfloraImportBase
implements ICdmIO
<EfloraImportState
> {
92 private static final Logger logger
= Logger
.getLogger(EfloraTaxonImport
.class);
94 private static int modCount
= 30000;
95 private final NonViralNameParserImpl parser
= new NonViralNameParserImpl();
97 public EfloraTaxonImport(){
103 public boolean doCheck(EfloraImportState state
){
104 boolean result
= true;
108 //TODO make part of state, but state is renewed when invoking the import a second time
109 private UnmatchedLeads unmatchedLeads
;
112 public void doInvoke(EfloraImportState state
){
113 logger
.info("start make Taxa ...");
116 state
.putTree(null, null);
117 // UnmatchedLeads unmatchedLeads = state.getOpenKeys();
118 if (unmatchedLeads
== null){
119 unmatchedLeads
= UnmatchedLeads
.NewInstance();
121 state
.setUnmatchedLeads(unmatchedLeads
);
123 TransactionStatus tx
= startTransaction();
124 unmatchedLeads
.saveToSession(getPolytomousKeyNodeService());
127 //TODO generally do not store the reference object in the config
128 Reference sourceReference
= state
.getConfig().getSourceReference();
129 getReferenceService().saveOrUpdate(sourceReference
);
131 Set
<TaxonBase
> taxaToSave
= new HashSet
<TaxonBase
>();
132 ResultWrapper
<Boolean
> success
= ResultWrapper
.NewInstance(true);
134 Element elbody
= getBodyElement(state
.getConfig());
135 List
<Element
> elTaxonList
= elbody
.getChildren();
139 Set
<String
> unhandledTitleClassess
= new HashSet
<String
>();
140 Set
<String
> unhandledNomeclatureChildren
= new HashSet
<String
>();
141 Set
<String
> unhandledDescriptionChildren
= new HashSet
<String
>();
143 Taxon lastTaxon
= getLastTaxon(state
);
146 for (Element elTaxon
: elTaxonList
){
148 if ((i
++ % modCount
) == 0 && i
> 1){ logger
.info("Taxa handled: " + (i
-1));}
149 if (! elTaxon
.getName().equalsIgnoreCase("taxon")){
150 logger
.warn("body has element other than 'taxon'");
153 BotanicalName botanicalName
= BotanicalName
.NewInstance(Rank
.SPECIES());
154 Taxon taxon
= Taxon
.NewInstance(botanicalName
, state
.getConfig().getSourceReference());
156 handleTaxonAttributes(elTaxon
, taxon
, state
);
159 List
<Element
> children
= elTaxon
.getChildren();
160 handleTaxonElement(state
, unhandledTitleClassess
, unhandledNomeclatureChildren
, unhandledDescriptionChildren
, taxon
, children
);
161 handleTaxonRelation(state
, taxon
, lastTaxon
);
163 taxaToSave
.add(taxon
);
164 state
.getConfig().setLastTaxonUuid(lastTaxon
.getUuid());
166 } catch (Exception e
) {
167 logger
.warn("Exception occurred in Sapindacea taxon import: " + e
);
173 System
.out
.println(state
.getUnmatchedLeads().toString());
174 logger
.warn("There are taxa with attributes 'excluded' and 'dubious'");
176 logger
.info("Children for nomenclature are: " + unhandledNomeclatureChildren
);
177 logger
.info("Children for description are: " + unhandledDescriptionChildren
);
178 logger
.info("Children for homotypes are: " + unhandledHomotypeChildren
);
179 logger
.info("Children for nom are: " + unhandledNomChildren
);
182 //invokeRelations(source, cdmApp, deleteAll, taxonMap, referenceMap);
183 logger
.info(i
+ " taxa handled. Saving ...");
184 getTaxonService().saveOrUpdate(taxaToSave
);
185 getFeatureTreeService().saveOrUpdateFeatureNodesAll(state
.getFeatureNodesToSave());
186 state
.getFeatureNodesToSave().clear();
187 commitTransaction(tx
);
189 logger
.info("end makeTaxa ...");
190 logger
.info("start makeKey ...");
191 // invokeDoKey(state);
192 logger
.info("end makeKey ...");
194 if (! success
.getValue()){
195 state
.setUnsuccessfull();
201 private void handleTaxonAttributes(Element elTaxon
, Taxon taxon
, EfloraImportState state
) {
202 List
<Attribute
> attrList
= elTaxon
.getAttributes();
203 for (Attribute attr
: attrList
){
204 String attrName
= attr
.getName();
205 String attrValue
= attr
.getValue();
206 if ("class".equals(attrName
)){
207 if (attrValue
.equalsIgnoreCase("dubious") || attrValue
.equalsIgnoreCase("DUBIOUS GENUS") || attrValue
.equalsIgnoreCase("DOUBTFUL SPECIES") ){
208 taxon
.setDoubtful(true);
210 MarkerType markerType
= getMarkerType(state
, attrValue
);
211 if (markerType
== null){
212 logger
.warn("Class attribute value for taxon not yet supported: " + attrValue
);
214 taxon
.addMarker(Marker
.NewInstance(markerType
, true));
217 }else if ("num".equals(attrName
)){
218 logger
.warn("num not yet supported");
220 logger
.warn("Attribute " + attrName
+ " not yet supported for element taxon");
227 private Taxon
getLastTaxon(EfloraImportState state
) {
228 if (state
.getConfig().getLastTaxonUuid() == null){
231 return (Taxon
)getTaxonService().find(state
.getConfig().getLastTaxonUuid());
236 // private void invokeDoKey(SapindaceaeImportState state) {
237 // TransactionStatus tx = startTransaction();
239 // Set<FeatureNode> nodesToSave = new HashSet<FeatureNode>();
240 // ITaxonService taxonService = getTaxonService();
241 // ResultWrapper<Boolean> success = ResultWrapper.NewInstance(true);
243 // Element elbody= getBodyElement(state.getConfig());
244 // List<Element> elTaxonList = elbody.getChildren();
249 // for (Element elTaxon : elTaxonList){
250 // if ((i++ % modCount) == 0 && i > 1){ logger.info("Taxa handled: " + (i-1));}
251 // if (! elTaxon.getName().equalsIgnoreCase("taxon")){
255 // List<Element> children = elTaxon.getChildren("key");
256 // for (Element element : children){
257 // handleKeys(state, element, null);
259 // nodesToSave.add(taxon);
267 private void handleTaxonElement(EfloraImportState state
, Set
<String
> unhandledTitleClassess
, Set
<String
> unhandledNomeclatureChildren
, Set
<String
> unhandledDescriptionChildren
, Taxon taxon
, List
<Element
> children
) {
268 AnnotatableEntity lastEntity
= null;
269 for (Element element
: children
){
270 String elName
= element
.getName();
272 if (elName
.equalsIgnoreCase("title")){
273 handleTitle(state
, element
, taxon
, unhandledTitleClassess
);
275 }else if(elName
.equalsIgnoreCase("nomenclature")){
276 handleNomenclature(state
, element
, taxon
, unhandledNomeclatureChildren
);
278 }else if(elName
.equalsIgnoreCase("description")){
279 handleDescription(state
, element
, taxon
, unhandledDescriptionChildren
);
281 }else if(elName
.equalsIgnoreCase("habitatecology")){
282 lastEntity
= handleEcology(state
, element
, taxon
);
283 }else if(elName
.equalsIgnoreCase("distribution")){
284 lastEntity
= handleDistribution(state
, element
, taxon
);
285 }else if(elName
.equalsIgnoreCase("uses")){
286 lastEntity
= handleUses(state
, element
, taxon
);
287 }else if(elName
.equalsIgnoreCase("notes")){
288 lastEntity
= handleTaxonNotes(state
, element
, taxon
);
289 }else if(elName
.equalsIgnoreCase("chromosomes")){
290 lastEntity
= handleChromosomes(state
, element
, taxon
);
291 }else if(elName
.equalsIgnoreCase("vernacularnames")){
292 handleVernaculars(state
, element
, taxon
);
293 }else if(elName
.equalsIgnoreCase("key")){
294 lastEntity
= handleKeys(state
, element
, taxon
);
295 }else if(elName
.equalsIgnoreCase("references")){
296 handleReferences(state
, element
, taxon
, lastEntity
);
298 }else if(elName
.equalsIgnoreCase("taxon")){
299 logger
.warn("A taxon should not be part of a taxon");
300 }else if(elName
.equalsIgnoreCase("homotypes")){
301 logger
.warn("Homotypes should be included in the nomenclature flag but is child of taxon [XPath: body/taxon/homotypes]");
303 logger
.warn("Unexpected child for taxon: " + elName
);
309 private void handleVernaculars(EfloraImportState state
, Element elVernacular
, Taxon taxon
) {
310 verifyNoAttribute(elVernacular
);
311 verifyNoChildren(elVernacular
, false);
312 String value
= elVernacular
.getTextNormalize();
313 Feature feature
= Feature
.COMMON_NAME();
314 value
= replaceStart(value
, "Noms vernaculaires");
315 String
[] dialects
= value
.split(";");
316 for (String singleDialect
: dialects
){
317 handleSingleDialect(taxon
, singleDialect
, feature
, state
);
323 private void handleSingleDialect(Taxon taxon
, String singleDialect
, Feature feature
, EfloraImportState state
) {
324 singleDialect
= singleDialect
.trim();
325 TaxonDescription description
= getDescription(taxon
);
326 String reDialect
= "\\(dial\\.\\s.*\\)";
327 // String reDialect = "\\(.*\\)";
328 Pattern patDialect
= Pattern
.compile(reDialect
);
329 Matcher matcher
= patDialect
.matcher(singleDialect
);
331 String dialect
= singleDialect
.substring(matcher
.start(), matcher
.end());
332 dialect
= dialect
.replace("(dial. ", "").replace(")", "");
334 Language language
= null;
336 language
= this.getLanguage(state
, state
.getTransformer().getLanguageUuid(dialect
), dialect
, dialect
, dialect
);
337 } catch (UndefinedTransformerMethodException e
) {
338 logger
.error(e
.getMessage());
341 String commonNames
= singleDialect
.substring(0, matcher
.start());
342 String
[] splitNames
= commonNames
.split(",");
343 for (String commonNameString
: splitNames
){
344 commonNameString
= commonNameString
.trim();
345 CommonTaxonName commonName
= CommonTaxonName
.NewInstance(commonNameString
, language
);
346 description
.addElement(commonName
);
349 logger
.warn("No dialect match: " + singleDialect
);
354 private void handleReferences(EfloraImportState state
, Element elReferences
, Taxon taxon
, AnnotatableEntity lastEntity
) {
355 verifyNoAttribute(elReferences
);
356 verifyNoChildren(elReferences
, true);
357 String refString
= elReferences
.getTextNormalize();
358 if (lastEntity
== null){
359 logger
.warn("No last entity defined: " + refString
);
363 Annotation annotation
= Annotation
.NewInstance(refString
, AnnotationType
.EDITORIAL(), Language
.DEFAULT());
364 lastEntity
.addAnnotation(annotation
);
368 private PolytomousKey
handleKeys(EfloraImportState state
, Element elKey
, Taxon taxon
) {
369 UnmatchedLeads openKeys
= state
.getUnmatchedLeads();
372 String title
= makeKeyTitle(elKey
);
375 PolytomousKey key
= PolytomousKey
.NewTitledInstance(title
);
377 //TODO add covered taxa etc.
378 verifyNoAttribute(elKey
);
381 makeKeyNotes(elKey
, key
);
384 List
<Element
> keychoices
= new ArrayList
<Element
>();
385 keychoices
.addAll(elKey
.getChildren("keycouplet"));
386 keychoices
.addAll(elKey
.getChildren("keychoice"));
389 for (Element elKeychoice
: keychoices
){
390 handleKeyChoices(state
, openKeys
, key
, elKeychoice
, taxon
);
391 elKey
.removeContent(elKeychoice
);
395 verifyNoChildren(elKey
);
396 logger
.info("Unmatched leads after key handling:" + openKeys
.toString());
399 if (state
.getConfig().isDoPrintKeys()){
400 key
.print(System
.err
);
402 getPolytomousKeyService().save(key
);
415 private void handleKeyChoices(EfloraImportState state
, UnmatchedLeads openKeys
, PolytomousKey key
, Element elKeychoice
, Taxon taxon
) {
418 //TODO it's still unclear if char is a feature and needs to be a new attribute
419 //or if it is handled as question. Therefore both cases are handled but feature
420 //is finally not yet set
421 KeyStatement question
= handleKeychoiceChar(state
, elKeychoice
);
422 Feature feature
= handleKeychoiceCharAsFeature(state
, elKeychoice
);
425 List
<PolytomousKeyNode
> childNodes
= handleKeychoiceLeads(state
, key
, elKeychoice
, taxon
, question
, feature
);
427 //num -> match with unmatched leads
428 handleKeychoiceNum(openKeys
, key
, elKeychoice
, childNodes
);
431 verifyNoAttribute(elKeychoice
);
441 private void handleKeychoiceNum(UnmatchedLeads openKeys
, PolytomousKey key
, Element elKeychoice
, List
<PolytomousKeyNode
> childNodes
) {
442 Attribute numAttr
= elKeychoice
.getAttribute("num");
443 String num
= CdmUtils
.removeTrailingDot(numAttr
== null?
"":numAttr
.getValue());
444 UnmatchedLeadsKey okk
= UnmatchedLeadsKey
.NewInstance(key
, num
);
445 Set
<PolytomousKeyNode
> matchingNodes
= openKeys
.getNodes(okk
);
446 for (PolytomousKeyNode matchingNode
: matchingNodes
){
447 for (PolytomousKeyNode childNode
: childNodes
){
448 matchingNode
.addChild(childNode
);
450 openKeys
.removeNode(okk
, matchingNode
);
452 if (matchingNodes
.isEmpty()){
453 for (PolytomousKeyNode childNode
: childNodes
){
454 key
.getRoot().addChild(childNode
);
458 elKeychoice
.removeAttribute("num");
470 private List
<PolytomousKeyNode
> handleKeychoiceLeads( EfloraImportState state
, PolytomousKey key
, Element elKeychoice
, Taxon taxon
, KeyStatement question
, Feature feature
) {
471 List
<PolytomousKeyNode
> childNodes
= new ArrayList
<PolytomousKeyNode
>();
472 List
<Element
> leads
= elKeychoice
.getChildren("lead");
473 for(Element elLead
: leads
){
474 PolytomousKeyNode childNode
= handleLead(state
, key
, elLead
, taxon
, question
, feature
);
475 childNodes
.add(childNode
);
486 private KeyStatement
handleKeychoiceChar(EfloraImportState state
, Element elKeychoice
) {
487 KeyStatement statement
= null;
488 Attribute charAttr
= elKeychoice
.getAttribute("char");
489 if (charAttr
!= null){
490 String charStr
= charAttr
.getValue();
491 if (StringUtils
.isNotBlank(charStr
)){
492 statement
= KeyStatement
.NewInstance(charStr
);
494 elKeychoice
.removeAttribute("char");
504 private Feature
handleKeychoiceCharAsFeature(EfloraImportState state
, Element elKeychoice
) {
505 Feature feature
= null;
506 Attribute charAttr
= elKeychoice
.getAttribute("char");
507 if (charAttr
!= null){
508 String charStr
= charAttr
.getValue();
509 feature
= getFeature(charStr
, state
);
510 elKeychoice
.removeAttribute("char");
516 private PolytomousKeyNode
handleLead(EfloraImportState state
, PolytomousKey key
, Element elLead
, Taxon taxon
, KeyStatement question
, Feature feature
) {
517 PolytomousKeyNode node
= PolytomousKeyNode
.NewInstance();
518 //TODO the char attribute in the keychoice is more a feature than a question
519 //needs to be discussed on model side
520 node
.setQuestion(question
);
521 // node.setFeature(feature);
524 String text
= handleLeadText(elLead
, node
);
527 handleLeadNum(elLead
, text
);
530 handleLeadGoto(state
, key
, elLead
, taxon
, node
);
533 verifyNoAttribute(elLead
);
544 private String
handleLeadText(Element elLead
, PolytomousKeyNode node
) {
545 String text
= elLead
.getAttributeValue("text").trim();
546 if (StringUtils
.isBlank(text
)){
547 logger
.warn("Empty text in lead");
549 elLead
.removeAttribute("text");
550 KeyStatement statement
= KeyStatement
.NewInstance(text
);
551 node
.setStatement(statement
);
563 private void handleLeadGoto(EfloraImportState state
, PolytomousKey key
, Element elLead
, Taxon taxon
, PolytomousKeyNode node
) {
564 Attribute gotoAttr
= elLead
.getAttribute("goto");
565 if (gotoAttr
!= null){
566 String strGoto
= gotoAttr
.getValue().trim();
568 UnmatchedLeadsKey gotoKey
= null;
569 if (isInternalNode(strGoto
)){
570 gotoKey
= UnmatchedLeadsKey
.NewInstance(key
, strGoto
);
572 String taxonKey
= makeTaxonKey(strGoto
, taxon
);
573 gotoKey
= UnmatchedLeadsKey
.NewInstance(taxonKey
);
576 UnmatchedLeads openKeys
= state
.getUnmatchedLeads();
577 if (gotoKey
.isInnerLead()){
578 Set
<PolytomousKeyNode
> existingNodes
= openKeys
.getNodes(gotoKey
);
579 for (PolytomousKeyNode existingNode
: existingNodes
){
580 node
.addChild(existingNode
);
583 openKeys
.addKey(gotoKey
, node
);
584 //remove attribute (need for consistency check)
585 elLead
.removeAttribute("goto");
587 logger
.warn("lead has no goto attribute");
596 private void handleLeadNum(Element elLead
, String text
) {
597 Attribute numAttr
= elLead
.getAttribute("num");
598 if (numAttr
!= null){
600 String num
= numAttr
.getValue();
601 elLead
.removeAttribute("num");
603 logger
.info("Keychoice has no num attribute: " + text
);
608 private String
makeTaxonKey(String strGoto
, Taxon taxon
) {
610 if (strGoto
== null){
613 String strGenusName
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class).getGenusOrUninomial();
614 strGoto
= strGoto
.replaceAll("\\([^\\(\\)]*\\)", ""); //replace all brackets
615 strGoto
= strGoto
.replaceAll("\\s+", " "); //replace multiple whitespaces by exactly one whitespace
617 strGoto
= strGoto
.trim();
618 String
[] split
= strGoto
.split("\\s");
619 for (int i
= 0; i
<split
.length
; i
++){
620 String single
= split
[i
];
621 if (isGenusAbbrev(single
, strGenusName
)){
622 split
[i
] = strGenusName
;
624 // if (isInfraSpecificMarker(single)){
625 // String strSpeciesName = CdmBase.deproxy(taxon.getName(), NonViralName.class).getSpecificEpithet();
626 // split[i] = strGenusName + " " + strSpeciesName + " ";
628 result
= (result
+ " " + split
[i
]).trim();
634 private boolean isInfraSpecificMarker(String single
) {
636 if (Rank
.getRankByIdInVoc(single
).isInfraSpecific()){
639 } catch (UnknownCdmTypeException e
) {
646 private boolean isGenusAbbrev(String single
, String strGenusName
) {
647 if (! single
.matches("[A-Z]\\.?")) {
649 }else if (single
.length() == 0 || strGenusName
== null || strGenusName
.length() == 0){
652 return single
.charAt(0) == strGenusName
.charAt(0);
657 private boolean isInternalNode(String strGoto
) {
658 return CdmUtils
.isNumeric(strGoto
);
662 private void makeKeyNotes(Element keyElement
, PolytomousKey key
) {
663 Element elNotes
= keyElement
.getChild("notes");
664 if (elNotes
!= null){
665 keyElement
.removeContent(elNotes
);
666 String notes
= elNotes
.getTextNormalize();
667 if (StringUtils
.isNotBlank(notes
)){
668 key
.addAnnotation(Annotation
.NewInstance(notes
, AnnotationType
.EDITORIAL(), Language
.DEFAULT()));
674 private String
makeKeyTitle(Element keyElement
) {
675 String title
= "- no title - ";
676 Attribute titleAttr
= keyElement
.getAttribute("title");
677 keyElement
.removeAttribute(titleAttr
);
678 if (titleAttr
== null){
679 Element elTitle
= keyElement
.getChild("keytitle");
680 keyElement
.removeContent(elTitle
);
681 if (elTitle
!= null){
682 title
= elTitle
.getTextNormalize();
685 title
= titleAttr
.getValue();
696 private TextData
handleChromosomes(EfloraImportState state
, Element element
, Taxon taxon
) {
697 Feature chromosomeFeature
= getFeature("chromosomes", state
);
698 verifyNoAttribute(element
);
699 verifyNoChildren(element
);
700 String value
= element
.getTextNormalize();
701 value
= replaceStart(value
, "Chromosomes");
702 String chromosomesPart
= getChromosomesPart(value
);
703 String references
= value
.replace(chromosomesPart
, "").trim();
704 chromosomesPart
= chromosomesPart
.replace(":", "").trim();
705 return addDescriptionElement(state
, taxon
, chromosomesPart
, chromosomeFeature
, references
);
714 private void makeOriginalSourceReferences(ISourceable sourcable
, String splitter
, String refAll
) {
715 String
[] splits
= refAll
.split(splitter
);
716 for (String strRef
: splits
){
717 Reference ref
= ReferenceFactory
.newGeneric();
718 ref
.setTitleCache(strRef
, true);
719 String refDetail
= parseReferenceYearAndDetail(ref
);
720 sourcable
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
, null, null, ref
, refDetail
);
724 //TODO use regex instead
725 /* String detailResult = null;
726 String titleToParse = ref.getTitleCache();
727 String reReference = "^\\.{1,}";
728 // String reYear = "\\([1-2]{1}[0-9]{3}\\)";
729 String reYear = "\\([1-2]{1}[0-9]{3}\\)";
730 String reYearPeriod = reYear + "(-" + reYear + ")+";
731 String reDetail = "\\.{1,10}$";
740 private String
getChromosomesPart(String str
) {
741 Pattern pattern
= Pattern
.compile("2n\\s*=\\s*\\d{1,2}:");
742 Matcher matcher
= pattern
.matcher(str
);
744 return matcher
.group(0);
746 logger
.warn("Chromosomes could not be parsed: " + str
);
757 private TextData
handleTaxonNotes(EfloraImportState state
, Element element
, Taxon taxon
) {
758 TextData result
= null;
759 verifyNoChildren(element
, true);
760 //verifyNoAttribute(element);
761 List
<Attribute
> attributes
= element
.getAttributes();
762 for (Attribute attribute
: attributes
){
763 if (! attribute
.getName().equalsIgnoreCase("class")){
764 logger
.warn("Char has unhandled attribute " + attribute
.getName());
766 String classValue
= attribute
.getValue();
767 result
= handleDescriptiveElement(state
, element
, taxon
, classValue
);
770 //if no class attribute exists, handle as note
771 if (attributes
.isEmpty()){
772 result
= handleDescriptiveElement(state
, element
, taxon
, "Note");
775 //Annotation annotation = Annotation.NewInstance(value, AnnotationType.EDITORIAL(), Language.ENGLISH());
776 //taxon.addAnnotation(annotation);
777 return result
; //annotation;
789 private TextData
handleDescriptiveElement(EfloraImportState state
, Element element
, Taxon taxon
, String classValue
) {
790 TextData result
= null;
791 Feature feature
= getFeature(classValue
, state
);
792 if (feature
== null){
793 logger
.warn("Unhandled feature: " + classValue
);
795 String value
= element
.getValue();
796 value
= replaceStart(value
, "Notes");
797 value
= replaceStart(value
, "Note");
798 result
= addDescriptionElement(state
, taxon
, value
, feature
, null);
804 private void removeBr(Element element
) {
805 element
.removeChildren("Br");
806 element
.removeChildren("br");
807 element
.removeChildren("BR");
816 private TextData
handleUses(EfloraImportState state
, Element element
, Taxon taxon
) {
817 verifyNoAttribute(element
);
818 verifyNoChildren(element
, true);
819 String value
= element
.getTextNormalize();
820 value
= replaceStart(value
, "Uses");
821 Feature feature
= Feature
.USES();
822 return addDescriptionElement(state
, taxon
, value
, feature
, null);
831 * @param unhandledDescriptionChildren
833 private DescriptionElementBase
handleDistribution(EfloraImportState state
, Element element
, Taxon taxon
) {
834 verifyNoAttribute(element
);
835 verifyNoChildren(element
, true);
836 String value
= element
.getTextNormalize();
837 value
= replaceStart(value
, "Distribution");
838 Feature feature
= Feature
.DISTRIBUTION();
839 //distribution parsing almost impossible as there is lots of freetext in the distribution tag
840 return addDescriptionElement(state
, taxon
, value
, feature
, null);
848 * @param unhandledDescriptionChildren
850 private TextData
handleEcology(EfloraImportState state
, Element elEcology
, Taxon taxon
) {
851 verifyNoAttribute(elEcology
);
852 verifyNoChildren(elEcology
, true);
853 String value
= elEcology
.getTextNormalize();
854 Feature feature
= Feature
.ECOLOGY();
855 if (value
.startsWith("Habitat & Ecology")){
856 feature
= getFeature("Habitat & Ecology", state
);
857 value
= replaceStart(value
, "Habitat & Ecology");
858 }else if (value
.startsWith("Habitat")){
859 value
= replaceStart(value
, "Habitat");
860 feature
= getFeature("Habitat", state
);
862 return addDescriptionElement(state
, taxon
, value
, feature
, null);
869 * @param replacementString
871 private String
replaceStart(String value
, String replacementString
) {
872 if (value
.startsWith(replacementString
) ){
873 value
= value
.substring(replacementString
.length()).trim();
875 while (value
.startsWith("-") || value
.startsWith("–") ){
876 value
= value
.substring("-".length()).trim();
884 * @param replacementString
886 protected String
removeTrailing(String value
, String replacementString
) {
890 if (value
.endsWith(replacementString
) ){
891 value
= value
.substring(0, value
.length() - replacementString
.length()).trim();
900 * @param unhandledNomeclatureChildren
902 private void handleNomenclature(EfloraImportState state
, Element elNomenclature
, Taxon taxon
, Set
<String
> unhandledChildren
) {
903 verifyNoAttribute(elNomenclature
);
905 List
<Element
> elements
= elNomenclature
.getChildren();
906 for (Element element
: elements
){
907 if (element
.getName().equals("homotypes")){
908 handleHomotypes(state
, element
, taxon
);
909 }else if (element
.getName().equals("notes")){
910 handleNomenclatureNotes(state
, element
, taxon
);
912 unhandledChildren
.add(element
.getName());
920 private void handleNomenclatureNotes(EfloraImportState state
, Element elNotes
, Taxon taxon
) {
921 verifyNoAttribute(elNotes
);
922 verifyNoChildren(elNotes
);
923 String notesText
= elNotes
.getTextNormalize();
924 Annotation annotation
= Annotation
.NewInstance(notesText
, AnnotationType
.EDITORIAL(), Language
.DEFAULT());
925 taxon
.addAnnotation(annotation
);
930 private static Set
<String
> unhandledHomotypeChildren
= new HashSet
<String
>();
936 private void handleHomotypes(EfloraImportState state
, Element elHomotypes
, Taxon taxon
) {
937 verifyNoAttribute(elHomotypes
);
939 List
<Element
> elements
= elHomotypes
.getChildren();
940 HomotypicalGroup homotypicalGroup
= null;
941 for (Element element
: elements
){
942 if (element
.getName().equals("nom")){
943 homotypicalGroup
= handleNom(state
, element
, taxon
, homotypicalGroup
);
945 unhandledHomotypeChildren
.add(element
.getName());
951 private static Set
<String
> unhandledNomChildren
= new HashSet
<String
>();
958 private HomotypicalGroup
handleNom(EfloraImportState state
, Element elNom
, Taxon taxon
, HomotypicalGroup homotypicalGroup
) {
959 List
<Attribute
> attributes
= elNom
.getAttributes();
961 boolean taxonBaseClassType
= false;
962 for (Attribute attribute
: attributes
){
963 if (! attribute
.getName().equalsIgnoreCase("class")){
964 logger
.warn("Nom has unhandled attribute " + attribute
.getName());
966 String classValue
= attribute
.getValue();
967 if (classValue
.equalsIgnoreCase("acceptedname")){
968 homotypicalGroup
= handleNomTaxon(state
, elNom
, taxon
,homotypicalGroup
, false);
969 taxonBaseClassType
= true;
970 }else if (classValue
.equalsIgnoreCase("synonym")){
971 homotypicalGroup
= handleNomTaxon(state
, elNom
, taxon
, homotypicalGroup
, true);
972 taxonBaseClassType
= true;
973 }else if (classValue
.equalsIgnoreCase("typeref")){
974 handleTypeRef(state
, elNom
, taxon
, homotypicalGroup
);
976 logger
.warn("Unhandled class value for nom: " + classValue
);
982 List
<Element
> elements
= elNom
.getChildren();
983 for (Element element
: elements
){
984 if (element
.getName().equals("name") || element
.getName().equals("homonym") ){
985 if (taxonBaseClassType
== false){
986 logger
.warn("Name or homonym tag not allowed in non taxon nom tag");
989 unhandledNomChildren
.add(element
.getName());
993 return homotypicalGroup
;
1001 * @param homotypicalGroup
1003 protected void handleTypeRef(EfloraImportState state
, Element elNom
, Taxon taxon
, HomotypicalGroup homotypicalGroup
) {
1004 verifyNoChildren(elNom
);
1005 String typeRef
= elNom
.getTextNormalize();
1006 typeRef
= removeStartingTypeRefMinus(typeRef
);
1008 String
[] split
= typeRef
.split(":");
1009 if (split
.length
< 2){
1010 logger
.warn("typeRef has no ':' : " + typeRef
);
1011 }else if (split
.length
> 2){
1012 logger
.warn("typeRef has more than 1 ':' : " + typeRef
);
1014 StringBuffer typeType
= new StringBuffer(split
[0]);
1015 String typeText
= split
[1].trim();
1016 TypeDesignationBase typeDesignation
= getTypeDesignationAndReference(typeType
);
1018 //Name Type Desitnations
1019 if (typeDesignation
instanceof NameTypeDesignation
){
1020 makeNameTypeDesignations(typeType
, typeText
, typeDesignation
);
1022 //SpecimenTypeDesignations
1023 else if (typeDesignation
instanceof SpecimenTypeDesignation
){
1024 makeSpecimenTypeDesignation(typeType
, typeText
, typeDesignation
);
1026 logger
.error("Unhandled type designation class" + typeDesignation
.getClass().getName());
1028 for (TaxonNameBase name
: homotypicalGroup
.getTypifiedNames()){
1029 name
.addTypeDesignation(typeDesignation
, true);
1039 protected String
removeStartingTypeRefMinus(String typeRef
) {
1040 typeRef
= replaceStart(typeRef
, "-");
1041 typeRef
= replaceStart(typeRef
, "—");
1042 typeRef
= replaceStart(typeRef
, "\u002d");
1043 typeRef
= replaceStart(typeRef
, "\u2013");
1044 typeRef
= replaceStart(typeRef
, "--");
1051 * @param typeDesignation
1053 private void makeNameTypeDesignations(StringBuffer typeType
, String typeText
, TypeDesignationBase typeDesignation
) {
1054 if (typeType
.toString().trim().equalsIgnoreCase("Type")){
1056 }else if (typeType
.toString().trim().equalsIgnoreCase("Lectotype")){
1057 typeDesignation
.setTypeStatus(SpecimenTypeDesignationStatus
.LECTOTYPE());
1058 }else if (typeType
.toString().trim().equalsIgnoreCase("Syntype")){
1059 typeDesignation
.setTypeStatus(SpecimenTypeDesignationStatus
.SYNTYPE());
1061 logger
.warn("Unhandled type string: " + typeType
+ "(" + CharUtils
.unicodeEscaped(typeType
.charAt(0)) + ")");
1064 typeText
= cleanNameType(typeText
);
1066 BotanicalName nameType
= (BotanicalName
)parser
.parseFullName(typeText
, NomenclaturalCode
.ICNAFP
, Rank
.SPECIES());
1067 ((NameTypeDesignation
) typeDesignation
).setTypeName(nameType
);
1068 //TODO wie können NameTypes den Namen zugeordnet werden? - wird aber vom Portal via NameCache matching gemacht
1072 private String
cleanNameType(String typeText
) {
1074 String
[] split
= typeText
.split("\\[.*\\].?");
1083 * @param typeDesignation
1085 protected void makeSpecimenTypeDesignation(StringBuffer typeType
, String typeText
, TypeDesignationBase typeDesignation
) {
1086 if (typeType
.toString().trim().equalsIgnoreCase("Type")){
1088 }else if (typeType
.toString().trim().equalsIgnoreCase("Neotype") || typeType
.toString().trim().equalsIgnoreCase("Neotypes")){
1089 typeDesignation
.setTypeStatus(SpecimenTypeDesignationStatus
.NEOTYPE());
1090 }else if (typeType
.toString().trim().equalsIgnoreCase("Syntype") || typeType
.toString().trim().equalsIgnoreCase("Syntypes")){
1091 typeDesignation
.setTypeStatus(SpecimenTypeDesignationStatus
.SYNTYPE());
1092 }else if (typeType
.toString().trim().equalsIgnoreCase("Lectotype")){
1093 typeDesignation
.setTypeStatus(SpecimenTypeDesignationStatus
.LECTOTYPE());
1094 }else if (typeType
.toString().trim().equalsIgnoreCase("Paratype")){
1095 typeDesignation
.setTypeStatus(SpecimenTypeDesignationStatus
.PARATYPE());
1097 logger
.warn("Unhandled type string: " + typeType
);
1099 DerivedUnit specimen
= DerivedUnit
.NewPreservedSpecimenInstance();
1100 if (typeText
.length() > 255){
1101 specimen
.setTitleCache(typeText
.substring(0, 252) + "...", true);
1103 specimen
.setTitleCache(typeText
, true);
1105 specimen
.putDefinition(Language
.ENGLISH(), typeText
);
1106 ((SpecimenTypeDesignation
) typeDesignation
).setTypeSpecimen(specimen
);
1109 private TypeDesignationBase
getTypeDesignationAndReference(StringBuffer typeType
) {
1110 TypeDesignationBase result
;
1111 Reference ref
= parseTypeDesignationReference(typeType
);
1112 if (typeType
.indexOf(" species")>-1 || typeType
.indexOf("genus")>-1){
1113 if (typeType
.indexOf(" species")>-1 ){
1114 result
= NameTypeDesignation
.NewInstance();
1115 int start
= typeType
.indexOf(" species");
1116 typeType
.replace(start
, start
+ " species".length(), "");
1118 result
= NameTypeDesignation
.NewInstance();
1119 int start
= typeType
.indexOf(" genus");
1120 typeType
.replace(start
, start
+ " genus".length(), "");
1123 result
= SpecimenTypeDesignation
.NewInstance();
1125 result
.setCitation(ref
);
1130 private Reference
parseTypeDesignationReference(StringBuffer typeType
) {
1131 Reference result
= null;
1132 String reBracketReference
= "\\(.*\\)";
1133 Pattern patBracketReference
= Pattern
.compile(reBracketReference
);
1134 Matcher matcher
= patBracketReference
.matcher(typeType
);
1135 if (matcher
.find()){
1136 String refString
= matcher
.group();
1137 int start
= typeType
.indexOf(refString
);
1138 typeType
.replace(start
, start
+ refString
.length(), "");
1139 refString
= refString
.replace("(", "").replace(")", "").trim();
1140 Reference ref
= ReferenceFactory
.newGeneric();
1141 ref
.setTitleCache(refString
, true);
1154 private HomotypicalGroup
handleNomTaxon(EfloraImportState state
, Element elNom
, Taxon taxon
, HomotypicalGroup homotypicalGroup
, boolean isSynonym
) {
1155 NonViralName name
= makeName(taxon
, homotypicalGroup
, isSynonym
);
1158 boolean hasGenusInfo
= false;
1159 TeamOrPersonBase lastTeam
= null;
1162 List
<Element
> elGenus
= XmlHelp
.getAttributedChildListWithValue(elNom
, "name", "class", "genus");
1163 if (elGenus
.size() > 0){
1164 hasGenusInfo
= true;
1166 logger
.debug ("No Synonym Genus");
1168 //infra rank -> needed to handle authors correctly
1169 List
<Element
> elInfraRank
= XmlHelp
.getAttributedChildListWithValue(elNom
, "name", "class", "infrank");
1170 Rank infraRank
= null;
1171 infraRank
= handleInfRank(name
, elInfraRank
, infraRank
);
1173 //get left over elements
1174 List
<Element
> elements
= elNom
.getChildren();
1175 elements
.removeAll(elInfraRank
);
1177 for (Element element
: elements
){
1178 if (element
.getName().equals("name")){
1179 String classValue
= element
.getAttributeValue("class");
1180 String value
= element
.getValue().trim();
1181 if (classValue
.equalsIgnoreCase("genus") || classValue
.equalsIgnoreCase("family") ){
1182 name
.setGenusOrUninomial(value
);
1183 }else if (classValue
.equalsIgnoreCase("family") ){
1184 name
.setGenusOrUninomial(value
);
1185 name
.setRank(Rank
.FAMILY());
1186 }else if (classValue
.equalsIgnoreCase("subgenus")){
1187 //name.setInfraGenericEpithet(value);
1188 name
.setNameCache(value
.replace(":", "").trim());
1189 name
.setRank(Rank
.SUBGENUS());
1190 }else if (classValue
.equalsIgnoreCase("epithet") ){
1191 if (hasGenusInfo
== true){
1192 name
.setSpecificEpithet(value
);
1194 handleInfraspecificEpithet(element
, classValue
, name
);
1196 }else if (classValue
.equalsIgnoreCase("author")){
1197 handleNameAuthors(element
, name
);
1198 }else if (classValue
.equalsIgnoreCase("paraut")){
1199 handleBasionymAuthor(state
, element
, name
, false);
1200 }else if (classValue
.equalsIgnoreCase("infrauthor") || classValue
.equalsIgnoreCase("infraut")){
1201 handleInfrAuthor(state
, element
, name
, true);
1202 }else if (classValue
.equalsIgnoreCase("infrapar") || classValue
.equalsIgnoreCase("infrpar") || classValue
.equalsIgnoreCase("parauthor") ){
1203 handleBasionymAuthor(state
, element
, name
, true);
1204 }else if (classValue
.equalsIgnoreCase("infrepi")){
1205 handleInfrEpi(name
, infraRank
, value
);
1206 }else if (classValue
.equalsIgnoreCase("pub")){
1207 lastTeam
= handleNomenclaturalReference(name
, value
);
1208 }else if (classValue
.equalsIgnoreCase("usage")){
1209 lastTeam
= handleNameUsage(taxon
, name
, value
, lastTeam
);
1210 }else if (classValue
.equalsIgnoreCase("note")){
1211 handleNameNote(name
, value
);
1212 }else if (classValue
.equalsIgnoreCase("num")){
1214 logger
.warn("Duplicate num: " + value
);
1218 if (isSynonym
== true){
1219 logger
.warn("Synonym should not have a num");
1221 }else if (classValue
.equalsIgnoreCase("typification")){
1222 logger
.warn("Typification should not be a nom class");
1224 logger
.warn("Unhandled name class: " + classValue
);
1226 }else if(element
.getName().equals("homonym")){
1227 handleHomonym(state
, element
, name
);
1229 // child element is not "name"
1230 unhandledNomChildren
.add(element
.getName());
1236 String taxonString
= name
.getNameCache();
1237 //try to find matching lead nodes
1238 UnmatchedLeadsKey leadsKey
= UnmatchedLeadsKey
.NewInstance(num
, taxonString
);
1239 Set
<PolytomousKeyNode
> matchingNodes
= handleMatchingNodes(state
, taxon
, leadsKey
);
1240 //same without using the num
1242 UnmatchedLeadsKey noNumLeadsKey
= UnmatchedLeadsKey
.NewInstance("", taxonString
);
1243 handleMatchingNodes(state
, taxon
, noNumLeadsKey
);
1245 if (matchingNodes
.isEmpty() && num
!= null){
1246 logger
.warn("Taxon has num but no matching nodes exist: " + num
+ ", Key: " + leadsKey
.toString());
1250 //test nom element has no text
1251 if (StringUtils
.isNotBlank(elNom
.getTextNormalize().replace("—", "").replace("\u002d","").replace("\u2013", ""))){
1252 String strElNom
= elNom
.getTextNormalize();
1253 if ("?".equals(strElNom
)){
1254 handleQuestionMark(name
, taxon
);
1256 // Character c = strElNom.charAt(0);
1257 //System.out.println(CharUtils.unicodeEscaped(c));
1258 logger
.warn("Nom tag has text: " + strElNom
);
1261 return name
.getHomotypicalGroup();
1265 private void handleQuestionMark(NonViralName name
, Taxon taxon
) {
1266 int count
= name
.getTaxonBases().size();
1268 logger
.warn("Name has " + count
+ " taxa. This is not handled for question mark");
1270 TaxonBase taxonBase
= (TaxonBase
)name
.getTaxonBases().iterator().next();
1271 taxonBase
.setDoubtful(true);
1276 //merge with handleNomTaxon
1277 private void handleHomonym(EfloraImportState state
, Element elHomonym
, NonViralName upperName
) {
1278 verifyNoAttribute(elHomonym
);
1281 BotanicalName homonymName
= BotanicalName
.NewInstance(upperName
.getRank());
1282 homonymName
.setGenusOrUninomial(upperName
.getGenusOrUninomial());
1283 homonymName
.setInfraGenericEpithet(upperName
.getInfraGenericEpithet());
1284 homonymName
.setSpecificEpithet(upperName
.getSpecificEpithet());
1285 homonymName
.setInfraSpecificEpithet(upperName
.getInfraSpecificEpithet());
1287 for (Element elName
: (List
<Element
>)elHomonym
.getChildren("name")){
1288 String classValue
= elName
.getAttributeValue("class");
1289 String value
= elName
.getValue().trim();
1290 if (classValue
.equalsIgnoreCase("genus") ){
1291 homonymName
.setGenusOrUninomial(value
);
1292 }else if (classValue
.equalsIgnoreCase("epithet") ){
1293 homonymName
.setSpecificEpithet(value
);
1294 }else if (classValue
.equalsIgnoreCase("author")){
1295 handleNameAuthors(elName
, homonymName
);
1296 }else if (classValue
.equalsIgnoreCase("paraut")){
1297 handleBasionymAuthor(state
, elName
, homonymName
, true);
1298 }else if (classValue
.equalsIgnoreCase("pub")){
1299 handleNomenclaturalReference(homonymName
, value
);
1300 }else if (classValue
.equalsIgnoreCase("note")){
1301 handleNameNote(homonymName
, value
);
1303 logger
.warn("Unhandled class value: " + classValue
);
1306 //TODO verify other information
1310 boolean homonymIsLater
= false;
1311 NameRelationshipType relType
= NameRelationshipType
.LATER_HOMONYM();
1312 if (upperName
.getNomenclaturalReference() != null && homonymName
.getNomenclaturalReference() != null){
1313 TimePeriod homonymYear
= homonymName
.getNomenclaturalReference().getDatePublished();
1314 TimePeriod nameYear
= upperName
.getNomenclaturalReference().getDatePublished();
1315 homonymIsLater
= homonymYear
.getStart().compareTo(nameYear
.getStart()) > 0;
1317 if (upperName
.getNomenclaturalReference() == null){
1318 logger
.warn("Homonym parent does not have a nomenclatural reference or year: " + upperName
.getTitleCache());
1320 if (homonymName
.getNomenclaturalReference() == null){
1321 logger
.warn("Homonym does not have a nomenclatural reference or year: " + homonymName
.getTitleCache());
1324 if (homonymIsLater
){
1325 homonymName
.addRelationshipToName(upperName
, relType
, null);
1327 upperName
.addRelationshipToName(homonymName
, relType
, null);
1339 private Set
<PolytomousKeyNode
> handleMatchingNodes(EfloraImportState state
, Taxon taxon
, UnmatchedLeadsKey leadsKey
) {
1340 Set
<PolytomousKeyNode
> matchingNodes
= state
.getUnmatchedLeads().getNodes(leadsKey
);
1341 for (PolytomousKeyNode matchingNode
: matchingNodes
){
1342 state
.getUnmatchedLeads().removeNode(leadsKey
, matchingNode
);
1343 matchingNode
.setTaxon(taxon
);
1344 state
.getPolytomousKeyNodesToSave().add(matchingNode
);
1346 return matchingNodes
;
1350 private void handleNameNote(NonViralName name
, String value
) {
1351 logger
.warn("Name note: " + value
+ ". Available in portal?");
1352 Annotation annotation
= Annotation
.NewInstance(value
, AnnotationType
.EDITORIAL(), Language
.DEFAULT());
1353 name
.addAnnotation(annotation
);
1362 protected TeamOrPersonBase
handleNameUsage(Taxon taxon
, NonViralName
<?
> name
, String referenceTitle
, TeamOrPersonBase lastTeam
) {
1363 Reference ref
= ReferenceFactory
.newGeneric();
1364 referenceTitle
= removeStartingSymbols(referenceTitle
, ref
);
1366 ref
.setTitleCache(referenceTitle
, true);
1367 String microReference
= parseReferenceYearAndDetail(ref
);
1368 TeamOrPersonBase
<?
> team
= getReferenceAuthor(ref
);
1369 parseReferenceType(ref
);
1373 ref
.setAuthorship(team
);
1375 TaxonDescription description
= getDescription(taxon
);
1376 TextData textData
= TextData
.NewInstance(Feature
.CITATION());
1377 textData
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
, null, null, ref
, microReference
, name
, null);
1378 description
.addElement(textData
);
1384 * @param referenceTitle
1388 private String
removeStartingSymbols(String referenceTitle
, Reference ref
) {
1389 if (referenceTitle
.startsWith(";") || referenceTitle
.startsWith(",") || referenceTitle
.startsWith(":")){
1390 referenceTitle
= referenceTitle
.substring(1).trim();
1391 ref
.setTitleCache(referenceTitle
);
1393 return referenceTitle
;
1397 private void parseReferenceType(Reference ref
) {
1398 String title
= ref
.getTitle();
1402 title
= title
.trim();
1404 if (! title
.startsWith("in ")){
1405 ref
.setType(ReferenceType
.Book
);
1409 title
= title
.substring(3);
1412 if (title
.indexOf(",") == -1){
1413 ref
.setType(ReferenceType
.Article
);
1414 IJournal journal
= ReferenceFactory
.newJournal();
1415 journal
.setTitle(title
);
1417 ref
.setInJournal(journal
);
1421 ref
.setType(ReferenceType
.BookSection
);
1422 String
[] split
= (title
).split(",\\s*[A-Z]");
1423 if (split
.length
<= 1){
1424 logger
.warn("Can not fully decide what reference type. Guess it is a book section: " + title
);
1426 IBook book
= ReferenceFactory
.newBook();
1427 Team bookTeam
= Team
.NewTitledInstance(split
[0].trim(), split
[0].trim());
1429 title
= title
.substring(split
[0].length() + 1).trim();
1430 } catch (Exception e
) {
1431 logger
.error("ERROR occurred when trying to split title: " + title
+ "; split[0]: + " + split
[0]);
1433 book
.setTitle(title
);
1434 book
.setAuthorship(bookTeam
);
1435 book
.setDatePublished(ref
.getDatePublished());
1437 ref
.setInBook(book
);
1442 protected Team
getReferenceAuthor (Reference ref
) {
1443 boolean isCache
= false;
1444 String referenceTitle
= ref
.getTitle();
1445 if (referenceTitle
== null){
1447 referenceTitle
= ref
.getTitleCache();
1450 String
[] split
= (" " + referenceTitle
).split(" in ");
1451 if (split
.length
> 1){
1452 if (StringUtils
.isNotBlank(split
[0])){
1453 //' in ' is within the reference string, take the preceding string as the team
1454 Team team
= Team
.NewTitledInstance(split
[0].trim(), split
[0].trim());
1456 ref
.setTitle("in " + split
[1]);
1460 //string starts with in therefore no author is given
1465 split
= referenceTitle
.split(",");
1466 if (split
.length
< 2){
1467 //no author is given
1472 split
= (referenceTitle
).split(",\\s*[A-Z]");
1473 if (split
.length
> 1){
1474 Team team
= Team
.NewTitledInstance(split
[0].trim(), split
[0].trim());
1476 ref
.setTitle(referenceTitle
.substring(split
[0].length()+1).trim());
1480 logger
.warn("Can't decide if a usage has an author: " + referenceTitle
);
1487 * Replaced by <homonym> tag but still in use for exceptions
1492 protected String
parseHomonym(String detail
, NonViralName name
) {
1494 if (detail
== null){
1500 String reNon
= "(\\s|,)non\\s";
1501 Pattern patReference
= Pattern
.compile(reNon
);
1502 Matcher matcher
= patReference
.matcher(detail
);
1503 if (matcher
.find()){
1504 int start
= matcher
.start();
1505 int end
= matcher
.end();
1507 if (detail
!= null){
1508 logger
.warn("Unhandled non part: " + detail
.substring(start
));
1512 result
= detail
.substring(0, start
);
1515 String homonymString
= detail
.substring(end
);
1518 BotanicalName homonymName
= BotanicalName
.NewInstance(name
.getRank());
1519 homonymName
.setGenusOrUninomial(name
.getGenusOrUninomial());
1520 homonymName
.setInfraGenericEpithet(name
.getInfraGenericEpithet());
1521 homonymName
.setSpecificEpithet(name
.getSpecificEpithet());
1522 homonymName
.setInfraSpecificEpithet(name
.getInfraSpecificEpithet());
1523 Reference homonymNomRef
= ReferenceFactory
.newGeneric();
1524 homonymNomRef
.setTitleCache(homonymString
);
1525 String homonymNomRefDetail
= parseReferenceYearAndDetail(homonymNomRef
);
1526 homonymName
.setNomenclaturalMicroReference(homonymNomRefDetail
);
1527 String authorTitle
= homonymNomRef
.getTitleCache();
1528 Team team
= Team
.NewTitledInstance(authorTitle
, authorTitle
);
1529 homonymNomRef
.setAuthorship(team
);
1530 homonymNomRef
.setTitle("");
1531 homonymNomRef
.setProtectedTitleCache(false);
1534 boolean homonymIsLater
= false;
1535 NameRelationshipType relType
= NameRelationshipType
.LATER_HOMONYM();
1536 TimePeriod homonymYear
= homonymNomRef
.getDatePublished();
1537 if (name
.getNomenclaturalReference() != null){
1538 TimePeriod nameYear
= name
.getNomenclaturalReference().getDatePublished();
1539 homonymIsLater
= homonymYear
.getStart().compareTo(nameYear
.getStart()) > 0;
1541 logger
.warn("Classification name has no nomenclatural reference");
1543 if (homonymIsLater
){
1544 homonymName
.addRelationshipToName(name
, relType
, null);
1546 name
.addRelationshipToName(homonymName
, relType
, null);
1557 * @Xpath body/taxon/nomenclature/homotypes/nom/name[@class="pub"]
1561 protected TeamOrPersonBase
handleNomenclaturalReference(NonViralName name
, String value
) {
1562 Reference nomRef
= ReferenceFactory
.newGeneric();
1563 nomRef
.setTitleCache(value
, true);
1564 parseNomStatus(nomRef
, name
);
1565 String microReference
= parseReferenceYearAndDetail(nomRef
);
1566 name
.setNomenclaturalReference(nomRef
);
1567 microReference
= parseHomonym(microReference
, name
);
1568 name
.setNomenclaturalMicroReference(microReference
);
1569 TeamOrPersonBase team
= name
.getCombinationAuthorship();
1571 logger
.warn("Name has nom. ref. but no author team. Name: " + name
.getTitleCache() + ", Nom.Ref.: " + value
);
1573 nomRef
.setAuthorship(team
);
1578 private void handleInfrAuthor(EfloraImportState state
, Element elAuthor
, NonViralName name
, boolean overwrite
) {
1579 String strAuthor
= elAuthor
.getValue().trim();
1580 if (strAuthor
.endsWith(",")){
1581 strAuthor
= strAuthor
.substring(0, strAuthor
.length() -1);
1583 TeamOrPersonBase
[] team
= getTeam(strAuthor
);
1584 if (name
.getCombinationAuthorship() != null && overwrite
== false){
1585 logger
.warn("Try to write combination author for a name that already has a combination author. Neglected.");
1587 name
.setCombinationAuthorship(team
[0]);
1588 name
.setExCombinationAuthorship(team
[1]);
1596 * Sets the names rank according to the infrank value
1599 * @param elInfraRank
1603 private Rank
handleInfRank(NonViralName name
, List
<Element
> elInfraRank
, Rank infraRank
) {
1604 if (elInfraRank
.size() == 1){
1605 String strRank
= elInfraRank
.get(0).getTextNormalize();
1607 infraRank
= Rank
.getRankByNameOrIdInVoc(strRank
);
1608 } catch (UnknownCdmTypeException e
) {
1610 infraRank
= Rank
.getRankByNameOrIdInVoc(strRank
+ ".");
1611 } catch (UnknownCdmTypeException e2
) {
1612 logger
.warn("Unknown infrank " + strRank
+ ". Set infraRank to (null).");
1615 }else if (elInfraRank
.size() > 1){
1616 logger
.warn ("There is more than 1 infrank");
1618 if (infraRank
!= null){
1619 name
.setRank(infraRank
);
1625 private void handleInfrEpi(NonViralName
<?
> name
, Rank infraRank
, String value
) {
1626 if (infraRank
!= null && infraRank
.isInfraSpecific()){
1627 name
.setInfraSpecificEpithet(value
);
1628 if (CdmUtils
.isCapital(value
)){
1629 logger
.warn("Infraspecific epithet starts with a capital letter: " + value
);
1631 }else if (infraRank
!= null && infraRank
.isInfraGeneric()){
1632 name
.setInfraGenericEpithet(value
);
1633 if (! CdmUtils
.isCapital(value
)){
1634 logger
.warn("Infrageneric epithet does not start with a capital letter: " + value
);
1637 logger
.warn("Infrepi could not be handled: " + value
);
1644 * Returns the (empty) with the correct homotypical group depending on the taxon status
1646 * @param homotypicalGroup
1650 private NonViralName
makeName(Taxon taxon
,HomotypicalGroup homotypicalGroup
, boolean isSynonym
) {
1653 name
= BotanicalName
.NewInstance(Rank
.SPECIES(), homotypicalGroup
);
1654 SynonymType synonymType
= SynonymType
.HETEROTYPIC_SYNONYM_OF();
1655 if (taxon
.getHomotypicGroup().equals(homotypicalGroup
)){
1656 synonymType
= SynonymType
.HOMOTYPIC_SYNONYM_OF();
1658 taxon
.addSynonymName(name
, synonymType
);
1660 name
= (NonViralName
)taxon
.getName();
1670 private void handleInfraspecificEpithet(Element element
, String attrValue
, NonViralName
<?
> name
) {
1671 String value
= element
.getTextNormalize();
1672 if (value
.indexOf("subsp.") != -1){
1673 //TODO genus and species epi
1674 String infrEpi
= value
.substring(value
.indexOf("subsp.") + 6).trim();
1675 name
.setInfraSpecificEpithet(infrEpi
);
1676 name
.setRank(Rank
.SUBSPECIES());
1677 }else if (value
.indexOf("var.") != -1){
1678 //TODO genus and species epi
1679 String infrEpi
= value
.substring(value
.indexOf("var.") + 4).trim();
1680 name
.setInfraSpecificEpithet(infrEpi
);
1681 name
.setRank(Rank
.VARIETY());
1683 logger
.warn("Unhandled infraspecific type: " + value
);
1693 private void handleBasionymAuthor(EfloraImportState state
, Element elBasionymAuthor
, NonViralName name
, boolean overwrite
) {
1694 String strAuthor
= elBasionymAuthor
.getValue().trim();
1695 Pattern reBasionymAuthor
= Pattern
.compile("^\\(.*\\)$");
1696 if (reBasionymAuthor
.matcher(strAuthor
).matches()){
1697 strAuthor
= strAuthor
.substring(1, strAuthor
.length()-1);
1699 logger
.warn("Brackets are missing for original combination author " + strAuthor
);
1701 TeamOrPersonBase
[] basionymTeam
= getTeam(strAuthor
);
1702 if (name
.getBasionymAuthorship() != null && overwrite
== false){
1703 logger
.warn("Try to write basionym author for a name that already has a basionym author. Neglected.");
1705 name
.setBasionymAuthorship(basionymTeam
[0]);
1706 name
.setExBasionymAuthorship(basionymTeam
[1]);
1711 private final Map
<String
, UUID
> teamMap
= new HashMap
<String
, UUID
>();
1717 private void handleNameAuthors(Element elAuthor
, NonViralName name
) {
1718 if (name
.getCombinationAuthorship() != null){
1719 logger
.warn("Name already has a combination author. Name: " + name
.getTitleCache() + ", Author: " + elAuthor
.getTextNormalize());
1721 String strAuthor
= elAuthor
.getValue().trim();
1722 if (strAuthor
.endsWith(",")){
1723 strAuthor
= strAuthor
.substring(0, strAuthor
.length() -1);
1725 if (strAuthor
.indexOf("(") > -1 || strAuthor
.indexOf(")") > -1){
1726 logger
.warn("Author has brackets. Basionym authors should be handled in separate tags: " + strAuthor
);
1728 TeamOrPersonBase
[] team
= getTeam(strAuthor
);
1729 name
.setCombinationAuthorship(team
[0]);
1730 name
.setExCombinationAuthorship(team
[1]);
1738 private TeamOrPersonBase
[] getTeam(String strAuthor
) {
1739 TeamOrPersonBase
[] result
= new TeamOrPersonBase
[2];
1740 String
[] split
= strAuthor
.split(" ex ");
1741 String strBaseAuthor
= null;
1742 String strExAuthor
= null;
1744 if (split
.length
== 2){
1745 strBaseAuthor
= split
[1];
1746 strExAuthor
= split
[0];
1747 }else if (split
.length
== 1){
1748 strBaseAuthor
= split
[0];
1750 logger
.warn("Could not parse (ex) author: " + strAuthor
);
1752 result
[0] = getUuidTeam(strBaseAuthor
);
1753 if (result
[0] == null){
1754 result
[0] = parseSingleTeam(strBaseAuthor
);
1755 teamMap
.put(strBaseAuthor
, result
[0].getUuid());
1757 if (strExAuthor
!= null){
1758 result
[1] = getUuidTeam(strExAuthor
);
1759 if (result
[1] == null){
1760 result
[1] = Team
.NewInstance();
1761 result
[1].setTitleCache(strExAuthor
, true);
1762 teamMap
.put(strExAuthor
, result
[1].getUuid());
1770 protected TeamOrPersonBase
parseSingleTeam(String strBaseAuthor
) {
1771 TeamOrPersonBase result
;
1772 String
[] split
= strBaseAuthor
.split("&");
1773 if (split
.length
> 1){
1774 result
= Team
.NewInstance();
1775 for (String personString
: split
){
1776 Person person
= makePerson(personString
);
1777 ((Team
)result
).addTeamMember(person
);
1780 result
= makePerson(strBaseAuthor
.trim());
1787 * @param personString
1790 private Person
makePerson(String personString
) {
1791 personString
= personString
.trim();
1792 Person person
= Person
.NewTitledInstance(personString
);
1793 person
.setNomenclaturalTitle(personString
);
1800 * @param strBaseAuthor
1802 private TeamOrPersonBase
getUuidTeam(String strBaseAuthor
) {
1803 UUID uuidTeam
= teamMap
.get(strBaseAuthor
);
1804 return CdmBase
.deproxy(getAgentService().find(uuidTeam
), TeamOrPersonBase
.class);
1808 private void handleDescription(EfloraImportState state
, Element elDescription
, Taxon taxon
, Set
<String
> unhandledChildren
) {
1809 verifyNoAttribute(elDescription
);
1811 List
<Element
> elements
= elDescription
.getChildren();
1812 for (Element element
: elements
){
1813 if (element
.getName().equalsIgnoreCase("char")){
1814 handleChar(state
, element
, taxon
);
1816 logger
.warn("Unhandled description child: " + element
.getName());
1828 private void handleChar(EfloraImportState state
, Element element
, Taxon taxon
) {
1829 List
<Attribute
> attributes
= element
.getAttributes();
1830 for (Attribute attribute
: attributes
){
1831 if (! attribute
.getName().equalsIgnoreCase("class")){
1832 logger
.warn("Char has unhandled attribute " + attribute
.getName());
1834 String classValue
= attribute
.getValue();
1835 Feature feature
= getFeature(classValue
, state
);
1836 if (feature
== null){
1837 logger
.warn("Unhandled feature: " + classValue
);
1839 String value
= element
.getValue();
1840 addDescriptionElement(state
, taxon
, value
, feature
, null);
1846 List
<Element
> elements
= element
.getChildren();
1847 if (! elements
.isEmpty()){
1848 logger
.warn("Char has unhandled children");
1857 protected TaxonDescription
getDescription(Taxon taxon
) {
1858 for (TaxonDescription description
: taxon
.getDescriptions()){
1859 if (! description
.isImageGallery()){
1863 TaxonDescription newDescription
= TaxonDescription
.NewInstance(taxon
);
1864 return newDescription
;
1872 * @throws UndefinedTransformerMethodException
1874 private Feature
getFeature(String classValue
, EfloraImportState state
) {
1877 uuid
= state
.getTransformer().getFeatureUuid(classValue
);
1879 logger
.info("Uuid is null for " + classValue
);
1881 String featureText
= StringUtils
.capitalize(classValue
);
1882 //TODO eFlora feature vocabulary
1883 Feature feature
= getFeature(state
, uuid
, featureText
, featureText
, classValue
, null);
1884 if (feature
== null){
1885 throw new NullPointerException(classValue
+ " not recognized as a feature");
1888 } catch (Exception e
) {
1889 logger
.warn("Could not create feature for " + classValue
+ ": " + e
.getMessage()) ;
1890 return Feature
.UNKNOWN();
1899 * @param unhandledTitleClassess
1901 private void handleTitle(EfloraImportState state
, Element element
, Taxon taxon
, Set
<String
> unhandledTitleClassess
) {
1903 List
<Attribute
> attributes
= element
.getAttributes();
1904 for (Attribute attribute
: attributes
){
1905 if (! attribute
.getName().equalsIgnoreCase("class") ){
1906 if (! attribute
.getName().equalsIgnoreCase("num")){
1907 logger
.warn("Title has unhandled attribute " + attribute
.getName());
1909 //TODO num attribute in taxon
1912 String classValue
= attribute
.getValue();
1916 rank
= Rank
.getRankByNameOrIdInVoc(classValue
);
1917 } catch (Exception e
) {
1919 rank
= Rank
.getRankByEnglishName(classValue
, NomenclaturalCode
.ICNAFP
, false);
1921 taxon
.getName().setRank(rank
);
1922 if (rank
.equals(Rank
.FAMILY()) || rank
.equals(Rank
.GENUS())){
1923 handleGenus(element
.getValue(), taxon
.getName());
1924 }else if (rank
.equals(Rank
.SUBGENUS())){
1925 handleSubGenus(element
.getValue(), taxon
.getName());
1926 }else if (rank
.equals(Rank
.SECTION_BOTANY())){
1927 handleSection(element
.getValue(), taxon
.getName());
1928 }else if (rank
.equals(Rank
.SPECIES())){
1929 handleSpecies(element
.getValue(), taxon
.getName());
1930 }else if (rank
.equals(Rank
.SUBSPECIES())){
1931 handleSubSpecies(element
.getValue(), taxon
.getName());
1932 }else if (rank
.equals(Rank
.VARIETY())){
1933 handleVariety(element
.getValue(), taxon
.getName());
1935 logger
.warn("Unhandled rank: " + rank
.getLabel());
1937 } catch (UnknownCdmTypeException e
) {
1938 logger
.warn("Unknown rank " + classValue
);
1939 unhandledTitleClassess
.add(classValue
);
1943 List
<Element
> elements
= element
.getChildren();
1944 if (! elements
.isEmpty()){
1945 logger
.warn("Title has unexpected children");
1947 UUID uuidTitle
= EfloraTransformer
.uuidTitle
;
1948 ExtensionType titleExtension
= this.getExtensionType(state
, uuidTitle
, "title", "title", "title");
1949 taxon
.addExtension(element
.getTextNormalize(), titleExtension
);
1956 * @param taxonNameBase
1958 private void handleSubGenus(String value
, TaxonNameBase taxonNameBase
) {
1959 String name
= value
.replace("Subgenus", "").trim();
1960 ((NonViralName
)taxonNameBase
).setInfraGenericEpithet(name
);
1965 * @param taxonNameBase
1967 private void handleSection(String value
, TaxonNameBase taxonNameBase
) {
1968 String name
= value
.replace("Section", "").trim();
1969 ((NonViralName
)taxonNameBase
).setInfraGenericEpithet(name
);
1974 * @param taxonNameBase
1976 private void handleSpecies(String value
, TaxonNameBase taxonNameBase
) {
1982 * @param taxonNameBase
1984 private void handleVariety(String value
, TaxonNameBase taxonNameBase
) {
1990 * @param taxonNameBase
1992 private void handleSubSpecies(String value
, TaxonNameBase taxonNameBase
) {
1997 private final Pattern rexGenusAuthor
= Pattern
.compile("(\\[|\\().*(\\]|\\))");
2001 * @param taxonNameBase
2003 protected void handleGenus(String value
, TaxonNameBase taxonName
) {
2004 Matcher matcher
= rexGenusAuthor
.matcher(value
);
2005 if (matcher
.find()){
2006 String author
= matcher
.group();
2007 // String genus = value.replace(author, "");
2008 author
= author
.substring(1, author
.length() - 1);
2009 Team team
= Team
.NewInstance();
2010 team
.setTitleCache(author
, true);
2011 Credit credit
= Credit
.NewInstance(team
, null);
2012 taxonName
.addCredit(credit
);
2013 // NonViralName nvn = (NonViralName)taxonName;
2014 // nvn.setCombinationAuthorship(team);
2015 // nvn.setGenusOrUninomial(genus);
2017 logger
.info("No Author match for " + value
);
2026 private void handleTaxonRelation(EfloraImportState state
, Taxon taxon
, Taxon lastTaxon
) {
2028 Classification tree
= getTree(state
);
2029 if (lastTaxon
== null){
2030 tree
.addChildTaxon(taxon
, null, null);
2033 Rank thisRank
= taxon
.getName().getRank();
2034 Rank lastRank
= lastTaxon
.getName().getRank();
2035 if (lastTaxon
.getTaxonNodes().size() > 0){
2036 TaxonNode lastNode
= lastTaxon
.getTaxonNodes().iterator().next();
2037 if (thisRank
.isLower(lastRank
) ){
2038 lastNode
.addChildTaxon(taxon
, null, null);
2039 fillMissingEpithetsForTaxa(lastTaxon
, taxon
);
2040 }else if (thisRank
.equals(lastRank
)){
2041 TaxonNode parent
= lastNode
.getParent();
2042 if (parent
!= null){
2043 parent
.addChildTaxon(taxon
, null, null);
2044 fillMissingEpithetsForTaxa(parent
.getTaxon(), taxon
);
2046 tree
.addChildTaxon(taxon
, null, null);
2048 }else if (thisRank
.isHigher(lastRank
)){
2049 handleTaxonRelation(state
, taxon
, lastNode
.getParent().getTaxon());
2050 // TaxonNode parentNode = handleTaxonRelation(state, taxon, lastNode.getParent().getTaxon());
2051 // parentNode.addChildTaxon(taxon, null, null, null);
2054 logger
.warn("Last taxon has no node");
2064 private Classification
getTree(EfloraImportState state
) {
2065 Classification result
= state
.getTree(null);
2066 if (result
== null){
2067 UUID uuid
= state
.getConfig().getClassificationUuid();
2069 logger
.warn("No classification uuid is defined");
2070 result
= getNewClassification(state
);
2072 result
= getClassificationService().find(uuid
);
2073 if (result
== null){
2074 result
= getNewClassification(state
);
2075 result
.setUuid(uuid
);
2078 state
.putTree(null, result
);
2084 private Classification
getNewClassification(EfloraImportState state
) {
2085 Classification result
;
2086 result
= Classification
.NewInstance(state
.getConfig().getClassificationTitle());
2087 state
.putTree(null, result
);
2099 private TextData
addDescriptionElement(EfloraImportState state
, Taxon taxon
, String value
, Feature feature
, String references
) {
2100 TextData textData
= TextData
.NewInstance(feature
);
2101 Language textLanguage
= getDefaultLanguage(state
);
2102 textData
.putText(textLanguage
, value
);
2103 TaxonDescription description
= getDescription(taxon
);
2104 description
.addElement(textData
);
2105 if (references
!= null){
2106 makeOriginalSourceReferences(textData
, ";", references
);
2111 private Language
getDefaultLanguage(EfloraImportState state
) {
2112 UUID defaultLanguageUuid
= state
.getConfig().getDefaultLanguageUuid();
2113 if (defaultLanguageUuid
!= null){
2114 Language result
= state
.getDefaultLanguage();
2115 if (result
== null || ! result
.getUuid().equals(defaultLanguageUuid
)){
2116 result
= (Language
)getTermService().find(defaultLanguageUuid
);
2117 state
.setDefaultLanguage(result
);
2118 if (result
== null){
2119 logger
.warn("Default language for " + defaultLanguageUuid
+ " does not exist.");
2124 return Language
.DEFAULT();
2130 * @param elNomenclature
2132 private void verifyNoAttribute(Element element
) {
2133 List
<Attribute
> attributes
= element
.getAttributes();
2134 if (! attributes
.isEmpty()){
2135 logger
.warn(element
.getName() + " has unhandled attributes: " + attributes
.get(0).getValue() + "..." );
2140 * @param elNomenclature
2142 protected void verifyNoChildren(Element element
) {
2143 verifyNoChildren(element
, false);
2147 * @param elNomenclature
2149 private void verifyNoChildren(Element element
, boolean ignoreLineBreak
) {
2150 List
<Element
> children
= element
.getChildren();
2151 if (! children
.isEmpty()){
2152 if (ignoreLineBreak
== true){
2153 for (Element child
: children
){
2154 if (! child
.getName().equalsIgnoreCase("BR")){
2155 logger
.warn(element
.getName() + " has unhandled child: " + child
.getName());
2159 logger
.warn(element
.getName() + " has unhandled children");
2167 * Parses the nomenclatural status from the references titleCache. If a nomenclatural status
2168 * exists it is added to the name and the nom. status part of the references title cache is
2169 * removed. Requires protected title cache.
2171 * @param nonViralName
2173 protected void parseNomStatus(Reference ref
, NonViralName nonViralName
) {
2174 String titleToParse
= ref
.getTitleCache();
2176 String noStatusTitle
= parser
.parseNomStatus(titleToParse
, nonViralName
, true);
2177 if (! noStatusTitle
.equals(titleToParse
)){
2178 ref
.setTitleCache(noStatusTitle
, true);
2184 * Extracts the date published part and returns micro reference
2188 private String
parseReferenceYearAndDetail(Reference ref
){
2189 String detailResult
= null;
2190 String titleToParse
= ref
.getTitleCache();
2191 titleToParse
= removeStartingSymbols(titleToParse
, ref
);
2192 String reReference
= "^\\.{1,}";
2193 // String reYear = "\\([1-2]{1}[0-9]{3}\\)";
2194 String oneMonth
= "(Feb.|Dec.|March|June|July)";
2195 String reYear
= oneMonth
+ "?\\s?[1-2]\\s?[0-9]\\s?[0-9]\\s?[0-9]\\s?";
2196 String secondYear
= "(\\s?[1-2]\\s?[0-9])?\\s?[0-9]\\s?[0-9]\\s?";
2198 String reYearPeriod
= "\\(" + reYear
+ "(\\-" + secondYear
+ ")?\\)";
2199 String reDetail
= "\\.{1,10}$";
2201 //pattern for the whole string
2202 Pattern patReference
= Pattern
.compile(/*reReference +*/ reYearPeriod
/*+ reDetail */);
2203 Matcher matcher
= patReference
.matcher(titleToParse
);
2204 if (matcher
.find()){
2205 int start
= matcher
.start();
2206 int end
= matcher
.end();
2208 //title and other information precedes the year part
2209 String title
= titleToParse
.substring(0, start
).trim();
2210 //detail follows the year part
2211 String detail
= titleToParse
.substring(end
).trim();
2214 String strPeriod
= matcher
.group().trim();
2215 strPeriod
= strPeriod
.substring(1, strPeriod
.length()-1); //remove brackets
2216 Pattern patStartMonth
= Pattern
.compile("^" + oneMonth
);
2217 matcher
= patStartMonth
.matcher(strPeriod
);
2218 strPeriod
= strPeriod
.replace(" ", "");
2219 Integer startMonth
= null;
2220 if (matcher
.find()){
2221 end
= matcher
.end();
2222 strPeriod
= strPeriod
.substring(0, end
) + " " + strPeriod
.substring(end
);
2223 startMonth
= getMonth(strPeriod
.substring(0, end
));
2226 TimePeriod datePublished
= TimePeriodParser
.parseString(strPeriod
);
2227 if (startMonth
!= null){
2228 datePublished
.setStartMonth(startMonth
);
2230 ref
.setDatePublished(datePublished
);
2231 ref
.setTitle(title
);
2232 detailResult
= CdmUtils
.removeTrailingDot(detail
);
2233 if (detailResult
.endsWith(".") || detailResult
.endsWith(";") || detailResult
.endsWith(",") ){
2234 detailResult
= detailResult
.substring(0, detailResult
.length() -1);
2236 ref
.setProtectedTitleCache(false);
2238 logger
.warn("Could not parse reference: " + titleToParse
);
2240 return detailResult
;
2246 private Integer
getMonth(String month
) {
2247 if (month
.startsWith("Jan")){
2249 }else if (month
.startsWith("Feb")){
2251 }else if (month
.startsWith("Mar")){
2253 }else if (month
.startsWith("Apr")){
2255 }else if (month
.startsWith("May")){
2257 }else if (month
.startsWith("Jun")){
2259 }else if (month
.startsWith("Jul")){
2261 }else if (month
.startsWith("Aug")){
2263 }else if (month
.startsWith("Sep")){
2265 }else if (month
.startsWith("Oct")){
2267 }else if (month
.startsWith("Nov")){
2269 }else if (month
.startsWith("Dec")){
2272 logger
.warn("Month not yet supported: " + month
);
2279 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
2282 protected boolean isIgnore(EfloraImportState state
){
2283 return ! state
.getConfig().isDoTaxa();