From a0f2736dd5fe07b58a4f4ca48694afa878e222bb Mon Sep 17 00:00:00 2001 From: TobiasNx <61879957+TobiasNx@users.noreply.github.com> Date: Wed, 26 Jun 2024 09:52:16 +0200 Subject: [PATCH 1/3] Write valid leader without counting #527 Instead of just appending the incoming leader elements, this adds the static values for Pos: 00-04, 10-16, 20-23. By using `0`as default value for Pos: 00-04 and 12-16. https://github.com/metafacture/metafacture-core/issues/527#issuecomment-2076585889 --- .../main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java index 2271dafaa..9dba83d51 100644 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java +++ b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java @@ -435,7 +435,7 @@ private void writeLeader() { if (!leader.isEmpty()) { prettyPrintIndentation(); writeTag(Tag.leader::open); - writeRaw(leader); + writeRaw("0000" + leader.substring(0, 4) + "2200000" + leader.substring(5, 7) + "4500"); // creates a valid leader without counted elements writeTag(Tag.leader::close); prettyPrintNewLine(); } From 06a62ffb61c202220f627b49ded1de01fc419f43 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Mon, 18 Nov 2024 13:52:39 +0100 Subject: [PATCH 2/3] Add option 'validateLeader' (#567) --- .../biblio/marc21/Marc21Encoder.java | 23 +++++++++++++++---- .../biblio/marc21/Marc21EncoderTest.java | 19 +++++++++++++++ 2 files changed, 38 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..f81d864c2 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,21 @@ 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")); + } + } From d9aded3716b6fb6447faaa8e5d6a9218ea017eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20B=C3=BClte?= Date: Tue, 19 Nov 2024 11:16:38 +0100 Subject: [PATCH 3/3] Add conditional that tests if the incoming leader has length of 8 #527 If the incoming leader is 8 Metafacture is now able to create correct leaders for marc xml with static default values. --- .../metafacture/biblio/marc21/MarcXmlEncoder.java | 12 +++++++++++- .../biblio/marc21/MarcXmlEncoderTest.java | 8 ++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java index 9fb12b016..19bc9888b 100644 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java +++ b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java @@ -95,6 +95,11 @@ public String close(final Object[] args) { private static final int LEADER_ENTITY_LENGTH = 5; + private static final int LEADER_CONCAT_ENTITIES_LENGTH = 8; + private static final int LEADER_CONCAT_ENTITIES_POS_04 = 4; + private static final int LEADER_CONCAT_ENTITIES_POS_05 = 5; + private static final int LEADER_CONCAT_ENTITIES_POS_07 = 7; + private static final int IND1_BEGIN = 3; private static final int IND1_END = 4; private static final int IND2_BEGIN = 4; @@ -445,7 +450,12 @@ private void writeLeader() { } writeTagLeader(Tag.leader::open); - writeRawLeader("0000" + leader.substring(0, 4) + "2200000" + leader.substring(5, 7) + "4500"); // creates a valid leader without counted elements + if (leader.length() == LEADER_CONCAT_ENTITIES_LENGTH) { + writeRawLeader("0000" + leader.substring(0, LEADER_CONCAT_ENTITIES_POS_04) + "2200000" + leader.substring(LEADER_CONCAT_ENTITIES_POS_05, LEADER_CONCAT_ENTITIES_POS_07) + "4500"); // creates a valid leader without counted elements + } + else { + writeRawLeader(leader); + } writeTagLeader(Tag.leader::close); if (formatted) { diff --git a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java index 2e1c8df23..5d62ff243 100644 --- a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java +++ b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java @@ -301,13 +301,13 @@ public void issue548_failWhenLeaderIsNotFirst() { } @Test - public void issue527_shouldEmitLeaderAlwaysAsWholeString() { + public void issue527_shouldEmitLeaderAlwaysAsWholeStringWithAddedDefaultLeaderValuesIfLeaderLengthIs8() { createRecordWithLeader("1", "a", "o", "a", " ", "a", "z", "u", " "); createRecordWithLeader("2", "d", "u", "m", " ", "m", "y", "#", " "); encoder.closeStream(); String expected = XML_DECLARATION + XML_ROOT_OPEN - + "aoa azu " - + "dum my# " + XML_MARC_COLLECTION_END_TAG; + + "0000aoa 2200000zu4500" + + "0000dum 2200000y#4500" + XML_MARC_COLLECTION_END_TAG; String actual = resultCollector.toString(); assertEquals(expected, actual); } @@ -315,7 +315,7 @@ public void issue527_shouldEmitLeaderAlwaysAsWholeString() { @Test(expected = MissingIdException.class) public void issue527_shouldEmitLeaderAlwaysAsWholeString_ensureCorrectMarc21Xml() { encoder.setEnsureCorrectMarc21Xml(true); - issue527_shouldEmitLeaderAlwaysAsWholeString(); + issue527_shouldEmitLeaderAlwaysAsWholeStringWithAddedDefaultLeaderValuesIfLeaderLengthIs8(); } @Test