Skip to content

Commit

Permalink
Ignore _elseNestedEntity if entity is ruled by morph
Browse files Browse the repository at this point in the history
This reimplemets commit f009e28:

 "Data which is handled by metamorph rules will NOT be passed through
 (hence the aptly named "_else"). If you want to use data in the morph
 AND pass it through, you have to add an explicit rule for this, as usual."

This behaviour is not the default, though.
It can be achieved by this morph rule:

<entity name='$name' sameEntity='true' reset='true' flushWith='$name' flushIncomplete='true'>"

where $name is the name of the entity you want to treat.

See #338.
  • Loading branch information
dr0i committed Nov 10, 2020
1 parent 6f871cc commit 9b2f70e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePi
private boolean elseNestedEntityStarted;
private String currentLiteralName;
private static final Logger LOG = LoggerFactory.getLogger(Metamorph.class);

final private Pattern literalPatternOfEntityMarker = Pattern.compile(flattener.getEntityMarker(), Pattern.LITERAL);
protected Metamorph() {
// package private
init();
Expand Down Expand Up @@ -280,7 +280,6 @@ public void startEntity(final String name) {
++entityCount;
currentEntityCount = entityCount;
entityCountStack.push(Integer.valueOf(entityCount));

flattener.startEntity(name);
}

Expand Down Expand Up @@ -318,11 +317,10 @@ public void closeStream() {

private void dispatch(final String path, final String value, final List<NamedValueReceiver> fallbackReceiver, final boolean endEntity) {
final List<NamedValueReceiver> matchingData = dataRegistry.get(path);

if (matchingData != null && !matchingData.isEmpty()) {
send(path, value, matchingData);
}
else if (fallbackReceiver != null) {
else if (fallbackReceiver != null && (literalPatternOfEntityMarker.split(path).length >0 && dataRegistry.get(literalPatternOfEntityMarker.split(path)[0]).isEmpty())) {
if (endEntity) {
if (elseNestedEntityStarted) {
outputStreamReceiver.endEntity();
Expand All @@ -337,7 +335,6 @@ else if (fallbackReceiver != null) {
outputStreamReceiver.startEntity(entityName);
elseNestedEntityStarted = true;
}

send(currentLiteralName, value, fallbackReceiver);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,69 @@ else if (separatesFromEntity) {
);
}

@Test
public void shouldNotHandleDataByElseNestedSourceIfDataBelonginToEntityIsRuledByMorph() {
assertMorph(receiver,
"<rules>" +
" <entity name='USA1'>" +
" <data source='USA1.Sylt' name='Hawaii' />" +
" </entity>" +
" <entity name='USA2' sameEntity='true' reset='true' flushWith='USA2' flushIncomplete='true'>" +
" <data source='USA2.Sylt' name='Hawaii' />" +
" <data source='USA2.Langeoog' name='Langeoog' />" +
" </entity>" +
" <entity name='USA3' sameEntity='true' reset='true' flushWith='USA3' flushIncomplete='true'>" +
" <data source='USA3.Sylt' name='Hawaii' />" +
" </entity>" +
" <data source='_elseNested' />" +
"</rules>",
i -> {
i.startRecord("1");
i.literal("Shikotan", "Aekap");
i.startEntity("Germany");
i.literal("Langeoog", "Moin");
i.literal("Baltrum", "Moin Moin");
i.endEntity();
i.startEntity("USA1");
i.literal("Sylt", "Aloha");
i.endEntity();
i.startEntity("USA2");
i.literal("Sylt", "Aloha");
i.literal("Langeoog", "Moin");
i.literal("Baltrum", "Moin Moin");
i.endEntity();
i.startEntity("USA2");
i.literal("Langeoog", "Moin");
i.literal("Baltrum", "Moin Moin");
i.endEntity();
i.startEntity("USA3");
i.literal("Baltrum", "Moin Moin");
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().literal("Shikotan", "Aekap");
o.get().startEntity("Germany");
o.get().literal("Langeoog", "Moin");
o.get().literal("Baltrum", "Moin Moin");
o.get().endEntity();
o.get().startEntity("USA1");
o.get().literal("Hawaii", "Aloha");
o.get().endEntity();
o.get().startEntity("USA2");
o.get().literal("Hawaii", "Aloha");
o.get().literal("Langeoog", "Moin");
o.get().endEntity();
o.get().startEntity("USA2");
o.get().literal("Langeoog", "Moin");
o.get().endEntity();
o.get().endRecord();
}
);
}


@Test
public void shouldMatchCharacterWithQuestionMarkWildcard() {
assertMorph(receiver,
Expand Down

0 comments on commit 9b2f70e

Please sign in to comment.