Skip to content

Commit

Permalink
Fix #231: Ignore type literals in Marc21Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
cboehme committed Jun 1, 2015
1 parent 61274fe commit 9a7ad30
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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</li>
*
* <li>literal named "type", which may be produced by {@link MarcXmlHandler}
* are ignored.</li>
* </ul>
*
* <p>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}.
* </p>
*
* <p>The record identifier in {@code startRecord} is ignored. To add an identifier
Expand All @@ -76,10 +82,13 @@
public final class Marc21Encoder extends
DefaultStreamPipe<ObjectReceiver<String>> {

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;

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}

}

0 comments on commit 9a7ad30

Please sign in to comment.