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\" >" ;
44
- private static final String ROOT_CLOSE = "</marc:collection>" ;
43
+ private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim" ;
44
+ private static final String NAMESPACE_NAME = "marc" ;
45
+ /*package-private*/ static final String NAMESPACE_PREFIX = NAMESPACE_NAME + ":" ;
46
+ private static final String NAMESPACE_SUFFIX = ":" + NAMESPACE_NAME ;
45
47
46
- private static final String RECORD_OPEN = "<marc:record>" ;
47
- private static final String RECORD_CLOSE = "</marc:record>" ;
48
+ private static final String SCHEMA_ATTRIBUTES = " xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\" " + NAMESPACE + " http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\" " ;
49
+
50
+ /*package-private*/ static final String ROOT_OPEN_TEMPLATE = "<%scollection xmlns%s=\" " + NAMESPACE + "\" %s>" ;
51
+ private static final String ROOT_CLOSE_TEMPLATE = "</%scollection>" ;
52
+
53
+ private static final String RECORD_OPEN_TEMPLATE = "<%srecord>" ;
54
+ private static final String RECORD_CLOSE_TEMPLATE = "</%srecord>" ;
48
55
49
56
private static final String ATTRIBUTE_TEMPLATE = " %s=\" %s\" " ;
50
57
51
- private static final String CONTROLFIELD_OPEN_TEMPLATE = "<marc:controlfield tag=\" %s\" >" ;
52
- private static final String CONTROLFIELD_CLOSE = "</marc:controlfield >" ;
58
+ private static final String CONTROLFIELD_OPEN_TEMPLATE = "<%scontrolfield tag=\" %s\" >" ;
59
+ private static final String CONTROLFIELD_CLOSE_TEMPLATE = "</%scontrolfield >" ;
53
60
54
- private static final String DATAFIELD_OPEN_TEMPLATE = "<marc:datafield tag=\" %s\" ind1=\" %s\" ind2=\" %s\" >" ;
55
- private static final String DATAFIELD_CLOSE = "</marc:datafield >" ;
61
+ private static final String DATAFIELD_OPEN_TEMPLATE = "<%sdatafield tag=\" %s\" ind1=\" %s\" ind2=\" %s\" >" ;
62
+ private static final String DATAFIELD_CLOSE_TEMPLATE = "</%sdatafield >" ;
56
63
57
- private static final String SUBFIELD_OPEN_TEMPLATE = "<marc:subfield code=\" %s\" >" ;
58
- private static final String SUBFIELD_CLOSE = "</marc:subfield >" ;
64
+ private static final String SUBFIELD_OPEN_TEMPLATE = "<%ssubfield code=\" %s\" >" ;
65
+ private static final String SUBFIELD_CLOSE_TEMPLATE = "</%ssubfield >" ;
59
66
60
- private static final String LEADER_OPEN_TEMPLATE = "<marc:leader>" ;
61
- private static final String LEADER_CLOSE_TEMPLATE = "</marc:leader>" ;
67
+ private static final String LEADER_TEMPLATE = "<%sleader>%s</%sleader>" ;
62
68
63
69
private static final String NEW_LINE = "\n " ;
64
70
private static final String INDENT = "\t " ;
71
+ private static final String EMPTY = "" ;
65
72
66
73
private static final String XML_DECLARATION_TEMPLATE = "<?xml version=\" %s\" encoding=\" %s\" ?>" ;
67
74
@@ -82,6 +89,9 @@ public final class MarcXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Strin
82
89
private String xmlVersion ;
83
90
private String xmlEncoding ;
84
91
92
+ private boolean emitNamespace ;
93
+ private String namespacePrefix ;
94
+
85
95
private String currentEntity ;
86
96
private int indentationLevel ;
87
97
private int recordAttributeOffset ;
@@ -99,6 +109,13 @@ public MarcXmlEncoder() {
99
109
100
110
this .indentationLevel = 0 ;
101
111
this .formatted = true ;
112
+
113
+ setEmitNamespace (true );
114
+ }
115
+
116
+ public void setEmitNamespace (final boolean emitNamespace ) {
117
+ this .emitNamespace = emitNamespace ;
118
+ namespacePrefix = emitNamespace ? NAMESPACE_PREFIX : EMPTY ;
102
119
}
103
120
104
121
public void omitXmlDeclaration (final boolean currentOmitXmlDeclaration ) {
@@ -130,14 +147,16 @@ public void startRecord(final String identifier) {
130
147
writeHeader ();
131
148
prettyPrintNewLine ();
132
149
}
133
- writeRaw (ROOT_OPEN );
150
+ writeRaw (String .format (ROOT_OPEN_TEMPLATE , namespacePrefix ,
151
+ emitNamespace ? NAMESPACE_SUFFIX : EMPTY ,
152
+ emitNamespace ? SCHEMA_ATTRIBUTES : EMPTY ));
134
153
prettyPrintNewLine ();
135
154
incrementIndentationLevel ();
136
155
}
137
156
atStreamStart = false ;
138
157
139
158
prettyPrintIndentation ();
140
- writeRaw (RECORD_OPEN );
159
+ writeRaw (String . format ( RECORD_OPEN_TEMPLATE , namespacePrefix ) );
141
160
recordAttributeOffset = builder .length () - 1 ;
142
161
prettyPrintNewLine ();
143
162
@@ -148,7 +167,7 @@ public void startRecord(final String identifier) {
148
167
public void endRecord () {
149
168
decrementIndentationLevel ();
150
169
prettyPrintIndentation ();
151
- writeRaw (RECORD_CLOSE );
170
+ writeRaw (String . format ( RECORD_CLOSE_TEMPLATE , namespacePrefix ) );
152
171
prettyPrintNewLine ();
153
172
sendAndClearData ();
154
173
}
@@ -167,7 +186,7 @@ public void startEntity(final String name) {
167
186
final String ind1 = name .substring (IND1_BEGIN , IND1_END );
168
187
final String ind2 = name .substring (IND2_BEGIN , IND2_END );
169
188
prettyPrintIndentation ();
170
- writeRaw (String .format (DATAFIELD_OPEN_TEMPLATE , tag , ind1 , ind2 ));
189
+ writeRaw (String .format (DATAFIELD_OPEN_TEMPLATE , namespacePrefix , tag , ind1 , ind2 ));
171
190
prettyPrintNewLine ();
172
191
incrementIndentationLevel ();
173
192
}
@@ -178,7 +197,7 @@ public void endEntity() {
178
197
if (!currentEntity .equals (Marc21EventNames .LEADER_ENTITY )) {
179
198
decrementIndentationLevel ();
180
199
prettyPrintIndentation ();
181
- writeRaw (DATAFIELD_CLOSE );
200
+ writeRaw (String . format ( DATAFIELD_CLOSE_TEMPLATE , namespacePrefix ) );
182
201
prettyPrintNewLine ();
183
202
}
184
203
currentEntity = "" ;
@@ -194,19 +213,19 @@ public void literal(final String name, final String value) {
194
213
}
195
214
else if (!writeLeader (name , value )) {
196
215
prettyPrintIndentation ();
197
- writeRaw (String .format (CONTROLFIELD_OPEN_TEMPLATE , name ));
216
+ writeRaw (String .format (CONTROLFIELD_OPEN_TEMPLATE , namespacePrefix , name ));
198
217
if (value != null ) {
199
218
writeEscaped (value .trim ());
200
219
}
201
- writeRaw (CONTROLFIELD_CLOSE );
220
+ writeRaw (String . format ( CONTROLFIELD_CLOSE_TEMPLATE , namespacePrefix ) );
202
221
prettyPrintNewLine ();
203
222
}
204
223
}
205
224
else if (!writeLeader (currentEntity , value )) {
206
225
prettyPrintIndentation ();
207
- writeRaw (String .format (SUBFIELD_OPEN_TEMPLATE , name ));
226
+ writeRaw (String .format (SUBFIELD_OPEN_TEMPLATE , namespacePrefix , name ));
208
227
writeEscaped (value .trim ());
209
- writeRaw (SUBFIELD_CLOSE );
228
+ writeRaw (String . format ( SUBFIELD_CLOSE_TEMPLATE , namespacePrefix ) );
210
229
prettyPrintNewLine ();
211
230
}
212
231
}
@@ -243,7 +262,7 @@ private void writeHeader() {
243
262
244
263
/** Closes the root tag */
245
264
private void writeFooter () {
246
- writeRaw (ROOT_CLOSE );
265
+ writeRaw (String . format ( ROOT_CLOSE_TEMPLATE , namespacePrefix ) );
247
266
}
248
267
249
268
/** Writes a unescaped sequence */
@@ -259,7 +278,7 @@ private void writeEscaped(final String str) {
259
278
private boolean writeLeader (final String name , final String value ) {
260
279
if (name .equals (Marc21EventNames .LEADER_ENTITY )) {
261
280
prettyPrintIndentation ();
262
- writeRaw (LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE );
281
+ writeRaw (String . format ( LEADER_TEMPLATE , namespacePrefix , value , namespacePrefix ) );
263
282
prettyPrintNewLine ();
264
283
265
284
return true ;
0 commit comments