39
39
@ Out (String .class )
40
40
@ FluxCommand ("encode-marcxml" )
41
41
public final class MarcXmlEncoder extends DefaultStreamPipe <ObjectReceiver <String >> {
42
+
42
43
private static final String ROOT_OPEN = "<marc:collection xmlns:marc=\" http://www.loc.gov/MARC21/slim\" xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\" http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\" >" ;
43
44
private static final String ROOT_CLOSE = "</marc:collection>" ;
44
45
45
46
private static final String RECORD_OPEN = "<marc:record>" ;
46
47
private static final String RECORD_CLOSE = "</marc:record>" ;
47
48
49
+ private static final String ATTRIBUTE_TEMPLATE = " %s=\" %s\" " ;
50
+
48
51
private static final String CONTROLFIELD_OPEN_TEMPLATE = "<marc:controlfield tag=\" %s\" >" ;
49
52
private static final String CONTROLFIELD_CLOSE = "</marc:controlfield>" ;
50
53
@@ -81,6 +84,7 @@ public final class MarcXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Strin
81
84
82
85
private String currentEntity ;
83
86
private int indentationLevel ;
87
+ private int recordAttributeOffset ;
84
88
private boolean formatted ;
85
89
86
90
public MarcXmlEncoder () {
@@ -134,6 +138,7 @@ public void startRecord(final String identifier) {
134
138
135
139
prettyPrintIndentation ();
136
140
writeRaw (RECORD_OPEN );
141
+ recordAttributeOffset = builder .length () - 1 ;
137
142
prettyPrintNewLine ();
138
143
139
144
incrementIndentationLevel ();
@@ -182,7 +187,12 @@ public void endEntity() {
182
187
@ Override
183
188
public void literal (final String name , final String value ) {
184
189
if ("" .equals (currentEntity )) {
185
- if (!writeLeader (name , value )) {
190
+ if (name .equals (Marc21EventNames .MARCXML_TYPE_LITERAL )) {
191
+ if (value != null ) {
192
+ builder .insert (recordAttributeOffset , String .format (ATTRIBUTE_TEMPLATE , name , value ));
193
+ }
194
+ }
195
+ else if (!writeLeader (name , value )) {
186
196
prettyPrintIndentation ();
187
197
writeRaw (String .format (CONTROLFIELD_OPEN_TEMPLATE , name ));
188
198
if (value != null ) {
@@ -199,7 +209,6 @@ else if (!writeLeader(currentEntity, value)) {
199
209
writeRaw (SUBFIELD_CLOSE );
200
210
prettyPrintNewLine ();
201
211
}
202
-
203
212
}
204
213
205
214
@ Override
@@ -276,5 +285,7 @@ private void prettyPrintNewLine() {
276
285
private void sendAndClearData () {
277
286
getReceiver ().process (builder .toString ());
278
287
builder .delete (0 , builder .length ());
288
+ recordAttributeOffset = 0 ;
279
289
}
290
+
280
291
}
0 commit comments