Skip to content

Commit

Permalink
Treat empty literal name as default value tag name.
Browse files Browse the repository at this point in the history
Related to #394 (8819115).
  • Loading branch information
blackwinter committed Sep 27, 2021
1 parent 8819115 commit 6612148
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str

public static final String DEFAULT_ROOT_TAG = "records";
public static final String DEFAULT_RECORD_TAG = "record";
public static final String DEFAULT_VALUE_TAG = null;
public static final String DEFAULT_VALUE_TAG = "";

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

@Override
public void literal(final String name, final String value) {
if (name.isEmpty() || name.equals(valueTag)) {
if (name.equals(valueTag)) {
element.setText(value);
}
else if (name.startsWith(attributeMarker)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceTo
}

@Test
public void testShouldEncodeEmptyLiteralsAsText() {
public void testShouldEncodeUnnamedLiteralsAsText() {
simpleXmlEncoder.startRecord("");
simpleXmlEncoder.literal("", VALUE);
simpleXmlEncoder.endRecord();
Expand All @@ -195,6 +195,25 @@ public void testShouldEncodeEmptyLiteralsAsText() {
getResultXml());
}

@Test
public void testShouldStillEncodeUnnamedLiteralsAsTextWithConfiguredValueTagName() {
simpleXmlEncoder.setValueTag("data");

simpleXmlEncoder.startRecord("");
simpleXmlEncoder.literal("", VALUE);
simpleXmlEncoder.endRecord();
simpleXmlEncoder.closeStream();

// SimpleXmlEncoder.Element.writeElement() does not write child elements with empty name
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<records>" +
"<record>" +
"value" +
"</record>" +
"</records>",
getResultXml());
}

@Test
public void testShouldNotEncodeLiteralsWithDifferentValueTagNameAsText() {
simpleXmlEncoder.setValueTag("data");
Expand Down

0 comments on commit 6612148

Please sign in to comment.