@Override public Iter iter(final QueryContext ctx) throws QueryException { final Iter iter = ctx.iter(root); final Value cv = ctx.value; final long cs = ctx.size; final long cp = ctx.pos; // cache results to support last() function final ItemCache ic = new ItemCache(); for (Item i; (i = iter.next()) != null; ) ic.add(i); // evaluate predicates for (final Expr p : pred) { final long is = ic.size(); ctx.size = is; ctx.pos = 1; int c = 0; for (int s = 0; s < is; ++s) { ctx.value = ic.get(s); if (p.test(ctx, input) != null) ic.set(ic.get(s), c++); ctx.pos++; } ic.size(c); } ctx.value = cv; ctx.size = cs; ctx.pos = cp; return ic; }
@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); } }
/** * Creates an item iterator for the given XML fragment. * * @param xml fragment * @param frag fragment flag * @return iterator */ private ItemCache toIter(final String xml, final boolean frag) { final ItemCache it = new ItemCache(); try { String str = frag ? "<X>" + xml + "</X>" : xml; final Data d = CreateDB.xml(IO.get(str), context); for (int p = frag ? 2 : 0; p < d.meta.size; p += d.size(p, d.kind(p))) it.add(new DBNode(d, p)); } catch (final IOException ex) { return new ItemCache(new Item[] {Str.get(Long.toString(System.nanoTime()))}, 1); } return it; }