29
29
import org .metafacture .framework .helpers .DefaultXmlPipe ;
30
30
31
31
import java .io .IOException ;
32
+ import java .io .StringReader ;
32
33
import java .net .URL ;
33
34
import java .util .ArrayList ;
34
35
import java .util .Collections ;
@@ -68,6 +69,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str
68
69
69
70
private static final String XML_HEADER = "<?xml version=\" %s\" encoding=\" %s\" ?>\n " ;
70
71
private static final String XMLNS_MARKER = " xmlns" ;
72
+ private static final String DEFAULT = "__default" ;
71
73
72
74
private final StringBuilder builder = new StringBuilder ();
73
75
@@ -141,9 +143,7 @@ public void setNamespaceFile(final String file) {
141
143
catch (final IOException e ) {
142
144
throw new MetafactureException ("Failed to load namespaces list" , e );
143
145
}
144
- for (final Entry <Object , Object > entry : properties .entrySet ()) {
145
- namespaces .put (entry .getKey ().toString (), entry .getValue ().toString ());
146
- }
146
+ propertiesToMap (properties );
147
147
}
148
148
149
149
/**
@@ -159,9 +159,7 @@ public void setNamespaceFile(final URL url) {
159
159
catch (final IOException e ) {
160
160
throw new MetafactureException ("Failed to load namespaces list" , e );
161
161
}
162
- for (final Entry <Object , Object > entry : properties .entrySet ()) {
163
- namespaces .put (entry .getKey ().toString (), entry .getValue ().toString ());
164
- }
162
+ propertiesToMap (properties );
165
163
}
166
164
167
165
/**
@@ -218,6 +216,31 @@ public void setNamespaces(final Map<String, String> namespaces) {
218
216
this .namespaces = namespaces ;
219
217
}
220
218
219
+ /**
220
+ * Sets the namespace(s).
221
+ *
222
+ * @param namespacesString the namespaces as a String. It allows Java Properties
223
+ * structure, i.e. a key-value structure where the key is separated from the value
224
+ * by an equal sign '=', a semicolon ':' or a white space ' '.Multiple namespaces
225
+ * are separated by a line feed '\n'
226
+ */
227
+ public void setNamespaces (final String namespacesString ) {
228
+ final Properties properties = new Properties ();
229
+ final StringReader sr = new StringReader (namespacesString );
230
+ try {
231
+ properties .load (sr );
232
+ }
233
+ catch (final IOException e ) {
234
+ throw new MetafactureException ("Failed to create namespace list" );
235
+ }
236
+ finally {
237
+ if (sr != null ) {
238
+ sr .close ();
239
+ }
240
+ }
241
+ propertiesToMap (properties );
242
+ }
243
+
221
244
/**
222
245
* Sets the attribute marker.
223
246
*
@@ -256,7 +279,7 @@ else if (atStreamStart) {
256
279
private void addNamespacesToElement () {
257
280
for (final Entry <String , String > namespace : namespaces .entrySet ()) {
258
281
final String key = namespace .getKey ();
259
- final String name = XMLNS_MARKER + (key . isEmpty ( ) ? "" : ":" ) + key ;
282
+ final String name = XMLNS_MARKER + (isDefaultNamespace ( key ) ? "" : ":" + key ) ;
260
283
element .addAttribute (name , namespace .getValue ());
261
284
}
262
285
}
@@ -326,7 +349,7 @@ private void writeHeader() {
326
349
builder .append (rootTag );
327
350
for (final Entry <String , String > entry : namespaces .entrySet ()) {
328
351
builder .append (XMLNS_MARKER );
329
- if (!entry .getKey (). isEmpty ( )) {
352
+ if (!isDefaultNamespace ( entry .getKey ())) {
330
353
builder .append (':' );
331
354
builder .append (entry .getKey ());
332
355
}
@@ -351,6 +374,16 @@ protected static void writeEscaped(final StringBuilder builder, final String str
351
374
builder .append (XmlUtil .escape (str , false ));
352
375
}
353
376
377
+ private boolean isDefaultNamespace (final String ns ) {
378
+ return ns .isEmpty () || ns .equals (DEFAULT );
379
+ }
380
+
381
+ private void propertiesToMap (final Properties properties ) {
382
+ for (final Entry <Object , Object > entry : properties .entrySet ()) {
383
+ namespaces .put (entry .getKey ().toString (), entry .getValue ().toString ());
384
+ }
385
+ }
386
+
354
387
/**
355
388
* An XML element.
356
389
*
0 commit comments