From 70fefd5e60ff41656b3acfe94d9be091127e69d3 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Fri, 24 Sep 2021 19:24:46 +0200 Subject: [PATCH] Make generic XML handler attribute marker configurable. Fixes #379. --- .../metafacture/xml/GenericXmlHandler.java | 13 +++++- .../xml/GenericXMLHandlerTest.java | 44 ++++++++++++++++++- 2 files changed, 55 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 abc3aaeb9..1b3b06670 100644 --- a/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java +++ b/metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java @@ -40,12 +40,15 @@ @FluxCommand("handle-generic-xml") public final class GenericXmlHandler extends DefaultXmlPipe { + public static final String DEFAULT_ATTRIBUTE_MARKER = ""; + public static final String DEFAULT_RECORD_TAG = "record"; public static final boolean EMIT_NAMESPACE = false; private static final Pattern TABS = Pattern.compile("\t+"); + private String attributeMarker = DEFAULT_ATTRIBUTE_MARKER; private String recordTagName = DEFAULT_RECORD_TAG; private boolean inRecord; @@ -110,6 +113,14 @@ public boolean getEmitNamespace() { return this.emitNamespace; } + public void setAttributeMarker(final String attributeMarker) { + this.attributeMarker = attributeMarker; + } + + public String getAttributeMarker() { + return attributeMarker; + } + @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) { if (inRecord) { @@ -170,7 +181,7 @@ private void writeAttributes(final Attributes attributes) { for (int i = 0; i < length; ++i) { final String name = emitNamespace ? attributes.getQName(i) : attributes.getLocalName(i); final String value = attributes.getValue(i); - getReceiver().literal(name, value); + getReceiver().literal(attributeMarker + 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 e376c24b1..7ba0a47f3 100644 --- a/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java +++ b/metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java @@ -25,6 +25,7 @@ import org.metafacture.framework.StreamReceiver; import org.mockito.InOrder; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.xml.sax.helpers.AttributesImpl; @@ -141,6 +142,47 @@ public void shouldEmitNamespaceOnEntityElementAndAttribute() { final InOrder ordered = inOrder(receiver); ordered.verify(receiver).startEntity("ns:entity"); - ordered.verify(receiver).literal("ns:attr","attr-value"); + ordered.verify(receiver).literal("ns:attr", "attr-value"); } + + @Test + public void shouldNotEncodeAttributesAsMarkedLiterals() { + attributes.addAttribute("", "attr", "attr", "CDATA", "attr-value"); + genericXmlHandler.startElement("", "record", "record", attributes); + genericXmlHandler.endElement("", "record", "record"); + + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startRecord(""); + ordered.verify(receiver).literal("attr", "attr-value"); + ordered.verify(receiver).endRecord(); + ordered.verifyNoMoreInteractions(); + Mockito.verifyNoMoreInteractions(receiver); + } + + @Test + public void issue379_shouldEncodeAttributesAsLiteralsWithConfiguredMarker() { + final String marker = "~"; + genericXmlHandler.setAttributeMarker(marker); + + genericXmlHandler.startElement("", "record", "record", attributes); + attributes.addAttribute("", "authority", "authority", "CDATA", "marcrelator"); + attributes.addAttribute("", "type", "type", "CDATA", "text"); + genericXmlHandler.startElement("", "roleTerm", "roleTerm", attributes); + final char[] charData = "Author".toCharArray(); + genericXmlHandler.characters(charData, 0, charData.length); + genericXmlHandler.endElement("", "roleTerm", "roleTerm"); + genericXmlHandler.endElement("", "record", "record"); + + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startRecord(""); + ordered.verify(receiver).startEntity("roleTerm"); + ordered.verify(receiver).literal(marker + "authority", "marcrelator"); + ordered.verify(receiver).literal(marker + "type", "text"); + ordered.verify(receiver).literal("value", "Author"); + ordered.verify(receiver).endEntity(); + ordered.verify(receiver).endRecord(); + ordered.verifyNoMoreInteractions(); + Mockito.verifyNoMoreInteractions(receiver); + } + }