Skip to content

Commit 5244968

Browse files
committed
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.
1 parent 9a7ad30 commit 5244968

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/main/java/org/culturegraph/mf/stream/converter/xml/MarcXmlHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* A marc xml reader.
3030
* @author Markus Michael Geipel
31-
*
31+
*
3232
*/
3333
@Description("A marc xml reader")
3434
@In(XmlReceiver.class)
@@ -55,7 +55,7 @@ public void startElement(final String uri, final String localName, final String
5555
getReceiver().startEntity(attributes.getValue("tag") + attributes.getValue("ind1") + attributes.getValue("ind2"));
5656
}else if(CONTROLFIELD.equals(localName)){
5757
builder = new StringBuilder();
58-
currentTag = attributes.getValue(0);
58+
currentTag = attributes.getValue("tag");
5959
}else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){
6060
getReceiver().startRecord("");
6161
getReceiver().literal(TYPE, attributes.getValue(TYPE));
@@ -69,18 +69,18 @@ public void startElement(final String uri, final String localName, final String
6969
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
7070
if(SUBFIELD.equals(localName)){
7171
getReceiver().literal(currentTag, builder.toString().trim());
72-
72+
7373
}else if(DATAFIELD.equals(localName)){
7474
getReceiver().endEntity();
7575
}else if(CONTROLFIELD.equals(localName)){
7676
getReceiver().literal(currentTag, builder.toString().trim());
77-
77+
7878
}else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){
7979
getReceiver().endRecord();
80-
80+
8181
}else if(LEADER.equals(localName)){
8282
getReceiver().literal(currentTag, builder.toString().trim());
83-
83+
8484
}
8585
}
8686

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2014 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.stream.converter.xml;
17+
18+
import static org.mockito.Mockito.verify;
19+
20+
import org.culturegraph.mf.framework.StreamReceiver;
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
26+
import org.xml.sax.SAXException;
27+
import org.xml.sax.helpers.AttributesImpl;
28+
29+
/**
30+
* @author Christoph Böhme
31+
*
32+
*/
33+
public final class MarcXmlHandlerTest {
34+
35+
private static final String CONTROLFIELD = "controlfield";
36+
private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim";
37+
38+
private MarcXmlHandler marcXmlHandler;
39+
40+
@Mock
41+
private StreamReceiver receiver;
42+
43+
@Before
44+
public void setup() {
45+
MockitoAnnotations.initMocks(this);
46+
marcXmlHandler = new MarcXmlHandler();
47+
marcXmlHandler.setReceiver(receiver);
48+
}
49+
50+
@After
51+
public void cleanup() {
52+
marcXmlHandler.closeStream();
53+
}
54+
55+
@Test
56+
public void shouldFindTagAttributeAtSecondPositionInControlFieldElement()
57+
throws SAXException {
58+
final AttributesImpl attributes = new AttributesImpl();
59+
attributes.addAttribute(NAMESPACE, "id", "id", "CDATA", "id-1");
60+
attributes.addAttribute(NAMESPACE, "tag", "tag", "CDATA", "001");
61+
62+
final String fieldValue = "1234";
63+
64+
marcXmlHandler.startElement(NAMESPACE, CONTROLFIELD, "", attributes);
65+
marcXmlHandler.characters(fieldValue.toCharArray(), 0, fieldValue.length());
66+
marcXmlHandler.endElement(NAMESPACE, CONTROLFIELD, "");
67+
68+
verify(receiver).literal("001", fieldValue);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)