Skip to content

Commit 9626868

Browse files
committed
Add parameter "namespaces" (#505)
Set a namespace or a list of namespaces. A namespace is a Java Properties structure, i.e. a key-value structure where the key is separated from the value by an equal sign '=', a semicolon ':' or white space ' '.Multiple namespaces are separated by a line feed '\n'.
1 parent 0ca90b7 commit 9626868

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

metafacture-xml/src/main/java/org/metafacture/xml/SimpleXmlEncoder.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.metafacture.framework.helpers.DefaultXmlPipe;
3030

3131
import java.io.IOException;
32+
import java.io.StringReader;
3233
import java.net.URL;
3334
import java.util.ArrayList;
3435
import 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,31 @@ 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 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+
221244
/**
222245
* Sets the attribute marker.
223246
*
@@ -256,7 +279,7 @@ else if (atStreamStart) {
256279
private void addNamespacesToElement() {
257280
for (final Entry<String, String> namespace : namespaces.entrySet()) {
258281
final String key = namespace.getKey();
259-
final String name = XMLNS_MARKER + (key.isEmpty() ? "" : ":") + key;
282+
final String name = XMLNS_MARKER + (isDefaultNamespace(key) ? "" : ":" + key);
260283
element.addAttribute(name, namespace.getValue());
261284
}
262285
}
@@ -326,7 +349,7 @@ private void writeHeader() {
326349
builder.append(rootTag);
327350
for (final Entry<String, String> entry : namespaces.entrySet()) {
328351
builder.append(XMLNS_MARKER);
329-
if (!entry.getKey().isEmpty()) {
352+
if (!isDefaultNamespace(entry.getKey())) {
330353
builder.append(':');
331354
builder.append(entry.getKey());
332355
}
@@ -351,6 +374,16 @@ protected static void writeEscaped(final StringBuilder builder, final String str
351374
builder.append(XmlUtil.escape(str, false));
352375
}
353376

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+
354387
/**
355388
* An XML element.
356389
*

metafacture-xml/src/test/java/org/metafacture/xml/SimpleXmlEncoderTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ public void shouldAddNamespaceToRootElement() {
112112
getResultXml());
113113
}
114114

115+
@Test
116+
public void shouldAddMultipleNamespacesFromParameterToRootElement() {
117+
simpleXmlEncoder.setNamespaces("__default=http://default.org/ns\nns=http://example.org/ns\nns1=http://example.org/ns1");
118+
119+
emitEmptyRecord();
120+
121+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><records xmlns=\"http://default.org/ns\" xmlns:ns=\"http://example.org/ns\" xmlns:ns1=\"http://example.org/ns1\"><record /></records>",
122+
getResultXml());
123+
}
115124
@Test
116125
public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRootTag() {
117126
final Map<String, String> namespaces = new HashMap<String, String>();
@@ -124,6 +133,18 @@ public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRootTag() {
124133
getResultXml());
125134
}
126135

136+
@Test
137+
public void shouldAddNamespaceWithDefaultKeyAsDefaultNamespaceToRootTag() {
138+
final Map<String, String> namespaces = new HashMap<String, String>();
139+
namespaces.put("__default", "http://example.org/ns");
140+
simpleXmlEncoder.setNamespaces(namespaces);
141+
142+
emitEmptyRecord();
143+
144+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><records xmlns=\"http://example.org/ns\"><record /></records>",
145+
getResultXml());
146+
}
147+
127148
@Test
128149
public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceToRootTag() {
129150
simpleXmlEncoder.setNamespaceFile("org/metafacture/xml/SimpleXmlEncoderTest_namespaces.properties");
@@ -170,6 +191,19 @@ public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRecordTag() {
170191
getResultXml());
171192
}
172193

194+
@Test
195+
public void shouldAddNamespaceWithDefaultKeyAsDefaultNamespaceToRecordTag() {
196+
final Map<String, String> namespaces = new HashMap<String, String>();
197+
namespaces.put("__default", "http://example.org/ns");
198+
simpleXmlEncoder.setNamespaces(namespaces);
199+
simpleXmlEncoder.setWriteRootTag(false);
200+
201+
emitEmptyRecord();
202+
203+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><record xmlns=\"http://example.org/ns\" />",
204+
getResultXml());
205+
}
206+
173207
@Test
174208
public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceToRecordTag() {
175209
simpleXmlEncoder.setNamespaceFile("org/metafacture/xml/SimpleXmlEncoderTest_namespaces.properties");
@@ -180,6 +214,16 @@ public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceTo
180214
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><record xmlns=\"http://example.org/ns\" />",
181215
getResultXml());
182216
}
217+
@Test
218+
public void shouldAddNamespaceWithEmptyKeyFromParameterAsDefaultNamespaceToRecordTag() {
219+
simpleXmlEncoder.setNamespaces("=http://example.org/ns");
220+
simpleXmlEncoder.setWriteRootTag(false);
221+
222+
emitEmptyRecord();
223+
224+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><record xmlns=\"http://example.org/ns\" />",
225+
getResultXml());
226+
}
183227

184228
@Test
185229
public void testShouldEncodeUnnamedLiteralsAsText() {

0 commit comments

Comments
 (0)