Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option "ignorenamespace" #575

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
blackwinter marked this conversation as resolved.
Show resolved Hide resolved
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