Skip to content

Commit

Permalink
Encode "leader" also if it's passed as one literal (#454)
Browse files Browse the repository at this point in the history
This makes the claim "The stream expected by the encoder is compatible to the
streams emitted by the {@link Marc21Decoder} and the {@link MarcXmlHandler}."
true again.
  • Loading branch information
dr0i committed Apr 19, 2024
1 parent e40c106 commit 0ec05e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,13 @@ public void literal(final String name, final String value) {
case IN_FIELD_ENTITY:
builder.appendSubfield(name.toCharArray(), value);
break;
case IN_LEADER_ENTITY:
processLiteralInLeader(name, value);
case IN_LEADER_ENTITY: {
if (name == Marc21EventNames.LEADER_ENTITY){
processLeaderAsOneLiteral(value);
} else {
processLeaderAsSubfields(name, value);
}
}
break;
case IN_RECORD:
processTopLevelLiteral(name, value);
Expand All @@ -190,7 +195,22 @@ public void literal(final String name, final String value) {
}
}

private void processLiteralInLeader(final String name, final String value) {
private void processLeaderAsOneLiteral(final String value) {
if (value.length() != 24) {
throw new FormatException(
"leader literal must contain 24 characters:" + value);
}
processLeaderAsSubfields(Marc21EventNames.RECORD_STATUS_LITERAL,String.valueOf(value.charAt(5)));
processLeaderAsSubfields(Marc21EventNames.RECORD_TYPE_LITERAL,String.valueOf(value.charAt(6)));
processLeaderAsSubfields(Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL,String.valueOf(value.charAt(7)));
processLeaderAsSubfields(Marc21EventNames.TYPE_OF_CONTROL_LITERAL,String.valueOf(value.charAt(8)));
processLeaderAsSubfields(Marc21EventNames.CHARACTER_CODING_LITERAL,String.valueOf(value.charAt(9)));
processLeaderAsSubfields(Marc21EventNames.ENCODING_LEVEL_LITERAL,String.valueOf(value.charAt(17)));
processLeaderAsSubfields(Marc21EventNames.CATALOGING_FORM_LITERAL,String.valueOf(value.charAt(18)));
processLeaderAsSubfields(Marc21EventNames.MULTIPART_LEVEL_LITERAL,String.valueOf(value.charAt(19)));
}

private void processLeaderAsSubfields(final String name, final String value) {
if (value.length() != 1) {
throw new FormatException(
"literal must only contain a single character:" + name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,16 @@ public void issue278ShouldNotFailWhenProcessingLeaderEntity() {
verify(receiver).process(any(String.class));
}

@Test
public void issue454ShouldNotFailWhenProcessingLeaderAsOneString() {
marc21Encoder.startRecord("");
marc21Encoder.startEntity(LEADER_ENTITY);
marc21Encoder.literal("leader", "02602pam a2200529 c 4500");
marc21Encoder.endEntity();
marc21Encoder.endEntity();
marc21Encoder.endRecord();

verify(receiver).process(matches("00026pam a2200025 c 4500\u001e\u001d"));
}

}

0 comments on commit 0ec05e7

Please sign in to comment.