Skip to content

Commit

Permalink
Make generic XML handler attribute marker configurable.
Browse files Browse the repository at this point in the history
Fixes #379.
  • Loading branch information
blackwinter committed Sep 24, 2021
1 parent 7576665 commit 70fefd5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@
@FluxCommand("handle-generic-xml")
public final class GenericXmlHandler extends DefaultXmlPipe<StreamReceiver> {

public static final String DEFAULT_ATTRIBUTE_MARKER = "";

public static final String DEFAULT_RECORD_TAG = "record";

public static final boolean EMIT_NAMESPACE = false;

private static final Pattern TABS = Pattern.compile("\t+");

private String attributeMarker = DEFAULT_ATTRIBUTE_MARKER;
private String recordTagName = DEFAULT_RECORD_TAG;

private boolean inRecord;
Expand Down Expand Up @@ -110,6 +113,14 @@ public boolean getEmitNamespace() {
return this.emitNamespace;
}

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

public String getAttributeMarker() {
return attributeMarker;
}

@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) {
if (inRecord) {
Expand Down Expand Up @@ -170,7 +181,7 @@ private void writeAttributes(final Attributes attributes) {
for (int i = 0; i < length; ++i) {
final String name = emitNamespace ? attributes.getQName(i) : attributes.getLocalName(i);
final String value = attributes.getValue(i);
getReceiver().literal(name, value);
getReceiver().literal(attributeMarker + name, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.metafacture.framework.StreamReceiver;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.xml.sax.helpers.AttributesImpl;

Expand Down Expand Up @@ -141,6 +142,47 @@ public void shouldEmitNamespaceOnEntityElementAndAttribute() {

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startEntity("ns:entity");
ordered.verify(receiver).literal("ns:attr","attr-value");
ordered.verify(receiver).literal("ns:attr", "attr-value");
}

@Test
public void shouldNotEncodeAttributesAsMarkedLiterals() {
attributes.addAttribute("", "attr", "attr", "CDATA", "attr-value");
genericXmlHandler.startElement("", "record", "record", attributes);
genericXmlHandler.endElement("", "record", "record");

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("");
ordered.verify(receiver).literal("attr", "attr-value");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
Mockito.verifyNoMoreInteractions(receiver);
}

@Test
public void issue379_shouldEncodeAttributesAsLiteralsWithConfiguredMarker() {
final String marker = "~";
genericXmlHandler.setAttributeMarker(marker);

genericXmlHandler.startElement("", "record", "record", attributes);
attributes.addAttribute("", "authority", "authority", "CDATA", "marcrelator");
attributes.addAttribute("", "type", "type", "CDATA", "text");
genericXmlHandler.startElement("", "roleTerm", "roleTerm", attributes);
final char[] charData = "Author".toCharArray();
genericXmlHandler.characters(charData, 0, charData.length);
genericXmlHandler.endElement("", "roleTerm", "roleTerm");
genericXmlHandler.endElement("", "record", "record");

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("");
ordered.verify(receiver).startEntity("roleTerm");
ordered.verify(receiver).literal(marker + "authority", "marcrelator");
ordered.verify(receiver).literal(marker + "type", "text");
ordered.verify(receiver).literal("value", "Author");
ordered.verify(receiver).endEntity();
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
Mockito.verifyNoMoreInteractions(receiver);
}

}

0 comments on commit 70fefd5

Please sign in to comment.