Skip to content

Commit 9a7ad30

Browse files
committed
Fix #231: Ignore type literals in Marc21Encoder
1 parent 61274fe commit 9a7ad30

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/main/java/org/culturegraph/mf/stream/converter/bib/Marc21Encoder.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import org.culturegraph.mf.iso2709.Iso2709Format;
2626
import org.culturegraph.mf.iso2709.RecordBuilder;
2727
import org.culturegraph.mf.iso2709.RecordFormat;
28+
import org.culturegraph.mf.stream.converter.xml.MarcXmlHandler;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2831

2932
/**
3033
* Encodes a stream in MARC21 format.
@@ -54,10 +57,13 @@
5457
* 2709:2008 record label and some of its contents (record status,
5558
* implementation codes, user system characters) are copied into the generated
5659
* record</li>
60+
*
61+
* <li>literal named "type", which may be produced by {@link MarcXmlHandler}
62+
* are ignored.</li>
5763
* </ul>
5864
*
5965
* <p>The stream expected by the encoder is compatible to the streams emitted by
60-
* the {@link MarcDecoder} and the {MarcXmlHandler}.
66+
* the {@link MarcDecoder} and the {@link MarcXmlHandler}.
6167
* </p>
6268
*
6369
* <p>The record identifier in {@code startRecord} is ignored. To add an identifier
@@ -76,10 +82,13 @@
7682
public final class Marc21Encoder extends
7783
DefaultStreamPipe<ObjectReceiver<String>> {
7884

79-
public static final String LEADER_LITERAL = "leader";
85+
private static Logger LOG = LoggerFactory.getLogger(Marc21Encoder.class);
8086

8187
private static final RecordFormat MARC21 = new RecordFormat();
8288

89+
public static final String LEADER_LITERAL = "leader";
90+
public static final String TYPE_LITERAL = "type";
91+
8392
private final RecordBuilder builder = new RecordBuilder(MARC21);
8493
private final int nameLength;
8594

@@ -131,12 +140,20 @@ public void endEntity() {
131140

132141
@Override
133142
public void literal(final String name, final String value) {
134-
if (LEADER_LITERAL.equals(name)) {
135-
setRecordLabel(value);
136-
} else if (inField) {
143+
if (inField) {
137144
builder.appendSubfield(name, value);
138145
} else {
139-
builder.appendReferenceField(name, value);
146+
if (LEADER_LITERAL.equals(name)) {
147+
setRecordLabel(value);
148+
} else if (TYPE_LITERAL.equals(name)) {
149+
// MarcXmlHandler may output `type` literals. The
150+
// information in these literals is not included in
151+
// marc21 records. Therefore, we need to ignore
152+
// these literals here.
153+
return;
154+
} else {
155+
builder.appendReferenceField(name, value);
156+
}
140157
}
141158
}
142159

src/test/java/org/culturegraph/mf/stream/converter/bib/Marc21EncoderTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.culturegraph.mf.stream.converter.bib;
1717

18+
import static org.mockito.Matchers.any;
1819
import static org.mockito.Matchers.matches;
1920
import static org.mockito.Mockito.verify;
2021

@@ -107,4 +108,13 @@ public void shouldThrowFormatExceptionIfEntityNameLengthIsNotFive() {
107108
marc21Encoder.startEntity("012abc");
108109
}
109110

111+
@Test
112+
public void issue231ShouldIgnoreTypeLiterals() {
113+
marc21Encoder.startRecord("");
114+
marc21Encoder.literal("type", "ignoreme");
115+
marc21Encoder.endRecord();
116+
117+
verify(receiver).process(any(String.class));
118+
}
119+
110120
}

0 commit comments

Comments
 (0)