Skip to content

Commit 6ec7d7a

Browse files
committed
Merge #383 from branch '377-preserveNamespaceInGenericXmlHandler' of https://github.com/metafacture/metafacture-core
2 parents 6f3a197 + 6c175d6 commit 6ec7d7a

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 32 additions & 4 deletions
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.
@@ -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,9 @@ public final class GenericXmlHandler extends DefaultXmlPipe<StreamReceiver> {
4948
private boolean inRecord;
5049
private StringBuilder valueBuffer = new StringBuilder();
5150

51+
public static final boolean EMIT_NAMESPACE = false;
52+
private boolean emitNamespace = EMIT_NAMESPACE;
53+
5254
public GenericXmlHandler() {
5355
super();
5456
final String recordTagNameProperty = System.getProperty(
@@ -91,13 +93,35 @@ public String getRecordTagName() {
9193
return recordTagName;
9294
}
9395

96+
/**
97+
* Triggers namespace awareness. If set to "true" input data like "foo:bar"
98+
* will be passed through as "foo:bar". For backward compatibility the default
99+
* is set to "false", thus only "bar" is emitted.
100+
* <p>
101+
* <strong>Default value: {@value EMIT_NAMESPACE}</strong>
102+
*
103+
* @param emitNamespace set to "true" if namespace should be emitted. Defaults
104+
* to "false".
105+
*/
106+
public void setEmitNamespace(boolean emitNamespace) {
107+
this.emitNamespace = emitNamespace;
108+
}
109+
110+
public boolean getEmitNamespace() {
111+
return this.emitNamespace;
112+
}
113+
94114
@Override
95115
public void startElement(final String uri, final String localName,
96116
final String qName, final Attributes attributes) {
97117

98118
if (inRecord) {
99119
writeValue();
100-
getReceiver().startEntity(localName);
120+
if (emitNamespace) {
121+
getReceiver().startEntity(qName);
122+
} else {
123+
getReceiver().startEntity(localName);
124+
}
101125
writeAttributes(attributes);
102126
} else if (localName.equals(recordTagName)) {
103127
final String identifier = attributes.getValue("id");
@@ -145,7 +169,11 @@ private void writeAttributes(final Attributes attributes) {
145169
final int length = attributes.getLength();
146170

147171
for (int i = 0; i < length; ++i) {
148-
final String name = attributes.getLocalName(i);
172+
String name;
173+
if (emitNamespace) {
174+
name = attributes.getQName(i);
175+
} else
176+
name = attributes.getLocalName(i);
149177
final String value = attributes.getValue(i);
150178
getReceiver().literal(name, value);
151179
}

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

Lines changed: 12 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,15 @@ public void shouldEmitPCDataAsALiteralNamedValue() {
131131
ordered.verify(receiver).literal("value", "char-data");
132132
}
133133

134+
@Test
135+
public void shouldEmitNamespaceOnEntityElementAndAttribute() {
136+
genericXmlHandler.setEmitNamespace(true);
137+
attributes.addAttribute("", "attr", "ns:attr", "CDATA", "attr-value");
138+
genericXmlHandler.startElement("", "record", "record", attributes);
139+
genericXmlHandler.startElement("", "entity", "ns:entity", attributes);
140+
141+
final InOrder ordered = inOrder(receiver);
142+
ordered.verify(receiver).startEntity("ns:entity");
143+
ordered.verify(receiver).literal("ns:attr","attr-value");
144+
}
134145
}

0 commit comments

Comments
 (0)