diff --git a/src/main/java/org/culturegraph/mf/stream/converter/bib/Marc21Encoder.java b/src/main/java/org/culturegraph/mf/stream/converter/bib/Marc21Encoder.java index ca34b04aa..02cc736c4 100644 --- a/src/main/java/org/culturegraph/mf/stream/converter/bib/Marc21Encoder.java +++ b/src/main/java/org/culturegraph/mf/stream/converter/bib/Marc21Encoder.java @@ -25,6 +25,9 @@ import org.culturegraph.mf.iso2709.Iso2709Format; import org.culturegraph.mf.iso2709.RecordBuilder; import org.culturegraph.mf.iso2709.RecordFormat; +import org.culturegraph.mf.stream.converter.xml.MarcXmlHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Encodes a stream in MARC21 format. @@ -54,10 +57,13 @@ * 2709:2008 record label and some of its contents (record status, * implementation codes, user system characters) are copied into the generated * record + * + *
  • literal named "type", which may be produced by {@link MarcXmlHandler} + * are ignored.
  • * * *

    The stream expected by the encoder is compatible to the streams emitted by - * the {@link MarcDecoder} and the {MarcXmlHandler}. + * the {@link MarcDecoder} and the {@link MarcXmlHandler}. *

    * *

    The record identifier in {@code startRecord} is ignored. To add an identifier @@ -76,10 +82,13 @@ public final class Marc21Encoder extends DefaultStreamPipe> { - public static final String LEADER_LITERAL = "leader"; + private static Logger LOG = LoggerFactory.getLogger(Marc21Encoder.class); private static final RecordFormat MARC21 = new RecordFormat(); + public static final String LEADER_LITERAL = "leader"; + public static final String TYPE_LITERAL = "type"; + private final RecordBuilder builder = new RecordBuilder(MARC21); private final int nameLength; @@ -131,12 +140,20 @@ public void endEntity() { @Override public void literal(final String name, final String value) { - if (LEADER_LITERAL.equals(name)) { - setRecordLabel(value); - } else if (inField) { + if (inField) { builder.appendSubfield(name, value); } else { - builder.appendReferenceField(name, value); + if (LEADER_LITERAL.equals(name)) { + setRecordLabel(value); + } else if (TYPE_LITERAL.equals(name)) { + // MarcXmlHandler may output `type` literals. The + // information in these literals is not included in + // marc21 records. Therefore, we need to ignore + // these literals here. + return; + } else { + builder.appendReferenceField(name, value); + } } } diff --git a/src/test/java/org/culturegraph/mf/stream/converter/bib/Marc21EncoderTest.java b/src/test/java/org/culturegraph/mf/stream/converter/bib/Marc21EncoderTest.java index e35adbbe3..b274971b6 100644 --- a/src/test/java/org/culturegraph/mf/stream/converter/bib/Marc21EncoderTest.java +++ b/src/test/java/org/culturegraph/mf/stream/converter/bib/Marc21EncoderTest.java @@ -15,6 +15,7 @@ */ package org.culturegraph.mf.stream.converter.bib; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.matches; import static org.mockito.Mockito.verify; @@ -107,4 +108,13 @@ public void shouldThrowFormatExceptionIfEntityNameLengthIsNotFive() { marc21Encoder.startEntity("012abc"); } + @Test + public void issue231ShouldIgnoreTypeLiterals() { + marc21Encoder.startRecord(""); + marc21Encoder.literal("type", "ignoreme"); + marc21Encoder.endRecord(); + + verify(receiver).process(any(String.class)); + } + }