示例#1
0
  @Override
  public Iter iter(final QueryContext ctx) throws QueryException {
    final Iter iter = ctx.iter(expr);
    final Item it = iter.next();
    if (it == null) {
      if (type.mayBeZero()) return Empty.ITER;
      throw XPEMPTY.thrw(input, desc());
    }
    if (type.zeroOrOne()) {
      if (iter.next() != null) NOTREATS.thrw(input, desc(), type);
      if (!it.type.instance(type.type)) NOTREAT.thrw(input, desc(), type, it.type);
      return it.iter();
    }

    return new Iter() {
      Item i = it;

      @Override
      public Item next() throws QueryException {
        if (i == null) return null;
        if (!i.type.instance(type.type)) NOTREAT.thrw(input, desc(), type, i.type);
        final Item ii = i;
        i = iter.next();
        return ii;
      }
    };
  }
示例#2
0
  @Override
  public Iter iter(final QueryContext ctx) throws QueryException {
    switch (def) {
      case ERROR:
        final int al = expr.length;
        if (al == 0) FUNERR1.thrw(input);

        String code = FUNERR1.code();
        String msg = FUNERR1.desc;

        final Item it = expr[0].item(ctx, input);
        if (it == null) {
          if (al == 1) XPEMPTY.thrw(input, desc());
        } else {
          code = Token.string(((QNm) checkType(it, AtomType.QNM)).ln());
        }
        if (al > 1) msg = Token.string(checkEStr(expr[1], ctx));
        final Value val = al > 2 ? expr[2].value(ctx) : null;
        final QueryException ex = new QueryException(input, code, val, msg);
        throw ex;
      case TRACE:
        return new Iter() {
          final Iter ir = expr[0].iter(ctx);
          final byte[] s = checkEStr(expr[1], ctx);

          @Override
          public Item next() throws QueryException {
            final Item i = ir.next();
            if (i != null) {
              final TokenBuilder tb = new TokenBuilder(s);
              if (s.length != 0) tb.add(": ");
              tb.add(i.toString());
              // if GUI is used, or if user is no admin, trace info is cached
              if (Prop.gui || !ctx.context.user.perm(User.ADMIN)) {
                ctx.evalInfo(tb.finish());
              } else {
                Util.errln(tb);
              }
            }
            return i;
          }
        };
      case ENVARS:
        final ItemCache ic = new ItemCache();
        for (final Object k : System.getenv().keySet().toArray()) {
          ic.add(Str.get(k));
        }
        return ic;
      default:
        return super.iter(ctx);
    }
  }
示例#3
0
  @Override
  public Value value(final QueryContext ctx) throws QueryException {
    final Value val = ctx.value(expr);

    final long len = val.size();
    if (len == 0) {
      if (type.mayBeZero()) return val;
      throw XPEMPTY.thrw(input, desc());
    }
    if (type.zeroOrOne()) {
      if (len > 1) throw NOTREATS.thrw(input, desc(), type);
      final Item it = val.itemAt(0);
      if (!it.type.instance(type.type)) NOTREAT.thrw(input, desc(), type, it.type);
      return it;
    }

    for (long i = 0; i < len; i++) {
      final Item it = val.itemAt(i);
      if (!it.type.instance(type.type)) NOTREAT.thrw(input, desc(), type, it.type);
    }

    return val;
  }
示例#4
0
 /**
  * Checks if the specified item is an empty sequence; if yes, throws an exception.
  *
  * @param it item to be checked
  * @return specified item
  * @throws QueryException query exception
  */
 public final Item checkEmpty(final Item it) throws QueryException {
   if (it == null) XPEMPTY.thrw(input, desc());
   return it;
 }