Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public void setXmlEncoding(String xmlEncoding) {
/**
* Formats the resulting xml, by indentation.
*
* @param formatted
* True, if formatting is activated.
* @param formatted True, if formatting is activated.
*/
public void setFormatted(boolean formatted) {
this.formatted = formatted;
Expand Down Expand Up @@ -156,7 +155,11 @@ public void startEntity(final String name) {
writeRaw(String.format(DATAFIELD_OPEN_TEMPLATE, tag, ind1, ind2));
prettyPrintNewLine();
incrementIndentationLevel();
} else {
prettyPrintIndentation();
writeRaw(LEADER_OPEN_TEMPLATE);
}

}

@Override
Expand All @@ -172,22 +175,23 @@ public void endEntity() {

@Override
public void literal(final String name, final String value) {
if (value == null || value.isEmpty())
return;
if (currentEntity.equals("")) {
prettyPrintIndentation();
writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name));
writeRaw(String.format(CONTROLFIELD_OPEN_TEMPLATE, name.replaceFirst("\\W", "")));
writeEscaped(value.trim());
writeRaw(CONTROLFIELD_CLOSE);
prettyPrintNewLine();
} else if (!currentEntity.equals(Marc21EventNames.LEADER_ENTITY)) {
prettyPrintIndentation();
writeRaw(String.format(SUBFIELD_OPEN_TEMPLATE, name));
writeRaw(String.format(SUBFIELD_OPEN_TEMPLATE, name.charAt(name.indexOf('.') + 1)));
writeEscaped(value.trim());
writeRaw(SUBFIELD_CLOSE);
prettyPrintNewLine();
} else {
if (name.equals(Marc21EventNames.LEADER_ENTITY)) {
prettyPrintIndentation();
writeRaw(LEADER_OPEN_TEMPLATE + value + LEADER_CLOSE_TEMPLATE);
{
writeRaw(value + LEADER_CLOSE_TEMPLATE);
prettyPrintNewLine();
}
}
Expand Down Expand Up @@ -256,4 +260,4 @@ private void sendAndClearData() {
getReceiver().process(builder.toString());
builder.delete(0, builder.length());
}
}
}
6 changes: 4 additions & 2 deletions metamorph-test/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
<complexType>
<sequence>
<element ref="tns:name" maxOccurs="1" minOccurs="0" />
<element ref="tns:version" maxOccurs="1" minOccurs="0" />
<element ref="tns:annotation" maxOccurs="1" minOccurs="0" />
</sequence>
</complexType>
</element>

<element name="name" type="string" />
<element name="version" type="string" />
<element name="annotation" type="string" />

<element name="macros">
Expand Down Expand Up @@ -302,9 +304,9 @@
<attribute name="value" type="string" use="required" />

<attribute name="reset" type="boolean" use="optional"
default="false" />
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
default="false" />
default="false" />
<attribute name="flushWith" type="string" use="optional" />
</complexType>
</element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public void startEntity(final String name) {
entityCountStack.push(Integer.valueOf(entityCount));

flattener.startEntity(name);


if (maps.containsKey(METADATA) && maps.get(METADATA).getOrDefault("version","").equals("1.1"))
outputStreamReceiver.startEntity(name);

}

Expand All @@ -278,7 +278,8 @@ public void endEntity() {
dispatch(flattener.getCurrentPath(), "", null);
currentEntityCount = entityCountStack.pop().intValue();
flattener.endEntity();

if (maps.containsKey(METADATA) && maps.get(METADATA).getOrDefault("version","").equals("1.1"))
outputStreamReceiver.endEntity();
}


Expand Down
2 changes: 2 additions & 0 deletions metamorph/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
<complexType>
<sequence>
<element ref="tns:name" maxOccurs="1" minOccurs="0" />
<element ref="tns:version" maxOccurs="1" minOccurs="0" />
<element ref="tns:annotation" maxOccurs="1" minOccurs="0" />
</sequence>
</complexType>
</element>

<element name="name" type="string" />
<element name="version" type="string" />
<element name="annotation" type="string" />

<element name="macros">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

Expand All @@ -30,9 +31,11 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.metafacture.framework.StreamReceiver;
import org.metafacture.framework.helpers.DefaultStreamReceiver;
import org.metafacture.metamorph.api.Maps;
import org.metafacture.metamorph.api.NamedValueReceiver;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
Expand All @@ -45,13 +48,15 @@
* @author Christoph Böhme (rewrite)
*/
public final class MetamorphTest {

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Mock
private NamedValueReceiver namedValueReceiver;

@Mock
private StreamReceiver receiver;

private Metamorph metamorph;

@Before
Expand Down Expand Up @@ -189,4 +194,57 @@ public void shouldThrowIllegalStateExceptionIfEntityIsNotClosed() {
metamorph.endRecord(); // Exception expected
}

@Test
public void metamorph1() {
metamorph = InlineMorph.in(this) //
.with("<rules>") //
.with(" <data source='_else'/>")//
.with("</rules>")//
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("clone");
metamorph.literal("id", "0");
metamorph.endEntity();
metamorph.startEntity("clone");
metamorph.literal("id", "1");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).literal("clone.id", "0");
ordered.verify(receiver).literal("clone.id", "1");
ordered.verify(receiver).endRecord();
}

@Test
public void metamorph1_1() {
metamorph = InlineMorph.in(this).with("<meta>") //
.with("<version>1.1</version>")//
.with("</meta>")//
.with("<rules>")//
.with(" <data source='_else'/>")//
.with("</rules>")//
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("clone");
metamorph.literal("id", "0");
metamorph.endEntity();
metamorph.startEntity("clone");
metamorph.literal("id", "1");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).startEntity("clone");
ordered.verify(receiver).literal("clone.id", "0");
ordered.verify(receiver).endEntity();
ordered.verify(receiver).startEntity("clone");
ordered.verify(receiver).literal("clone.id", "1");
ordered.verify(receiver).endEntity();
ordered.verify(receiver).endRecord();
}
}