Skip to content

Commit

Permalink
Merge branch 'blackwinter-330-marcxml-record-namespace'
Browse files Browse the repository at this point in the history
See #331.
  • Loading branch information
dr0i committed Sep 8, 2020
2 parents dd1a9e9 + ba81e9f commit 2528980
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 2528980

Please sign in to comment.