Example #1
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   if (func.length == 0) return;
   ser.openElement(this);
   for (int i = 0; i < func.length; ++i) func[i].plan(ser);
   ser.closeElement();
 }
Example #2
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.openElement(this);
   weight.plan(ser);
   expr[0].plan(ser);
   ser.closeElement();
 }
Example #3
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.openElement(this);
   for (final TypeCase c : cases) c.plan(ser);
   ts.plan(ser);
   ser.closeElement();
 }
Example #4
0
 @Override
 public final void plan(final Serializer ser) throws IOException {
   ser.openElement(this);
   ser.attribute(AXIS, Token.token(axis.name));
   ser.attribute(TEST, Token.token(test.toString()));
   super.plan(ser);
   ser.closeElement();
 }
Example #5
0
  /**
   * 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();
    }
  }
Example #6
0
  /** Saves the displayed text. */
  private void save() {
    final BaseXFileChooser fc =
        new BaseXFileChooser(SAVE_AS, gui.gopts.get(GUIOptions.WORKPATH), gui).suffix(IO.XMLSUFFIX);

    final IO file = fc.select(Mode.FSAVE);
    if (file == null) return;
    gui.gopts.set(GUIOptions.WORKPATH, file.path());

    gui.cursor(CURSORWAIT, true);
    final MainOptions opts = gui.context.options;
    final int mh = opts.get(MainOptions.MAXHITS);
    opts.set(MainOptions.MAXHITS, -1);
    opts.set(MainOptions.CACHEQUERY, false);

    try (final PrintOutput out = new PrintOutput(file.toString())) {
      if (cmd != null) {
        cmd.execute(gui.context, out);
      } else if (ns != null) {
        ns.serialize(Serializer.get(out));
      } else {
        final byte[] txt = text.getText();
        for (final byte t : txt) if (t < 0 || t > ' ' || ws(t)) out.write(t);
      }
    } catch (final IOException ex) {
      BaseXDialog.error(gui, Util.info(FILE_NOT_SAVED_X, file));
    } finally {
      opts.set(MainOptions.MAXHITS, mh);
      opts.set(MainOptions.CACHEQUERY, true);
      gui.cursor(CURSORARROW, true);
    }
  }
Example #7
0
 /**
  * Returns a serializer for the given output stream. Optional output declarations within the query
  * will be included in the serializer instance.
  *
  * @param os output stream
  * @return serializer instance
  * @throws IOException query exception
  * @throws QueryException query exception
  */
 public Serializer getSerializer(final OutputStream os) throws IOException, QueryException {
   compile();
   try {
     return Serializer.get(os, qc.serParams());
   } catch (final QueryIOException ex) {
     throw ex.getCause();
   }
 }
Example #8
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   try {
     ser.emptyElement(ITM, VAL, atom(null), TYP, Token.token(name()));
   } catch (final QueryException ex) {
     // only function items throw exceptions in atomization, and they should
     // override plan(Serializer) sensibly
     Util.notexpected(ex);
   }
 }
Example #9
0
 /**
  * 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;
 }
Example #10
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.emptyElement(
       this,
       DATA,
       token(ictx.data.meta.name),
       MIN,
       token(ind.min),
       MAX,
       token(ind.max),
       TYP,
       token(ind.type.toString()));
 }
Example #11
0
 /**
  * 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);
   }
 }
Example #12
0
  @Override
  public void action(final Object comp) {
    final String pth = path();
    final IOFile io = new IOFile(pth);
    String inf = io.isDir() && io.children().length > 0 ? DIR_NOT_EMPTY : null;
    ok = !pth.isEmpty();

    final SerialMethod mth = SerialMethod.valueOf(method.getSelectedItem());
    final OptionsOption<? extends Options> opts =
        mth == SerialMethod.JSON
            ? SerializerOptions.JSON
            : mth == SerialMethod.CSV ? SerializerOptions.CSV : null;
    final boolean showmparams = opts != null;
    mparams.setEnabled(showmparams);

    if (ok) {
      gui.gopts.set(GUIOptions.INPUTPATH, pth);
      try {
        if (comp == method) {
          if (showmparams) {
            final Options mopts = options(null).get(opts);
            mparams.setToolTipText(tooltip(mopts));
            mparams.setText(mopts.toString());
          } else {
            mparams.setToolTipText(null);
            mparams.setText("");
          }
        }
        Serializer.get(new ArrayOutput(), options(mth));
      } catch (final IOException ex) {
        ok = false;
        inf = ex.getMessage();
      }
    }

    info.setText(inf, ok ? Msg.WARN : Msg.ERROR);
    enableOK(buttons, B_OK, ok);
  }
Example #13
0
  /**
   * Evaluates the specified query.
   *
   * @param query query
   * @return success flag
   */
  final boolean query(final String query) {
    final Performance p = new Performance();
    String error;
    if (exception != null) {
      error = Util.message(exception);
    } else {
      try {
        long hits = 0;
        final boolean run = options.get(MainOptions.RUNQUERY);
        final boolean serial = options.get(MainOptions.SERIALIZE);
        final int runs = Math.max(1, options.get(MainOptions.RUNS));
        for (int r = 0; r < runs; ++r) {
          // reuse existing processor instance
          if (r != 0) qp = null;
          qp(query, context);
          parse(p);
          if (r == 0) plan(false);

          qp.compile();
          info.compiling += p.time();
          if (r == 0) plan(true);
          if (!run) continue;

          final PrintOutput po = r == 0 && serial ? out : new NullOutput();
          try (final Serializer ser = qp.getSerializer(po)) {
            if (maxResults >= 0) {
              result = qp.cache(maxResults);
              info.evaluating += p.time();
              result.serialize(ser);
              hits = result.size();
            } else {
              hits = 0;
              final Iter ir = qp.iter();
              info.evaluating += p.time();
              for (Item it; (it = ir.next()) != null; ) {
                ser.serialize(it);
                ++hits;
                checkStop();
              }
            }
          }
          qp.close();
          info.serializing += p.time();
        }
        // dump some query info
        // out.flush();

        // remove string list if global locking is used and if query is updating
        if (soptions.get(StaticOptions.GLOBALLOCK) && qp.updating) {
          info.readLocked = null;
          info.writeLocked = null;
        }
        return info(info.toString(qp, out.size(), hits, options.get(MainOptions.QUERYINFO)));

      } catch (final QueryException | IOException ex) {
        exception = ex;
        error = Util.message(ex);
      } catch (final ProcException ex) {
        error = INTERRUPTED;
      } catch (final StackOverflowError ex) {
        Util.debug(ex);
        error = BASX_STACKOVERFLOW.desc;
      } catch (final RuntimeException ex) {
        extError("");
        Util.debug(info());
        throw ex;
      } finally {
        // close processor after exceptions
        if (qp != null) qp.close();
      }
    }
    return extError(error);
  }
Example #14
0
 /**
  * Serializes the item.
  *
  * @param ser serializer
  * @throws IOException I/O exception
  */
 public void serialize(final Serializer ser) throws IOException {
   ser.item(this);
 }
Example #15
0
 /**
  * Serializes the path node.
  *
  * @param ser serializer
  * @throws IOException I/O exception
  */
 public void plan(final Serializer ser) throws IOException {
   ser.openElement(PATH);
   root.plan(data, ser);
   ser.closeElement();
 }
Example #16
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.openElement(this, TYP, Token.token(type.toString()));
   expr.plan(ser);
   ser.closeElement();
 }
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.openElement(this, DATA, token(ictx.data.meta.name));
   ftexpr.plan(ser);
   ser.closeElement();
 }
Example #18
0
  /**
   * Evaluates the specified function and creates a response.
   *
   * @throws Exception exception (including unexpected ones)
   */
  void create() throws Exception {
    // bind variables
    final StaticFunc sf = function.function;
    final Expr[] args = new Expr[sf.args.length];
    function.bind(http, args, error);

    // wrap function with a function call
    final MainModule mm = new MainModule(sf, args);

    // assign main module and http context and register process
    query.mainModule(mm);
    query.http(http);
    query.context.register(query);

    String redirect = null, forward = null;
    RestXqRespBuilder resp = null;
    try {
      // compile and evaluate query
      query.compile();
      final Iter iter = query.iter();
      Item item = iter.next();

      // handle response element
      if (item != null && item instanceof ANode) {
        final ANode node = (ANode) item;
        // send redirect to browser
        if (REST_REDIRECT.eq(node)) {
          final ANode ch = node.children().next();
          if (ch == null || ch.type != NodeType.TXT) throw function.error(NO_VALUE, node.name());
          redirect = string(ch.string()).trim();
          return;
        }
        // server-side forwarding
        if (REST_FORWARD.eq(node)) {
          final ANode ch = node.children().next();
          if (ch == null || ch.type != NodeType.TXT) throw function.error(NO_VALUE, node.name());
          forward = string(ch.string()).trim();
          return;
        }
        if (REST_RESPONSE.eq(node)) {
          resp = new RestXqRespBuilder();
          resp.build(node, function, iter, http);
          return;
        }
      }

      // HEAD method must return a single response element
      if (function.methods.size() == 1 && function.methods.contains(HTTPMethod.HEAD.name()))
        throw function.error(HEAD_METHOD);

      // serialize result
      final SerializerOptions sp = function.output;
      http.sopts(sp);
      http.initResponse();
      final Serializer ser = Serializer.get(http.res.getOutputStream(), sp);
      for (; item != null; item = iter.next()) ser.serialize(item);
      ser.close();

    } finally {
      query.close();
      query.context.unregister(query);

      if (redirect != null) {
        http.res.sendRedirect(redirect);
      } else if (forward != null) {
        http.req.getRequestDispatcher(forward).forward(http.req, http.res);
      } else if (resp != null) {
        if (resp.status != 0) http.status(resp.status, resp.message, resp.error);
        http.res.getOutputStream().write(resp.cache.toArray());
      }
    }
  }
Example #19
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.openElement(this, OP, Token.token(calc.name));
   for (final Expr e : expr) e.plan(ser);
   ser.closeElement();
 }
Example #20
0
 @Override
 public void plan(final Serializer ser) throws IOException {
   ser.emptyElement(this);
 }