From 751388543326b0406b8919520de68605cfe5549c Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Thu, 21 Nov 2024 17:23:54 +0100 Subject: [PATCH 1/3] Add option "ignorenamespace" (#569) Setting "ignorenamespace" to "true" ignores checking the namespace. --- .../biblio/marc21/MarcXmlHandler.java | 15 ++++- .../biblio/marc21/MarcXmlHandlerTest.java | 57 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) 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 b0333faf0..396223efd 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 8a9f8c869..7521e2b37 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(); From 57f6a719a05265f485ac4b7bc57634d12bbbb02c Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Mon, 2 Dec 2024 10:27:12 +0100 Subject: [PATCH 2/3] Make "ignorenamespace" order-independent (#569) Even if a namespace is set after setting "ignorenamespace" to "true" the namespace is ignored. --- .../biblio/marc21/MarcXmlHandler.java | 7 +++---- .../biblio/marc21/MarcXmlHandlerTest.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) 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 396223efd..d43b8302d 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 @@ -51,6 +51,7 @@ public final class MarcXmlHandler extends DefaultXmlPipe { private String currentTag = ""; private String namespace = NAMESPACE; private StringBuilder builder = new StringBuilder(); + private boolean ignoreNamespace; /** * Creates an instance of {@link MarcXmlHandler}. @@ -78,13 +79,11 @@ public void setNamespace(final String namespace) { * @param ignoreNamespace true if the namespace should be ignored */ public void setIgnoreNamespace(final boolean ignoreNamespace) { - if (ignoreNamespace) { - this.namespace = null; - } + this.ignoreNamespace = ignoreNamespace; } private boolean checkNamespace(final String uri) { - return namespace == null || namespace.equals(uri); + return namespace == null || ignoreNamespace || 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 7521e2b37..7372680e3 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 @@ -161,6 +161,7 @@ public void shouldRecognizeRecordsWithoutNamespace() verifyNoMoreInteractions(receiver); } + @Test public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace() throws SAXException { @@ -189,6 +190,23 @@ public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace() verifyNoMoreInteractions(receiver); } + @Test + public void issue569ShouldRecognizeRecordsWithAndWithoutNamespaceOrderIndependently() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.setIgnoreNamespace(true); + marcXmlHandler.setNamespace(""); + 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 { From 8305cafd27ab202bff8877a96f478790b83ceed8 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Mon, 2 Dec 2024 10:38:43 +0100 Subject: [PATCH 3/3] Fix indentation (#569) --- .../metafacture/biblio/marc21/MarcXmlHandlerTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 7372680e3..16710f9b7 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 @@ -148,7 +148,7 @@ public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace() @Test public void shouldRecognizeRecordsWithoutNamespace() - throws SAXException { + throws SAXException { final AttributesImpl attributes = new AttributesImpl(); marcXmlHandler.setNamespace(""); @@ -164,7 +164,7 @@ public void shouldRecognizeRecordsWithoutNamespace() @Test public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace() - throws SAXException { + throws SAXException { final AttributesImpl attributes = new AttributesImpl(); marcXmlHandler.setNamespace(""); @@ -176,7 +176,7 @@ public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace @Test public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace() - throws SAXException { + throws SAXException { final AttributesImpl attributes = new AttributesImpl(); marcXmlHandler.setIgnoreNamespace(true); @@ -192,7 +192,7 @@ public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace() @Test public void issue569ShouldRecognizeRecordsWithAndWithoutNamespaceOrderIndependently() - throws SAXException { + throws SAXException { final AttributesImpl attributes = new AttributesImpl(); marcXmlHandler.setIgnoreNamespace(true); @@ -209,7 +209,7 @@ public void issue569ShouldRecognizeRecordsWithAndWithoutNamespaceOrderIndependen @Test public void issue569ShouldNotRecognizeRecordsWithAndWithoutNamespace() - throws SAXException { + throws SAXException { final AttributesImpl attributes = new AttributesImpl(); marcXmlHandler.setIgnoreNamespace(false);