Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve same entity in nested pass-through (_elseNested). #342

Merged
merged 7 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
82 changes: 43 additions & 39 deletions metamorph/src/main/java/org/metafacture/metamorph/Metamorph.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePi
private int recordCount;
private final List<FlushListener> recordEndListener = new ArrayList<>();
private boolean elseNested;
final private Pattern literalPatternOfEntityMarker = Pattern.compile(flattener.getEntityMarker(), Pattern.LITERAL);
private boolean elseNestedEntityStarted;
private String currentLiteralName;
private static final Logger LOG = LoggerFactory.getLogger(Metamorph.class);

protected Metamorph() {
Expand All @@ -122,7 +123,6 @@ public Metamorph(final String morphDef, final InterceptorFactory interceptorFact

public Metamorph(final String morphDef, final Map<String, String> vars,
final InterceptorFactory interceptorFactory) {

this(getInputSource(morphDef), vars, interceptorFactory);
}

Expand All @@ -140,7 +140,6 @@ public Metamorph(final Reader morphDef, final InterceptorFactory interceptorFact

public Metamorph(final Reader morphDef, final Map<String, String> vars,
final InterceptorFactory interceptorFactory) {

this(new InputSource(morphDef), vars, interceptorFactory);
}

Expand All @@ -158,7 +157,6 @@ public Metamorph(final InputStream morphDef, final InterceptorFactory intercepto

public Metamorph(final InputStream morphDef, final Map<String, String> vars,
final InterceptorFactory interceptorFactory) {

this(new InputSource(morphDef), vars, interceptorFactory);
}

Expand Down Expand Up @@ -205,7 +203,7 @@ private void init() {
flattener.setReceiver(new DefaultStreamReceiver() {
@Override
public void literal(final String name, final String value) {
dispatch(name, value, getElseSources());
dispatch(name, value, getElseSources(), false);
}
});
}
Expand All @@ -224,14 +222,16 @@ public void setErrorHandler(final MorphErrorHandler errorHandler) {

protected void registerNamedValueReceiver(final String source, final NamedValueReceiver data) {
if (ELSE_NESTED_KEYWORD.equals(source)) {
this.elseNested = true;
elseNested = true;
}

if (ELSE_KEYWORD.equals(source) || ELSE_FLATTENED_KEYWORD.equals(source) || elseNested) {
if (elseSources.isEmpty())
if (elseSources.isEmpty()) {
elseSources.add(data);
else
LOG.warn(
"Only one of '_else', '_elseFlattened' and '_elseNested' is allowed. Ignoring the superflous ones.");
}
else {
LOG.warn("Only one of '_else', '_elseFlattened' and '_elseNested' is allowed. Ignoring the superflous ones.");
}
} else {
dataRegistry.register(source, data);
}
Expand All @@ -253,12 +253,11 @@ public void startRecord(final String identifier) {
final String identifierFinal = identifier;

outputStreamReceiver.startRecord(identifierFinal);
dispatch(StandardEventNames.ID, identifierFinal, null);
dispatch(StandardEventNames.ID, identifierFinal, null, false);
}

@Override
public void endRecord() {

for(final FlushListener listener: recordEndListener){
listener.flush(recordCount, currentEntityCount);
}
Expand Down Expand Up @@ -287,17 +286,16 @@ public void startEntity(final String name) {

@Override
public void endEntity() {
dispatch(flattener.getCurrentPath(), "", null);
dispatch(flattener.getCurrentPath(), "", getElseSources(), true);
currentEntityCount = entityCountStack.pop().intValue();
flattener.endEntity();

}


@Override
public void literal(final String name, final String value) {
currentLiteralName = name;
flattener.literal(name, value);

}

@Override
Expand All @@ -318,38 +316,44 @@ public void closeStream() {
outputStreamReceiver.closeStream();
}

protected void dispatch(final String path, final String value, final List<NamedValueReceiver> fallbackReceiver) {
List<NamedValueReceiver> matchingData = dataRegistry.get(path);
boolean fallback = false;
if (matchingData == null || matchingData.isEmpty()) {
fallback = true;
matchingData = fallbackReceiver;
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);
}
if (null != matchingData) {
send(path, value, matchingData, fallback);
else if (fallbackReceiver != null) {
if (endEntity) {
if (elseNestedEntityStarted) {
outputStreamReceiver.endEntity();
elseNestedEntityStarted = false;
}
}
else {
final String entityName = elseNested ? flattener.getCurrentEntityName() : null;

if (entityName != null) {
if (!elseNestedEntityStarted) {
outputStreamReceiver.startEntity(entityName);
elseNestedEntityStarted = true;
}

send(currentLiteralName, value, fallbackReceiver);
}
else {
send(path, value, fallbackReceiver);
}
}
}
}

private void send(final String path, final String value, final List<NamedValueReceiver> dataList,
final boolean fallback) {
private void send(final String path, final String value, final List<NamedValueReceiver> dataList) {
for (final NamedValueReceiver data : dataList) {
String key = path;
if (fallback && elseNested) {
if (flattener.getCurrentEntityName() != null) {
outputStreamReceiver.startEntity(flattener.getCurrentEntityName());
key = literalPatternOfEntityMarker.split(path)[1];
}
}
try {
data.receive(key, value, null, recordCount, currentEntityCount);
data.receive(path, value, null, recordCount, currentEntityCount);
} catch (final RuntimeException e) {
errorHandler.error(e);
}
if (fallback && elseNested) {
if (flattener.getCurrentEntityName() != null) {
outputStreamReceiver.endEntity();
}
}
}
}

Expand Down Expand Up @@ -379,7 +383,7 @@ public void receive(final String name, final String value, final NamedValueSourc
}

if (name.length() != 0 && name.charAt(0) == FEEDBACK_CHAR) {
dispatch(name, value, null);
dispatch(name, value, null, false);
return;
}

Expand Down
Loading