safeguards for date stamps

This commit is contained in:
Jörg Prante 2023-01-30 17:37:13 +01:00
parent 87628fe3e2
commit de875b95f6
2 changed files with 27 additions and 12 deletions

View file

@ -21,6 +21,7 @@ import static org.xbib.marc.json.MarcJsonWriter.TYPE_TAG;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.time.DateTimeException;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeFormatterBuilder;
@ -178,24 +179,38 @@ public class MarcRecord implements Map<String, Object> {
return recordLabel; return recordLabel;
} }
public LocalDate getCreationDate() { public LocalDate getCreationDate(LocalDate defaultDate) {
if (marcFields != null) { if (marcFields != null) {
String value = getFirst("008").recoverControlFieldValue(); MarcField marcField = getFirst("008");
if (value != null && value.length() >= 6) { if (marcField != null) {
String value = marcField.recoverControlFieldValue();
if (value != null && value.length() >= 6 && value.substring(0, 6).matches("\\d+")) {
try {
return LocalDate.parse(value.substring(0, 6), SHORT_DATE_FORMAT); return LocalDate.parse(value.substring(0, 6), SHORT_DATE_FORMAT);
} catch (DateTimeException e) {
return defaultDate;
} }
} }
return null; }
}
return defaultDate;
} }
public LocalDate getLastModificationDate() { public LocalDate getLastModificationDate(LocalDate defaultDate) {
if (marcFields != null) { if (marcFields != null) {
String value = getFirst("005").recoverControlFieldValue(); MarcField marcField = getFirst("005");
if (value != null && value.length() >= 8) { if (marcField != null) {
String value = marcField.recoverControlFieldValue();
if (value != null && value.length() >= 8 && value.substring(0, 8).matches("\\d+")) {
try {
return LocalDate.parse(value.substring(0, 8), DATE_FORMAT); return LocalDate.parse(value.substring(0, 8), DATE_FORMAT);
} catch (DateTimeException e) {
return defaultDate;
} }
} }
return null; }
}
return defaultDate;
} }
public void filterFields(Comparator<MarcField> comparator) { public void filterFields(Comparator<MarcField> comparator) {

View file

@ -104,8 +104,8 @@ public class MarcRecordTest {
.setCharset(Charset.forName("ANSEL")); .setCharset(Charset.forName("ANSEL"));
// only single record // only single record
for (MarcRecord marcRecord : builder.iterable()) { for (MarcRecord marcRecord : builder.iterable()) {
assertEquals(LocalDate.of(2002, 8, 5), marcRecord.getCreationDate()); assertEquals(LocalDate.of(2002, 8, 5), marcRecord.getCreationDate(LocalDate.now()));
assertEquals(LocalDate.of(2003, 6, 16), marcRecord.getLastModificationDate()); assertEquals(LocalDate.of(2003, 6, 16), marcRecord.getLastModificationDate(LocalDate.now()));
// check if single 245 field // check if single 245 field
List<MarcField> list = new ArrayList<>(); List<MarcField> list = new ArrayList<>();
Pattern pattern = Pattern.compile("^245.*"); Pattern pattern = Pattern.compile("^245.*");