Revision bf3b1132
Added by Andreas Müller almost 4 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/parser/TimePeriodParser.java | ||
---|---|---|
35 | 35 |
private static final Logger logger = Logger.getLogger(TimePeriodParser.class); |
36 | 36 |
|
37 | 37 |
private static final String dotOrWs = "(\\.\\s*|\\s+)"; |
38 |
private static final String dashOrWs = "(-"+TimePeriod.SEP+"\\s*|\\s+)"; |
|
39 |
|
|
40 |
public static final String SEP = "(-|"+TimePeriod.SEP+"|"+UTF8.EN_DASH + ")"; |
|
38 | 41 |
|
39 | 42 |
//patter for first year in string; |
40 | 43 |
private static final Pattern firstYearPattern = Pattern.compile("\\d{4}"); |
... | ... | |
54 | 57 |
private static final Pattern lifeSpanPattern = Pattern.compile(String.format("%s--%s", firstYearPattern, firstYearPattern)); |
55 | 58 |
private static final String strMonthes = "((Jan|Feb|Aug|Sept?|Oct(ober)?|Nov|Dec)\\.?|(Mar(ch)?|Apr(il)?|May|June?|July?))"; |
56 | 59 |
private static final String strDateWithMonthes = "([0-3]?\\d" + dotOrWs + ")?" + strMonthes + dotOrWs + "\\d{4,4}\\+?"; |
57 |
private static final Pattern dateWithMonthNamePattern = Pattern.compile(strDateWithMonthes); |
|
60 |
public static final String strDateYearMonthDay = "(\\d{4,4}" + dashOrWs + ")?" + strMonthes + "(" + dashOrWs + "[0-3]?\\d)?\\+?"; |
|
61 |
private static final Pattern dateWithMonthNamePattern = Pattern.compile(strDateWithMonthes); |
|
62 |
private static final Pattern dateYearMonthDayPattern = Pattern.compile(strDateYearMonthDay); |
|
58 | 63 |
|
59 | 64 |
public static <T extends TimePeriod> T parseString(T timePeriod, String periodString){ |
60 | 65 |
//TODO until now only quick and dirty (and partly wrong) |
... | ... | |
96 | 101 |
parseSlashDatePattern(periodString, result); |
97 | 102 |
}else if (dateWithMonthNamePattern.matcher(periodString).matches()){ |
98 | 103 |
parseDateWithMonthName(periodString, result); |
104 |
}else if (dateYearMonthDayPattern.matcher(periodString).matches()){ |
|
105 |
parseDateYearMonthDay(periodString, result); |
|
99 | 106 |
}else if (lifeSpanPattern.matcher(periodString).matches()){ |
100 | 107 |
parseLifeSpanPattern(periodString, result); |
101 | 108 |
}else if (standardPattern.matcher(periodString).matches()){ |
... | ... | |
305 | 312 |
} |
306 | 313 |
} |
307 | 314 |
|
315 |
private static void parseDateYearMonthDay(String dateString, TimePeriod result) { |
|
316 |
String[] dates = dateString.split(dashOrWs); |
|
317 |
|
|
318 |
if (dates.length > 3 || dates.length < 1){ |
|
319 |
logger.warn("Not 2 or 3 date parts in date string: " + dateString); |
|
320 |
result.setFreeText(dateString); |
|
321 |
}else { |
|
322 |
|
|
323 |
boolean hasYear = dates[0].trim().matches("\\d{4,4}"); |
|
324 |
String strYear = hasYear ? dates[0] : null; |
|
325 |
String strMonth = hasYear ? dates[1] : dates[0]; |
|
326 |
String strDay = (hasYear && dates.length == 2 || dates.length == 1) ? null : hasYear ? dates[2] : dates[1]; |
|
327 |
try { |
|
328 |
if (strDay != null && strDay.endsWith("+")){ |
|
329 |
strDay = strDay.substring(0, strDay.length()-1).trim(); |
|
330 |
result.setContinued(true); |
|
331 |
} |
|
332 |
Integer year = strYear == null ? null : Integer.valueOf(strYear.trim()); |
|
333 |
Integer month = monthNrFormName(strMonth.trim()); |
|
334 |
Integer day = strDay == null ? null : Integer.valueOf(strDay.trim()); |
|
335 |
|
|
336 |
Partial partial = makePartialFromDateParts(year, month, day); |
|
337 |
|
|
338 |
result.setStart(partial); |
|
339 |
} catch (IllegalArgumentException e) { |
|
340 |
result.setFreeText(dateString); |
|
341 |
} |
|
342 |
} |
|
343 |
} |
|
308 | 344 |
|
309 | 345 |
/** |
310 | 346 |
* @param year |
... | ... | |
315 | 351 |
public static Partial makePartialFromDateParts(Integer year, Integer month, Integer day) { |
316 | 352 |
Partial partial = new Partial(); |
317 | 353 |
//TODO deduplicate code with other routines |
318 |
if (year < 1000 && year > 2100){ |
|
319 |
logger.warn("Not a valid year: " + year + ". Year must be between 1000 and 2100"); |
|
320 |
}else if (year < 1700 && year > 2100){ |
|
321 |
logger.warn("Not a valid taxonomic year: " + year + ". Year must be between 1750 and 2100"); |
|
322 |
partial = partial.with(TimePeriod.YEAR_TYPE, year); |
|
323 |
}else{ |
|
324 |
partial = partial.with(TimePeriod.YEAR_TYPE, year); |
|
354 |
if (year != null){ |
|
355 |
if (year < 1000 && year > 2100){ |
|
356 |
logger.warn("Not a valid year: " + year + ". Year must be between 1000 and 2100"); |
|
357 |
}else if (year < 1700 && year > 2100){ |
|
358 |
logger.warn("Not a valid taxonomic year: " + year + ". Year must be between 1750 and 2100"); |
|
359 |
partial = partial.with(TimePeriod.YEAR_TYPE, year); |
|
360 |
}else{ |
|
361 |
partial = partial.with(TimePeriod.YEAR_TYPE, year); |
|
362 |
} |
|
325 | 363 |
} |
326 | 364 |
if (month != null && month != 0){ |
327 | 365 |
partial = partial.with(TimePeriod.MONTH_TYPE, month); |
Also available in: Unified diff
ref #9071, ref #6276 Change TimePeriodFormatting to YYYY-MMM-DD