2929import org .metafacture .framework .helpers .DefaultXmlPipe ;
3030
3131import java .io .IOException ;
32+ import java .io .StringReader ;
3233import java .net .URL ;
3334import java .util .ArrayList ;
3435import java .util .Collections ;
@@ -68,6 +69,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str
6869
6970 private static final String XML_HEADER = "<?xml version=\" %s\" encoding=\" %s\" ?>\n " ;
7071 private static final String XMLNS_MARKER = " xmlns" ;
72+ private static final String DEFAULT = "__default" ;
7173
7274 private final StringBuilder builder = new StringBuilder ();
7375
@@ -141,9 +143,7 @@ public void setNamespaceFile(final String file) {
141143 catch (final IOException e ) {
142144 throw new MetafactureException ("Failed to load namespaces list" , e );
143145 }
144- for (final Entry <Object , Object > entry : properties .entrySet ()) {
145- namespaces .put (entry .getKey ().toString (), entry .getValue ().toString ());
146- }
146+ propertiesToMap (properties );
147147 }
148148
149149 /**
@@ -159,9 +159,7 @@ public void setNamespaceFile(final URL url) {
159159 catch (final IOException e ) {
160160 throw new MetafactureException ("Failed to load namespaces list" , e );
161161 }
162- for (final Entry <Object , Object > entry : properties .entrySet ()) {
163- namespaces .put (entry .getKey ().toString (), entry .getValue ().toString ());
164- }
162+ propertiesToMap (properties );
165163 }
166164
167165 /**
@@ -218,6 +216,25 @@ public void setNamespaces(final Map<String, String> namespaces) {
218216 this .namespaces = namespaces ;
219217 }
220218
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 colon ':' 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+ try (StringReader sr = new StringReader (namespacesString )) {
230+ properties .load (sr );
231+ }
232+ catch (final IOException e ) {
233+ throw new MetafactureException ("Failed to create namespace list" );
234+ }
235+ propertiesToMap (properties );
236+ }
237+
221238 /**
222239 * Sets the attribute marker.
223240 *
@@ -256,7 +273,7 @@ else if (atStreamStart) {
256273 private void addNamespacesToElement () {
257274 for (final Entry <String , String > namespace : namespaces .entrySet ()) {
258275 final String key = namespace .getKey ();
259- final String name = XMLNS_MARKER + (key . isEmpty ( ) ? "" : ":" ) + key ;
276+ final String name = XMLNS_MARKER + (isDefaultNamespace ( key ) ? "" : ":" + key ) ;
260277 element .addAttribute (name , namespace .getValue ());
261278 }
262279 }
@@ -326,7 +343,7 @@ private void writeHeader() {
326343 builder .append (rootTag );
327344 for (final Entry <String , String > entry : namespaces .entrySet ()) {
328345 builder .append (XMLNS_MARKER );
329- if (!entry .getKey (). isEmpty ( )) {
346+ if (!isDefaultNamespace ( entry .getKey ())) {
330347 builder .append (':' );
331348 builder .append (entry .getKey ());
332349 }
@@ -351,6 +368,14 @@ protected static void writeEscaped(final StringBuilder builder, final String str
351368 builder .append (XmlUtil .escape (str , false ));
352369 }
353370
371+ private boolean isDefaultNamespace (final String ns ) {
372+ return ns .isEmpty () || ns .equals (DEFAULT );
373+ }
374+
375+ private void propertiesToMap (final Properties properties ) {
376+ properties .forEach ((k , v ) -> namespaces .put (k .toString (), v .toString ()));
377+ }
378+
354379 /**
355380 * An XML element.
356381 *
0 commit comments