Skip to content

Commit

Permalink
Make simple XML encoder attribute marker configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Sep 24, 2021
1 parent 8af6c69 commit 7576665
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
7 changes: 7 additions & 0 deletions metafacture-xml/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ dependencies {
testImplementation 'org.mockito:mockito-core:2.5.5'
testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.21'
}

test {
testLogging {
showStandardStreams = true
exceptionFormat = 'full'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public final class SimpleXmlEncoder extends DefaultStreamPipe<ObjectReceiver<Str

private final StringBuilder builder = new StringBuilder();

private String attributeMarker = ATTRIBUTE_MARKER;
private String rootTag = DEFAULT_ROOT_TAG;
private String recordTag = DEFAULT_RECORD_TAG;
private Map<String, String> namespaces = new HashMap<String, String>();
Expand Down Expand Up @@ -146,6 +147,14 @@ public void setNamespaces(final Map<String, String> namespaces) {
this.namespaces = namespaces;
}

public void setAttributeMarker(final String attributeMarker) {
this.attributeMarker = attributeMarker;
}

public String getAttributeMarker() {
return attributeMarker;
}

@Override
public void startRecord(final String identifier) {
if (separateRoots) {
Expand Down Expand Up @@ -195,8 +204,8 @@ public void literal(final String name, final String value) {
if (name.isEmpty()) {
element.setText(value);
}
else if (name.startsWith(ATTRIBUTE_MARKER)) {
element.addAttribute(name.substring(1), value);
else if (name.startsWith(attributeMarker)) {
element.addAttribute(name.substring(attributeMarker.length()), value);
}
else {
element.createChild(name).setText(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,85 @@ public void shouldAddNamespaceWithEmptyKeyFromPropertiesFileAsDefaultNamespaceTo
getResultXml());
}

@Test
public void testShouldEncodeMarkedLiteralsAsAttributes() {
simpleXmlEncoder.startRecord("");
simpleXmlEncoder.literal(TAG, VALUE);
simpleXmlEncoder.literal("~attr", VALUE);
simpleXmlEncoder.endRecord();
simpleXmlEncoder.closeStream();

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<records>" +
"<record attr=\"value\">" +
"<tag>value</tag>" +
"</record>" +
"</records>",
getResultXml());
}

@Test
public void testShouldNotEncodeMarkedEntitiesAsAttributes() {
simpleXmlEncoder.setAttributeMarker("*");

simpleXmlEncoder.startRecord("");
simpleXmlEncoder.startEntity("~entity");
simpleXmlEncoder.literal(TAG, VALUE);
simpleXmlEncoder.endEntity();
simpleXmlEncoder.endRecord();
simpleXmlEncoder.closeStream();

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<records>" +
"<record>" +
"<~entity>" +
"<tag>value</tag>" +
"</~entity>" +
"</record>" +
"</records>",
getResultXml());
}

@Test
public void testShouldNotEncodeLiteralsWithDifferentMarkerAsAttributes() {
simpleXmlEncoder.setAttributeMarker("*");

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

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<records>" +
"<record>" +
"<tag>value</tag>" +
"<~attr>value</~attr>" +
"</record>" +
"</records>",
getResultXml());
}

@Test
public void testShouldEncodeMarkedLiteralsWithConfiguredMarkerAsAttributes() {
final String marker = "**";
simpleXmlEncoder.setAttributeMarker(marker);

simpleXmlEncoder.startRecord("");
simpleXmlEncoder.literal(TAG, VALUE);
simpleXmlEncoder.literal(marker + "attr", VALUE);
simpleXmlEncoder.endRecord();
simpleXmlEncoder.closeStream();

assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<records>" +
"<record attr=\"value\">" +
"<tag>value</tag>" +
"</record>" +
"</records>",
getResultXml());
}

private void emitTwoRecords() {
simpleXmlEncoder.startRecord("X");
simpleXmlEncoder.literal(TAG, VALUE);
Expand Down

0 comments on commit 7576665

Please sign in to comment.