From 52449689d8c04672e03734c9c976a422ae004ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=B6hme?= Date: Mon, 1 Jun 2015 13:19:44 +0200 Subject: [PATCH] Fix #232: access xml attributes by name not by index Changes the `MarcXmlHandler` to access the attributes of the `controlfield` element by name and not by index as this makes it depending on the attribute order. --- .../stream/converter/xml/MarcXmlHandler.java | 12 ++-- .../converter/xml/MarcXmlHandlerTest.java | 71 +++++++++++++++++++ 2 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandlerTest.java diff --git a/src/main/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandler.java b/src/main/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandler.java index e7c3b3d0a..dc2f8faec 100644 --- a/src/main/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandler.java +++ b/src/main/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandler.java @@ -28,7 +28,7 @@ /** * A marc xml reader. * @author Markus Michael Geipel - * + * */ @Description("A marc xml reader") @In(XmlReceiver.class) @@ -55,7 +55,7 @@ public void startElement(final String uri, final String localName, final String getReceiver().startEntity(attributes.getValue("tag") + attributes.getValue("ind1") + attributes.getValue("ind2")); }else if(CONTROLFIELD.equals(localName)){ builder = new StringBuilder(); - currentTag = attributes.getValue(0); + currentTag = attributes.getValue("tag"); }else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){ getReceiver().startRecord(""); getReceiver().literal(TYPE, attributes.getValue(TYPE)); @@ -69,18 +69,18 @@ public void startElement(final String uri, final String localName, final String public void endElement(final String uri, final String localName, final String qName) throws SAXException { if(SUBFIELD.equals(localName)){ getReceiver().literal(currentTag, builder.toString().trim()); - + }else if(DATAFIELD.equals(localName)){ getReceiver().endEntity(); }else if(CONTROLFIELD.equals(localName)){ getReceiver().literal(currentTag, builder.toString().trim()); - + }else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){ getReceiver().endRecord(); - + }else if(LEADER.equals(localName)){ getReceiver().literal(currentTag, builder.toString().trim()); - + } } diff --git a/src/test/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandlerTest.java b/src/test/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandlerTest.java new file mode 100644 index 000000000..60ef89704 --- /dev/null +++ b/src/test/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandlerTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 Deutsche Nationalbibliothek + * + * Licensed under the Apache License, Version 2.0 the "License"; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.culturegraph.mf.stream.converter.xml; + +import static org.mockito.Mockito.verify; + +import org.culturegraph.mf.framework.StreamReceiver; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; + +/** + * @author Christoph Böhme + * + */ +public final class MarcXmlHandlerTest { + + private static final String CONTROLFIELD = "controlfield"; + private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim"; + + private MarcXmlHandler marcXmlHandler; + + @Mock + private StreamReceiver receiver; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + marcXmlHandler = new MarcXmlHandler(); + marcXmlHandler.setReceiver(receiver); + } + + @After + public void cleanup() { + marcXmlHandler.closeStream(); + } + + @Test + public void shouldFindTagAttributeAtSecondPositionInControlFieldElement() + throws SAXException { + final AttributesImpl attributes = new AttributesImpl(); + attributes.addAttribute(NAMESPACE, "id", "id", "CDATA", "id-1"); + attributes.addAttribute(NAMESPACE, "tag", "tag", "CDATA", "001"); + + final String fieldValue = "1234"; + + marcXmlHandler.startElement(NAMESPACE, CONTROLFIELD, "", attributes); + marcXmlHandler.characters(fieldValue.toCharArray(), 0, fieldValue.length()); + marcXmlHandler.endElement(NAMESPACE, CONTROLFIELD, ""); + + verify(receiver).literal("001", fieldValue); + } + +}