Skip to content

Commit

Permalink
Encode top-level MARC record leader as proper XML element instead of …
Browse files Browse the repository at this point in the history
…control field.

The XML decoder (not knowing anything about MARC) emits the leader element as a top-level literal. This is in contrast to the MARC21 decoder which wraps it in an entity.

The latter seems to be a historical artifact, originating from the removal of the `splitLeader` setting in `Marc21Decoder` (6d04d69) and the subsequent introduction of the `emitLeaderAsWhole` setting (76eb9fd).

Fixes #336.
  • Loading branch information
blackwinter committed Oct 3, 2021
1 parent fb527e4 commit 259a909
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
+ "<marc:record><marc:leader>dummy</marc:leader></marc:record>" + XML_MARC_COLLECTION_END_TAG;
String actual = resultCollector.toString();
assertEquals(expected, actual);
}

@Test
public void sendDataAndClearWhenRecordStartedAndStreamResets() {
encoder.startRecord("1");
Expand Down

0 comments on commit 259a909

Please sign in to comment.