From 922dd0ddb4158351a86df439d883af1ec5b6f332 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Mon, 18 Nov 2024 13:52:39 +0100 Subject: [PATCH] Add option 'validateLeader' (#567) --- .../biblio/marc21/Marc21Encoder.java | 23 +++++++++++++++---- .../biblio/marc21/Marc21EncoderTest.java | 20 ++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java index 3cd536fe7..fb8ad50b2 100644 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java +++ b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/Marc21Encoder.java @@ -81,6 +81,7 @@ public final class Marc21Encoder extends private State state = State.IN_STREAM; private boolean generateIdField; + private boolean validateLeader = true; /** * Initializes the encoder with MARC 21 constants and charset. @@ -108,6 +109,18 @@ public void setGenerateIdField(final boolean generateIdField) { this.generateIdField = generateIdField; } + /** + * Controls whether the leader should be validated. + *

+ * The default value of {@code validateLeader} is true. + *

+ * + * @param validateLeader if false the leader is not validated + */ + public void setValidateLeader(final boolean validateLeader) { + this.validateLeader = validateLeader; + } + /** * Gets the flag to decide whether the ID field is generated. * @@ -259,12 +272,14 @@ private void processLeaderAsSubfields(final String name, final char code) { } private void requireValidCode(final char code, final char[] validCodes) { - for (final char validCode: validCodes) { - if (validCode == code) { - return; + if (validateLeader) { + for (final char validCode : validCodes) { + if (validCode == code) { + return; + } } + throw new FormatException("invalid code '" + code + "'; allowed codes are: " + Arrays.toString(validCodes)); } - throw new FormatException("invalid code '" + code + "'; allowed codes are: " + Arrays.toString(validCodes)); } private void processTopLevelLiteral(final String name, final String value) { diff --git a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java index e8e70325d..53ceba315 100644 --- a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java +++ b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/Marc21EncoderTest.java @@ -38,6 +38,8 @@ */ public final class Marc21EncoderTest { + private static final String BAD_LEADER = "00600ny a22002053n 4500"; + private Marc21Encoder marc21Encoder; @Mock @@ -147,4 +149,22 @@ public void issue524ShouldComputeValidLeader() { verify(receiver).process(matches("00055pam a2200037 c 4500021001700000\u001e.*\u001d")); } + @Test(expected = FormatException.class) + public void issue567ShouldFailValidateLeaderAsDefault() { + marc21Encoder.startRecord(""); + marc21Encoder.literal(LEADER_ENTITY, BAD_LEADER); + marc21Encoder.endRecord(); + } + + @Test + public void issue567ShouldNotValidateLeader() { + marc21Encoder.setValidateLeader(false); + + marc21Encoder.startRecord(""); + marc21Encoder.literal(LEADER_ENTITY, BAD_LEADER ); + marc21Encoder.endRecord(); + + verify(receiver).process(matches("00026ny a22000253n 4500\u001e\u001d")); + } + }