Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter "namespaces" #522

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.metafacture.framework.helpers.DefaultXmlPipe;

import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -68,6 +69,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str

private static final String XML_HEADER = "<?xml version=\"%s\" encoding=\"%s\"?>\n";
private static final String XMLNS_MARKER = " xmlns";
private static final String DEFAULT = "__default";

private final StringBuilder builder = new StringBuilder();

Expand Down Expand Up @@ -141,9 +143,7 @@ public void setNamespaceFile(final String file) {
catch (final IOException e) {
throw new MetafactureException("Failed to load namespaces list", e);
}
for (final Entry<Object, Object> entry : properties.entrySet()) {
namespaces.put(entry.getKey().toString(), entry.getValue().toString());
}
propertiesToMap(properties);
}

/**
Expand All @@ -159,9 +159,7 @@ public void setNamespaceFile(final URL url) {
catch (final IOException e) {
throw new MetafactureException("Failed to load namespaces list", e);
}
for (final Entry<Object, Object> entry : properties.entrySet()) {
namespaces.put(entry.getKey().toString(), entry.getValue().toString());
}
propertiesToMap(properties);
}

/**
Expand Down Expand Up @@ -218,6 +216,25 @@ public void setNamespaces(final Map<String, String> namespaces) {
this.namespaces = namespaces;
}

/**
* Sets the namespace(s).
*
* @param namespacesString the namespaces as a String. It allows Java Properties
* structure, i.e. a key-value structure where the key is separated from the value
* by an equal sign '=', a colon ':' or a white space ' '. Multiple namespaces
* are separated by a line feed '\n'
*/
public void setNamespaces(final String namespacesString) {
final Properties properties = new Properties();
try (StringReader sr = new StringReader(namespacesString)) {
properties.load(sr);
}
catch (final IOException e) {
throw new MetafactureException("Failed to create namespace list");
}
propertiesToMap(properties);
}

/**
* Sets the attribute marker.
*
Expand Down Expand Up @@ -256,7 +273,7 @@ else if (atStreamStart) {
private void addNamespacesToElement() {
for (final Entry<String, String> namespace : namespaces.entrySet()) {
final String key = namespace.getKey();
final String name = XMLNS_MARKER + (key.isEmpty() ? "" : ":") + key;
final String name = XMLNS_MARKER + (isDefaultNamespace(key) ? "" : ":" + key);
element.addAttribute(name, namespace.getValue());
}
}
Expand Down Expand Up @@ -326,7 +343,7 @@ private void writeHeader() {
builder.append(rootTag);
for (final Entry<String, String> entry : namespaces.entrySet()) {
builder.append(XMLNS_MARKER);
if (!entry.getKey().isEmpty()) {
if (!isDefaultNamespace(entry.getKey())) {
builder.append(':');
builder.append(entry.getKey());
}
Expand All @@ -351,6 +368,14 @@ protected static void writeEscaped(final StringBuilder builder, final String str
builder.append(XmlUtil.escape(str, false));
}

private boolean isDefaultNamespace(final String ns) {
return ns.isEmpty() || ns.equals(DEFAULT);
}

private void propertiesToMap(final Properties properties) {
properties.forEach((k, v) -> namespaces.put(k.toString(), v.toString()));
}

/**
* An XML element.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public void shouldAddNamespaceToRootElement() {
getResultXml());
}

@Test
public void shouldAddMultipleNamespacesFromParameterToRootElement() {
simpleXmlEncoder.setNamespaces("__default=http://default.org/ns\nns=http://example.org/ns\nns1=http://example.org/ns1");

emitEmptyRecord();

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><records xmlns:ns=\"http://example.org/ns\" xmlns=\"http://default.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 All @@ -124,6 +133,18 @@ public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRootTag() {
getResultXml());
}

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

emitEmptyRecord();

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

@Test
public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceToRootTag() {
simpleXmlEncoder.setNamespaceFile("org/metafacture/xml/SimpleXmlEncoderTest_namespaces.properties");
Expand Down Expand Up @@ -170,6 +191,19 @@ public void shouldAddNamespaceWithEmptyKeyAsDefaultNamespaceToRecordTag() {
getResultXml());
}

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

emitEmptyRecord();

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

@Test
public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceToRecordTag() {
simpleXmlEncoder.setNamespaceFile("org/metafacture/xml/SimpleXmlEncoderTest_namespaces.properties");
Expand All @@ -180,6 +214,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
Loading