/** * Executes a query. * * @param query query to be executed * @return list of serialized result items * @throws IOException error during query execution */ private StringList execute(final WebDAVQuery query) throws IOException { final ClassLoader cl = getClass().getClassLoader(); final InputStream s = cl.getResourceAsStream(FILE); if (s == null) throw new IOException("WebDAV module not found"); final byte[] module = new IOStream(s).read(); final QueryProcessor qp = new QueryProcessor(query.toString(), http.context()); try { for (final Entry<String, Object> entry : query.entries()) { qp.bind(entry.getKey(), entry.getValue()); } qp.ctx.parseLibrary(string(module), FILE, qp.sc); final Result r = qp.execute(); final int n = (int) r.size(); final StringList items = new StringList(n); for (int i = 0; i < n; i++) { final ArrayOutput ao = new ArrayOutput(); r.serialize(Serializer.get(ao), 0); items.add(ao.toString()); } return items; } catch (final QueryException ex) { throw new BaseXException(ex); } catch (final Exception ex) { Util.debug(ex); throw new BaseXException(ex); } finally { qp.close(); } }
/** * Sets the output text. * * @param out cached output */ public void setText(final ArrayOutput out) { final byte[] buf = out.buffer(); final int size = (int) out.size(); final byte[] chop = token(DOTS); if (out.finished() && size >= chop.length) { System.arraycopy(chop, 0, buf, size - chop.length, chop.length); } text.setText(buf, size); header.setText((out.finished() ? CHOPPED : "") + RESULT); home.setEnabled(gui.context.data() != null); }
@Override protected String run(final String... args) throws IOException { try { final ArrayOutput ao = new ArrayOutput(); System.setOut(new PrintStream(ao)); System.setErr(NULL); new BaseX(args); return ao.toString(); } finally { System.setOut(OUT); System.setErr(ERR); } }
/** * Serializes the specified nodes. * * @param n nodes to display */ private void setText(final DBNodes n) { if (visible()) { try { final ArrayOutput ao = new ArrayOutput(); ao.setLimit(gui.gopts.get(GUIOptions.MAXTEXT)); if (n != null) n.serialize(Serializer.get(ao)); setText(ao); cmd = null; ns = ao.finished() ? n : null; } catch (final IOException ex) { Util.debug(ex); } } else { home.setEnabled(gui.context.data() != null); } }
/** * Runs a request with the specified arguments and server arguments. * * @param args command-line arguments * @param sargs server arguments * @return result * @throws IOException I/O exception */ private static String run(final String[] args, final String[] sargs) throws IOException { final BaseXServer server = createServer(sargs); final ArrayOutput ao = new ArrayOutput(); System.setOut(new PrintStream(ao)); System.setErr(NULL); final StringList sl = new StringList(); sl.add("-p9999").add("-U" + Text.S_ADMIN).add("-P" + Text.S_ADMIN).add(args); try { new BaseXClient(sl.finish()); return ao.toString(); } finally { System.setErr(ERR); stopServer(server); } }
/** * Sends an event to the registered sessions. * * @param ctx query context * @return event result * @throws QueryException query exception */ private Item event(final QueryContext ctx) throws QueryException { final byte[] name = checkStr(expr[0], ctx); final ArrayOutput ao = new ArrayOutput(); try { // run serialization final Serializer ser = Serializer.get(ao, ctx.serProp(true)); final ValueIter ir = ctx.value(expr[1]).iter(); for (Item it; (it = ir.next()) != null; ) it.serialize(ser); ser.close(); } catch (final SerializerException ex) { throw ex.getCause(input); } catch (final IOException ex) { SERANY.thrw(input, ex); } // throw exception if event is unknown if (!ctx.context.events.notify(ctx.context, name, ao.toArray())) { NOEVENT.thrw(input, name); } return null; }
/** * Caches the output. * * @param out cached output * @param c command * @param r result * @throws QueryException query exception */ public void cacheText(final ArrayOutput out, final Command c, final Result r) throws QueryException { // cache command or node set cmd = null; ns = null; final int mh = gui.context.options.get(MainOptions.MAXHITS); boolean parse = false; if (mh >= 0 && r != null && r.size() >= mh) { parse = true; } else if (out.finished()) { if (r instanceof DBNodes) ns = (DBNodes) r; else parse = true; } // create new command instance if (parse) cmd = new CommandParser(c.toString(), gui.context).parseSingle(); }
/** * Queries binary content (works only if output stream is specified). * * @throws IOException I/O exception */ @Test public void queryBinary() throws IOException { if (out == null) return; session.execute("create db " + NAME); final byte[] tmp = {0, 1, 2, 127, 0, -1, -2, -128}; session.store("X", new ArrayInput(tmp)); final String retr = _DB_RETRIEVE.args(NAME, "X"); // check command session.execute("xquery " + RAW + retr + ',' + retr); assertTrue(eq(out.toArray(), concat(tmp, tmp))); out.reset(); // check query execution session.query(RAW + retr + ',' + retr).execute(); assertTrue(eq(out.toArray(), concat(tmp, tmp))); out.reset(); // check iterator final Query q = session.query(RAW + retr + ',' + retr); q.next(); assertTrue(eq(out.toArray(), tmp)); out.reset(); q.next(); assertTrue(eq(out.toArray(), tmp)); assertNull(q.next()); }
/** * Returns the output as byte array. * * @return byte array */ public final byte[] toArray() { return ao.toArray(); }
/** * Executes a command and returns the result as string or serializes it to the specified output * stream. * * @param command command to be parsed * @return result, or {@code null} reference * @throws IOException I/O exception */ public final String execute(final String command) throws IOException { final ArrayOutput ao = out == null ? new ArrayOutput() : null; execute(command, ao != null ? ao : out); return ao != null ? ao.toString() : null; }
/** * Runs the specified command. * * @param c command * @return string result * @throws HTTPException HTTP exception */ final String run(final Command c) throws HTTPException { final ArrayOutput ao = new ArrayOutput(); run(c, ao); return ao.toString(); }
/** * Executes the command and returns the result as string. If an exception occurs, a {@link * BaseXException} is thrown. * * @param ctx database context * @return string result * @throws BaseXException command exception */ public final String execute(final Context ctx) throws BaseXException { final ArrayOutput ao = new ArrayOutput(); execute(ctx, ao); return ao.toString(); }
/** * Checks if the most recent output equals the specified string. * * @param exp expected string * @param ret string returned from the client API */ private void assertEqual(final Object exp, final Object ret) { final String result = (out != null ? out : ret).toString(); if (out != null) out.reset(); assertEquals(exp.toString(), result.replaceAll("\\r|\\n", "")); }