From 52de5e375e8643380d0a69b558c174d889452c5b Mon Sep 17 00:00:00 2001 From: Philipp Bumann Date: Wed, 11 Sep 2024 13:36:51 +0200 Subject: [PATCH 1/4] Fix for default namespace overwritten by empty namespace Fixed a few SonarLint warnings --- .../java/io/gdcc/xoai/xml/EchoElement.java | 2 +- .../gdcc/xoai/tests/util/EchoElementTest.java | 48 +++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java b/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java index 88fb7b10..eb8680b2 100644 --- a/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java +++ b/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java @@ -105,7 +105,7 @@ private void addNamespaceIfRequired(XmlWriter writer, QName name) throws XMLStre // Search for namespace in scope, starting from the root. for (Set ancestorNamespaces : declaredPrefixes) { if (ancestorNamespaces.contains( - name.getPrefix() + name.getNamespaceURI())) { // Prefixes might be reused. + name.getPrefix() + name.getNamespaceURI()) || name.getNamespaceURI().isBlank()) { // Prefixes might be reused. return; } } diff --git a/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java b/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java index 7f037318..362ef374 100644 --- a/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java +++ b/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java @@ -5,7 +5,6 @@ import io.gdcc.xoai.xml.EchoElement; import io.gdcc.xoai.xml.XmlWriter; -import io.gdcc.xoai.xmlio.exceptions.XmlWriteException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -22,7 +21,7 @@ public class EchoElementTest { */ @Test public void handleEarlyNamespaceDeclarations() - throws XMLStreamException, XmlWriteException, IOException { + throws XMLStreamException { String xml = "" + + "\n" + + " \n" + + " \n" + + " \n" + + " Titel\n" + + " \n" + + " \n" + + " Published by\n" + + " Organisation\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Organisation\n" + + " \n" + + " \n" + + " 2022-11-30\n" + + " Handschriftenportal\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + String result = echoXml(xml); + + assertThat("EchoElement does not add empty namespaces", result, equalTo(xml)); + } + + private static String echoXml(String xml) throws XMLStreamException { final ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); From ff81f73f742047d6b59f745dad9107160f3c821e Mon Sep 17 00:00:00 2001 From: Philipp Bumann Date: Wed, 11 Sep 2024 17:34:36 +0200 Subject: [PATCH 2/4] Reformatting code with spotless --- .../java/io/gdcc/xoai/xml/EchoElement.java | 4 +- .../gdcc/xoai/tests/util/EchoElementTest.java | 70 +++++++++---------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java b/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java index eb8680b2..6aefe98a 100644 --- a/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java +++ b/xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java @@ -104,8 +104,8 @@ private void write(final XmlWriter writer, final InputStream inStream) private void addNamespaceIfRequired(XmlWriter writer, QName name) throws XMLStreamException { // Search for namespace in scope, starting from the root. for (Set ancestorNamespaces : declaredPrefixes) { - if (ancestorNamespaces.contains( - name.getPrefix() + name.getNamespaceURI()) || name.getNamespaceURI().isBlank()) { // Prefixes might be reused. + if (ancestorNamespaces.contains(name.getPrefix() + name.getNamespaceURI()) + || name.getNamespaceURI().isBlank()) { // Prefixes might be reused. return; } } diff --git a/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java b/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java index 362ef374..b5caabdd 100644 --- a/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java +++ b/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java @@ -20,8 +20,7 @@ public class EchoElementTest { * are likely to be used later. */ @Test - public void handleEarlyNamespaceDeclarations() - throws XMLStreamException { + public void handleEarlyNamespaceDeclarations() throws XMLStreamException { String xml = "" - + "\n" - + " \n" - + " \n" - + " \n" - + " Titel\n" - + " \n" - + " \n" - + " Published by\n" - + " Organisation\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " Organisation\n" - + " \n" - + " \n" - + " 2022-11-30\n" - + " Handschriftenportal\n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + " \n" - + ""; + // given + String xml = + "\n" + + " \n" + + " \n" + + " \n" + + " Titel\n" + + " \n" + + " \n" + + " Published by\n" + + " Organisation\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Organisation\n" + + " \n" + + " \n" + + " 2022-11-30\n" + + " Handschriftenportal\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; String result = echoXml(xml); From 0ddf771c392773f99d9658d35bb61cecbab2d1b9 Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Thu, 12 Sep 2024 10:11:28 +0200 Subject: [PATCH 3/4] chore(deps): update Maven pre-commit hook --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e4217cb7..83635d37 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,6 @@ repos: - id: check-json - id: check-added-large-files - repo: https://github.com/ejba/pre-commit-maven - rev: v0.3.3 + rev: v0.3.4 hooks: - id: maven-spotless-apply From 26a8e79c1b986291597bed6ee20c52e199de5705 Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Thu, 12 Sep 2024 10:11:50 +0200 Subject: [PATCH 4/4] style: rearrange linebreaks in EchoElementTest --- .../test/java/io/gdcc/xoai/tests/util/EchoElementTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java b/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java index b5caabdd..0509702d 100644 --- a/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java +++ b/xoai-common/src/test/java/io/gdcc/xoai/tests/util/EchoElementTest.java @@ -103,8 +103,8 @@ public void copyFromInputStream() throws XMLStreamException, IOException { public void defaultNamespaceDeclaration() throws XMLStreamException { // given String xml = - "\n" + "" + + "\n" + " \n" + " \n"