suppress format/type attribute in MarcXML, add creation/modification date to MarcRecord, allow rebuild of record fields by external comparator

This commit is contained in:
Jörg Prante 2023-01-28 23:08:24 +01:00
parent 62c5a73771
commit 09db520c2e
5 changed files with 70 additions and 7 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib group = org.xbib
name = marc name = marc
version = 2.9.16 version = 2.9.17
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL

View file

@ -21,6 +21,8 @@ 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.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.xbib.marc.label.RecordLabel; import org.xbib.marc.label.RecordLabel;
@ -45,6 +47,10 @@ public class MarcRecord implements Map<String, Object> {
private static final MarcRecord EMPTY_RECORD = Marc.builder().buildRecord(); private static final MarcRecord EMPTY_RECORD = Marc.builder().buildRecord();
private static final DateTimeFormatter field5DateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
private static final DateTimeFormatter field8DateFormat = DateTimeFormatter.ofPattern("yyMMdd");
private Map<String, Object> delegate; private Map<String, Object> delegate;
private String format; private String format;
@ -166,6 +172,26 @@ public class MarcRecord implements Map<String, Object> {
return recordLabel; return recordLabel;
} }
public LocalDate getCreationDate() {
if (marcFields != null) {
String value = getFirst("008").getValue();
if (value != null && value.length() >= 6) {
return LocalDate.parse(value.substring(0, 6), field8DateFormat);
}
}
return null;
}
public LocalDate getLastModificationDate() {
if (marcFields != null) {
String value = getFirst("005").getValue();
if (value != null && value.length() >= 8) {
return LocalDate.parse(value.substring(0, 8), field5DateFormat);
}
}
return null;
}
public void filterFields(Comparator<MarcField> comparator) { public void filterFields(Comparator<MarcField> comparator) {
if (marcFields != null) { if (marcFields != null) {
Stream<MarcField> stream = marcFields.stream(); Stream<MarcField> stream = marcFields.stream();
@ -422,7 +448,11 @@ public class MarcRecord implements Map<String, Object> {
} }
public void rebuildMap() { public void rebuildMap() {
this.delegate = createMapFromMarcFields(Comparator.naturalOrder()); rebuildMap(Comparator.naturalOrder());
}
public void rebuildMap(Comparator<String> comparator) {
this.delegate = createMapFromMarcFields(comparator);
} }
@Override @Override

View file

@ -300,10 +300,14 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
try { try {
if (!recordStarted) { if (!recordStarted) {
List<Attribute> attrs = new LinkedList<>(); List<Attribute> attrs = new LinkedList<>();
String realformat = getFormat() != null ? getFormat() : format != null ? format : getDefaultFormat(); if (createFormatAttribute()) {
attrs.add(eventFactory.createAttribute(FORMAT_ATTRIBUTE, realformat)); String realformat = getFormat() != null ? getFormat() : format != null ? format : getDefaultFormat();
String realtype = getType() != null ? getType() : type != null ? type : getDefaultType(); attrs.add(eventFactory.createAttribute(FORMAT_ATTRIBUTE, realformat));
attrs.add(eventFactory.createAttribute(TYPE_ATTRIBUTE, realtype)); }
if (createTypeAttribute()) {
String realtype = getType() != null ? getType() : type != null ? type : getDefaultType();
attrs.add(eventFactory.createAttribute(TYPE_ATTRIBUTE, realtype));
}
if (!schemaWritten) { if (!schemaWritten) {
writeSchema(attrs); writeSchema(attrs);
schemaWritten = true; schemaWritten = true;
@ -373,6 +377,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
String ind2 = indicator != null && indicator.length() > 1 ? indicator.substring(1, 2) : " "; String ind2 = indicator != null && indicator.length() > 1 ? indicator.substring(1, 2) : " ";
List<Attribute> attrs = new LinkedList<>(); List<Attribute> attrs = new LinkedList<>();
attrs.add(eventFactory.createAttribute(TAG_ATTRIBUTE, transform(tag))); attrs.add(eventFactory.createAttribute(TAG_ATTRIBUTE, transform(tag)));
// not full MarcXchange indicators
attrs.add(eventFactory.createAttribute(IND_ATTRIBUTE + "1", transform(ind1))); attrs.add(eventFactory.createAttribute(IND_ATTRIBUTE + "1", transform(ind1)));
attrs.add(eventFactory.createAttribute(IND_ATTRIBUTE + "2", transform(ind2))); attrs.add(eventFactory.createAttribute(IND_ATTRIBUTE + "2", transform(ind2)));
xmlEventConsumer.add(eventFactory.createStartElement(getDatafieldElement(), attrs.iterator(), namespaces)); xmlEventConsumer.add(eventFactory.createStartElement(getDatafieldElement(), attrs.iterator(), namespaces));
@ -505,6 +510,14 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable,
return eventFactory.createNamespace("", NAMESPACE_URI); return eventFactory.createNamespace("", NAMESPACE_URI);
} }
protected boolean createFormatAttribute() {
return true;
}
protected boolean createTypeAttribute() {
return true;
}
protected void writeSchema(List<Attribute> attrs) throws XMLStreamException { protected void writeSchema(List<Attribute> attrs) throws XMLStreamException {
attrs.add(eventFactory.createAttribute("xmlns:xsi", attrs.add(eventFactory.createAttribute("xmlns:xsi",
XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)); XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI));

View file

@ -73,10 +73,21 @@ public class MarcXmlWriter extends MarcXchangeWriter {
super(consumer); super(consumer);
} }
@Override
protected Namespace createNameSpace() { protected Namespace createNameSpace() {
return eventFactory.createNamespace("", NAMESPACE_URI); return eventFactory.createNamespace("", NAMESPACE_URI);
} }
@Override
protected boolean createFormatAttribute() {
return false;
}
@Override
protected boolean createTypeAttribute() {
return false;
}
@Override @Override
protected void writeSchema(List<Attribute> attrs) throws XMLStreamException { protected void writeSchema(List<Attribute> attrs) throws XMLStreamException {
attrs.add(eventFactory.createAttribute("xmlns:xsi", attrs.add(eventFactory.createAttribute("xmlns:xsi",
@ -85,26 +96,32 @@ public class MarcXmlWriter extends MarcXchangeWriter {
NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION)); NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION));
} }
@Override
protected QName getCollectionElement() { protected QName getCollectionElement() {
return COLLECTION_ELEMENT; return COLLECTION_ELEMENT;
} }
@Override
protected QName getRecordElement() { protected QName getRecordElement() {
return RECORD_ELEMENT; return RECORD_ELEMENT;
} }
@Override
protected QName getLeaderElement() { protected QName getLeaderElement() {
return LEADER_ELEMENT; return LEADER_ELEMENT;
} }
@Override
protected QName getControlfieldElement() { protected QName getControlfieldElement() {
return CONTROLFIELD_ELEMENT; return CONTROLFIELD_ELEMENT;
} }
@Override
protected QName getDatafieldElement() { protected QName getDatafieldElement() {
return DATAFIELD_ELEMENT; return DATAFIELD_ELEMENT;
} }
@Override
protected QName getSubfieldElement() { protected QName getSubfieldElement() {
return SUBFIELD_ELEMENT; return SUBFIELD_ELEMENT;
} }

View file

@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.content.XContentBuilder; import org.xbib.content.XContentBuilder;
import org.xbib.content.json.JsonXContent; import org.xbib.content.json.JsonXContent;
@ -103,7 +104,9 @@ 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()) {
// single 245 field assertEquals(LocalDate.of(2002, 8, 5), marcRecord.getCreationDate());
assertEquals(LocalDate.of(2003, 6, 16), marcRecord.getLastModificationDate());
// 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.*");
marcRecord.all(field -> pattern.matcher(field.getTag()).matches(), list::add); marcRecord.all(field -> pattern.matcher(field.getTag()).matches(), list::add);