Skip to content

Commit

Permalink
Add parameter "namespaces" (#505)
Browse files Browse the repository at this point in the history
Set a namespace or a list of namespaces. A namespace is a key-value structure,
separated by a space. Multiple namespaces are separated by a semicolon.
  • Loading branch information
dr0i committed Mar 18, 2024
1 parent 0ca90b7 commit c0ed493
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -218,6 +220,20 @@ public void setNamespaces(final Map<String, String> namespaces) {
this.namespaces = namespaces;
}

/**
* Sets the namespace(s).
*
* @param namespacesString the namespaces as a String. Key and value are separated by a space. Multiple entries are
* separated by a semicolon ';'
*/
public void setNamespaces(final String namespacesString) {
final String[] namespacesArray = namespacesString.split(";");
for (Iterator<String> it = Arrays.stream(namespacesArray).iterator(); it.hasNext(); ) {
final String[] entry = it.next().split(" ");
namespaces.put(entry[0], entry[1]);
}
}

/**
* Sets the attribute marker.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ public void shouldAddNamespaceToRootElement() {
getResultXml());
}

@Test
public void shouldAddMultipleNamespacesFromParameterToRootElement() {
final Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put("ns", "http://example.org/ns");
simpleXmlEncoder.setNamespaces("ns http://example.org/ns;ns1 http://example.org/ns1");

emitEmptyRecord();

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><records xmlns:ns=\"http://example.org/ns\" xmlns:ns1=\"http://example.org/ns1\"><record /></records>",
getResultXml());
}
@Test
public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRootTag() {
final Map<String, String> namespaces = new HashMap<String, String>();
Expand Down Expand Up @@ -180,6 +191,16 @@ public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceTo
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><record xmlns=\"http://example.org/ns\" />",
getResultXml());
}
@Test
public void shouldAddNamespaceWithEmptyKeyFromParameterAsDefaultNamespaceToRecordTag() {
simpleXmlEncoder.setNamespaces(" http://example.org/ns");
simpleXmlEncoder.setWriteRootTag(false);

emitEmptyRecord();

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><record xmlns=\"http://example.org/ns\" />",
getResultXml());
}

@Test
public void testShouldEncodeUnnamedLiteralsAsText() {
Expand Down

0 comments on commit c0ed493

Please sign in to comment.