diff --git a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlHandler.java b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlHandler.java index b0333faf..396223ef 100644 --- a/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlHandler.java +++ b/metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlHandler.java @@ -32,7 +32,7 @@ * @author Markus Michael Geipel * */ -@Description("A MARC XML reader. To read marc data without namespace specification set option `namespace=\"\"`") +@Description("A MARC XML reader. To read marc data without namespace specification set option `namespace=\"\"`. To ignore namespace specification set option `ignorenamespace=\"true\".") @In(XmlReceiver.class) @Out(StreamReceiver.class) @FluxCommand("handle-marcxml") @@ -70,6 +70,19 @@ public void setNamespace(final String namespace) { this.namespace = namespace; } + /** + * Sets whether to ignore the namespace. + * + * Default value: false + * + * @param ignoreNamespace true if the namespace should be ignored + */ + public void setIgnoreNamespace(final boolean ignoreNamespace) { + if (ignoreNamespace) { + this.namespace = null; + } + } + private boolean checkNamespace(final String uri) { return namespace == null || namespace.equals(uri); } diff --git a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlHandlerTest.java b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlHandlerTest.java index 8a9f8c86..7521e2b3 100644 --- a/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlHandlerTest.java +++ b/metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlHandlerTest.java @@ -146,6 +146,63 @@ public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace() verifyNoMoreInteractions(receiver); } + @Test + public void shouldRecognizeRecordsWithoutNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.setNamespace(""); + marcXmlHandler.startElement("", RECORD, "", attributes); + marcXmlHandler.endElement("", RECORD, ""); + + verify(receiver).startRecord(""); + verify(receiver).literal(TYPE, null); + verify(receiver).endRecord(); + + verifyNoMoreInteractions(receiver); + } + @Test + public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.setNamespace(""); + marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes); + marcXmlHandler.endElement(NAMESPACE, RECORD, ""); + + verifyNoMoreInteractions(receiver); + } + + @Test + public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.setIgnoreNamespace(true); + marcXmlHandler.startElement(null, RECORD, "", attributes); + marcXmlHandler.endElement(NAMESPACE, RECORD, ""); + + verify(receiver).startRecord(""); + verify(receiver).literal(TYPE, null); + verify(receiver).endRecord(); + + verifyNoMoreInteractions(receiver); + } + + @Test + public void issue569ShouldNotRecognizeRecordsWithAndWithoutNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.setIgnoreNamespace(false); + marcXmlHandler.startElement(null, RECORD, "", attributes); + marcXmlHandler.endElement(NAMESPACE, RECORD, ""); + + verify(receiver).endRecord(); + + verifyNoMoreInteractions(receiver); + } + @Test public void shouldNotEncodeTypeAttributeAsMarkedLiteral() throws SAXException { final AttributesImpl attributes = new AttributesImpl();