@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; } }; }
@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; }
/** * 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; }