Skip to content

Commit 6612148

Browse files
committed
Treat empty literal name as default value tag name.
Related to #394 (8819115).
1 parent 8819115 commit 6612148

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str
5555

5656
public static final String DEFAULT_ROOT_TAG = "records";
5757
public static final String DEFAULT_RECORD_TAG = "record";
58-
public static final String DEFAULT_VALUE_TAG = null;
58+
public static final String DEFAULT_VALUE_TAG = "";
5959

6060
private static final String NEW_LINE = "\n";
6161
private static final String INDENT = "\t";
@@ -211,7 +211,7 @@ public void endEntity() {
211211

212212
@Override
213213
public void literal(final String name, final String value) {
214-
if (name.isEmpty() || name.equals(valueTag)) {
214+
if (name.equals(valueTag)) {
215215
element.setText(value);
216216
}
217217
else if (name.startsWith(attributeMarker)) {

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceTo
180180
}
181181

182182
@Test
183-
public void testShouldEncodeEmptyLiteralsAsText() {
183+
public void testShouldEncodeUnnamedLiteralsAsText() {
184184
simpleXmlEncoder.startRecord("");
185185
simpleXmlEncoder.literal("", VALUE);
186186
simpleXmlEncoder.endRecord();
@@ -195,6 +195,25 @@ public void testShouldEncodeEmptyLiteralsAsText() {
195195
getResultXml());
196196
}
197197

198+
@Test
199+
public void testShouldStillEncodeUnnamedLiteralsAsTextWithConfiguredValueTagName() {
200+
simpleXmlEncoder.setValueTag("data");
201+
202+
simpleXmlEncoder.startRecord("");
203+
simpleXmlEncoder.literal("", VALUE);
204+
simpleXmlEncoder.endRecord();
205+
simpleXmlEncoder.closeStream();
206+
207+
// SimpleXmlEncoder.Element.writeElement() does not write child elements with empty name
208+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
209+
"<records>" +
210+
"<record>" +
211+
"value" +
212+
"</record>" +
213+
"</records>",
214+
getResultXml());
215+
}
216+
198217
@Test
199218
public void testShouldNotEncodeLiteralsWithDifferentValueTagNameAsText() {
200219
simpleXmlEncoder.setValueTag("data");

0 commit comments

Comments
 (0)