Skip to content

Commit

Permalink
Merge #575 from remote-tracking branch 'origin/569-ignoreNamespace'
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Dec 2, 2024
2 parents 328afe8 + 8305caf commit 9073421
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author Markus Michael Geipel
*
*/
@Description("A MARC XML reader. To read marc data without namespace specification set option `namespace=\"\"`")
@Description("A MARC XML reader. To read marc data without namespace specification set option `namespace=\"\"`. To ignore namespace specification set option `ignorenamespace=\"true\".")
@In(XmlReceiver.class)
@Out(StreamReceiver.class)
@FluxCommand("handle-marcxml")
Expand All @@ -51,6 +51,7 @@ public final class MarcXmlHandler extends DefaultXmlPipe<StreamReceiver> {
private String currentTag = "";
private String namespace = NAMESPACE;
private StringBuilder builder = new StringBuilder();
private boolean ignoreNamespace;

/**
* Creates an instance of {@link MarcXmlHandler}.
Expand All @@ -70,8 +71,19 @@ public void setNamespace(final String namespace) {
this.namespace = namespace;
}

/**
* Sets whether to ignore the namespace.
*
* <strong>Default value: false</strong>
*
* @param ignoreNamespace true if the namespace should be ignored
*/
public void setIgnoreNamespace(final boolean ignoreNamespace) {
this.ignoreNamespace = ignoreNamespace;
}

private boolean checkNamespace(final String uri) {
return namespace == null || namespace.equals(uri);
return namespace == null || ignoreNamespace || namespace.equals(uri);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,81 @@ public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace()
verifyNoMoreInteractions(receiver);
}

@Test
public void shouldRecognizeRecordsWithoutNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

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

verify(receiver).startRecord("");
verify(receiver).literal(TYPE, null);
verify(receiver).endRecord();

verifyNoMoreInteractions(receiver);
}

@Test
public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

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

verifyNoMoreInteractions(receiver);
}

@Test
public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

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

verify(receiver).startRecord("");
verify(receiver).literal(TYPE, null);
verify(receiver).endRecord();

verifyNoMoreInteractions(receiver);
}

@Test
public void issue569ShouldRecognizeRecordsWithAndWithoutNamespaceOrderIndependently()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

marcXmlHandler.setIgnoreNamespace(true);
marcXmlHandler.setNamespace("");
marcXmlHandler.startElement(null, RECORD, "", attributes);
marcXmlHandler.endElement(NAMESPACE, RECORD, "");

verify(receiver).startRecord("");
verify(receiver).literal(TYPE, null);
verify(receiver).endRecord();

verifyNoMoreInteractions(receiver);
}

@Test
public void issue569ShouldNotRecognizeRecordsWithAndWithoutNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

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

verify(receiver).endRecord();

verifyNoMoreInteractions(receiver);
}

@Test
public void shouldNotEncodeTypeAttributeAsMarkedLiteral() throws SAXException {
final AttributesImpl attributes = new AttributesImpl();
Expand Down

0 comments on commit 9073421

Please sign in to comment.