Skip to content
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
@@ -1,5 +1,5 @@
/*
* Copyright 2013, 2014 Deutsche Nationalbibliothek
* Copyright 2013, 2014, 2021 Deutsche Nationalbibliothek et al
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,6 @@
import java.util.regex.Pattern;

import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.MetafactureException;
import org.metafacture.framework.StreamReceiver;
import org.metafacture.framework.XmlReceiver;
import org.metafacture.framework.annotations.Description;
Expand Down Expand Up @@ -49,6 +48,9 @@ public final class GenericXmlHandler extends DefaultXmlPipe<StreamReceiver> {
private boolean inRecord;
private StringBuilder valueBuffer = new StringBuilder();

public static final boolean EMIT_NAMESPACE = false;
private boolean emitNamespace = EMIT_NAMESPACE;

public GenericXmlHandler() {
super();
final String recordTagNameProperty = System.getProperty(
Expand Down Expand Up @@ -91,13 +93,35 @@ public String getRecordTagName() {
return recordTagName;
}

/**
* Triggers namespace awareness. If set to "true" input data like "foo:bar"
* will be passed through as "foo:bar". For backward compatibility the default
* is set to "false", thus only "bar" is emitted.
* <p>
* <strong>Default value: {@value EMIT_NAMESPACE}</strong>
*
* @param emitNamespace set to "true" if namespace should be emitted. Defaults
* to "false".
*/
public void setEmitNamespace(boolean emitNamespace) {
this.emitNamespace = emitNamespace;
}

public boolean getEmitNamespace() {
return this.emitNamespace;
}

@Override
public void startElement(final String uri, final String localName,
final String qName, final Attributes attributes) {

if (inRecord) {
writeValue();
getReceiver().startEntity(localName);
if (emitNamespace) {
getReceiver().startEntity(qName);
} else {
getReceiver().startEntity(localName);
}
writeAttributes(attributes);
} else if (localName.equals(recordTagName)) {
final String identifier = attributes.getValue("id");
Expand Down Expand Up @@ -145,7 +169,11 @@ private void writeAttributes(final Attributes attributes) {
final int length = attributes.getLength();

for (int i = 0; i < length; ++i) {
final String name = attributes.getLocalName(i);
String name;
if (emitNamespace) {
name = attributes.getQName(i);
} else
name = attributes.getLocalName(i);
final String value = attributes.getValue(i);
getReceiver().literal(name, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013, 2014 Deutsche Nationalbibliothek
* Copyright 2013, 2014, 2021 Deutsche Nationalbibliothek et al
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -131,4 +131,15 @@ public void shouldEmitPCDataAsALiteralNamedValue() {
ordered.verify(receiver).literal("value", "char-data");
}

@Test
public void shouldEmitNamespaceOnEntityElementAndAttribute() {
genericXmlHandler.setEmitNamespace(true);
attributes.addAttribute("", "attr", "ns:attr", "CDATA", "attr-value");
genericXmlHandler.startElement("", "record", "record", attributes);
genericXmlHandler.startElement("", "entity", "ns:entity", attributes);

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startEntity("ns:entity");
ordered.verify(receiver).literal("ns:attr","attr-value");
}
}