Project

General

Profile

Revision ed31afc0

IDed31afc0690a6bc21319b107b504cd9aebcc023c
Parent f34ca874
Child 4cf9ce0b

Added by Andreas Müller over 9 years ago

recognize doted dates

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/TimePeriod.java
444 444
	private static final Pattern prefixedYearPattern =  Pattern.compile("(fl|c)\\.\\s*\\d{4}(\\s*-\\s*\\d{4})?\\??");
445 445
	//standard
446 446
	private static final Pattern standardPattern =  Pattern.compile("\\s*\\d{2,4}(\\s*-(\\s*\\d{2,4})?)?");
447
	private static final String strDotDate = "[0-3]?\\d\\.[01]?\\d\\.\\d{4,4}";
448
	private static final String strDotDatePeriodPattern = String.format("%s(\\s*-\\s*%s?)?", strDotDate, strDotDate);
449
	private static final Pattern dotDatePattern =  Pattern.compile(strDotDatePeriodPattern);
447 450
	
448 451
	
449 452
	public static TimePeriod parseString(TimePeriod timePeriod, String periodString){
......
480 483
				String endYear = yearMatcher.group();
481 484
				result.setEndYear(Integer.valueOf(endYear));
482 485
			}
486
		}else if (dotDatePattern.matcher(periodString).matches()){
487
			parseDotDatePattern(periodString, result);
483 488
		}else if (standardPattern.matcher(periodString).matches()){
484
			String[] years = periodString.split("-");
485
			Partial dtStart = null;
486
			Partial dtEnd = null;
487
			
488
			if (years.length > 2 || years.length <= 0){
489
				logger.warn("More than 1 '-' in period String: " + periodString);
490
			}else {
491
				try {
492
					//start
493
					if (! CdmUtils.isEmpty(years[0])){
494
						dtStart = parseSingleDate(years[0].trim());
495
					}
496
					
497
					//end
498
					if (years.length >= 2 && ! CdmUtils.isEmpty(years[1])){
499
						years[1] = years[1].trim();
500
						if (years[1].length()==2 && dtStart != null && dtStart.isSupported(DateTimeFieldType.year())){
501
							years[1] = String.valueOf(dtStart.get(DateTimeFieldType.year())/100) + years[1];
502
						}
503
						dtEnd = parseSingleDate(years[1]);
504
					}
505
					
506
					result.setStart(dtStart);
507
					result.setEnd(dtEnd);
508
				} catch (IllegalArgumentException e) {
509
					//logger.warn(e.getMessage());
510
					result.setFreeText(periodString);
511
				}
512
			}
489
			parseStandardPattern(periodString, result);
513 490
		}else{
514 491
			result.setFreeText(periodString);
515 492
		}
516 493
		return result;
517 494
	}
495

  
496
	/**
497
	 * @param periodString
498
	 * @param result
499
	 */
500
	private static void parseDotDatePattern(String periodString,TimePeriod result) {
501
		String[] dates = periodString.split("-");
502
		Partial dtStart = null;
503
		Partial dtEnd = null;
504
		
505
		if (dates.length > 2 || dates.length <= 0){
506
			logger.warn("More than 1 '-' in period String: " + periodString);
507
			result.setFreeText(periodString);
508
		}else {
509
			try {
510
				//start
511
				if (! CdmUtils.isEmpty(dates[0])){
512
					dtStart = parseSingleDotDate(dates[0].trim());
513
				}
514
				
515
				//end
516
				if (dates.length >= 2 && ! CdmUtils.isEmpty(dates[1])){
517
					dtEnd = parseSingleDotDate(dates[1].trim());
518
				}
519
				
520
				result.setStart(dtStart);
521
				result.setEnd(dtEnd);
522
			} catch (IllegalArgumentException e) {
523
				//logger.warn(e.getMessage());
524
				result.setFreeText(periodString);
525
			}
526
		}
527
	}
528
	
529
	
530
	/**
531
	 * @param periodString
532
	 * @param result
533
	 */
534
	private static void parseStandardPattern(String periodString,
535
			TimePeriod result) {
536
		String[] years = periodString.split("-");
537
		Partial dtStart = null;
538
		Partial dtEnd = null;
539
		
540
		if (years.length > 2 || years.length <= 0){
541
			logger.warn("More than 1 '-' in period String: " + periodString);
542
		}else {
543
			try {
544
				//start
545
				if (! CdmUtils.isEmpty(years[0])){
546
					dtStart = parseSingleDate(years[0].trim());
547
				}
548
				
549
				//end
550
				if (years.length >= 2 && ! CdmUtils.isEmpty(years[1])){
551
					years[1] = years[1].trim();
552
					if (years[1].length()==2 && dtStart != null && dtStart.isSupported(DateTimeFieldType.year())){
553
						years[1] = String.valueOf(dtStart.get(DateTimeFieldType.year())/100) + years[1];
554
					}
555
					dtEnd = parseSingleDate(years[1]);
556
				}
557
				
558
				result.setStart(dtStart);
559
				result.setEnd(dtEnd);
560
			} catch (IllegalArgumentException e) {
561
				//logger.warn(e.getMessage());
562
				result.setFreeText(periodString);
563
			}
564
		}
565
	}
518 566
	
519 567
	public static TimePeriod parseString(String strPeriod) {
520 568
		TimePeriod timePeriod = TimePeriod.NewInstance();
......
548 596

  
549 597
	}
550 598
	
599
	protected static Partial parseSingleDotDate(String singleDateString) throws IllegalArgumentException{
600
		Partial partial =  new Partial();
601
		singleDateString = singleDateString.trim();
602
		String[] split = singleDateString.split("\\.");
603
		int length = split.length;
604
		if (length > 3){
605
			throw new IllegalArgumentException(String.format("More than 2 dots in date '%s'", singleDateString));
606
		}
607
		String strYear = split[split.length-1];
608
		String strMonth = length >= 2? split[split.length-2]: null;
609
		String strDay = length >= 3? split[split.length-3]: null;
610
		
611
		
612
		try {
613
			Integer year = Integer.valueOf(strYear.trim());
614
			Integer month = Integer.valueOf(strMonth.trim());
615
			Integer day = Integer.valueOf(strDay.trim());
616
			if (year < 1000 && year > 2100){
617
				logger.warn("Not a valid year: " + year + ". Year must be between 1000 and 2100");
618
			}else if (year < 1700 && year > 2100){
619
				logger.warn("Not a valid taxonomic year: " + year + ". Year must be between 1750 and 2100");
620
				partial = partial.with(YEAR_TYPE, year);
621
			}else{
622
				partial = partial.with(YEAR_TYPE, year);
623
			}
624
			if (month != null && month != 0){
625
				partial = partial.with(MONTH_TYPE, month);
626
			}
627
			if (day != null && day != 0){
628
				partial = partial.with(DAY_TYPE, day);
629
			}
630
		} catch (NumberFormatException e) {
631
			logger.debug("Not a Integer format somewhere in " + singleDateString);
632
			throw new IllegalArgumentException(e);
633
		}
634
		return partial;
635

  
636
	}
637
	
551 638
	
552 639
	
553 640
	private class TimePeriodPartialFormatter extends DateTimeFormatter{
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/common/TimePeriodTest.java
23 23
import org.junit.AfterClass;
24 24
import org.junit.Before;
25 25
import org.junit.BeforeClass;
26
import org.junit.Ignore;
27 26
import org.junit.Test;
28 27

  
29 28
/**
......
364 363
		Assert.assertEquals(Integer.valueOf(1810), tpC.getEndYear());
365 364
		Assert.assertEquals("1806-1810", tpC.getYear());
366 365
		
366
		//1.1.2011
367
		String strDotDate = "1.2.2011";
368
		TimePeriod tp = TimePeriod.parseString(strDotDate);
369
		assertNotNull(tp);
370
		Assert.assertEquals(strDotDate, tp.toString());
371
		Assert.assertEquals("2011", tp.getYear());
372
		Assert.assertEquals(Integer.valueOf(2), tp.getStartMonth());
373
		Assert.assertEquals(Integer.valueOf(1), tp.getStartDay());
374
		
375
		strDotDate = "31.03.2012";
376
		tp = TimePeriod.parseString(strDotDate);
377
		assertNotNull(tp);
378
		Assert.assertEquals("31.3.2012", tp.toString());
379
		Assert.assertEquals("2012", tp.getYear());
380
		Assert.assertEquals(Integer.valueOf(3), tp.getStartMonth());
381
		Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
382
		
383
		strDotDate = "00.04.2013";
384
		tp = TimePeriod.parseString(strDotDate);
385
		assertNotNull(tp);
386
		Assert.assertEquals("4.2013", tp.toString());
387
		Assert.assertEquals("2013", tp.getYear());
388
		Assert.assertEquals(Integer.valueOf(4), tp.getStartMonth());
389
		Assert.assertEquals(null, tp.getStartDay());
390
		
391
		strDotDate = "13.00.2014";
392
		tp = TimePeriod.parseString(strDotDate);
393
		assertNotNull(tp);
394
		Assert.assertEquals("13.xx.2014", tp.toString());
395
		Assert.assertEquals("2014", tp.getYear());
396
		Assert.assertEquals(null, tp.getStartMonth());
397
		Assert.assertEquals(Integer.valueOf(13), tp.getStartDay());
398
		
399
		strDotDate = "31.12.2015 - 02.01.2016";
400
		tp = TimePeriod.parseString(strDotDate);
401
		assertNotNull(tp);
402
		Assert.assertEquals("31.12.2015-2.1.2016", tp.toString());
403
		Assert.assertEquals("2015-2016", tp.getYear());
404
		Assert.assertEquals(Integer.valueOf(2015), tp.getStartYear());
405
		Assert.assertEquals(Integer.valueOf(12), tp.getStartMonth());
406
		Assert.assertEquals(Integer.valueOf(31), tp.getStartDay());
407
		Assert.assertEquals(Integer.valueOf(2016), tp.getEndYear());
408
		Assert.assertEquals(Integer.valueOf(1), tp.getEndMonth());
409
		Assert.assertEquals(Integer.valueOf(2), tp.getEndDay());
410
		
367 411
	}
368 412
	
369 413
	@Test

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)