Skip to content

Commit ba81e9f

Browse files
committed
Make MARCXML namespace for record elements configurable.
Not all MARCXML sources specify the required namespace URI (notably, Alma General Publishing). By allowing the expected namespace to be configured, or even skipped (by setting it to `null`), this will resolve #330. The record element is the only element that is restricted by namespace; however, the original commit [1] doesn't provide any context as to why the change was introduced. [1] https://sourceforge.net/p/culturegraph/code/1507/
1 parent dd1a9e9 commit ba81e9f

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlHandler.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ public final class MarcXmlHandler extends DefaultXmlPipe<StreamReceiver> {
4545
private static final String LEADER = "leader";
4646
private static final String TYPE = "type";
4747
private String currentTag = "";
48+
private String namespace = NAMESPACE;
4849
private StringBuilder builder = new StringBuilder();
4950

51+
public void setNamespace(final String namespace) {
52+
this.namespace = namespace;
53+
}
54+
55+
private boolean checkNamespace(final String uri) {
56+
return namespace == null || namespace.equals(uri);
57+
}
58+
5059
@Override
5160
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
5261
throws SAXException {
@@ -58,7 +67,7 @@ public void startElement(final String uri, final String localName, final String
5867
}else if(CONTROLFIELD.equals(localName)){
5968
builder = new StringBuilder();
6069
currentTag = attributes.getValue("tag");
61-
}else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){
70+
}else if(RECORD.equals(localName) && checkNamespace(uri)){
6271
getReceiver().startRecord("");
6372
getReceiver().literal(TYPE, attributes.getValue(TYPE));
6473
}else if(LEADER.equals(localName)){
@@ -77,7 +86,7 @@ public void endElement(final String uri, final String localName, final String qN
7786
}else if(CONTROLFIELD.equals(localName)){
7887
getReceiver().literal(currentTag, builder.toString().trim());
7988

80-
}else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){
89+
}else if(RECORD.equals(localName) && checkNamespace(uri)){
8190
getReceiver().endRecord();
8291

8392
}else if(LEADER.equals(localName)){

metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlHandlerTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.metafacture.biblio.marc21;
1717

1818
import static org.mockito.Mockito.verify;
19+
import static org.mockito.Mockito.verifyNoMoreInteractions;
1920

2021
import org.junit.After;
2122
import org.junit.Before;
@@ -37,6 +38,8 @@ public final class MarcXmlHandlerTest {
3738
private static final String LEADER = "leader";
3839
private static final String CONTROLFIELD = "controlfield";
3940
private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim";
41+
private static final String RECORD = "record";
42+
private static final String TYPE = "type";
4043

4144
private MarcXmlHandler marcXmlHandler;
4245

@@ -84,4 +87,46 @@ public void issue233ShouldNotRemoveWhitespaceFromLeader()
8487
verify(receiver).literal("leader", leaderValue);
8588
}
8689

90+
@Test
91+
public void shouldRecognizeRecordsWithNamespace()
92+
throws SAXException {
93+
final AttributesImpl attributes = new AttributesImpl();
94+
95+
marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes);
96+
marcXmlHandler.endElement(NAMESPACE, RECORD, "");
97+
98+
verify(receiver).startRecord("");
99+
verify(receiver).literal(TYPE, null);
100+
verify(receiver).endRecord();
101+
102+
verifyNoMoreInteractions(receiver);
103+
}
104+
105+
@Test
106+
public void shouldNotRecognizeRecordsWithoutNamespace()
107+
throws SAXException {
108+
final AttributesImpl attributes = new AttributesImpl();
109+
110+
marcXmlHandler.startElement(null, RECORD, "", attributes);
111+
marcXmlHandler.endElement(null, RECORD, "");
112+
113+
verifyNoMoreInteractions(receiver);
114+
}
115+
116+
@Test
117+
public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace()
118+
throws SAXException {
119+
final AttributesImpl attributes = new AttributesImpl();
120+
121+
marcXmlHandler.setNamespace(null);
122+
marcXmlHandler.startElement(null, RECORD, "", attributes);
123+
marcXmlHandler.endElement(null, RECORD, "");
124+
125+
verify(receiver).startRecord("");
126+
verify(receiver).literal(TYPE, null);
127+
verify(receiver).endRecord();
128+
129+
verifyNoMoreInteractions(receiver);
130+
}
131+
87132
}

0 commit comments

Comments
 (0)