Project

General

Profile

Download (54.9 KB) Statistics
| Branch: | Tag: | Revision:
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

    
10
package eu.etaxonomy.cdm.strategy.parser;
11

    
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.regex.Matcher;
15
import java.util.regex.Pattern;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.apache.log4j.Logger;
19
import org.joda.time.DateTimeFieldType;
20
import org.joda.time.Partial;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24
import eu.etaxonomy.cdm.model.agent.Person;
25
import eu.etaxonomy.cdm.model.agent.Team;
26
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.IParsable;
29
import eu.etaxonomy.cdm.model.common.TimePeriod;
30
import eu.etaxonomy.cdm.model.name.BacterialName;
31
import eu.etaxonomy.cdm.model.name.BotanicalName;
32
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
33
import eu.etaxonomy.cdm.model.name.HybridRelationship;
34
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
35
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
36
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
37
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
38
import eu.etaxonomy.cdm.model.name.NonViralName;
39
import eu.etaxonomy.cdm.model.name.Rank;
40
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
41
import eu.etaxonomy.cdm.model.name.ZoologicalName;
42
import eu.etaxonomy.cdm.model.reference.IBook;
43
import eu.etaxonomy.cdm.model.reference.IBookSection;
44
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
45
import eu.etaxonomy.cdm.model.reference.IVolumeReference;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
48
import eu.etaxonomy.cdm.model.reference.ReferenceType;
49
import eu.etaxonomy.cdm.strategy.exceptions.StringNotParsableException;
50
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
51

    
52

    
53
/**
54
 * @author a.mueller
55
 *
56
 */
57
public class NonViralNameParserImpl extends NonViralNameParserImplRegExBase implements INonViralNameParser<NonViralName> {
58
	private static final Logger logger = Logger.getLogger(NonViralNameParserImpl.class);
59

    
60
	// good intro: http://java.sun.com/docs/books/tutorial/essential/regex/index.html
61

    
62
	final static boolean MAKE_EMPTY = true;
63
	final static boolean MAKE_NOT_EMPTY = false;
64

    
65
	private final boolean authorIsAlwaysTeam = false;
66

    
67
	public static NonViralNameParserImpl NewInstance(){
68
		return new NonViralNameParserImpl();
69
	}
70

    
71
	@Override
72
    public NonViralName parseSimpleName(String simpleName){
73
		return parseSimpleName(simpleName, null, null);
74
	}
75

    
76
	@Override
77
    public NonViralName parseSimpleName(String simpleName, NomenclaturalCode code, Rank rank){
78
		//"parseSimpleName() not yet implemented. Uses parseFullName() instead");
79
		return parseFullName(simpleName, code, rank);
80
	}
81

    
82
	public void parseSimpleName(NonViralName nameToBeFilled, String simpleNameString, Rank rank, boolean makeEmpty){
83
		//"parseSimpleName() not yet implemented. Uses parseFullName() instead");
84
		parseFullName(nameToBeFilled, simpleNameString, rank, makeEmpty);
85
	}
86

    
87
	public NonViralName getNonViralNameInstance(String fullString, NomenclaturalCode code){
88
		return getNonViralNameInstance(fullString, code, null);
89
	}
90

    
91
	public NonViralName getNonViralNameInstance(String fullString, NomenclaturalCode code, Rank rank){
92
		NonViralName<?> result = null;
93
		if(code ==null) {
94
			boolean isBotanicalName = anyBotanicFullNamePattern.matcher(fullString).find();
95
			boolean isZoologicalName = anyZooFullNamePattern.matcher(fullString).find();;
96
			boolean isBacteriologicalName = false;
97
			boolean isCultivatedPlantName = false;
98
			if ( (isBotanicalName || isCultivatedPlantName) && ! isZoologicalName && !isBacteriologicalName){
99
				if (isBotanicalName){
100
					result = BotanicalName.NewInstance(rank);
101
				}else{
102
					result = CultivarPlantName.NewInstance(rank);
103
				}
104
			}else if ( isZoologicalName /*&& ! isBotanicalName*/ && !isBacteriologicalName && !isCultivatedPlantName){
105
				result = ZoologicalName.NewInstance(rank);
106
			}else if ( isZoologicalName && ! isBotanicalName && !isBacteriologicalName && !isCultivatedPlantName){
107
				result = BacterialName.NewInstance(rank);
108
			}else {
109
				result =  NonViralName.NewInstance(rank);
110
			}
111
		} else {
112
			switch (code) {
113
			case ICNAFP:
114
				result = BotanicalName.NewInstance(rank);
115
				break;
116
			case ICZN:
117
				result = ZoologicalName.NewInstance(rank);
118
				break;
119
			case ICNCP:
120
				logger.warn("ICNCP parsing not yet implemented");
121
				result = CultivarPlantName.NewInstance(rank);
122
				break;
123
			case ICNB:
124
				logger.warn("ICNB not yet implemented");
125
				result = BacterialName.NewInstance(rank);
126
				break;
127
			case ICVCN:
128
				logger.error("Viral name is not a NonViralName !!");
129
				break;
130
			default:
131
				// FIXME Unreachable code
132
				logger.error("Unknown Nomenclatural Code !!");
133
			}
134
		}
135
		return result;
136
	}
137

    
138
	@Override
139
    public NonViralName parseReferencedName(String fullReferenceString) {
140
		return parseReferencedName(fullReferenceString, null, null);
141
	}
142

    
143
	@Override
144
    public NonViralName parseReferencedName(String fullReferenceString, NomenclaturalCode nomCode, Rank rank) {
145
		if (fullReferenceString == null){
146
			return null;
147
		}else{
148
			NonViralName<?> result = getNonViralNameInstance(fullReferenceString, nomCode, rank);
149
			parseReferencedName(result, fullReferenceString, rank, MAKE_EMPTY);
150
			return result;
151
		}
152
	}
153

    
154
	private String standardize(NonViralName<?> nameToBeFilled, String fullReferenceString, boolean makeEmpty){
155
		//Check null and standardize
156
		if (fullReferenceString == null){
157
			//return null;
158
			return null;
159
		}
160
		if (makeEmpty){
161
			makeEmpty(nameToBeFilled);
162
		}
163
		fullReferenceString = fullReferenceString.replaceAll(oWs , " ");
164
		fullReferenceString = fullReferenceString.trim();
165
		if ("".equals(fullReferenceString)){
166
			fullReferenceString = null;
167
		}
168
		return fullReferenceString;
169
	}
170

    
171
	/**
172
	 * Returns the regEx to be used for the full-name depending on the code
173
	 * @param nameToBeFilled
174
	 * @return
175
	 */
176
	private String getCodeSpecificFullNameRegEx(NonViralName<?> nameToBeFilledOrig){
177
	    NonViralName<?> nameToBeFilled = HibernateProxyHelper.deproxy(nameToBeFilledOrig, NonViralName.class);
178
		if (nameToBeFilled instanceof ZoologicalName){
179
			return anyZooFullName;
180
		}else if (nameToBeFilled instanceof BotanicalName) {
181
			return anyBotanicFullName;
182
		}else if (nameToBeFilled instanceof NonViralName) {
183
			return anyBotanicFullName;  //TODO ?
184
		}else{
185
			logger.warn("nameToBeFilled class not supported ("+nameToBeFilled.getClass()+")");
186
			return null;
187
		}
188
	}
189

    
190
	/**
191
	 * Returns the regEx to be used for the fsimple-name depending on the code
192
	 * @param nameToBeFilled
193
	 * @return
194
	 */
195
	private String getCodeSpecificSimpleNameRegEx(NonViralName<?> nameToBeFilled){
196
		nameToBeFilled = HibernateProxyHelper.deproxy(nameToBeFilled, NonViralName.class);
197

    
198
		if (nameToBeFilled instanceof ZoologicalName){
199
			return anyZooName;
200
		}else if (nameToBeFilled instanceof NonViralName){
201
			return anyZooName;  //TODO ?
202
		}else if (nameToBeFilled instanceof BotanicalName) {
203
			return anyBotanicName;
204
		}else{
205
			logger.warn("nameToBeFilled class not supported ("+nameToBeFilled.getClass()+")");
206
			return null;
207
		}
208
	}
209

    
210
	private Matcher getMatcher(String regEx, String matchString){
211
		Pattern pattern = Pattern.compile(regEx);
212
		Matcher matcher = pattern.matcher(matchString);
213
		return matcher;
214
	}
215

    
216
	@Override
217
    public void parseReferencedName(NonViralName nameToBeFilled, String fullReferenceStringOrig, Rank rank, boolean makeEmpty) {
218
		//standardize
219
		String fullReferenceString = standardize(nameToBeFilled, fullReferenceStringOrig, makeEmpty);
220
		if (fullReferenceString == null){
221
			return;
222
		}
223
		// happens already in standardize(...)
224
//		makeProblemEmpty(nameToBeFilled);
225

    
226
		//make nomenclatural status and replace it by empty string
227
	    fullReferenceString = parseNomStatus(fullReferenceString, nameToBeFilled, makeEmpty);
228
	    nameToBeFilled.setProblemEnds(fullReferenceString.length());
229

    
230
	    //get full name reg
231
		String localFullNameRegEx = getCodeSpecificFullNameRegEx(nameToBeFilled);
232
		//get full name reg
233
		String localSimpleNameRegEx = getCodeSpecificSimpleNameRegEx(nameToBeFilled);
234

    
235
		//separate name and reference part
236
		String nameAndRefSeparatorRegEx = "(^" + localFullNameRegEx + ")("+ referenceSeperator + ")";
237
		Matcher nameAndRefSeparatorMatcher = getMatcher (nameAndRefSeparatorRegEx, fullReferenceString);
238

    
239
		Matcher onlyNameMatcher = getMatcher (localFullNameRegEx, fullReferenceString);
240
		Matcher hybridMatcher = hybridFormulaPattern.matcher(fullReferenceString);
241
		Matcher onlySimpleNameMatcher = getMatcher (localSimpleNameRegEx, fullReferenceString);
242

    
243
		if (onlyNameMatcher.matches()){
244
			makeEmpty = false;
245
			parseFullName(nameToBeFilled, fullReferenceString, rank, makeEmpty);
246
		} else if (nameAndRefSeparatorMatcher.find()){
247
			makeNameWithReference(nameToBeFilled, fullReferenceString, nameAndRefSeparatorMatcher, rank, makeEmpty);
248
		}else if (hybridMatcher.matches() ){
249
		    //I do not remember why we need makeEmpty = false for onlyNameMatcher,
250
		    //but for hybridMatcher we need to remove old Hybrid Relationships if necessary, therefore
251
		    //I removed it from here
252
            parseFullName(nameToBeFilled, fullReferenceString, rank, makeEmpty);
253
        }else if (onlySimpleNameMatcher.matches()){
254
			makeEmpty = false;
255
			parseFullName(nameToBeFilled, fullReferenceString, rank, makeEmpty);	//simpleName not yet implemented
256
		}else{
257
			makeNoFullRefMatch(nameToBeFilled, fullReferenceString, rank);
258
		}
259
		//problem handling. Start and end solved in subroutines
260
		if (! nameToBeFilled.hasProblem()){
261
			makeProblemEmpty(nameToBeFilled);
262
		}
263
	}
264

    
265
	private void makeProblemEmpty(IParsable parsable){
266
		boolean hasCheckRank = parsable.hasProblem(ParserProblem.CheckRank);
267
		parsable.setParsingProblem(0);
268
		if (hasCheckRank){
269
			parsable.addParsingProblem(ParserProblem.CheckRank);
270
		}
271
		parsable.setProblemStarts(-1);
272
		parsable.setProblemEnds(-1);
273
	}
274

    
275
	private void makeNoFullRefMatch(NonViralName<?> nameToBeFilled, String fullReferenceString, Rank rank){
276
	    //try to parse first part as name, but keep in mind full string is not parsable
277
		int start = 0;
278

    
279
		String localFullName = getCodeSpecificFullNameRegEx(nameToBeFilled);
280
		Matcher fullNameMatcher = getMatcher (pStart + localFullName, fullReferenceString);
281
		if (fullNameMatcher.find()){
282
			String fullNameString = fullNameMatcher.group(0);
283
			nameToBeFilled.setProtectedNameCache(false);
284
			parseFullName(nameToBeFilled, fullNameString, rank, false);
285
			String sure = nameToBeFilled.getNameCache();
286
			start = sure.length();
287
		}
288

    
289
//		String localSimpleName = getLocalSimpleName(nameToBeFilled);
290
//		Matcher simpleNameMatcher = getMatcher (start + localSimpleName, fullReferenceString);
291
//		if (simpleNameMatcher.find()){
292
//			String simpleNameString = simpleNameMatcher.group(0);
293
//			parseFullName(nameToBeFilled, simpleNameString, rank, false);
294
//			start = simpleNameString.length();
295
//		}
296

    
297
		//don't parse if name can't be separated
298
		nameToBeFilled.addParsingProblem(ParserProblem.NameReferenceSeparation);
299
		nameToBeFilled.setTitleCache(fullReferenceString, true);
300
		nameToBeFilled.setFullTitleCache(fullReferenceString, true);
301
		// FIXME Quick fix, otherwise search would not deliver results for unparsable names
302
		nameToBeFilled.setNameCache(fullReferenceString, true);
303
		// END
304
		nameToBeFilled.setProblemStarts(start);
305
		nameToBeFilled.setProblemEnds(fullReferenceString.length());
306
		logger.info("no applicable parsing rule could be found for \"" + fullReferenceString + "\"");
307
	}
308

    
309
	private void makeNameWithReference(NonViralName<?> nameToBeFilled,
310
			String fullReferenceString,
311
			Matcher nameAndRefSeparatorMatcher,
312
			Rank rank,
313
			boolean makeEmpty){
314

    
315
		String nameAndSeparator = nameAndRefSeparatorMatcher.group(0);
316
	    String name = nameAndRefSeparatorMatcher.group(1);
317
	    String referenceString = fullReferenceString.substring(nameAndRefSeparatorMatcher.end());
318

    
319
	    // is reference an in ref?
320
	    String separator = nameAndSeparator.substring(name.length());
321
		boolean isInReference = separator.matches(inReferenceSeparator);
322

    
323
	    //parse subparts
324

    
325
		int oldProblemEnds = nameToBeFilled.getProblemEnds();
326
		parseFullName(nameToBeFilled, name, rank, makeEmpty);
327
	    nameToBeFilled.setProblemEnds(oldProblemEnds);
328

    
329
		//zoological new combinations should not have a nom. reference to be parsed
330
	    if (nameToBeFilled.isInstanceOf(ZoologicalName.class)){
331
			ZoologicalName zooName = CdmBase.deproxy(nameToBeFilled, ZoologicalName.class);
332
			//is name new combination?
333
			if (zooName.getBasionymAuthorship() != null || zooName.getOriginalPublicationYear() != null){
334
				ParserProblem parserProblem = ParserProblem.NewCombinationHasPublication;
335
				zooName.addParsingProblem(parserProblem);
336
				nameToBeFilled.setProblemStarts((nameToBeFilled.getProblemStarts()> -1) ? nameToBeFilled.getProblemStarts(): name.length());
337
				nameToBeFilled.setProblemEnds(Math.max(fullReferenceString.length(), nameToBeFilled.getProblemEnds()));
338
			}
339
		}
340

    
341
	    parseReference(nameToBeFilled, referenceString, isInReference);
342
	    INomenclaturalReference ref = nameToBeFilled.getNomenclaturalReference();
343

    
344
	    //problem start
345
	    int start = nameToBeFilled.getProblemStarts();
346
	    int nameLength = name.length();
347
	    int nameAndSeparatorLength = nameAndSeparator.length();
348
	    int fullRefLength = nameToBeFilled.getFullTitleCache().length();
349

    
350
	    if (nameToBeFilled.isProtectedTitleCache() || nameToBeFilled.getParsingProblems().contains(ParserProblem.CheckRank)){
351
	    	start = Math.max(0, start);
352
		}else{
353
			if (ref != null && ref.getParsingProblem()!=0){
354
				start = Math.max(nameAndSeparatorLength, start);
355
		    	//TODO search within ref
356
			}
357
		}
358

    
359
	    //end
360
	    int end = nameToBeFilled.getProblemEnds();
361

    
362
	    if (ref != null && ref.getParsingProblem()!=0){
363
	    	end = Math.min(nameAndSeparatorLength + ref.getProblemEnds(), end);
364
	    }else{
365
	    	if (nameToBeFilled.isProtectedTitleCache() ){
366
	    		end = Math.min(end, nameAndSeparatorLength);
367
	    		//TODO search within name
368
			}
369
	    }
370
	    nameToBeFilled.setProblemStarts(start);
371
	    nameToBeFilled.setProblemEnds(end);
372

    
373
	    //delegate has problem to name
374
	    if (ref != null && ref.getParsingProblem()!=0){
375
	    	nameToBeFilled.addParsingProblems(ref.getParsingProblem());
376
	    }
377

    
378
	    Reference nomRef;
379
		if ( (nomRef = (Reference)nameToBeFilled.getNomenclaturalReference()) != null ){
380
			nomRef.setAuthorship(nameToBeFilled.getCombinationAuthorship());
381
		}
382
	}
383

    
384
	//TODO make it an Array of status
385
	/**
386
	 * Extracts a {@link NomenclaturalStatus} from the reference String and adds it to the @link {@link TaxonNameBase}.
387
	 * The nomenclatural status part ist deleted from the reference String.
388
	 * @return  String the new (shortend) reference String
389
	 */
390
	public String parseNomStatus(String fullString, NonViralName<?> nameToBeFilled, boolean makeEmpty) {
391
		Set<NomenclaturalStatusType> existingStatusTypeSet = new HashSet<NomenclaturalStatusType>();
392
		Set<NomenclaturalStatusType> newStatusTypeSet = new HashSet<NomenclaturalStatusType>();
393
		for (NomenclaturalStatus existingStatus : nameToBeFilled.getStatus()){
394
			existingStatusTypeSet.add(existingStatus.getType());
395
		}
396

    
397
		String statusString;
398
		Pattern hasStatusPattern = Pattern.compile("(" + pNomStatusPhrase + ")");
399
		Matcher hasStatusMatcher = hasStatusPattern.matcher(fullString);
400

    
401
		if (hasStatusMatcher.find()) {
402
			String statusPhrase = hasStatusMatcher.group(0);
403

    
404
			Pattern statusPattern = Pattern.compile(pNomStatus);
405
			Matcher statusMatcher = statusPattern.matcher(statusPhrase);
406
			statusMatcher.find();
407
			statusString = statusMatcher.group(0);
408
			try {
409
				NomenclaturalStatusType nomStatusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusString, nameToBeFilled);
410
				if (! existingStatusTypeSet.contains(nomStatusType)){
411
					NomenclaturalStatus nomStatus = NomenclaturalStatus.NewInstance(nomStatusType);
412
					nameToBeFilled.addStatus(nomStatus);
413
				}
414
				newStatusTypeSet.add(nomStatusType);
415
				fullString = fullString.replace(statusPhrase, "");
416
			} catch (UnknownCdmTypeException e) {
417
				//Do nothing
418
			}
419
		}
420
		//remove not existing nom status
421
		if (makeEmpty){
422
			Set<NomenclaturalStatus> tmpStatus = new HashSet<NomenclaturalStatus>();
423
			tmpStatus.addAll(nameToBeFilled.getStatus());
424
			for (NomenclaturalStatus status : tmpStatus){
425
				if (! newStatusTypeSet.contains(status.getType())){
426
					nameToBeFilled.removeStatus(status);
427
				}
428
			}
429
		}
430

    
431
		return fullString;
432
	}
433

    
434

    
435
	private void parseReference(NonViralName<?> nameToBeFilled, String strReference, boolean isInReference){
436

    
437
		INomenclaturalReference ref;
438
		String originalStrReference = strReference;
439

    
440
		//End (just delete end (e.g. '.', may be ambigous for yearPhrase, but no real information gets lost
441
		Matcher endMatcher = getMatcher(referenceEnd + end, strReference);
442
		if (endMatcher.find()){
443
			String endPart = endMatcher.group(0);
444
			strReference = strReference.substring(0, strReference.length() - endPart.length());
445
		}
446

    
447
//		String pDetailYear = ".*" + detailSeparator + detail + fWs + yearSeperator + fWs + yearPhrase + fWs + end;
448
//		Matcher detailYearMatcher = getMatcher(pDetailYear, strReference);
449

    
450
		String strReferenceWithYear = strReference;
451
		//year
452
		String yearPart = null;
453
		String pYearPhrase = yearSeperator + fWs + yearPhrase + fWs + end;
454
		Matcher yearPhraseMatcher = getMatcher(pYearPhrase, strReference);
455
		if (yearPhraseMatcher.find()){
456
			yearPart = yearPhraseMatcher.group(0);
457
			strReference = strReference.substring(0, strReference.length() - yearPart.length());
458
			yearPart = yearPart.replaceFirst(pStart + yearSeperator, "").trim();
459
		}else{
460
			if (nameToBeFilled.isInstanceOf(ZoologicalName.class)){
461
				ZoologicalName zooName = CdmBase.deproxy(nameToBeFilled, ZoologicalName.class);
462
				yearPart = String.valueOf(zooName.getPublicationYear());
463
				//continue
464
			}else{
465
				ref = makeDetailYearUnparsable(nameToBeFilled,strReference);
466
				ref.setDatePublished(TimePeriodParser.parseString(yearPart));
467
				return;
468
			}
469
		}
470

    
471

    
472
		//detail
473
		String pDetailPhrase = detailSeparator + fWs + detail + fWs + end;
474
		Matcher detailPhraseMatcher = getMatcher(pDetailPhrase, strReference);
475
		if (detailPhraseMatcher.find()){
476
			String detailPart = detailPhraseMatcher.group(0);
477
			strReference = strReference.substring(0, strReference.length() - detailPart.length());
478
			detailPart = detailPart.replaceFirst(pStart + detailSeparator, "").trim();
479
			nameToBeFilled.setNomenclaturalMicroReference(detailPart);
480
		}else{
481
			makeDetailYearUnparsable(nameToBeFilled, strReferenceWithYear);
482
			return;
483
		}
484
		//parse title and author
485
		ref = parseReferenceTitle(strReference, yearPart, isInReference);
486
		if (ref.hasProblem()){
487
		    //we need to protect both caches otherwise the titleCache is incorrectly build from atomized parts
488
			ref.setTitleCache( (isInReference ? "in ":"") +  originalStrReference, true);
489
			ref.setAbbrevTitleCache( (isInReference ? "in ":"") +  originalStrReference, true);
490
		}
491
		nameToBeFilled.setNomenclaturalReference(ref);
492
		int end = Math.min(strReference.length(), ref.getProblemEnds());
493
		ref.setProblemEnds(end);
494
	}
495

    
496
	/**
497
	 * @param nameToBeFilled
498
	 * @param strReference
499
	 * @return
500
	 */
501
	private Reference makeDetailYearUnparsable(NonViralName<?> nameToBeFilled, String strReference) {
502
		Reference ref;
503

    
504
		ref = ReferenceFactory.newGeneric();
505
		ref.setTitleCache(strReference, true);
506
        ref.setAbbrevTitleCache(strReference, true);
507
		ref.setProblemEnds(strReference.length());
508
		ref.addParsingProblem(ParserProblem.CheckDetailOrYear);
509
		nameToBeFilled.addParsingProblem(ParserProblem.CheckDetailOrYear);
510
		nameToBeFilled.setNomenclaturalReference(ref);
511
		return ref;
512
	}
513

    
514
	/**
515
	 * Parses the referenceTitlePart, including the author volume and edition.
516
	 * @param reference
517
	 * @param year
518
	 * @return
519
	 */
520
	private INomenclaturalReference parseReferenceTitle(String strReference, String year, boolean isInReference){
521
		IBook result = null;
522

    
523
		Matcher refSineDetailMatcher = referenceSineDetailPattern.matcher(strReference);
524
		if (! refSineDetailMatcher.matches()){
525
			//TODO ?
526
		}
527

    
528
		Matcher articleMatcher = getMatcher(pArticleReference, strReference);
529
		Matcher bookMatcher = getMatcher(pBookReference, strReference);
530

    
531
		Matcher softArticleMatcher = getMatcher(pSoftArticleReference, strReference);
532
		Matcher bookSectionMatcher = getMatcher(pBookSectionReference, strReference);
533

    
534

    
535
		if(isInReference == false){
536
			if (bookMatcher.matches() ){
537
				result = parseBook(strReference);
538
			}else{
539
				logger.warn("Non-InRef must be book but does not match book: "+ strReference);
540
				result = ReferenceFactory.newBook();
541
				makeUnparsableRefTitle(result, strReference);
542
			}
543
		}else{  //inRef
544
			if (articleMatcher.matches()){
545
				//article without separators like ","
546
				result = parseArticle(strReference);
547
			}else if (softArticleMatcher.matches()){
548
				result = parseArticle(strReference);
549
			}else if (bookSectionMatcher.matches()){
550
				result = parseBookSection(strReference);
551
			}else{
552
				result =  ReferenceFactory.newGeneric();
553
				makeUnparsableRefTitle(result, "in " + strReference);
554
			}
555
		}
556
		//make year
557
		if (makeYear(result, year) == false){
558
			//TODO
559
			logger.warn("Year could not be parsed");
560
		}
561
		result.setProblemStarts(0);
562
		result.setProblemEnds(strReference.length());
563
		return result;
564
	}
565

    
566
	private void makeUnparsableRefTitle(INomenclaturalReference result, String reference){
567
	    //need to set both to protected otherwise titleCache is created from atomized parts
568
	    result.setTitleCache(reference, true);
569
		result.setAbbrevTitleCache(reference, true);
570
		result.addParsingProblem(ParserProblem.UnparsableReferenceTitle);
571
	}
572

    
573
	/**
574
	 * Parses a single date string. If the string is not parsable a StringNotParsableException is thrown
575
	 * @param singleDateString
576
	 * @return
577
	 * @throws StringNotParsableException
578
	 */
579
	private static Partial parseSingleDate(String singleDateString)
580
			throws StringNotParsableException{
581
		Partial dt = new Partial();
582
		if (CdmUtils.isNumeric(singleDateString)){
583
			try {
584
				Integer year = Integer.valueOf(singleDateString.trim());
585
				if (year > 1750 && year < 2050){
586
					dt = dt.with(DateTimeFieldType.year(), year);
587
				}else{
588
					dt = null;
589
				}
590
			} catch (NumberFormatException e) {
591
				logger.debug("Not a Integer format in getCalendar()");
592
				throw new StringNotParsableException(singleDateString + "is not parsable as a single Date");
593
			}
594
		}
595
		return dt;
596
	}
597

    
598

    
599
	/**
600
	 * Parses the publication date part.
601
	 * @param nomRef
602
	 * @param year
603
	 * @return If the string is not parsable <code>false</code>
604
	 * is returned. <code>True</code> otherwise
605
	 */
606
	private boolean makeYear(INomenclaturalReference nomRef, String year){
607
		boolean result = true;
608
		if (year == null){
609
			return false;
610
		}
611
		if ("".equals(year.trim())){
612
			return true;
613
		}
614
		TimePeriod datePublished = TimePeriodParser.parseString(year);
615

    
616
		if (nomRef.getType().equals(ReferenceType.BookSection)){
617
			handleBookSectionYear((IBookSection)nomRef, datePublished);
618
		}else if (nomRef instanceof Reference){
619
			((Reference)nomRef).setDatePublished(datePublished);
620
		}else{
621
			throw new ClassCastException("nom Ref is not of type Reference but " + (nomRef == null? "(null)" : nomRef.getClass()));
622
		}
623
		return result;
624
	}
625

    
626
	private String makeVolume(IVolumeReference nomRef, String strReference){
627
		//volume
628
		String volPart = null;
629
		String pVolPhrase = volumeSeparator +  volume + end;
630
		Matcher volPhraseMatcher = getMatcher(pVolPhrase, strReference);
631
		if (volPhraseMatcher.find()){
632
			volPart = volPhraseMatcher.group(0);
633
			strReference = strReference.substring(0, strReference.length() - volPart.length());
634
			volPart = volPart.replaceFirst(pStart + volumeSeparator, "").trim();
635
			nomRef.setVolume(volPart);
636
		}
637
		return strReference;
638
	}
639

    
640
	private String makeEdition(IBook book, String strReference){
641
		//volume
642
		String editionPart = null;
643
		Matcher editionPhraseMatcher = getMatcher(pEditionPart, strReference);
644

    
645
		Matcher editionVolumeMatcher = getMatcher(pEditionVolPart, strReference);
646
		boolean isEditionAndVol = editionVolumeMatcher.find();
647

    
648
		if (editionPhraseMatcher.find()){
649
			editionPart = editionPhraseMatcher.group(0);
650
			int pos = strReference.indexOf(editionPart);
651
			int posEnd = pos + editionPart.length();
652
			if (isEditionAndVol){
653
				posEnd++;  //delete also comma
654
			}
655
			strReference = strReference.substring(0, pos) + strReference.substring(posEnd);
656
			editionPart = editionPart.replaceFirst(pStart + editionSeparator, "").trim();
657
			book.setEdition(editionPart);
658
		}
659
		return strReference;
660
	}
661

    
662
	private IBook parseBook(String reference){
663
		IBook result = ReferenceFactory.newBook();
664
		reference = makeEdition(result, reference);
665
		reference = makeVolume(result, reference);
666
		result.setAbbrevTitle(reference);
667
		return result;
668
	}
669

    
670

    
671
	private Reference parseArticle(String reference){
672
		//if (articlePatter)
673
		//(type, author, title, volume, editor, series;
674
		Reference result = ReferenceFactory.newArticle();
675
		reference = makeVolume(result, reference);
676
		Reference inJournal = ReferenceFactory.newJournal();
677
		inJournal.setAbbrevTitle(reference);
678
		result.setInReference(inJournal);
679
		return result;
680
	}
681

    
682
	private Reference parseBookSection(String reference){
683
		Reference result = ReferenceFactory.newBookSection();
684

    
685
		Pattern authorPattern = Pattern.compile("^" + authorTeam + referenceAuthorSeparator);
686
		Matcher authorMatcher = authorPattern.matcher(reference);
687
		boolean find = authorMatcher.find();
688
		if (find){
689
			String authorString = authorMatcher.group(0).trim();
690
			String bookString = reference.substring(authorString.length()).trim();
691
			authorString = authorString.substring(0, authorString.length() -1);
692

    
693
			TeamOrPersonBase<?> authorTeam = author(authorString);
694
			IBook inBook = parseBook(bookString);
695
			inBook.setAuthorship(authorTeam);
696
			result.setInBook(inBook);
697
		}else{
698
			logger.warn("Unexpected non matching book section author part");
699
			//TODO do we want to record a 'problem' here?
700
			result.setTitleCache(reference, true);
701
			result.setAbbrevTitleCache(reference, true);
702
		}
703

    
704
		return result;
705
	}
706

    
707
	/**
708
	 * If the publication date of a book section and it's inBook do differ this is usually
709
	 * caused by the fact that a book has been published during a period, because originally
710
	 * it consisted of several parts that only later where put together to one book.
711
	 * If so, the book section's publication date may be a point in time (year or month of year)
712
	 * whereas the books publication date may be a period of several years.
713
	 * Therefore a valid nomenclatural reference string should use the book sections
714
	 * publication date rather then the book's publication date.<BR>
715
	 * This method in general adds the publication date to the book section.
716
	 * An exception exists if the publication date is a period. Then the parser
717
	 * assumes that the nomenclatural reference string does not follow the above rule but
718
	 * the books publication date is set.
719
	 * @param bookSection
720
	 * @param datePublished
721
	 */
722
	private void handleBookSectionYear(IBookSection bookSection, TimePeriod datePublished){
723
		if (datePublished == null || datePublished.getStart() == null || bookSection == null){
724
			return;
725
		}
726
		if (datePublished.isPeriod() && bookSection.getInBook() != null){
727
			bookSection.getInBook().setDatePublished(datePublished);
728
		}else{
729
			bookSection.setDatePublished(datePublished);
730
		}
731
	}
732

    
733
	@Override
734
    public NonViralName parseFullName(String fullNameString){
735
		return parseFullName(fullNameString, null, null);
736
	}
737

    
738
	@Override
739
    public NonViralName parseFullName(String fullNameString, NomenclaturalCode nomCode, Rank rank) {
740

    
741
		if (fullNameString == null){
742
			return null;
743
		}else{
744
			NonViralName<?> result = getNonViralNameInstance(fullNameString, nomCode, rank);
745
			parseFullName(result, fullNameString, rank, false);
746
			return result;
747
		}
748
	}
749

    
750
	@Override
751
	public void parseFullName(NonViralName nameToBeFilledOrig, String fullNameStringOrig, Rank rank, boolean makeEmpty) {
752
	    NonViralName<?> nameToBeFilled = nameToBeFilledOrig;
753

    
754
	    //TODO prol. etc.
755
		boolean hasCheckRankProblem = false; //was rank guessed in a previous parsing process?
756
		if (nameToBeFilled == null){
757
			throw new IllegalArgumentException("NameToBeFilled must not be null in name parser");
758
		}else{
759
			hasCheckRankProblem = nameToBeFilled.hasProblem(ParserProblem.CheckRank);
760
			nameToBeFilled.removeParsingProblem(ParserProblem.CheckRank);
761
		}
762
		String authorString = null;
763
		if (fullNameStringOrig == null){
764
			return;
765
		}
766
		if (makeEmpty){
767
			makeEmpty(nameToBeFilled);
768
		}
769

    
770
		String fullNameString = fullNameStringOrig.replaceAll(oWs , " ").trim();
771

    
772
		fullNameString = removeHybridBlanks(fullNameString);
773
		String[] epi = pattern.split(fullNameString);
774
		try {
775
	    	//cultivars //TODO 2 implement cultivars
776
//		    if ( cultivarMarkerRE.match(fullName) ){ funktioniert noch nicht, da es z.B. auch Namen gibt, wie 't Hart
777
//		    	result = parseCultivar(fullName);
778
//		    }
779

    
780
		    if (genusOrSupraGenusPattern.matcher(fullNameString).matches()){
781
		    	//supraGeneric
782
				if (rank != null && ! hasCheckRankProblem  && (rank.isSupraGeneric()|| rank.isGenus())){
783
					nameToBeFilled.setRank(rank);
784
					nameToBeFilled.setGenusOrUninomial(epi[0]);
785
				}
786
				//genus or guess rank
787
				else {
788
					rank = guessUninomialRank(nameToBeFilled, epi[0]);
789
					nameToBeFilled.setRank(rank);
790
					nameToBeFilled.setGenusOrUninomial(epi[0]);
791
					nameToBeFilled.addParsingProblem(ParserProblem.CheckRank);
792
					nameToBeFilled.setProblemStarts(0);
793
					nameToBeFilled.setProblemEnds(epi[0].length());
794
				}
795
				authorString = fullNameString.substring(epi[0].length());
796
			}
797
			 //infra genus
798
			 else if (infraGenusPattern.matcher(fullNameString).matches()){
799
				Rank infraGenericRank;
800
				if ("[unranked]".equals(epi[1])){
801
					infraGenericRank = Rank.INFRAGENERICTAXON();
802
				}else{
803
				    String infraGenericRankMarker = epi[1];
804
				    if (infraGenericRankMarker.startsWith(notho)){  //#3868
805
                        nameToBeFilled.setBinomHybrid(true);
806
                        infraGenericRankMarker = infraGenericRankMarker.substring(notho.length());
807
                    }else if(infraGenericRankMarker.startsWith("n")){
808
                        nameToBeFilled.setBinomHybrid(true);
809
                        infraGenericRankMarker = infraGenericRankMarker.substring(1);
810
                    }
811
                    infraGenericRank = Rank.getRankByIdInVoc(infraGenericRankMarker, nameToBeFilledOrig.getNomenclaturalCode());
812
				}
813
				nameToBeFilled.setRank(infraGenericRank);
814
				nameToBeFilled.setGenusOrUninomial(epi[0]);
815
				nameToBeFilled.setInfraGenericEpithet(epi[2]);
816
				authorString = fullNameString.substring(epi[0].length() + 1 + epi[1].length()+ 1 + epi[2].length());
817
			}
818
			 //aggr. or group
819
			 else if (aggrOrGroupPattern.matcher(fullNameString).matches()){
820
				nameToBeFilled.setRank(Rank.getRankByIdInVoc(epi[2]));
821
				nameToBeFilled.setGenusOrUninomial(epi[0]);
822
				nameToBeFilled.setSpecificEpithet(epi[1]);
823
			}
824
		     //species
825
			 else if (speciesPattern.matcher(fullNameString).matches()){
826
				nameToBeFilled.setRank(Rank.SPECIES());
827
				nameToBeFilled.setGenusOrUninomial(epi[0]);
828
				nameToBeFilled.setSpecificEpithet(epi[1]);
829
				authorString = fullNameString.substring(epi[0].length() + 1 + epi[1].length());
830
			}
831
		    //species with infra generic epithet
832
			 else if (speciesWithInfraGenPattern.matcher(fullNameString).matches()){
833
			     nameToBeFilled.setRank(Rank.SPECIES());
834
	             nameToBeFilled.setGenusOrUninomial(epi[0]);
835
                 nameToBeFilled.setInfraGenericEpithet(epi[2]);
836
	             nameToBeFilled.setSpecificEpithet(epi[4]);
837
	             authorString = fullNameString.substring(epi[0].length() + 2 + epi[2].length() + 2 + epi[4].length());
838
			 }
839
			 //autonym
840
			 else if (autonymPattern.matcher(fullNameString).matches()){
841
				nameToBeFilled.setRank(Rank.getRankByIdInVoc(epi[epi.length - 2]));
842
				nameToBeFilled.setGenusOrUninomial(epi[0]);
843
				nameToBeFilled.setSpecificEpithet(epi[1]);
844
				nameToBeFilled.setInfraSpecificEpithet(epi[epi.length - 1]);
845
				int lenSpecies = 2 + epi[0].length()+epi[1].length();
846
				int lenInfraSpecies =  2 + epi[epi.length - 2].length() + epi[epi.length - 1].length();
847
				authorString = fullNameString.substring(lenSpecies, fullNameString.length() - lenInfraSpecies);
848
			}
849
			 //infraSpecies
850
			 else if (infraSpeciesPattern.matcher(fullNameString).matches()){
851
				String infraSpecRankMarker = epi[2];
852
				String infraSpecEpi = epi[3];
853
				if ("tax.".equals(infraSpecRankMarker)){
854
					infraSpecRankMarker += " " +  epi[3];
855
					infraSpecEpi = epi[4];
856
				}
857
				Rank infraSpecificRank;
858
				if ("[unranked]".equals(infraSpecRankMarker)){
859
					infraSpecificRank = Rank.INFRASPECIFICTAXON();
860
				}else{
861
					String localInfraSpecRankMarker;
862
					if (infraSpecRankMarker.startsWith(notho)){  //#3868
863
	                    nameToBeFilled.setTrinomHybrid(true);
864
	                    localInfraSpecRankMarker = infraSpecRankMarker.substring(notho.length());
865
					}else if(infraSpecRankMarker.startsWith("n")){
866
	                    nameToBeFilled.setTrinomHybrid(true);
867
	                    localInfraSpecRankMarker = infraSpecRankMarker.substring(1);
868
                    }else{
869
                        localInfraSpecRankMarker = infraSpecRankMarker;
870
                    }
871
				    infraSpecificRank = Rank.getRankByIdInVoc(localInfraSpecRankMarker);
872
				}
873
				nameToBeFilled.setRank(infraSpecificRank);
874
				nameToBeFilled.setGenusOrUninomial(epi[0]);
875
				nameToBeFilled.setSpecificEpithet(epi[1]);
876
				nameToBeFilled.setInfraSpecificEpithet(infraSpecEpi);
877
				authorString = fullNameString.substring(epi[0].length()+ 1 + epi[1].length() +1 + infraSpecRankMarker.length() + 1 + infraSpecEpi.length());
878

    
879
			 }
880
		      //infraSpecies without marker
881
			 else if (zooInfraSpeciesPattern.matcher(fullNameString).matches()){
882
					String infraSpecEpi = epi[2];
883
					Rank infraSpecificRank = Rank.SUBSPECIES();
884
					nameToBeFilled.setRank(infraSpecificRank);
885
					nameToBeFilled.setGenusOrUninomial(epi[0]);
886
					nameToBeFilled.setSpecificEpithet(epi[1]);
887
					nameToBeFilled.setInfraSpecificEpithet(infraSpecEpi);
888
					authorString = fullNameString.substring(epi[0].length()+ 1 + epi[1].length() +1 + infraSpecEpi.length());
889

    
890
			 }//old infraSpecies
891
			 else if (oldInfraSpeciesPattern.matcher(fullNameString).matches()){
892
				boolean implemented = false;
893
				if (implemented){
894
					nameToBeFilled.setRank(Rank.getRankByNameOrIdInVoc(epi[2]));
895
					nameToBeFilled.setGenusOrUninomial(epi[0]);
896
					nameToBeFilled.setSpecificEpithet(epi[1]);
897
					//TODO result.setUnnamedNamePhrase(epi[2] + " " + epi[3]);
898
					authorString = fullNameString.substring(epi[0].length()+ 1 + epi[1].length() +1 + epi[2].length() + 1 + epi[3].length());
899
				}else{
900
					nameToBeFilled.addParsingProblem(ParserProblem.OldInfraSpeciesNotSupported);
901
					nameToBeFilled.setTitleCache(fullNameString, true);
902
					// FIXME Quick fix, otherwise search would not deilver results for unparsable names
903
					nameToBeFilled.setNameCache(fullNameString,true);
904
					// END
905
					logger.info("Name string " + fullNameString + " could not be parsed because UnnnamedNamePhrase is not yet implemented!");
906
				}
907
			}
908
		     //hybrid formula
909
			 else if (hybridFormulaPattern.matcher(fullNameString).matches()){
910
				 Set<HybridRelationship> existingRelations = new HashSet<HybridRelationship>();
911
				 Set<HybridRelationship> notToBeDeleted = new HashSet<HybridRelationship>();
912

    
913
				 for ( HybridRelationship rel : nameToBeFilled.getHybridChildRelations()){
914
				     existingRelations.add(rel);
915
				 }
916

    
917
			     String firstNameString = "";
918
				 String secondNameString = "";
919
				 boolean isFirstName = true;
920
				 for (String str : epi){
921
					 if (str.matches(hybridSign)){
922
						 isFirstName = false;
923
					 }else if(isFirstName){
924
						 firstNameString += " " + str;
925
					 }else {
926
						 secondNameString += " " + str;
927
					 }
928
				 }
929
				 nameToBeFilled.setHybridFormula(true);
930
				 NomenclaturalCode code = nameToBeFilled.getNomenclaturalCode();
931
				 NonViralName<?> firstName = this.parseFullName(firstNameString.trim(), code, rank);
932
				 NonViralName<?> secondName = this.parseFullName(secondNameString.trim(), code, rank);
933
				 HybridRelationship firstRel = nameToBeFilled.addHybridParent(firstName, HybridRelationshipType.FIRST_PARENT(), null);
934
				 HybridRelationship second = nameToBeFilled.addHybridParent(secondName, HybridRelationshipType.SECOND_PARENT(), null);
935
				 checkRelationExist(firstRel, existingRelations, notToBeDeleted);
936
				 checkRelationExist(second, existingRelations, notToBeDeleted);
937

    
938
				 Rank newRank;
939
				 Rank firstRank = firstName.getRank();
940
				 Rank secondRank = secondName.getRank();
941

    
942
				 if (firstRank == null || firstRank.isHigher(secondRank)){
943
					 newRank = secondRank;
944
				 }else{
945
					 newRank = firstRank;
946
				 }
947
				 nameToBeFilled.setRank(newRank);
948
				 //remove not existing hybrid relation
949
				 if (makeEmpty){
950
		            Set<HybridRelationship> tmpChildRels = new HashSet<HybridRelationship>();
951
		            tmpChildRels.addAll(nameToBeFilled.getHybridChildRelations());
952
		            for (HybridRelationship rel : tmpChildRels){
953
		                if (! notToBeDeleted.contains(rel)){
954
		                    nameToBeFilled.removeHybridRelationship(rel);
955
		                }
956
		            }
957
				 }
958
			 }
959
		    //none
960
			else{
961
				nameToBeFilled.addParsingProblem(ParserProblem.UnparsableNamePart);
962
				nameToBeFilled.setTitleCache(fullNameString, true);
963
				// FIXME Quick fix, otherwise search would not deilver results for unparsable names
964
				nameToBeFilled.setNameCache(fullNameString, true);
965
				// END
966
				logger.info("no applicable parsing rule could be found for \"" + fullNameString + "\"");
967
		    }
968
		    //hybrid bits
969
		    handleHybridBits(nameToBeFilled);
970
		    if (!nameToBeFilled.isHybridFormula()){
971
		        Set<HybridRelationship> hybridChildRelations = new HashSet<HybridRelationship>();
972
		        hybridChildRelations.addAll(nameToBeFilled.getHybridChildRelations());
973

    
974
		        for (HybridRelationship hybridRelationship: hybridChildRelations){
975
		        	nameToBeFilled.removeHybridRelationship(hybridRelationship);
976
		        }
977
		    }
978

    
979
			//authors
980
		    if (StringUtils.isNotBlank(authorString) ){
981
				handleAuthors(nameToBeFilled, fullNameString, authorString);
982
			}
983
		    return;
984
		} catch (UnknownCdmTypeException e) {
985
			nameToBeFilled.addParsingProblem(ParserProblem.RankNotSupported);
986
			nameToBeFilled.setTitleCache(fullNameString, true);
987
			// FIXME Quick fix, otherwise search would not deilver results for unparsable names
988
			nameToBeFilled.setNameCache(fullNameString,true);
989
			// END
990
			logger.info("unknown rank (" + (rank == null? "null":rank) + ") or abbreviation in string " +  fullNameString);
991
			//return result;
992
			return;
993
		}
994
	}
995

    
996
	/**
997
     * Checks if a hybrid relation exists in the Set of existing relations
998
     * and <BR>
999
     *  if it does not adds it to relations not to be deleted <BR>
1000
     *  if it does adds the existing relations to the relations not to be deleted
1001
     *
1002
     * @param firstRel
1003
     * @param existingRelations
1004
     * @param notToBeDeleted
1005
     */
1006
    private void checkRelationExist(
1007
            HybridRelationship newRelation,
1008
            Set<HybridRelationship> existingRelations,
1009
            Set<HybridRelationship> notToBeDeleted) {
1010
        HybridRelationship relToKeep = newRelation;
1011
        for (HybridRelationship existingRelation : existingRelations){
1012
            if (existingRelation.equals(newRelation)){
1013
                relToKeep = existingRelation;
1014
                break;
1015
            }
1016
        }
1017
        notToBeDeleted.add(relToKeep);
1018
    }
1019

    
1020
    private void handleHybridBits(NonViralName<?> nameToBeFilled) {
1021
		//uninomial
1022
		String uninomial = CdmUtils.Nz(nameToBeFilled.getGenusOrUninomial());
1023
		boolean isUninomialHybrid = uninomial.startsWith(hybridSign);
1024
		if (isUninomialHybrid){
1025
			nameToBeFilled.setMonomHybrid(true);
1026
			nameToBeFilled.setGenusOrUninomial(uninomial.replace(hybridSign, ""));
1027
		}
1028
		//infrageneric
1029
		String infrageneric = CdmUtils.Nz(nameToBeFilled.getInfraGenericEpithet());
1030
		boolean isInfraGenericHybrid = infrageneric.startsWith(hybridSign);
1031
		if (isInfraGenericHybrid){
1032
			nameToBeFilled.setBinomHybrid(true);
1033
			nameToBeFilled.setInfraGenericEpithet(infrageneric.replace(hybridSign, ""));
1034
		}
1035
		//species Epi
1036
		String speciesEpi = CdmUtils.Nz(nameToBeFilled.getSpecificEpithet());
1037
		boolean isSpeciesHybrid = speciesEpi.startsWith(hybridSign);
1038
		if (isSpeciesHybrid){
1039
			if (StringUtils.isBlank(infrageneric)){
1040
				nameToBeFilled.setBinomHybrid(true);
1041
			}else{
1042
				nameToBeFilled.setTrinomHybrid(true);
1043
			}
1044
			nameToBeFilled.setSpecificEpithet(speciesEpi.replace(hybridSign, ""));
1045
		}
1046
		//infra species
1047
		String infraSpeciesEpi = CdmUtils.Nz(nameToBeFilled.getInfraSpecificEpithet());
1048
		boolean isInfraSpeciesHybrid = infraSpeciesEpi.startsWith(hybridSign);
1049
		if (isInfraSpeciesHybrid){
1050
			nameToBeFilled.setTrinomHybrid(true);
1051
			nameToBeFilled.setInfraSpecificEpithet(infraSpeciesEpi.replace(hybridSign, ""));
1052
		}
1053

    
1054
	}
1055

    
1056
	private String removeHybridBlanks(String fullNameString) {
1057
		String result = fullNameString
1058
		        .replaceAll(oWs + "[xX]" + oWs + "(?=[A-Z])", " " + hybridSign + " ")
1059
		        .replaceAll(hybridFull, " "+hybridSign).trim();
1060
		return result;
1061
	}
1062

    
1063
	/**
1064
	 * Author parser for external use
1065
	 * @param nonViralName
1066
	 * @param authorString
1067
	 * @throws StringNotParsableException
1068
	 */
1069
	@Override
1070
	public void parseAuthors(NonViralName nonViralNameOrig, String authorString) throws StringNotParsableException{
1071
	    NonViralName<?> nonViralName = nonViralNameOrig;
1072
	    TeamOrPersonBase<?>[] authors = new TeamOrPersonBase[4];
1073
		Integer[] years = new Integer[4];
1074
		Class<? extends NonViralName> clazz = nonViralName.getClass();
1075
		fullAuthors(authorString, authors, years, clazz);
1076
		nonViralName.setCombinationAuthorship(authors[0]);
1077
		nonViralName.setExCombinationAuthorship(authors[1]);
1078
		nonViralName.setBasionymAuthorship(authors[2]);
1079
		nonViralName.setExBasionymAuthorship(authors[3]);
1080
		if (nonViralName instanceof ZoologicalName){
1081
			ZoologicalName zooName = CdmBase.deproxy(nonViralName, ZoologicalName.class);
1082
			zooName.setPublicationYear(years[0]);
1083
			zooName.setOriginalPublicationYear(years[2]);
1084
		}
1085
	}
1086

    
1087
	/**
1088
	 * @param nameToBeFilled
1089
	 * @param fullNameString
1090
	 * @param authorString
1091
	 */
1092
	public void handleAuthors(NonViralName nameToBeFilledOrig, String fullNameString, String authorString) {
1093
	    NonViralName<?> nameToBeFilled = nameToBeFilledOrig;
1094
        TeamOrPersonBase<?>[] authors = new TeamOrPersonBase[4];
1095
		Integer[] years = new Integer[4];
1096
		try {
1097
			Class<? extends NonViralName> clazz = nameToBeFilled.getClass();
1098
			fullAuthors(authorString, authors, years, clazz);
1099
		} catch (StringNotParsableException e) {
1100
			nameToBeFilled.addParsingProblem(ParserProblem.UnparsableAuthorPart);
1101
			nameToBeFilled.setTitleCache(fullNameString, true);
1102
			// FIXME Quick fix, otherwise search would not deliver results for unparsable names
1103
			nameToBeFilled.setNameCache(fullNameString, true);
1104
			// END
1105
			logger.info("no applicable parsing rule could be found for \"" + fullNameString + "\"");;
1106
		}
1107
		nameToBeFilled.setCombinationAuthorship(authors[0]);
1108
		nameToBeFilled.setExCombinationAuthorship(authors[1]);
1109
		nameToBeFilled.setBasionymAuthorship(authors[2]);
1110
		nameToBeFilled.setExBasionymAuthorship(authors[3]);
1111
		if (nameToBeFilled instanceof ZoologicalName){
1112
			ZoologicalName zooName = (ZoologicalName)nameToBeFilled;
1113
			zooName.setPublicationYear(years[0]);
1114
			zooName.setOriginalPublicationYear(years[2]);
1115
		}
1116
	}
1117

    
1118
	/**
1119
	 * Guesses the rank of uninomial depending on the typical endings for ranks
1120
	 * @param nameToBeFilled
1121
	 * @param string
1122
	 */
1123
	private Rank guessUninomialRank(NonViralName nameToBeFilled, String uninomial) {
1124
		Rank result = Rank.GENUS();
1125
		if (nameToBeFilled.isInstanceOf(BotanicalName.class)){
1126
			if (false){
1127
				//
1128
			}else if (uninomial.endsWith("phyta") || uninomial.endsWith("mycota") ){  //plants, fungi
1129
				result = Rank.SECTION_BOTANY();
1130
			}else if (uninomial.endsWith("bionta")){
1131
				result = Rank.SUBKINGDOM();  //TODO
1132
			}else if (uninomial.endsWith("phytina")|| uninomial.endsWith("mycotina")  ){  //plants, fungi
1133
				result = Rank.SUBSECTION_BOTANY();
1134
			}else if (uninomial.endsWith("opsida") || uninomial.endsWith("phyceae") || uninomial.endsWith("mycetes")){  //plants, algae, fungi
1135
				result = Rank.CLASS();
1136
			}else if (uninomial.endsWith("idae") || uninomial.endsWith("phycidae") || uninomial.endsWith("mycetidae")){ //plants, algae, fungi
1137
				result = Rank.SUBCLASS();
1138
			}else if (uninomial.endsWith("ales")){
1139
				result = Rank.ORDER();
1140
			}else if (uninomial.endsWith("ineae")){
1141
				result = Rank.SUBORDER();
1142
			}else if (uninomial.endsWith("aceae")){
1143
					result = Rank.FAMILY();
1144
			}else if (uninomial.endsWith("oideae")){
1145
				result = Rank.SUBFAMILY();
1146
			}else if (uninomial.endsWith("eae")){
1147
				result = Rank.TRIBE();
1148
			}else if (uninomial.endsWith("inae")){
1149
				result = Rank.SUBTRIBE();
1150
			}else if (uninomial.endsWith("ota")){
1151
				result = Rank.KINGDOM();  //TODO
1152
			}
1153
		}else if (nameToBeFilled.isInstanceOf(ZoologicalName.class)){
1154
			if (false){
1155
				//
1156
			}else if (uninomial.endsWith("oideae")){
1157
				result = Rank.SUPERFAMILY();
1158
			}else if (uninomial.endsWith("idae")){
1159
					result = Rank.FAMILY();
1160
			}else if (uninomial.endsWith("inae")){
1161
				result = Rank.SUBFAMILY();
1162
			}else if (uninomial.endsWith("inae")){
1163
				result = Rank.SUBFAMILY();
1164
			}else if (uninomial.endsWith("ini")){
1165
				result = Rank.TRIBE();
1166
			}else if (uninomial.endsWith("ina")){
1167
				result = Rank.SUBTRIBE();
1168
			}
1169
		}else{
1170
			//
1171
		}
1172
		return result;
1173
	}
1174

    
1175
	/**
1176
	 * Parses the fullAuthorString
1177
	 * @param fullAuthorString
1178
	 * @return array of Teams containing the Team[0],
1179
	 * ExTeam[1], BasionymTeam[2], ExBasionymTeam[3]
1180
	 */
1181
	protected void fullAuthors (String fullAuthorStringOrig, TeamOrPersonBase<?>[] authors, Integer[] years, Class<? extends NonViralName> clazz)
1182
			throws StringNotParsableException{
1183
		if (fullAuthorStringOrig == null || clazz == null){
1184
			return;
1185
		}
1186
		String fullAuthorString = fullAuthorStringOrig.trim();
1187

    
1188
		//Botanic
1189
		if ( BotanicalName.class.isAssignableFrom(clazz) ){
1190
			if (! fullBotanicAuthorStringPattern.matcher(fullAuthorString).matches() ){
1191
				throw new StringNotParsableException("fullAuthorString (" +fullAuthorString+") not parsable: ");
1192
			}
1193
		}
1194
		//Zoo
1195
		else if ( ZoologicalName.class.isAssignableFrom(clazz) ){
1196
			if (! fullZooAuthorStringPattern.matcher(fullAuthorString).matches() ){
1197
				throw new StringNotParsableException("fullAuthorString (" +fullAuthorString+") not parsable: ");
1198
			}
1199
		}else {
1200
			//TODO
1201
			logger.warn ("Full author String parsable only for defined BotanicalNames or ZoologicalNames but this is " + clazz.getSimpleName());
1202
			throw new StringNotParsableException("fullAuthorString (" +fullAuthorString+") not parsable: ");
1203
		}
1204
		fullAuthorsChecked(fullAuthorString, authors, years);
1205
	}
1206

    
1207
	/*
1208
	 * like fullTeams but without trim and match check
1209
	 */
1210
	protected void fullAuthorsChecked (String fullAuthorString, TeamOrPersonBase<?>[] authors, Integer[] years){
1211
		int authorShipStart = 0;
1212
		Matcher basionymMatcher = basionymPattern.matcher(fullAuthorString);
1213

    
1214
		if (basionymMatcher.find(0)){
1215

    
1216
			String basString = basionymMatcher.group();
1217
			basString = basString.replaceFirst(basStart, "");
1218
			basString = basString.replaceAll(basEnd, "").trim();
1219
			authorShipStart = basionymMatcher.end(1);
1220

    
1221
			TeamOrPersonBase<?>[] basAuthors = new TeamOrPersonBase[2];
1222
			Integer[] basYears = new Integer[2];
1223
			authorsAndEx(basString, basAuthors, basYears);
1224
			authors[2]= basAuthors[0];
1225
			years[2] = basYears[0];
1226
			authors[3]= basAuthors[1];
1227
			years[3] = basYears[1];
1228
		}
1229
		if (fullAuthorString.length() >= authorShipStart){
1230
			TeamOrPersonBase<?>[] combinationAuthors = new TeamOrPersonBase[2];
1231
			Integer[] combinationYears = new Integer[2];
1232
			authorsAndEx(fullAuthorString.substring(authorShipStart), combinationAuthors, combinationYears);
1233
			authors[0]= combinationAuthors[0] ;
1234
			years[0] = combinationYears[0];
1235
			authors[1]= combinationAuthors[1];
1236
			years[1] = combinationYears[1];
1237
		}
1238
	}
1239

    
1240

    
1241
	/**
1242
	 * Parses the author and ex-author String
1243
	 * @param authorShipStringOrig String representing the author and the ex-author team
1244
	 * @return array of Teams containing the Team[0] and the ExTeam[1]
1245
	 */
1246
	protected void authorsAndEx (String authorShipStringOrig, TeamOrPersonBase<?>[] authors, Integer[] years){
1247
		//TODO noch allgemeiner am anfang durch Replace etc.
1248
		String authorShipString = authorShipStringOrig.trim();
1249
		authorShipString = authorShipString.replaceFirst(oWs + "ex" + oWs, " ex. " );
1250

    
1251
		//int authorEnd = authorTeamString.length();
1252
		int authorBegin = 0;
1253

    
1254
		Matcher exAuthorMatcher = exAuthorPattern.matcher(authorShipString);
1255
		if (exAuthorMatcher.find(0)){
1256
			authorBegin = exAuthorMatcher.end(0);
1257
			int exAuthorEnd = exAuthorMatcher.start(0);
1258
			String exString = authorShipString.substring(0, exAuthorEnd).trim();
1259
			authors [1] = author(exString);
1260
		}
1261
		zooOrBotanicAuthor(authorShipString.substring(authorBegin), authors, years );
1262
	}
1263

    
1264
	/**
1265
	 * Parses the authorString and if it matches an botanical or zoological authorTeam it fills
1266
	 * the computes the AuthorTeam and fills it into the first field of the team array. Same applies
1267
	 * to the year in case of an zoological name.
1268
	 * @param authorString
1269
	 * @param team
1270
	 * @param year
1271
	 */
1272
	protected void zooOrBotanicAuthor(String authorString, TeamOrPersonBase<?>[] team, Integer[] year){
1273
		if (authorString == null){
1274
			return;
1275
		}else if ((authorString = authorString.trim()).length() == 0){
1276
			return;
1277
		}
1278
		Matcher zooAuthorAddidtionMatcher = zooAuthorAddidtionPattern.matcher(authorString);
1279
		if (zooAuthorAddidtionMatcher.find()){
1280
			int index = zooAuthorAddidtionMatcher.start(0);
1281
			String strYear = authorString.substring(index);
1282
			strYear = strYear.replaceAll(zooAuthorYearSeperator, "").trim();
1283
			year[0] = Integer.valueOf(strYear);
1284
			authorString = authorString.substring(0, index).trim();
1285
		}
1286
		team[0] = author(authorString);
1287
	}
1288

    
1289

    
1290
	/**
1291
	 * Parses an authorTeam String and returns the Team
1292
	 * !!! TODO (atomization not yet implemented)
1293
	 * @param authorTeamString String representing the author team
1294
	 * @return an Team
1295
	 */
1296
	protected TeamOrPersonBase<?> author (String authorString){
1297
		if (authorString == null){
1298
			return null;
1299
		}else if ((authorString = authorString.trim()).length() == 0){
1300
			return null;
1301
		}else if (! finalTeamSplitterPattern.matcher(authorString).find() && ! authorIsAlwaysTeam){
1302
			//1 Person
1303
			Person result = Person.NewInstance();
1304
			result.setNomenclaturalTitle(authorString);
1305
			return result;
1306
		}else{
1307
			return parsedTeam(authorString);
1308
		}
1309

    
1310
	}
1311

    
1312
	/**
1313
	 * Parses an authorString (reprsenting a team into the single authors and add
1314
	 * them to the return Team.
1315
	 * @param authorString
1316
	 * @return Team
1317
	 */
1318
	protected Team parsedTeam(String authorString){
1319
		Team result = Team.NewInstance();
1320
		String[] authors = authorString.split(notFinalTeamSplitter);
1321
		for (int i = 0; i < authors.length; i++){
1322
		    String author = authors[i];
1323
		    if ("al.".equals(author.trim()) && i == authors.length - 1){  //final al. is handled as hasMoreMembers
1324
			    result.setHasMoreMembers(true);
1325
			}else{
1326
			    Person person = Person.NewInstance();
1327
			    person.setNomenclaturalTitle(author);
1328
			    result.addTeamMember(person);
1329
			}
1330
		}
1331
		return result;
1332
	}
1333

    
1334

    
1335
//	// Parsing of the given full name that has been identified as a cultivar already somwhere else.
1336
//	// The ... cv. ... syntax is not covered here as it is not according the rules for naming cultivars.
1337
	public BotanicalName parseCultivar(String fullName)	throws StringNotParsableException{
1338
		CultivarPlantName result = null;
1339
		    String[] words = oWsPattern.split(fullName);
1340

    
1341
		    /* ---------------------------------------------------------------------------------
1342
		     * cultivar
1343
		     * ---------------------------------------------------------------------------------*/
1344
			if (fullName.indexOf(" '") != 0){
1345
				//TODO location of 'xx' is probably not arbitrary
1346
				Matcher cultivarMatcher = cultivarPattern.matcher(fullName);
1347
				if (cultivarMatcher.find()){
1348
					String namePart = fullName.replaceFirst(cultivar, "");
1349

    
1350
					String cultivarPart = cultivarMatcher.group(0).replace("'","").trim();
1351
					//OLD: String cultivarPart = cultivarRE.getParen(0).replace("'","").trim();
1352

    
1353
					result = (CultivarPlantName)parseFullName(namePart);
1354
					result.setCultivarName(cultivarPart);
1355
				}
1356
			}else if (fullName.indexOf(" cv.") != 0){
1357
				// cv. is old form (not official)
1358
				throw new StringNotParsableException("Cultivars with only cv. not yet implemented in name parser!");
1359
			}
1360

    
1361
		    /* ---------------------------------------------------------------------------------
1362
		     * cultivar group
1363
		     * ---------------------------------------------------------------------------------
1364
		     */
1365
			// TODO in work
1366
			//Ann. this is not the official way of noting cultivar groups
1367
		    String group = oWs + "Group" + oWs + capitalEpiWord + end;
1368
			Pattern groupRE = Pattern.compile(group);
1369
			Matcher groupMatcher = groupRE.matcher(fullName);
1370
			if (groupMatcher.find()){
1371
		    	if (! words[words.length - 2].equals("group")){
1372
		            throw new StringNotParsableException ("fct ParseHybrid --> term before cultivar group name in " + fullName + " should be 'group'");
1373
		        }else{
1374

    
1375
		        	String namePart = fullName.substring(0, groupMatcher.start(0) - 0);
1376
		        	//OLD: String namePart = fullName.substring(0, groupRE.getParenStart(0) - 0);
1377

    
1378
		        	String cultivarPart = words[words.length -1];
1379
		        	result = (CultivarPlantName)parseFullName(namePart);
1380
		        	if (result != null){
1381
		        		result.setCultivarName(cultivarPart);
1382

    
1383
		        		//OLD: result.setCultivarGroupName(cultivarPart);
1384
		        	}
1385
		        }
1386

    
1387
		    }
1388
//		    // ---------------------------------------------------------------------------------
1389
//		    if ( result = "" ){
1390
//		        return "I: fct ParseCultivar: --> could not parse cultivar " + fullName;
1391
//		    }else{
1392
//		        return result;
1393
	//	    }
1394
			return result; //TODO
1395
	}
1396

    
1397

    
1398
	private void makeEmpty(NonViralName<?> nameToBeFilled){
1399
		nameToBeFilled.setRank(null);
1400
		nameToBeFilled.setTitleCache(null, false);
1401
		nameToBeFilled.setFullTitleCache(null, false);
1402
		nameToBeFilled.setNameCache(null, false);
1403

    
1404
		nameToBeFilled.setAppendedPhrase(null);
1405
		nameToBeFilled.setBasionymAuthorship(null);
1406
		nameToBeFilled.setCombinationAuthorship(null);
1407
		nameToBeFilled.setExBasionymAuthorship(null);
1408
		nameToBeFilled.setExCombinationAuthorship(null);
1409
		nameToBeFilled.setAuthorshipCache(null, false);
1410

    
1411

    
1412
		//delete problems except check rank
1413
		makeProblemEmpty(nameToBeFilled);
1414

    
1415
		// TODO ?
1416
		//nameToBeFilled.setHomotypicalGroup(newHomotypicalGroup);
1417

    
1418

    
1419
		nameToBeFilled.setGenusOrUninomial(null);
1420
		nameToBeFilled.setInfraGenericEpithet(null);
1421
		nameToBeFilled.setSpecificEpithet(null);
1422
		nameToBeFilled.setInfraSpecificEpithet(null);
1423

    
1424
		nameToBeFilled.setNomenclaturalMicroReference(null);
1425
		nameToBeFilled.setNomenclaturalReference(null);
1426

    
1427
		nameToBeFilled.setHybridFormula(false);
1428
		nameToBeFilled.setMonomHybrid(false);
1429
		nameToBeFilled.setBinomHybrid(false);
1430
		nameToBeFilled.setTrinomHybrid(false);
1431

    
1432
		if (nameToBeFilled.isInstanceOf(BotanicalName.class)){
1433
			BotanicalName botanicalName = (BotanicalName)nameToBeFilled;
1434
			botanicalName.setAnamorphic(false);
1435
		}
1436

    
1437
		if (nameToBeFilled.isInstanceOf(ZoologicalName.class)){
1438
			ZoologicalName zoologicalName = (ZoologicalName)nameToBeFilled;
1439
			zoologicalName.setBreed(null);
1440
			zoologicalName.setOriginalPublicationYear(null);
1441
		}
1442

    
1443
		//nom status handled in nom status parser, otherwise we loose additional information like reference etc.
1444
		//hybrid relationships handled in hybrid formula and at end of fullNameParser
1445
	}
1446

    
1447

    
1448

    
1449
}
(3-3/8)