diff --git a/metafix-runner/build.gradle b/metafix-runner/build.gradle index fe2f8d9b..9ebdd3e7 100644 --- a/metafix-runner/build.gradle +++ b/metafix-runner/build.gradle @@ -49,3 +49,13 @@ application { ] } } + +tasks.withType(JavaExec) { + doFirst { + def prefix = project.group + '.' + + System.properties.each { k, v -> + if (k.startsWith(prefix)) systemProperties[k] = v + } + } +} diff --git a/metafix/src/main/java/org/metafacture/metafix/Metafix.java b/metafix/src/main/java/org/metafacture/metafix/Metafix.java index 7d4bd12e..7b306041 100644 --- a/metafix/src/main/java/org/metafacture/metafix/Metafix.java +++ b/metafix/src/main/java/org/metafacture/metafix/Metafix.java @@ -101,6 +101,7 @@ public class Metafix implements StreamPipe, Maps { private boolean repeatedFieldsToEntities; private boolean strictnessHandlesProcessExceptions; private int entityCount; + private int maxEntityCount = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1); public Metafix() { this(NO_VARS); @@ -313,22 +314,36 @@ public void startEntity(final String name) { throw new IllegalArgumentException("Entity name must not be null."); } + ++entityCount; + if (maxEntityCountExceeded()) { + LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, maxEntityCount); + return; + } + final Value value = isArrayName(name) ? Value.newArray() : Value.newHash(); addValue(name, value); entities.add(value); - entityCountStack.push(++entityCount); + entityCountStack.push(entityCount); flattener.startEntity(name); } @Override public void endEntity() { + if (maxEntityCountExceeded()) { + return; + } + entityCountStack.pop(); flattener.endEntity(); } @Override public void literal(final String name, final String value) { + if (entityCountStack.size() > 1 && maxEntityCountExceeded()) { + return; + } + LOG.debug("Putting '{}': '{}'", name, value); flattener.literal(name, value); } @@ -438,6 +453,18 @@ public String getEntityMemberName() { return entityMemberName; } + public void setMaxEntityCount(final int maxEntityCount) { + this.maxEntityCount = maxEntityCount; + } + + public int getMaxEntityCount() { + return maxEntityCount; + } + + private boolean maxEntityCountExceeded() { + return maxEntityCount >= 0 && entityCount > maxEntityCount; + } + public enum Strictness { /**