Skip to content

Commit

Permalink
Fix #232: access xml attributes by name not by index
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cboehme committed Jun 1, 2015
1 parent 9a7ad30 commit 5244968
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* A marc xml reader.
* @author Markus Michael Geipel
*
*
*/
@Description("A marc xml reader")
@In(XmlReceiver.class)
Expand All @@ -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));
Expand All @@ -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());

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 5244968

Please sign in to comment.