Skip to content

Commit d651816

Browse files
committed
set maximum length of YEAR values to 5 digits; fixes #27819
1 parent 813d6b4 commit d651816

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

goobi-viewer-indexer/src/main/java/io/goobi/viewer/indexer/helper/DateTools.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ private DateTools() {
8484
* @should parse year ranges correctly
8585
* @should parse single years correctly
8686
* @should throw IllegalArgumentException if normalizeYearMinDigits less than 1
87+
* @should ignore values with too many digits
8788
*/
89+
private static final int MAX_YEAR_DIGITS = 5;
90+
8891
static List<PrimitiveDate> normalizeDate(String dateString, int normalizeYearMinDigits) {
8992
if (normalizeYearMinDigits < 1) {
9093
throw new IllegalArgumentException("normalizeYearMinDigits must be at least 1");
@@ -159,7 +162,7 @@ static List<PrimitiveDate> normalizeDate(String dateString, int normalizeYearMin
159162
while (m.find()) {
160163
try {
161164
String sub = dateString.substring(m.start(), m.end());
162-
if (sub.length() >= normalizeYearMinDigits) {
165+
if (sub.length() >= normalizeYearMinDigits && sub.length() <= MAX_YEAR_DIGITS) {
163166
int year = Integer.parseInt(sub);
164167
ret.add(new PrimitiveDate(year, null, null));
165168
}
@@ -175,7 +178,8 @@ static List<PrimitiveDate> normalizeDate(String dateString, int normalizeYearMin
175178
while (m.find()) {
176179
try {
177180
String sub = dateString.substring(m.start(), m.end());
178-
if (sub.length() >= (sub.charAt(0) == '-' ? (normalizeYearMinDigits + 1) : normalizeYearMinDigits)) {
181+
int digitLength = sub.charAt(0) == '-' ? sub.length() - 1 : sub.length();
182+
if (digitLength >= normalizeYearMinDigits && digitLength <= MAX_YEAR_DIGITS) {
179183
int year = Integer.parseInt(sub);
180184
ret.add(new PrimitiveDate(year, null, null));
181185
}

goobi-viewer-indexer/src/test/java/io/goobi/viewer/indexer/helper/DateToolsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ void normalizeDate_shouldThrowIllegalArgumentExceptionIfNormalizeYearMinDigitsLe
116116
Assertions.assertThrows(IllegalArgumentException.class, () -> DateTools.normalizeDate("05.08.2014", 0));
117117
}
118118

119+
/**
120+
* @see DateTools#normalizeDate(String,int)
121+
* @verifies ignore values with too many digits
122+
*/
123+
@Test
124+
void normalizeDate_shouldIgnoreValuesWithTooManyDigits() throws Exception {
125+
// Concatenated date range "1848-1848" stripped of hyphens should not produce year 18481848
126+
List<PrimitiveDate> ret = DateTools.normalizeDate("18481848", 3);
127+
Assertions.assertTrue(ret.isEmpty());
128+
129+
// Also test in a string with other content
130+
ret = DateTools.normalizeDate("18481848 1900", 3);
131+
Assertions.assertEquals(1, ret.size());
132+
Assertions.assertEquals(Integer.valueOf(1900), ret.get(0).getYear());
133+
}
134+
119135
/**
120136
* @see DateTools#normalizeDateFieldValue(String)
121137
* @verifies format years correctly

0 commit comments

Comments
 (0)