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 43df44e5f..110c9bb42 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 @@ -182,28 +182,23 @@ public void endEntity() { @Override public void literal(final String name, final String value) { if ("".equals(currentEntity)) { - prettyPrintIndentation(); - writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name)); - if (value != null) { - writeEscaped(value.trim()); + if (!writeLeader(name, value)) { + prettyPrintIndentation(); + writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name)); + if (value != null) { + writeEscaped(value.trim()); + } + writeRaw(CONTROLFIELD_CLOSE); + prettyPrintNewLine(); } - writeRaw(CONTROLFIELD_CLOSE); - prettyPrintNewLine(); } - else if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) { + else if (!writeLeader(currentEntity, value)) { prettyPrintIndentation(); writeRaw(String.format(SUBFIELD_OPEN_TEMPLATE, name)); writeEscaped(value.trim()); writeRaw(SUBFIELD_CLOSE); prettyPrintNewLine(); } - else { - if (name.equals(Marc21EventNames.LEADER_ENTITY)) { - prettyPrintIndentation(); - writeRaw(LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE); - prettyPrintNewLine(); - } - } } @@ -252,6 +247,19 @@ private void writeEscaped(final String str) { builder.append(XmlUtil.escape(str, false)); } + private boolean writeLeader(final String name, final String value) { + if (name.equals(Marc21EventNames.LEADER_ENTITY)) { + prettyPrintIndentation(); + writeRaw(LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE); + prettyPrintNewLine(); + + return true; + } + else { + return false; + } + } + private void prettyPrintIndentation() { if (formatted) { final String prefix = String.join("", Collections.nCopies(indentationLevel, INDENT)); 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 5f4d90e01..8351256e1 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 @@ -186,6 +186,18 @@ public void createAnRecordWithLeader() { assertEquals(expected, actual); } + @Test + public void issue336_createRecordWithTopLevelLeader() { + encoder.startRecord("1"); + encoder.literal(Marc21EventNames.LEADER_ENTITY, "dummy"); + encoder.endRecord(); + encoder.closeStream(); + String expected = XML_DECLARATION + XML_ROOT_OPEN + + "dummy" + XML_MARC_COLLECTION_END_TAG; + String actual = resultCollector.toString(); + assertEquals(expected, actual); + } + @Test public void sendDataAndClearWhenRecordStartedAndStreamResets() { encoder.startRecord("1");