Skip to content

Commit

Permalink
Make MARCXML namespace for record elements configurable.
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
blackwinter committed Sep 7, 2020
1 parent dd1a9e9 commit ba81e9f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ public final class MarcXmlHandler extends DefaultXmlPipe<StreamReceiver> {
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 {
Expand All @@ -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)){
Expand All @@ -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)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

}

0 comments on commit ba81e9f

Please sign in to comment.