diff --git a/gradle.properties b/gradle.properties index 5da3566..1fe10aa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = marc -version = 2.9.16 +version = 2.9.17 org.gradle.warning.mode = ALL diff --git a/src/main/java/org/xbib/marc/MarcRecord.java b/src/main/java/org/xbib/marc/MarcRecord.java index 273930f..b84e74a 100644 --- a/src/main/java/org/xbib/marc/MarcRecord.java +++ b/src/main/java/org/xbib/marc/MarcRecord.java @@ -21,6 +21,8 @@ import static org.xbib.marc.json.MarcJsonWriter.TYPE_TAG; import java.io.InputStream; import java.nio.charset.Charset; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.stream.Stream; import org.xbib.marc.label.RecordLabel; @@ -45,6 +47,10 @@ public class MarcRecord implements Map { 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 delegate; private String format; @@ -166,6 +172,26 @@ public class MarcRecord implements Map { 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 comparator) { if (marcFields != null) { Stream stream = marcFields.stream(); @@ -422,7 +448,11 @@ public class MarcRecord implements Map { } public void rebuildMap() { - this.delegate = createMapFromMarcFields(Comparator.naturalOrder()); + rebuildMap(Comparator.naturalOrder()); + } + + public void rebuildMap(Comparator comparator) { + this.delegate = createMapFromMarcFields(comparator); } @Override diff --git a/src/main/java/org/xbib/marc/xml/MarcXchangeWriter.java b/src/main/java/org/xbib/marc/xml/MarcXchangeWriter.java index 204e6bd..0c3a67f 100644 --- a/src/main/java/org/xbib/marc/xml/MarcXchangeWriter.java +++ b/src/main/java/org/xbib/marc/xml/MarcXchangeWriter.java @@ -300,10 +300,14 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable, try { if (!recordStarted) { List attrs = new LinkedList<>(); - String realformat = getFormat() != null ? getFormat() : format != null ? format : getDefaultFormat(); - attrs.add(eventFactory.createAttribute(FORMAT_ATTRIBUTE, realformat)); - String realtype = getType() != null ? getType() : type != null ? type : getDefaultType(); - attrs.add(eventFactory.createAttribute(TYPE_ATTRIBUTE, realtype)); + if (createFormatAttribute()) { + String realformat = getFormat() != null ? getFormat() : format != null ? format : getDefaultFormat(); + attrs.add(eventFactory.createAttribute(FORMAT_ATTRIBUTE, realformat)); + } + if (createTypeAttribute()) { + String realtype = getType() != null ? getType() : type != null ? type : getDefaultType(); + attrs.add(eventFactory.createAttribute(TYPE_ATTRIBUTE, realtype)); + } if (!schemaWritten) { writeSchema(attrs); schemaWritten = true; @@ -373,6 +377,7 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable, String ind2 = indicator != null && indicator.length() > 1 ? indicator.substring(1, 2) : " "; List attrs = new LinkedList<>(); 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 + "2", transform(ind2))); xmlEventConsumer.add(eventFactory.createStartElement(getDatafieldElement(), attrs.iterator(), namespaces)); @@ -505,6 +510,14 @@ public class MarcXchangeWriter extends MarcContentHandler implements Flushable, return eventFactory.createNamespace("", NAMESPACE_URI); } + protected boolean createFormatAttribute() { + return true; + } + + protected boolean createTypeAttribute() { + return true; + } + protected void writeSchema(List attrs) throws XMLStreamException { attrs.add(eventFactory.createAttribute("xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)); diff --git a/src/main/java/org/xbib/marc/xml/MarcXmlWriter.java b/src/main/java/org/xbib/marc/xml/MarcXmlWriter.java index 8e5c835..5c2e491 100644 --- a/src/main/java/org/xbib/marc/xml/MarcXmlWriter.java +++ b/src/main/java/org/xbib/marc/xml/MarcXmlWriter.java @@ -73,10 +73,21 @@ public class MarcXmlWriter extends MarcXchangeWriter { super(consumer); } + @Override protected Namespace createNameSpace() { return eventFactory.createNamespace("", NAMESPACE_URI); } + @Override + protected boolean createFormatAttribute() { + return false; + } + + @Override + protected boolean createTypeAttribute() { + return false; + } + @Override protected void writeSchema(List attrs) throws XMLStreamException { attrs.add(eventFactory.createAttribute("xmlns:xsi", @@ -85,26 +96,32 @@ public class MarcXmlWriter extends MarcXchangeWriter { NAMESPACE_URI + " " + NAMESPACE_SCHEMA_LOCATION)); } + @Override protected QName getCollectionElement() { return COLLECTION_ELEMENT; } + @Override protected QName getRecordElement() { return RECORD_ELEMENT; } + @Override protected QName getLeaderElement() { return LEADER_ELEMENT; } + @Override protected QName getControlfieldElement() { return CONTROLFIELD_ELEMENT; } + @Override protected QName getDatafieldElement() { return DATAFIELD_ELEMENT; } + @Override protected QName getSubfieldElement() { return SUBFIELD_ELEMENT; } diff --git a/src/test/java/org/xbib/marc/MarcRecordTest.java b/src/test/java/org/xbib/marc/MarcRecordTest.java index c705249..74ce3a1 100644 --- a/src/test/java/org/xbib/marc/MarcRecordTest.java +++ b/src/test/java/org/xbib/marc/MarcRecordTest.java @@ -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.assertTrue; import java.io.IOException; +import java.time.LocalDate; import org.junit.jupiter.api.Test; import org.xbib.content.XContentBuilder; import org.xbib.content.json.JsonXContent; @@ -103,7 +104,9 @@ public class MarcRecordTest { .setCharset(Charset.forName("ANSEL")); // only single record 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 list = new ArrayList<>(); Pattern pattern = Pattern.compile("^245.*"); marcRecord.all(field -> pattern.matcher(field.getTag()).matches(), list::add);