Ejemplo n.º 1
0
  private boolean eval(PredicateContext ctx) {
    if (CriteriaType.EXISTS == criteriaType) {
      boolean exists = ((Boolean) right);
      try {
        Configuration c =
            Configuration.builder()
                .jsonProvider(ctx.configuration().jsonProvider())
                .options(Option.REQUIRE_PROPERTIES)
                .build();
        ((Path) left).evaluate(ctx.item(), ctx.root(), c).getValue();
        return exists;
      } catch (PathNotFoundException e) {
        return !exists;
      }
    } else {
      try {
        Object leftVal = evaluateIfPath(left, ctx);
        Object rightVal = evaluateIfPath(right, ctx);

        return criteriaType.eval(rightVal, leftVal, ctx);
      } catch (ValueCompareException e) {
        return false;
      } catch (PathNotFoundException e) {
        return false;
      }
    }
  }
Ejemplo n.º 2
0
  private Object evaluateIfPath(Object target, PredicateContext ctx) {
    Object res = target;
    if (res instanceof Path) {
      Path leftPath = (Path) target;

      if (ctx instanceof PredicateContextImpl) {
        // This will use cache for document ($) queries
        PredicateContextImpl ctxi = (PredicateContextImpl) ctx;
        res = ctxi.evaluate(leftPath);
      } else {
        Object doc = leftPath.isRootPath() ? ctx.root() : ctx.item();
        res = leftPath.evaluate(doc, ctx.root(), ctx.configuration()).getValue();
      }
    }
    return res;
  }
  @SuppressWarnings("unchecked")
  @Override
  public boolean apply(PredicateContext ctx) {
    final Map<String, Object> items = ctx.item(Map.class);

    if (items != null) {
      for (Map.Entry<String, Object> entry : items.entrySet()) {

        final String value = entry.getValue().toString();

        if (isVariable(value)) {

          final String variable = extractVariable(value);
          final Object newValue = lookupContext.read(variable);

          entry.setValue(newValue);
        }
      }

      return isRootElement(items);
    }

    return false;
  }