Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode "leader" also if it's passed as one literal (#454) #526

Merged
merged 2 commits into from Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,9 +23,13 @@
* @author Christoph Böhme
*
*/
final class Iso2709Constants {
public final class Iso2709Constants {

static final int RECORD_LABEL_LENGTH = 24;
public static final int IMPL_CODES_START = 6;
public static final int IMPL_CODES_LENGTH = 4;
public static final int RECORD_LABEL_LENGTH = 24;
public static final int RECORD_STATUS_POS = 5;
public static final int SYSTEM_CHARS_START = 17;

static final int MIN_RECORD_LENGTH = RECORD_LABEL_LENGTH + 2;
static final int MAX_RECORD_LENGTH = 99_999;
Expand All @@ -38,18 +42,11 @@ final class Iso2709Constants {
static final int RECORD_LENGTH_START = 0;
static final int RECORD_LENGTH_LENGTH = 5;

static final int RECORD_STATUS_POS = 5;

static final int IMPL_CODES_START = 6;
static final int IMPL_CODES_LENGTH = 4;

static final int INDICATOR_LENGTH_POS = 10;
static final int IDENTIFIER_LENGTH_POS = 11;

static final int BASE_ADDRESS_START = 12;
static final int BASE_ADDRESS_LENGTH = 5;

static final int SYSTEM_CHARS_START = 17;
static final int SYSTEM_CHARS_LENGTH = 3;

static final int FIELD_LENGTH_LENGTH_POS = 20;
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.metafacture.biblio.marc21;

import org.metafacture.biblio.iso2709.Iso2709Constants;
import org.metafacture.biblio.iso2709.RecordBuilder;
import org.metafacture.biblio.iso2709.RecordFormat;
import org.metafacture.framework.FluxCommand;
Expand Down Expand Up @@ -180,7 +181,12 @@ public void literal(final String name, final String value) {
builder.appendSubfield(name.toCharArray(), value);
break;
case IN_LEADER_ENTITY:
processLiteralInLeader(name, value);
if (name == Marc21EventNames.LEADER_ENTITY) {
processLeaderAsOneLiteral(value);
}
else {
processLeaderAsSubfields(name, value);
}
break;
case IN_RECORD:
processTopLevelLiteral(name, value);
Expand All @@ -190,7 +196,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() != Iso2709Constants.RECORD_LABEL_LENGTH) {
throw new FormatException(
"leader literal must contain " + Iso2709Constants.RECORD_LABEL_LENGTH + " characters:" + value);
dr0i marked this conversation as resolved.
Show resolved Hide resolved
}
processLeaderAsSubfields(Marc21EventNames.RECORD_STATUS_LITERAL, String.valueOf(value.charAt(Iso2709Constants.RECORD_STATUS_POS)));
processLeaderAsSubfields(Marc21EventNames.RECORD_TYPE_LITERAL, String.valueOf(value.charAt(Iso2709Constants.IMPL_CODES_START)));
processLeaderAsSubfields(Marc21EventNames.BIBLIOGRAPHIC_LEVEL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.IMPL_CODES_START + 1)));
processLeaderAsSubfields(Marc21EventNames.TYPE_OF_CONTROL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.IMPL_CODES_START + 2)));
processLeaderAsSubfields(Marc21EventNames.CHARACTER_CODING_LITERAL, String.valueOf(value.charAt(Iso2709Constants.RECORD_STATUS_POS + Iso2709Constants.IMPL_CODES_LENGTH)));
processLeaderAsSubfields(Marc21EventNames.ENCODING_LEVEL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.SYSTEM_CHARS_START)));
processLeaderAsSubfields(Marc21EventNames.CATALOGING_FORM_LITERAL, String.valueOf(value.charAt(Iso2709Constants.SYSTEM_CHARS_START + 1)));
processLeaderAsSubfields(Marc21EventNames.MULTIPART_LEVEL_LITERAL, String.valueOf(value.charAt(Iso2709Constants.SYSTEM_CHARS_START + 2)));
}

private void processLeaderAsSubfields(final String name, final String value) {
dr0i marked this conversation as resolved.
Show resolved Hide resolved
if (value.length() != 1) {
throw new FormatException(
"literal must only contain a single character:" + name);
Expand Down Expand Up @@ -251,7 +272,12 @@ private void processTopLevelLiteral(final String name, final String value) {
// these literals here.
return;
}
builder.appendReferenceField(name.toCharArray(), value);
if (Marc21EventNames.LEADER_ENTITY.equals(name)) {
processLeaderAsOneLiteral(value);
}
else {
builder.appendReferenceField(name.toCharArray(), value);
}
}

@Override
Expand Down
Expand Up @@ -114,4 +114,26 @@ public void issue278ShouldNotFailWhenProcessingLeaderEntity() {
verify(receiver).process(any(String.class));
}

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

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

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

verify(receiver).process(matches("00026pam a2200025 c 4500\u001e\u001d"));
}
dr0i marked this conversation as resolved.
Show resolved Hide resolved

}