Skip to content

Commit f048c33

Browse files
committed
Merge #401 from '336-fixLeaderXmlElement' of https://github.com/metafacture/metafacture-core
2 parents fb527e4 + 259a909 commit f048c33

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlEncoder.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,23 @@ public void endEntity() {
182182
@Override
183183
public void literal(final String name, final String value) {
184184
if ("".equals(currentEntity)) {
185-
prettyPrintIndentation();
186-
writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name));
187-
if (value != null) {
188-
writeEscaped(value.trim());
185+
if (!writeLeader(name, value)) {
186+
prettyPrintIndentation();
187+
writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name));
188+
if (value != null) {
189+
writeEscaped(value.trim());
190+
}
191+
writeRaw(CONTROLFIELD_CLOSE);
192+
prettyPrintNewLine();
189193
}
190-
writeRaw(CONTROLFIELD_CLOSE);
191-
prettyPrintNewLine();
192194
}
193-
else if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) {
195+
else if (!writeLeader(currentEntity, value)) {
194196
prettyPrintIndentation();
195197
writeRaw(String.format(SUBFIELD_OPEN_TEMPLATE, name));
196198
writeEscaped(value.trim());
197199
writeRaw(SUBFIELD_CLOSE);
198200
prettyPrintNewLine();
199201
}
200-
else {
201-
if (name.equals(Marc21EventNames.LEADER_ENTITY)) {
202-
prettyPrintIndentation();
203-
writeRaw(LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE);
204-
prettyPrintNewLine();
205-
}
206-
}
207202

208203
}
209204

@@ -252,6 +247,19 @@ private void writeEscaped(final String str) {
252247
builder.append(XmlUtil.escape(str, false));
253248
}
254249

250+
private boolean writeLeader(final String name, final String value) {
251+
if (name.equals(Marc21EventNames.LEADER_ENTITY)) {
252+
prettyPrintIndentation();
253+
writeRaw(LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE);
254+
prettyPrintNewLine();
255+
256+
return true;
257+
}
258+
else {
259+
return false;
260+
}
261+
}
262+
255263
private void prettyPrintIndentation() {
256264
if (formatted) {
257265
final String prefix = String.join("", Collections.nCopies(indentationLevel, INDENT));

metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlEncoderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ public void createAnRecordWithLeader() {
186186
assertEquals(expected, actual);
187187
}
188188

189+
@Test
190+
public void issue336_createRecordWithTopLevelLeader() {
191+
encoder.startRecord("1");
192+
encoder.literal(Marc21EventNames.LEADER_ENTITY, "dummy");
193+
encoder.endRecord();
194+
encoder.closeStream();
195+
String expected = XML_DECLARATION + XML_ROOT_OPEN
196+
+ "<marc:record><marc:leader>dummy</marc:leader></marc:record>" + XML_MARC_COLLECTION_END_TAG;
197+
String actual = resultCollector.toString();
198+
assertEquals(expected, actual);
199+
}
200+
189201
@Test
190202
public void sendDataAndClearWhenRecordStartedAndStreamResets() {
191203
encoder.startRecord("1");

0 commit comments

Comments
 (0)