@Override public boolean matches(Object model) { return ctx.jsonProvider().isArray(model); } }
private static Object readObjectProperty(String property, Object model, EvaluationContextImpl ctx) { return ctx.jsonProvider().getMapValue(model, property); }
private static boolean hasProperty(String property, Object model, EvaluationContextImpl ctx) { return ctx.jsonProvider().getPropertyKeys(model).contains(property); }
@SuppressWarnings("unchecked") @Override public <T> T getValue(boolean unwrap) { if (path.isDefinite()) { if(resultIndex == 0){ throw new PathNotFoundException("No results for path: " + path.toString()); } int len = jsonProvider().length(valueResult); Object value = (len > 0) ? jsonProvider().getArrayIndex(valueResult, len-1) : null; if (value != null && unwrap){ value = jsonProvider().unwrap(value); } return (T) value; } return (T)valueResult; }
public static void walk(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) { if (ctx.jsonProvider().isMap(model)) { walkObject(pt, currentPath, parent, model, ctx, predicate); } else if (ctx.jsonProvider().isArray(model)) { walkArray(pt, currentPath, parent, model, ctx, predicate); } }
@Override public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { if (ctx.jsonProvider().isMap(model)) { for (String property : ctx.jsonProvider().getPropertyKeys(model)) { handleObjectProperty(currentPath, model, ctx, Collections.singletonList(property)); } } else if (ctx.jsonProvider().isArray(model)) { for (int idx = 0; idx < ctx.jsonProvider().length(model); idx++) { try { handleArrayIndex(idx, currentPath, model, ctx); } catch (PathNotFoundException p){ if(ctx.options().contains(Option.REQUIRE_PROPERTIES)){ throw p; } } } } }
private void sliceFrom(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { int length = ctx.jsonProvider().length(model); int from = operation.from(); if (from < 0) { //calculate slice start from array length from = length + from; } from = Math.max(0, from); logger.debug("Slice from index on array with length: {}. From index: {} to: {}. Input: {}", length, from, length - 1, toString()); if (length == 0 || from >= length) { return; } for (int i = from; i < length; i++) { handleArrayIndex(i, currentPath, model, ctx); } }
private void sliceTo(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { int length = ctx.jsonProvider().length(model); if (length == 0) { return; } int to = operation.to(); if (to < 0) { //calculate slice end from array length to = length + to; } to = Math.min(length, to); logger.debug("Slice to index on array with length: {}. From index: 0 to: {}. Input: {}", length, to, toString()); for (int i = 0; i < to; i++) { handleArrayIndex(i, currentPath, model, ctx); } }
public static void walkObject(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) { if (predicate.matches(model)) { pt.evaluate(currentPath, parent, model, ctx); } Collection<String> properties = ctx.jsonProvider().getPropertyKeys(model); for (String property : properties) { String evalPath = currentPath + "['" + property + "']"; Object propertyModel = ctx.jsonProvider().getMapValue(model, property); if (propertyModel != JsonProvider.UNDEFINED) { walk(pt, evalPath, PathRef.create(model, property), propertyModel, ctx, predicate); } } }
private void sliceBetween(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { int length = ctx.jsonProvider().length(model); int from = operation.from(); int to = operation.to(); to = Math.min(length, to); if (from >= to || length == 0) { return; } logger.debug("Slice between indexes on array with length: {}. From index: {} to: {}. Input: {}", length, from, to, toString()); for (int i = from; i < to; i++) { handleArrayIndex(i, currentPath, model, ctx); } }
@Override public boolean matches(Object model) { if (! ctx.jsonProvider().isMap(model)) { return false; Collection<String> keys = ctx.jsonProvider().getPropertyKeys(model); return keys.containsAll(propertyPathToken.getProperties());
protected void handleArrayIndex(int index, String currentPath, Object model, EvaluationContextImpl ctx) { String evalPath = Utils.concat(currentPath, "[", String.valueOf(index), "]"); PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, index) : PathRef.NO_OP; int effectiveIndex = index < 0 ? ctx.jsonProvider().length(model) + index : index; try { Object evalHit = ctx.jsonProvider().getArrayIndex(model, effectiveIndex); if (isLeaf()) { ctx.addResult(evalPath, pathRef, evalHit); } else { next().evaluate(evalPath, pathRef, evalHit, ctx); } } catch (IndexOutOfBoundsException e) { } }
@Override public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { // Can't assert it in ctor because isLeaf() could be changed later on. assert onlyOneIsTrueNonThrow(singlePropertyCase(), multiPropertyMergeCase(), multiPropertyIterationCase()); if (!ctx.jsonProvider().isMap(model)) { if (! isUpstreamDefinite()) { return; } else { String m = model == null ? "null" : model.getClass().getName(); throw new PathNotFoundException(String.format( "Expected to find an object with property %s in path %s but found '%s'. " + "This is not a json object according to the JsonProvider: '%s'.", getPathFragment(), currentPath, m, ctx.configuration().jsonProvider().getClass().getName())); } } if (singlePropertyCase() || multiPropertyMergeCase()) { handleObjectProperty(currentPath, model, ctx, properties); return; } assert multiPropertyIterationCase(); final List<String> currentlyHandledProperty = new ArrayList<String>(1); currentlyHandledProperty.add(null); for (final String property : properties) { currentlyHandledProperty.set(0, property); handleObjectProperty(currentPath, model, ctx, currentlyHandledProperty); } }
public static void walkArray(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) { if (predicate.matches(model)) { if (pt.isLeaf()) { pt.evaluate(currentPath, parent, model, ctx); } else { PathToken next = pt.next(); Iterable<?> models = ctx.jsonProvider().toIterable(model); int idx = 0; for (Object evalModel : models) { String evalPath = currentPath + "[" + idx + "]"; next.evaluate(evalPath, parent, evalModel, ctx); idx++; } } } Iterable<?> models = ctx.jsonProvider().toIterable(model); int idx = 0; for (Object evalModel : models) { String evalPath = currentPath + "[" + idx + "]"; walk(pt, evalPath, PathRef.create(model, idx), evalModel, ctx, predicate); idx++; } }
/** * Check if model is non-null and array. * @param currentPath * @param model * @param ctx * @return false if current evaluation call must be skipped, true otherwise * @throws PathNotFoundException if model is null and evaluation must be interrupted * @throws InvalidPathException if model is not an array and evaluation must be interrupted */ protected boolean checkArrayModel(String currentPath, Object model, EvaluationContextImpl ctx) { if (model == null){ if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException("The path " + currentPath + " is null"); } } if (!ctx.jsonProvider().isArray(model)) { if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException(format("Filter: %s can only be applied to arrays. Current context is: %s", toString(), model)); } } return true; } }
@Override public void evaluate(String currentPath, PathRef ref, Object model, EvaluationContextImpl ctx) { if (ctx.jsonProvider().isMap(model)) { if (accept(model, ctx.rootDocument(), ctx.configuration(), ctx)) { PathRef op = ctx.forUpdate() ? ref : PathRef.NO_OP; if (isLeaf()) { ctx.addResult(currentPath, op, model); } else { next().evaluate(currentPath, op, model, ctx); } } } else if (ctx.jsonProvider().isArray(model)){ int idx = 0; Iterable<?> objects = ctx.jsonProvider().toIterable(model); for (Object idxModel : objects) { if (accept(idxModel, ctx.rootDocument(), ctx.configuration(), ctx)) { handleArrayIndex(idx, currentPath, model, ctx); } idx++; } } else { if (isUpstreamDefinite()) { throw new InvalidPathException(format("Filter: %s can not be applied to primitives. Current context is: %s", toString(), model)); } } }
Object merged = ctx.jsonProvider().createMap(); for (String property : properties) { Object propertyVal; ctx.jsonProvider().setProperty(merged, property, propertyVal);
@Override public boolean matches(Object model) { return ctx.jsonProvider().isArray(model); } }
public static void walk(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) { if (ctx.jsonProvider().isMap(model)) { walkObject(pt, currentPath, parent, model, ctx, predicate); } else if (ctx.jsonProvider().isArray(model)) { walkArray(pt, currentPath, parent, model, ctx, predicate); } }
public void sliceBetween(ArraySliceOperation operation, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { int length = ctx.jsonProvider().length(model); int from = operation.from(); int to = operation.to(); to = Math.min(length, to); if (from >= to || length == 0) { return; } logger.debug("Slice between indexes on array with length: {}. From index: {} to: {}. Input: {}", length, from, to, toString()); for (int i = from; i < to; i++) { handleArrayIndex(i, currentPath, model, ctx); } }