|
1 | 1 | /*
|
2 |
| - * Copyright 2013, 2014 Deutsche Nationalbibliothek |
| 2 | + * Copyright 2013, 2014, 2021 Deutsche Nationalbibliothek et al |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 the "License";
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 | import java.util.regex.Pattern;
|
19 | 19 |
|
20 | 20 | import org.metafacture.framework.FluxCommand;
|
21 |
| -import org.metafacture.framework.MetafactureException; |
22 | 21 | import org.metafacture.framework.StreamReceiver;
|
23 | 22 | import org.metafacture.framework.XmlReceiver;
|
24 | 23 | import org.metafacture.framework.annotations.Description;
|
@@ -49,6 +48,9 @@ public final class GenericXmlHandler extends DefaultXmlPipe<StreamReceiver> {
|
49 | 48 | private boolean inRecord;
|
50 | 49 | private StringBuilder valueBuffer = new StringBuilder();
|
51 | 50 |
|
| 51 | + public static final boolean EMIT_NAMESPACE = false; |
| 52 | + private boolean emitNamespace = EMIT_NAMESPACE; |
| 53 | + |
52 | 54 | public GenericXmlHandler() {
|
53 | 55 | super();
|
54 | 56 | final String recordTagNameProperty = System.getProperty(
|
@@ -91,13 +93,35 @@ public String getRecordTagName() {
|
91 | 93 | return recordTagName;
|
92 | 94 | }
|
93 | 95 |
|
| 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 | + |
94 | 114 | @Override
|
95 | 115 | public void startElement(final String uri, final String localName,
|
96 | 116 | final String qName, final Attributes attributes) {
|
97 | 117 |
|
98 | 118 | if (inRecord) {
|
99 | 119 | writeValue();
|
100 |
| - getReceiver().startEntity(localName); |
| 120 | + if (emitNamespace) { |
| 121 | + getReceiver().startEntity(qName); |
| 122 | + } else { |
| 123 | + getReceiver().startEntity(localName); |
| 124 | + } |
101 | 125 | writeAttributes(attributes);
|
102 | 126 | } else if (localName.equals(recordTagName)) {
|
103 | 127 | final String identifier = attributes.getValue("id");
|
@@ -145,7 +169,11 @@ private void writeAttributes(final Attributes attributes) {
|
145 | 169 | final int length = attributes.getLength();
|
146 | 170 |
|
147 | 171 | 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); |
149 | 177 | final String value = attributes.getValue(i);
|
150 | 178 | getReceiver().literal(name, value);
|
151 | 179 | }
|
|
0 commit comments