Skip to content

Commit

Permalink
Merge #332 into oersi
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Sep 15, 2020
2 parents 11982de + 8bf5bcb commit 04acdae
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ public final class MarcXmlHandler extends DefaultXmlPipe<StreamReceiver> {
private static final String LEADER = "leader";
private static final String TYPE = "type";
private String currentTag = "";
private String namespace = NAMESPACE;
private StringBuilder builder = new StringBuilder();

public void setNamespace(final String namespace) {
this.namespace = namespace;
}

private boolean checkNamespace(final String uri) {
return namespace == null || namespace.equals(uri);
}

@Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
throws SAXException {
Expand All @@ -58,7 +67,7 @@ public void startElement(final String uri, final String localName, final String
}else if(CONTROLFIELD.equals(localName)){
builder = new StringBuilder();
currentTag = attributes.getValue("tag");
}else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){
}else if(RECORD.equals(localName) && checkNamespace(uri)){
getReceiver().startRecord("");
getReceiver().literal(TYPE, attributes.getValue(TYPE));
}else if(LEADER.equals(localName)){
Expand All @@ -77,7 +86,7 @@ public void endElement(final String uri, final String localName, final String qN
}else if(CONTROLFIELD.equals(localName)){
getReceiver().literal(currentTag, builder.toString().trim());

}else if(RECORD.equals(localName) && NAMESPACE.equals(uri)){
}else if(RECORD.equals(localName) && checkNamespace(uri)){
getReceiver().endRecord();

}else if(LEADER.equals(localName)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.metafacture.biblio.marc21;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.After;
import org.junit.Before;
Expand All @@ -37,6 +38,8 @@ public final class MarcXmlHandlerTest {
private static final String LEADER = "leader";
private static final String CONTROLFIELD = "controlfield";
private static final String NAMESPACE = "http://www.loc.gov/MARC21/slim";
private static final String RECORD = "record";
private static final String TYPE = "type";

private MarcXmlHandler marcXmlHandler;

Expand Down Expand Up @@ -84,4 +87,46 @@ public void issue233ShouldNotRemoveWhitespaceFromLeader()
verify(receiver).literal("leader", leaderValue);
}

@Test
public void shouldRecognizeRecordsWithNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

marcXmlHandler.startElement(NAMESPACE, RECORD, "", attributes);
marcXmlHandler.endElement(NAMESPACE, RECORD, "");

verify(receiver).startRecord("");
verify(receiver).literal(TYPE, null);
verify(receiver).endRecord();

verifyNoMoreInteractions(receiver);
}

@Test
public void shouldNotRecognizeRecordsWithoutNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

marcXmlHandler.startElement(null, RECORD, "", attributes);
marcXmlHandler.endElement(null, RECORD, "");

verifyNoMoreInteractions(receiver);
}

@Test
public void issue330ShouldOptionallyRecognizeRecordsWithoutNamespace()
throws SAXException {
final AttributesImpl attributes = new AttributesImpl();

marcXmlHandler.setNamespace(null);
marcXmlHandler.startElement(null, RECORD, "", attributes);
marcXmlHandler.endElement(null, RECORD, "");

verify(receiver).startRecord("");
verify(receiver).literal(TYPE, null);
verify(receiver).endRecord();

verifyNoMoreInteractions(receiver);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
*/
public abstract class AbstractFlushingCollect extends AbstractCollect {

private boolean flushIncomplete = true;

public final void setFlushIncomplete(final boolean flushIncomplete) {
this.flushIncomplete = flushIncomplete;
}

@Override
public final void flush(final int recordCount, final int entityCount) {
if (isSameRecord(recordCount) && sameEntityConstraintSatisfied(entityCount)) {
if(isConditionMet()) {
if(isConditionMet() && (flushIncomplete || isComplete())) {
emit();
}
if (getReset()) {
Expand Down
6 changes: 6 additions & 0 deletions metamorph/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@
<attribute name="name" type="string" use="required" />
<attribute name="value" type="string" use="required" />

<attribute name="flushIncomplete" type="boolean" use="optional"
default="true" />
<attribute name="reset" type="boolean" use="optional"
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
Expand All @@ -301,6 +303,8 @@
<attribute name="name" type="string" use="required" />
<attribute name="value" type="string" use="required" />

<attribute name="flushIncomplete" type="boolean" use="optional"
default="true" />
<attribute name="reset" type="boolean" use="optional"
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
Expand Down Expand Up @@ -442,6 +446,8 @@
have an entity-name element otherwise an empty name is emitted</documentation>
</annotation>
</attribute>
<attribute name="flushIncomplete" type="boolean" use="optional"
default="true" />
<attribute name="reset" type="boolean" use="optional"
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.metafacture.metamorph.collectors;

import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -165,6 +166,38 @@ public void shouldEmitCurrentValueOnFlushEvent() {
ordered.verifyNoMoreInteractions();
}

@Test
public void shouldNotEmitCurrentValueOnFlushEventIfIncomplete() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <combine name='combi' value='${one}${two}' flushWith='e' flushIncomplete='false' reset='true'>")
.with(" <data source='e.l' name='one' />")
.with(" <data source='e.m' name='two' />")
.with(" </combine>")
.with("</rules>")
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("e");
metamorph.literal("l", "1");
metamorph.endEntity();
metamorph.startEntity("e");
metamorph.literal("l", "2");
metamorph.literal("m", "2");
metamorph.endEntity();
metamorph.startEntity("e");
metamorph.literal("l", "3");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).literal("combi", "22");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
verifyNoMoreInteractions(receiver);
}

@Test
public void shouldPostprocessCombinedValue() {
metamorph = InlineMorph.in(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -119,6 +120,40 @@ public void shouldEmitEnityOnFlushEvent() {
ordered.verifyNoMoreInteractions();
}

@Test
public void shouldNotEmitEnityOnFlushEventIfIncomplete() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <entity name='entity' flushWith='record' flushIncomplete='false'>")
.with(" <data source='d1' name='l1' />")
.with(" <data source='d2' name='l2' />")
.with(" </entity>")
.with("</rules>")
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.literal("d1", "a");
metamorph.literal("d1", "b");
metamorph.literal("d2", "c");
metamorph.endRecord();
metamorph.startRecord("2");
metamorph.literal("d2", "c");
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).startEntity("entity");
ordered.verify(receiver).literal("l1", "a");
ordered.verify(receiver).literal("l1", "b");
ordered.verify(receiver).literal("l2", "c");
ordered.verify(receiver).endEntity();
ordered.verify(receiver).endRecord();
ordered.verify(receiver).startRecord("2");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
verifyNoMoreInteractions(receiver);
}

@Test
public void shouldEmitEntityOnEachFlushEvent() {
metamorph = InlineMorph.in(this)
Expand Down Expand Up @@ -488,7 +523,7 @@ public void shouldEmitEntityContentsAgainIfResetIsFalse() {
}

@Test
public void shouldNotEmitEntityContentsAgainIfResetIsFalse() {
public void shouldNotEmitEntityContentsAgainIfResetIsTrue() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <entity name='entity' reset='true'>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -233,4 +234,85 @@ public void shouldFireIfLiteralsInEntitiesAreReceivedThatAreNotListedInStatement
ordered.verifyNoMoreInteractions();
}

@Test
public void shouldFireOnFlush() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <equalsFilter name='equalsFiltered' value='${one}' flushWith='field1.data2'>")
.with(" <data source='field1.data1' name='one' />")
.with(" <data source='field1.data2' name='two' />")
.with(" </equalsFilter>")
.with("</rules>")
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("field1");
metamorph.literal("data1", "a");
metamorph.endEntity();
metamorph.endRecord();
metamorph.startRecord("2");
metamorph.startEntity("field1");
metamorph.literal("data2", "a");
metamorph.endEntity();
metamorph.endRecord();
metamorph.startRecord("3");
metamorph.startEntity("field1");
metamorph.literal("data1", "a");
metamorph.literal("data2", "a");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).endRecord();
ordered.verify(receiver).startRecord("2");
ordered.verify(receiver).literal("equalsFiltered", "");
ordered.verify(receiver).endRecord();
ordered.verify(receiver).startRecord("3");
ordered.verify(receiver).literal("equalsFiltered", "a");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
verifyNoMoreInteractions(receiver);
}

@Test
public void shouldNotFireOnFlushIfIncomplete() {
metamorph = InlineMorph.in(this)
.with("<rules>")
.with(" <equalsFilter name='equalsFiltered' value='${one}' flushWith='field1.data2' flushIncomplete='false'>")
.with(" <data source='field1.data1' name='one' />")
.with(" <data source='field1.data2' name='two' />")
.with(" </equalsFilter>")
.with("</rules>")
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("field1");
metamorph.literal("data1", "a");
metamorph.endEntity();
metamorph.endRecord();
metamorph.startRecord("2");
metamorph.startEntity("field1");
metamorph.literal("data2", "a");
metamorph.endEntity();
metamorph.endRecord();
metamorph.startRecord("3");
metamorph.startEntity("field1");
metamorph.literal("data1", "a");
metamorph.literal("data2", "a");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).endRecord();
ordered.verify(receiver).startRecord("2");
ordered.verify(receiver).endRecord();
ordered.verify(receiver).startRecord("3");
ordered.verify(receiver).literal("equalsFiltered", "a");
ordered.verify(receiver).endRecord();
ordered.verifyNoMoreInteractions();
verifyNoMoreInteractions(receiver);
}

}

0 comments on commit 04acdae

Please sign in to comment.