From 44a73667072b6e5758e7c4d1145a8156daa63bb5 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Tue, 24 Aug 2021 14:11:47 +0200 Subject: [PATCH 1/3] Allow namespace prefixes to be emitted This Introduces the new parameter "emitNamespace" to GenericXMLHandler. If set to "true" input data like "" will be passed through as "foo:bar". For backward compatibility the default is set to "false", thus only "bar" is emitted. See #377. --- .../metafacture/xml/GenericXmlHandler.java | 24 +++++++++++++++++-- .../xml/GenericXMLHandlerTest.java | 11 ++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) 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..4fa656730 100644 --- a/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java +++ b/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java @@ -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,8 @@ public final class GenericXmlHandler extends DefaultXmlPipe { private boolean inRecord; private StringBuilder valueBuffer = new StringBuilder(); + private boolean emitNamespace = false; + public GenericXmlHandler() { super(); final String recordTagNameProperty = System.getProperty( @@ -91,13 +92,32 @@ public String getRecordTagName() { return recordTagName; } + /** + * On entity level namespaces can be emitted, e.g.: "foo:bar". The default is to + * ignore the namespace so that only "bar" is emitted. + * + * @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"); 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..2cc65c97e 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,13 @@ public void shouldEmitPCDataAsALiteralNamedValue() { ordered.verify(receiver).literal("value", "char-data"); } + @Test + public void shouldEmitNamespaceOnEntityElement() { + genericXmlHandler.setEmitNamespace(true); + genericXmlHandler.startElement("", "record", "record", attributes); + genericXmlHandler.startElement("", "entity", "ns:entity", attributes); + + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startEntity("ns:entity"); + } } From ca6ab414894d9e100e9574e30b992c88b6fe7bf1 Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Tue, 24 Aug 2021 15:14:28 +0200 Subject: [PATCH 2/3] Allow namespace prefixes of attributes to be emitted Complements 44a73667072b6e5758e7c4d1145a8156daa63bb5. See #377. --- .../main/java/org/metafacture/xml/GenericXmlHandler.java | 6 +++++- .../java/org/metafacture/xml/GenericXMLHandlerTest.java | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) 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 4fa656730..2a28bfdc6 100644 --- a/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java +++ b/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java @@ -165,7 +165,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 2cc65c97e..595118409 100644 --- a/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java +++ b/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java @@ -132,12 +132,14 @@ public void shouldEmitPCDataAsALiteralNamedValue() { } @Test - public void shouldEmitNamespaceOnEntityElement() { + 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"); } } From 6c175d625796d7f14088bccd83f64401e5f3ceda Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Tue, 24 Aug 2021 17:01:10 +0200 Subject: [PATCH 3/3] Improve inline documentation See #377. --- .../java/org/metafacture/xml/GenericXmlHandler.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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 2a28bfdc6..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. @@ -48,7 +48,8 @@ public final class GenericXmlHandler extends DefaultXmlPipe { private boolean inRecord; private StringBuilder valueBuffer = new StringBuilder(); - private boolean emitNamespace = false; + public static final boolean EMIT_NAMESPACE = false; + private boolean emitNamespace = EMIT_NAMESPACE; public GenericXmlHandler() { super(); @@ -93,8 +94,11 @@ public String getRecordTagName() { } /** - * On entity level namespaces can be emitted, e.g.: "foo:bar". The default is to - * ignore the namespace so that only "bar" is emitted. + * 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".