Skip to content

Commit 9073421

Browse files
committed
Merge #575 from remote-tracking branch 'origin/569-ignoreNamespace'
2 parents 328afe8 + 8305caf commit 9073421

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

metafacture-biblio/src/main/java/org/metafacture/biblio/marc21/MarcXmlHandler.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @author Markus Michael Geipel
3333
*
3434
*/
35-
@Description("A MARC XML reader. To read marc data without namespace specification set option `namespace=\"\"`")
35+
@Description("A MARC XML reader. To read marc data without namespace specification set option `namespace=\"\"`. To ignore namespace specification set option `ignorenamespace=\"true\".")
3636
@In(XmlReceiver.class)
3737
@Out(StreamReceiver.class)
3838
@FluxCommand("handle-marcxml")
@@ -51,6 +51,7 @@ public final class MarcXmlHandler extends DefaultXmlPipe<StreamReceiver> {
5151
private String currentTag = "";
5252
private String namespace = NAMESPACE;
5353
private StringBuilder builder = new StringBuilder();
54+
private boolean ignoreNamespace;
5455

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

74+
/**
75+
* Sets whether to ignore the namespace.
76+
*
77+
* <strong>Default value: false</strong>
78+
*
79+
* @param ignoreNamespace true if the namespace should be ignored
80+
*/
81+
public void setIgnoreNamespace(final boolean ignoreNamespace) {
82+
this.ignoreNamespace = ignoreNamespace;
83+
}
84+
7385
private boolean checkNamespace(final String uri) {
74-
return namespace == null || namespace.equals(uri);
86+
return namespace == null || ignoreNamespace || namespace.equals(uri);
7587
}
7688

7789
/**

metafacture-biblio/src/test/java/org/metafacture/biblio/marc21/MarcXmlHandlerTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,81 @@ public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace()
146146
verifyNoMoreInteractions(receiver);
147147
}
148148

149+
@Test
150+
public void shouldRecognizeRecordsWithoutNamespace()
151+
throws SAXException {
152+
final AttributesImpl attributes = new AttributesImpl();
153+
154+
marcXmlHandler.setNamespace("");
155+
marcXmlHandler.startElement("", RECORD, "", attributes);
156+
marcXmlHandler.endElement("", RECORD, "");
157+
158+
verify(receiver).startRecord("");
159+
verify(receiver).literal(TYPE, null);
160+
verify(receiver).endRecord();
161+
162+
verifyNoMoreInteractions(receiver);
163+
}
164+
165+
@Test
166+
public void shouldNotRecognizeRecordsWithNamespaceWhenOptionallyWithoutNamespace()
167+
throws SAXException {
168+
final AttributesImpl attributes = new AttributesImpl();
169+
170+
marcXmlHandler.setNamespace("");
171+
marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes);
172+
marcXmlHandler.endElement(NAMESPACE, RECORD, "");
173+
174+
verifyNoMoreInteractions(receiver);
175+
}
176+
177+
@Test
178+
public void issue569ShouldRecognizeRecordsWithAndWithoutNamespace()
179+
throws SAXException {
180+
final AttributesImpl attributes = new AttributesImpl();
181+
182+
marcXmlHandler.setIgnoreNamespace(true);
183+
marcXmlHandler.startElement(null, RECORD, "", attributes);
184+
marcXmlHandler.endElement(NAMESPACE, RECORD, "");
185+
186+
verify(receiver).startRecord("");
187+
verify(receiver).literal(TYPE, null);
188+
verify(receiver).endRecord();
189+
190+
verifyNoMoreInteractions(receiver);
191+
}
192+
193+
@Test
194+
public void issue569ShouldRecognizeRecordsWithAndWithoutNamespaceOrderIndependently()
195+
throws SAXException {
196+
final AttributesImpl attributes = new AttributesImpl();
197+
198+
marcXmlHandler.setIgnoreNamespace(true);
199+
marcXmlHandler.setNamespace("");
200+
marcXmlHandler.startElement(null, RECORD, "", attributes);
201+
marcXmlHandler.endElement(NAMESPACE, RECORD, "");
202+
203+
verify(receiver).startRecord("");
204+
verify(receiver).literal(TYPE, null);
205+
verify(receiver).endRecord();
206+
207+
verifyNoMoreInteractions(receiver);
208+
}
209+
210+
@Test
211+
public void issue569ShouldNotRecognizeRecordsWithAndWithoutNamespace()
212+
throws SAXException {
213+
final AttributesImpl attributes = new AttributesImpl();
214+
215+
marcXmlHandler.setIgnoreNamespace(false);
216+
marcXmlHandler.startElement(null, RECORD, "", attributes);
217+
marcXmlHandler.endElement(NAMESPACE, RECORD, "");
218+
219+
verify(receiver).endRecord();
220+
221+
verifyNoMoreInteractions(receiver);
222+
}
223+
149224
@Test
150225
public void shouldNotEncodeTypeAttributeAsMarkedLiteral() throws SAXException {
151226
final AttributesImpl attributes = new AttributesImpl();

0 commit comments

Comments
 (0)