Skip to content

Commit

Permalink
Make MARC21 XML handler attribute marker configurable.
Browse files Browse the repository at this point in the history
Related to: #336, #379
  • Loading branch information
blackwinter committed Sep 24, 2021
1 parent 70fefd5 commit d2b0bdc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
7 changes: 7 additions & 0 deletions metafacture-biblio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.5.5'
}

test {
testLogging {
showStandardStreams = true
exceptionFormat = 'full'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@
@FluxCommand("handle-marcxml")
public final class MarcXmlHandler extends DefaultXmlPipe<StreamReceiver> {

public static final String DEFAULT_ATTRIBUTE_MARKER = "";

private static final String SUBFIELD = "subfield";
private static final String DATAFIELD = "datafield";
private static final String CONTROLFIELD = "controlfield";
private static final String RECORD = "record";
private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim";
private static final String LEADER = "leader";
private static final String TYPE = "type";

private String attributeMarker = DEFAULT_ATTRIBUTE_MARKER;
private String currentTag = "";
private String namespace = NAMESPACE;
private StringBuilder builder = new StringBuilder();
Expand All @@ -60,6 +64,14 @@ private boolean checkNamespace(final String uri) {
return namespace == null || namespace.equals(uri);
}

public void setAttributeMarker(final String attributeMarker) {
this.attributeMarker = attributeMarker;
}

public String getAttributeMarker() {
return attributeMarker;
}

@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
if (SUBFIELD.equals(localName)) {
Expand All @@ -75,7 +87,7 @@ else if (CONTROLFIELD.equals(localName)) {
}
else if (RECORD.equals(localName) && checkNamespace(uri)) {
getReceiver().startRecord("");
getReceiver().literal(TYPE, attributes.getValue(TYPE));
getReceiver().literal(attributeMarker + TYPE, attributes.getValue(TYPE));
}
else if (LEADER.equals(localName)) {
builder = new StringBuilder();
Expand All @@ -87,18 +99,15 @@ else if (LEADER.equals(localName)) {
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) && checkNamespace(uri)) {
getReceiver().endRecord();

}
else if (LEADER.equals(localName)) {
getReceiver().literal(currentTag, builder.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.junit.Before;
import org.junit.Test;
import org.metafacture.framework.StreamReceiver;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
Expand Down Expand Up @@ -130,4 +132,39 @@ public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace()
verifyNoMoreInteractions(receiver);
}

@Test
public void shouldNotEncodeTypeAttributeAsMarkedLiteral() throws SAXException {
final AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(NAMESPACE, "type", "type", "CDATA", "bibliographic");

marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes);
marcXmlHandler.endElement(NAMESPACE, RECORD, "");

final InOrder ordered = Mockito.inOrder(receiver);
ordered.verify(receiver).startRecord("");
ordered.verify(receiver).literal(TYPE, "bibliographic");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
verifyNoMoreInteractions(receiver);
}

@Test
public void issue336_shouldEncodeTypeAttributeAsLiteralWithConfiguredMarker() throws SAXException {
final String marker = "~";
marcXmlHandler.setAttributeMarker(marker);

final AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(NAMESPACE, "type", "type", "CDATA", "bibliographic");

marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes);
marcXmlHandler.endElement(NAMESPACE, RECORD, "");

final InOrder ordered = Mockito.inOrder(receiver);
ordered.verify(receiver).startRecord("");
ordered.verify(receiver).literal(marker + TYPE, "bibliographic");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
verifyNoMoreInteractions(receiver);
}

}

0 comments on commit d2b0bdc

Please sign in to comment.