@Override public String getItemAsString(final Properties props) throws XQException { try { return it.node() && it.type != NodeType.TXT ? serialize() : Token.string(it.atom(null)); } catch (final QueryException e) { throw new XQException(e.getMessage(), e.code()); } }
@Override public String getAtomicValue() throws XQException { opened(); if (it.node()) throw new BXQException(ATOM); try { return Token.string(it.atom(null)); } catch (final QueryException e) { // function item throw new BXQException(ATOM); } }
@Override public URI getNodeUri() throws XQException { opened(); if (!it.node()) throw new BXQException(NODE); final ANode node = (ANode) it; try { return new URI(Token.string(node.base())); } catch (final URISyntaxException ex) { throw new BXQException(ex.toString()); } }
@Override public final Item ebv(final QueryContext ctx, final InputInfo ii) throws QueryException { Item it = null; if (type().zeroOrOne()) { it = item(ctx, input); } else { final Iter ir = iter(ctx); it = ir.next(); if (it != null && !it.node() && ir.next() != null) CONDTYPE.thrw(input, this); } return it == null ? Bln.FALSE : it; }
@Override public void prepare() throws QueryException { // build data with all documents, to prevent dirty reads md = new MemData(data); for (final Item d : docs) { final MemData docData; if (d.node()) { // adding a document node final ANode doc = (ANode) d; if (doc.ndType() != NodeType.DOC) UPDOCTYPE.thrw(input, doc); docData = new MemData(data); new DataBuilder(docData).build(doc); } else if (d.str()) { // adding file(s) from a path final String docpath = string(d.atom(input)); final String nm = ctx.mprop.random(data.meta.name); final DirParser p = new DirParser(IO.get(docpath), ctx.prop); final MemBuilder b = new MemBuilder(nm, p, ctx.prop); try { docData = b.build(); } catch (final IOException e) { throw DOCERR.thrw(input, docpath); } } else { throw STRNODTYPE.thrw(input, this, d.type); } md.insert(md.meta.size, -1, docData); } // set new names, if needed final IntList pres = md.doc(); if (pres.size() == 1 && name != null) { // name is specified and a single document is added: set the name final byte[] nm = path == null ? name : concat(path, SLASH, name); md.update(pres.get(0), Data.DOC, nm); } else if (path != null) { // path is specified: replace the path of each new document for (int i = 0, is = pres.size(); i < is; i++) { final int d = pres.get(i); final byte[] old = md.text(d, true); final int p = lastIndexOf(old, '/'); final byte[] nm = p < 0 ? old : subtoken(old, p + 1); md.update(d, Data.DOC, concat(path, SLASH, nm)); } } }
@Override public Node getNode() throws XQException { opened(); if (!it.node()) throw new BXQException(WRONG, NodeType.NOD, it.type); return ((ANode) it).toJava(); }
/** * Checks if the specified expression is a node. Returns the node or an exception. * * @param it item to be checked * @return item * @throws QueryException query exception */ public final ANode checkNode(final Item it) throws QueryException { if (!it.node()) Err.type(this, NodeType.NOD, it); return (ANode) it; }