@@ -252,6 +252,7 @@ private static class Encoder extends DefaultStreamPipe<ObjectReceiver<String>> {
252
252
private int indentationLevel ;
253
253
private boolean formatted = PRETTY_PRINTED ;
254
254
private int recordAttributeOffset ;
255
+ private int recordLeaderOffset ;
255
256
256
257
private Encoder () {
257
258
}
@@ -294,7 +295,7 @@ public void startRecord(final String identifier) {
294
295
writeTag (Tag .record ::open );
295
296
recordAttributeOffset = builder .length () - 1 ;
296
297
prettyPrintNewLine ();
297
-
298
+ recordLeaderOffset = builder . length ();
298
299
incrementIndentationLevel ();
299
300
}
300
301
@@ -353,7 +354,7 @@ else if (!appendLeader(name, value)) {
353
354
if (value != null ) {
354
355
writeEscaped (value .trim ());
355
356
}
356
- writeTag (Tag .controlfield ::close );
357
+ writeTag (Tag .controlfield ::close , false );
357
358
prettyPrintNewLine ();
358
359
}
359
360
}
@@ -408,9 +409,20 @@ private void writeFooter() {
408
409
* @param str the unescaped sequence to be written
409
410
*/
410
411
private void writeRaw (final String str ) {
412
+
411
413
builder .append (str );
412
414
}
413
415
416
+ /**
417
+ * Writes the unescaped sequence to the leader position.
418
+ *
419
+ * @param str the unescaped sequence to be written to the leader position
420
+ */
421
+ private void writeRawLeader (final String str ) {
422
+ builder .insert (recordLeaderOffset , str );
423
+ recordLeaderOffset = recordLeaderOffset + str .length ();
424
+ }
425
+
414
426
private boolean appendLeader (final String name , final String value ) {
415
427
if (name .equals (Marc21EventNames .LEADER_ENTITY )) {
416
428
leaderBuilder .append (value );
@@ -432,11 +444,11 @@ private void writeEscaped(final String str) {
432
444
433
445
private void writeLeader () {
434
446
final String leader = leaderBuilder .toString ();
435
- if (! leader . isEmpty () ) {
447
+ if (leaderBuilder . length () > 0 ) {
436
448
prettyPrintIndentation ();
437
- writeTag (Tag .leader ::open );
438
- writeRaw (leader );
439
- writeTag (Tag .leader ::close );
449
+ writeTagLeader (Tag .leader ::open );
450
+ writeRawLeader (leader );
451
+ writeTagLeader (Tag .leader ::close );
440
452
prettyPrintNewLine ();
441
453
}
442
454
}
@@ -447,6 +459,11 @@ private void writeTag(final Function<Object[], String> function, final Object...
447
459
writeRaw (function .apply (allArgs ));
448
460
}
449
461
462
+ private void writeTagLeader (final Function <Object [], String > function ) {
463
+ writeRawLeader (function .apply (namespacePrefix ));
464
+ }
465
+
466
+
450
467
private void prettyPrintIndentation () {
451
468
if (formatted ) {
452
469
final String prefix = String .join ("" , Collections .nCopies (indentationLevel , INDENT ));
0 commit comments