|
28 | 28 | import java.util.LinkedList; |
29 | 29 | import java.util.List; |
30 | 30 | import java.util.Map; |
| 31 | +import java.util.regex.Pattern; |
31 | 32 |
|
32 | 33 | import org.metafacture.commons.ResourceUtil; |
33 | 34 | import org.metafacture.framework.FluxCommand; |
|
64 | 65 | @FluxCommand("morph") |
65 | 66 | public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePipe, Maps { |
66 | 67 |
|
| 68 | + private static final String ELSE_AND_PASS_ENTITY_EVENTS_KEYWORD = "_elseAndPassEntityEvents"; |
67 | 69 | public static final String ELSE_KEYWORD = "_else"; |
68 | 70 | public static final char FEEDBACK_CHAR = '@'; |
69 | 71 | public static final char ESCAPE_CHAR = '\\'; |
@@ -94,6 +96,8 @@ public final class Metamorph implements StreamPipe<StreamReceiver>, NamedValuePi |
94 | 96 | private MorphErrorHandler errorHandler = new DefaultErrorHandler(); |
95 | 97 | private int recordCount; |
96 | 98 | private final List<FlushListener> recordEndListener = new ArrayList<>(); |
| 99 | + private boolean passEntityEvents; |
| 100 | + final private Pattern literalPatternOfEntityMarker = Pattern.compile(flattener.getEntityMarker(), Pattern.LITERAL); |
97 | 101 |
|
98 | 102 | protected Metamorph() { |
99 | 103 | // package private |
@@ -215,7 +219,10 @@ public void setErrorHandler(final MorphErrorHandler errorHandler) { |
215 | 219 | } |
216 | 220 |
|
217 | 221 | protected void registerNamedValueReceiver(final String source, final NamedValueReceiver data) { |
218 | | - if (ELSE_KEYWORD.equals(source)) { |
| 222 | + if (ELSE_AND_PASS_ENTITY_EVENTS_KEYWORD.equals(source)) { |
| 223 | + this.passEntityEvents = true; |
| 224 | + } |
| 225 | + if (ELSE_KEYWORD.equals(source) || this.passEntityEvents) { |
219 | 226 | elseSources.add(data); |
220 | 227 | } else { |
221 | 228 | dataRegistry.register(source, data); |
@@ -268,9 +275,6 @@ public void startEntity(final String name) { |
268 | 275 | entityCountStack.push(Integer.valueOf(entityCount)); |
269 | 276 |
|
270 | 277 | flattener.startEntity(name); |
271 | | - |
272 | | - |
273 | | - |
274 | 278 | } |
275 | 279 |
|
276 | 280 | @Override |
@@ -306,28 +310,33 @@ public void closeStream() { |
306 | 310 | outputStreamReceiver.closeStream(); |
307 | 311 | } |
308 | 312 |
|
309 | | - protected void dispatch(final String path, final String value, final List<NamedValueReceiver> fallback) { |
310 | | - final List<NamedValueReceiver> matchingData = findMatchingData(path, fallback); |
311 | | - if (null != matchingData) { |
312 | | - send(path, value, matchingData); |
313 | | - } |
314 | | - } |
315 | | - |
316 | | - private List<NamedValueReceiver> findMatchingData(final String path, final List<NamedValueReceiver> fallback) { |
317 | | - final List<NamedValueReceiver> matchingData = dataRegistry.get(path); |
| 313 | + protected void dispatch(final String path, final String value, final List<NamedValueReceiver> fallbackReceiver) { |
| 314 | + List<NamedValueReceiver> matchingData = dataRegistry.get(path); |
| 315 | + boolean fallback = false; |
318 | 316 | if (matchingData == null || matchingData.isEmpty()) { |
319 | | - return fallback; |
| 317 | + fallback = true; |
| 318 | + matchingData = fallbackReceiver; |
| 319 | + } |
| 320 | + if (null != matchingData) { |
| 321 | + send(path, value, matchingData, fallback); |
320 | 322 | } |
321 | | - return matchingData; |
322 | 323 | } |
323 | 324 |
|
324 | | - private void send(final String key, final String value, final List<NamedValueReceiver> dataList) { |
| 325 | + private void send(final String path, final String value, final List<NamedValueReceiver> dataList, final boolean fallback) { |
325 | 326 | for (final NamedValueReceiver data : dataList) { |
| 327 | + String key=path; |
| 328 | + if (fallback && value != null && passEntityEvents) { |
| 329 | + outputStreamReceiver.startEntity(flattener.getCurrentEntityName()); |
| 330 | + key=literalPatternOfEntityMarker.split(path)[1]; |
| 331 | + } |
326 | 332 | try { |
327 | 333 | data.receive(key, value, null, recordCount, currentEntityCount); |
328 | 334 | } catch (final RuntimeException e) { |
329 | 335 | errorHandler.error(e); |
330 | 336 | } |
| 337 | + if (fallback && value!=null && passEntityEvents) { |
| 338 | + outputStreamReceiver.endEntity(); |
| 339 | + } |
331 | 340 | } |
332 | 341 | } |
333 | 342 |
|
|
0 commit comments