Skip to content

Commit f12a4da

Browse files
committed
Resolve #248: support default namespace in SimpleXmlDecoder
If the namespace map contains an entry with the empty string as its key then the URL in the value of this entry will be added as `xmlns="http://example.org/value-url"` to the root element.
1 parent f3e3bf2 commit f12a4da

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/main/java/org/culturegraph/mf/stream/converter/xml/SimpleXmlEncoder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str
6464
private static final String END_CLOSE_ELEMENT = ">";
6565

6666
private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
67-
private static final String XMLNS_MARKER = " xmlns:";
67+
private static final String XMLNS_MARKER = " xmlns";
6868

6969
private final StringBuilder builder = new StringBuilder();
7070

@@ -189,7 +189,10 @@ private void writeHeader() {
189189
builder.append(rootTag);
190190
for (final Entry<String, String> entry : namespaces.entrySet()) {
191191
builder.append(XMLNS_MARKER);
192-
builder.append(entry.getKey());
192+
if (!entry.getKey().isEmpty()) {
193+
builder.append(':');
194+
builder.append(entry.getKey());
195+
}
193196
builder.append(BEGIN_ATTRIBUTE);
194197
writeEscaped(builder, entry.getValue());
195198
builder.append(END_ATTRIBUTE);

src/test/java/org/culturegraph/mf/stream/converter/xml/SimpleXmlEncoderTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertTrue;
2323

24+
import java.util.HashMap;
25+
import java.util.Map;
26+
2427
import org.culturegraph.mf.framework.DefaultObjectReceiver;
2528
import org.junit.Before;
2629
import org.junit.Test;
@@ -90,6 +93,30 @@ public void shouldWrapAllRecordsInOneRootTagtIfSeparateRootsIsFalse() {
9093
getResultXml());
9194
}
9295

96+
@Test
97+
public void shouldAddNamespaceToRootElement() {
98+
final Map<String, String> namespaces = new HashMap<String, String>();
99+
namespaces.put("ns", "http://example.org/ns");
100+
simpleXmlEncoder.setNamespaces(namespaces);
101+
102+
emitEmptyRecord();
103+
104+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><records xmlns:ns=\"http://example.org/ns\"><record /></records>",
105+
getResultXml());
106+
}
107+
108+
@Test
109+
public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespace() {
110+
final Map<String, String> namespaces = new HashMap<String, String>();
111+
namespaces.put("", "http://example.org/ns");
112+
simpleXmlEncoder.setNamespaces(namespaces);
113+
114+
emitEmptyRecord();
115+
116+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><records xmlns=\"http://example.org/ns\"><record /></records>",
117+
getResultXml());
118+
}
119+
93120
private void emitTwoRecords() {
94121
simpleXmlEncoder.startRecord("X");
95122
simpleXmlEncoder.literal(TAG, VALUE);
@@ -100,6 +127,12 @@ private void emitTwoRecords() {
100127
simpleXmlEncoder.closeStream();
101128
}
102129

130+
private void emitEmptyRecord() {
131+
simpleXmlEncoder.startRecord("");
132+
simpleXmlEncoder.endRecord();
133+
simpleXmlEncoder.closeStream();
134+
}
135+
103136
private String getResultXml() {
104137
return resultCollector.toString().replaceAll("[\\n\\t]", "");
105138
}

0 commit comments

Comments
 (0)