diff --git a/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java b/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java index ecbf6b60a..878623953 100644 --- a/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java +++ b/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java @@ -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. @@ -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; @@ -49,6 +48,9 @@ public final class GenericXmlHandler extends DefaultXmlPipe { 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( @@ -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. + *

+ * Default value: {@value EMIT_NAMESPACE} + * + * @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"); @@ -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); } diff --git a/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java b/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java index 71c88e342..595118409 100644 --- a/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java +++ b/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java @@ -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. @@ -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"); + } }