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 d3fb82959..6f1264140 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 @@ -45,8 +45,17 @@ public final class MarcXmlHandler extends DefaultXmlPipe { private static final String LEADER = "leader"; private static final String TYPE = "type"; private String currentTag = ""; + private String namespace = NAMESPACE; private StringBuilder builder = new StringBuilder(); + public void setNamespace(final String namespace) { + this.namespace = namespace; + } + + private boolean checkNamespace(final String uri) { + return namespace == null || namespace.equals(uri); + } + @Override public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { @@ -58,7 +67,7 @@ public void startElement(final String uri, final String localName, final String }else if(CONTROLFIELD.equals(localName)){ builder = new StringBuilder(); currentTag = attributes.getValue("tag"); - }else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){ + }else if(RECORD.equals(localName) && checkNamespace(uri)){ getReceiver().startRecord(""); getReceiver().literal(TYPE, attributes.getValue(TYPE)); }else if(LEADER.equals(localName)){ @@ -77,7 +86,7 @@ public void endElement(final String uri, final String localName, final String qN }else if(CONTROLFIELD.equals(localName)){ getReceiver().literal(currentTag, builder.toString().trim()); - }else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){ + }else if(RECORD.equals(localName) && checkNamespace(uri)){ getReceiver().endRecord(); }else if(LEADER.equals(localName)){ 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 af6409f8b..e74d38827 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 @@ -16,6 +16,7 @@ package org.metafacture.biblio.marc21; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import org.junit.After; import org.junit.Before; @@ -37,6 +38,8 @@ public final class MarcXmlHandlerTest { private static final String LEADER = "leader"; private static final String CONTROLFIELD = "controlfield"; private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim"; + private static final String RECORD = "record"; + private static final String TYPE = "type"; private MarcXmlHandler marcXmlHandler; @@ -84,4 +87,46 @@ public void issue233ShouldNotRemoveWhitespaceFromLeader() verify(receiver).literal("leader", leaderValue); } + @Test + public void shouldRecognizeRecordsWithNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes); + marcXmlHandler.endElement(NAMESPACE, RECORD, ""); + + verify(receiver).startRecord(""); + verify(receiver).literal(TYPE, null); + verify(receiver).endRecord(); + + verifyNoMoreInteractions(receiver); + } + + @Test + public void shouldNotRecognizeRecordsWithoutNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.startElement(null, RECORD, "", attributes); + marcXmlHandler.endElement(null, RECORD, ""); + + verifyNoMoreInteractions(receiver); + } + + @Test + public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + + marcXmlHandler.setNamespace(null); + marcXmlHandler.startElement(null, RECORD, "", attributes); + marcXmlHandler.endElement(null, RECORD, ""); + + verify(receiver).startRecord(""); + verify(receiver).literal(TYPE, null); + verify(receiver).endRecord(); + + verifyNoMoreInteractions(receiver); + } + }