Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for default namespace overwritten by empty namespace #241

Merged
merged 4 commits into from
Sep 12, 2024
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions xoai-common/src/main/java/io/gdcc/xoai/xml/EchoElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> ancestorNamespaces : declaredPrefixes) {
if (ancestorNamespaces.contains(
name.getPrefix() + name.getNamespaceURI())) { // Prefixes might be reused.
if (ancestorNamespaces.contains(name.getPrefix() + name.getNamespaceURI())
|| name.getNamespaceURI().isBlank()) { // Prefixes might be reused.
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,8 +20,7 @@ public class EchoElementTest {
* are likely to be used later.
*/
@Test
public void handleEarlyNamespaceDeclarations()
throws XMLStreamException, XmlWriteException, IOException {
public void handleEarlyNamespaceDeclarations() throws XMLStreamException {
String xml =
"<?xml version='1.0' encoding='UTF-8'?><oai_dc:dc"
+ " xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\""
Expand All @@ -46,8 +44,7 @@ public void handleEarlyNamespaceDeclarations()
* a namespace declaration.
*/
@Test
public void repeatingNamespaceDeclarations()
throws XMLStreamException, XmlWriteException, IOException {
public void repeatingNamespaceDeclarations() throws XMLStreamException {
String xml =
"<?xml version='1.0' encoding='UTF-8'?><oai_dc:dc"
+ " xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\""
Expand All @@ -69,7 +66,7 @@ public void repeatingNamespaceDeclarations()
}

@Test
public void copyFromInputStream() throws XMLStreamException, XmlWriteException, IOException {
public void copyFromInputStream() throws XMLStreamException, IOException {
// given
String xml =
"<?xml version='1.0' encoding='UTF-8'?><oai_dc:dc"
Expand Down Expand Up @@ -102,7 +99,48 @@ public void copyFromInputStream() throws XMLStreamException, XmlWriteException,
assertThat("EchoElement handles InputStream", result, equalTo(xml));
}

private static String echoXml(String xml) throws XmlWriteException, XMLStreamException {
@Test
public void defaultNamespaceDeclaration() throws XMLStreamException {
// given
String xml =
"<?xml version='1.0' encoding='UTF-8'?>"
+ "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\n"
+ " <teiHeader xmlns:xml=\"http://www.w3.org/XML/1998/namespace\""
+ " xml:lang=\"de\">\n"
+ " <fileDesc>\n"
+ " <titleStmt>\n"
+ " <title>Titel\n"
+ " </title>\n"
+ " <respStmt>\n"
+ " <resp>Published by</resp>\n"
+ " <name type=\"org\">Organisation</name>\n"
+ " </respStmt>\n"
+ " </titleStmt>\n"
+ " <publicationStmt>\n"
+ " <publisher>\n"
+ " <name type=\"org\">Organisation</name>\n"
+ " <ptr target=\"http://www.organisation.org\"/>\n"
+ " </publisher>\n"
+ " <date when=\"2022-11-30\" type=\"issued\">2022-11-30</date>\n"
+ " <distributor>Handschriftenportal</distributor>\n"
+ " <availability status=\"free\">\n"
+ " <licence"
+ " target=\"https://creativecommons.org/publicdomain/zero/1.0/deed.de\">\n"
+ " </licence>\n"
+ " </availability>\n"
+ " <pubPlace>\n"
+ " </pubPlace>\n"
+ " </publicationStmt>\n"
+ " </fileDesc>\n"
+ " </teiHeader>\n"
+ "</TEI>";

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();

Expand Down
Loading