Skip to content

Commit 44a7366

Browse files
committed
Allow namespace prefixes to be emitted
This Introduces the new parameter "emitNamespace" to GenericXMLHandler. If set to "true" input data like "<foo:bar>" will be passed through as "foo:bar". For backward compatibility the default is set to "false", thus only "bar" is emitted. See #377.
1 parent 6f3a197 commit 44a7366

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

metafacture-xml/src/main/java/org/metafacture/xml/GenericXmlHandler.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.regex.Pattern;
1919

2020
import org.metafacture.framework.FluxCommand;
21-
import org.metafacture.framework.MetafactureException;
2221
import org.metafacture.framework.StreamReceiver;
2322
import org.metafacture.framework.XmlReceiver;
2423
import org.metafacture.framework.annotations.Description;
@@ -49,6 +48,8 @@ public final class GenericXmlHandler extends DefaultXmlPipe<StreamReceiver> {
4948
private boolean inRecord;
5049
private StringBuilder valueBuffer = new StringBuilder();
5150

51+
private boolean emitNamespace = false;
52+
5253
public GenericXmlHandler() {
5354
super();
5455
final String recordTagNameProperty = System.getProperty(
@@ -91,13 +92,32 @@ public String getRecordTagName() {
9192
return recordTagName;
9293
}
9394

95+
/**
96+
* On entity level namespaces can be emitted, e.g.: "foo:bar". The default is to
97+
* ignore the namespace so that only "bar" is emitted.
98+
*
99+
* @param emitNamespace set to "true" if namespace should be emitted. Defaults
100+
* to "false".
101+
*/
102+
public void setEmitNamespace(boolean emitNamespace) {
103+
this.emitNamespace = emitNamespace;
104+
}
105+
106+
public boolean getEmitNamespace() {
107+
return this.emitNamespace;
108+
}
109+
94110
@Override
95111
public void startElement(final String uri, final String localName,
96112
final String qName, final Attributes attributes) {
97113

98114
if (inRecord) {
99115
writeValue();
100-
getReceiver().startEntity(localName);
116+
if (emitNamespace) {
117+
getReceiver().startEntity(qName);
118+
} else {
119+
getReceiver().startEntity(localName);
120+
}
101121
writeAttributes(attributes);
102122
} else if (localName.equals(recordTagName)) {
103123
final String identifier = attributes.getValue("id");

metafacture-xml/src/test/java/org/metafacture/xml/GenericXMLHandlerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013, 2014 Deutsche Nationalbibliothek
2+
* Copyright 2013, 2014, 2021 Deutsche Nationalbibliothek et al
33
*
44
* Licensed under the Apache License, Version 2.0 the "License";
55
* you may not use this file except in compliance with the License.
@@ -131,4 +131,13 @@ public void shouldEmitPCDataAsALiteralNamedValue() {
131131
ordered.verify(receiver).literal("value", "char-data");
132132
}
133133

134+
@Test
135+
public void shouldEmitNamespaceOnEntityElement() {
136+
genericXmlHandler.setEmitNamespace(true);
137+
genericXmlHandler.startElement("", "record", "record", attributes);
138+
genericXmlHandler.startElement("", "entity", "ns:entity", attributes);
139+
140+
final InOrder ordered = inOrder(receiver);
141+
ordered.verify(receiver).startEntity("ns:entity");
142+
}
134143
}

0 commit comments

Comments
 (0)