Example #1
0
  @Override
  public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
    final Data data = checkData(qc);
    final String path = path(1, qc);
    final Item item = toItem(exprs[2], qc);
    final Options opts = toOptions(3, Q_OPTIONS, new Options(), qc);

    final Updates updates = qc.resources.updates();
    final IntList docs = data.resources.docs(path);
    int d = 0;

    // delete binary resources
    final IOFile bin = data.meta.binary(path);
    if (bin == null || bin.isDir()) throw BXDB_REPLACE_X.get(info, path);

    if (item instanceof Bin) {
      updates.add(new DBStore(data, path, item, info), qc);
    } else {
      if (bin.exists()) updates.add(new DBDelete(data, path, info), qc);
      final NewInput input = checkInput(item, token(path));
      if (docs.isEmpty() || docs.get(0) == 0) {
        // no replacement of first document (because of TableDiskAccess#insert, used > 0, pre = 0)
        updates.add(new DBAdd(data, input, opts, qc, info), qc);
      } else {
        updates.add(new ReplaceDoc(docs.get(0), data, input, opts, qc, info), qc);
        d = 1;
      }
    }

    // delete old documents
    final int ds = docs.size();
    for (; d < ds; d++) updates.add(new DeleteNode(docs.get(d), data, info), qc);
    return null;
  }
Example #2
0
    @Override
    public void execute(final GUI gui) {
      final DialogExport dialog = new DialogExport(gui);
      if (!dialog.ok()) return;

      final IOFile root = new IOFile(dialog.path());

      // check if existing files will be overwritten
      if (root.exists()) {
        IO file = null;
        boolean overwrite = false;
        final Data d = gui.context.data();
        final IntList il = d.resources.docs();
        final int is = il.size();
        for (int i = 0; i < is; i++) {
          file = root.merge(Token.string(d.text(il.get(i), true)));
          if (file.exists()) {
            if (overwrite) {
              // more than one file will be overwritten; check remaining tests
              file = null;
              break;
            }
            overwrite = true;
          }
        }
        if (overwrite) {
          // show message for overwriting files or directories
          final String msg = file == null ? FILES_REPLACE_X : FILE_EXISTS_X;
          if (file == null) file = root;
          if (!BaseXDialog.confirm(gui, Util.info(msg, file))) return;
        }
      }
      DialogProgress.execute(gui, new Export(root.path()));
    }
Example #3
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final Data data = checkData(qc);
   final String path = path(1, qc);
   if (data.inMemory()) return Bln.FALSE;
   final IOFile io = data.meta.binary(path);
   return Bln.get(io.exists() && !io.isDir());
 }
Example #4
0
  /**
   * Tests writing of request content when @src is set.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeFromResource() throws IOException {
    // Create a file form which will be read
    final IOFile file = new IOFile(Prop.TMP, Util.className(FnHttpTest.class));
    file.write(token("test"));

    // Request
    final HttpRequest req = new HttpRequest();
    req.payloadAttrs.put("src", file.url());
    req.payloadAttrs.put("method", "binary");
    // HTTP connection
    final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn.getOutputStream(), req);

    // Delete file
    file.delete();

    assertEquals(fakeConn.out.toString(Strings.UTF8), "test");
  }
Example #5
0
  @Override
  public B64 item(final QueryContext qc, final InputInfo ii) throws QueryException {
    checkCreate(qc);

    final IOFile root = new IOFile(toPath(0, qc).toString());
    final ArchOptions opts = toOptions(1, Q_OPTIONS, new ArchOptions(), qc);
    final Iter entries;
    if (exprs.length > 2) {
      entries = qc.iter(exprs[2]);
    } else {
      final TokenList tl = new TokenList();
      for (final String file : root.descendants()) tl.add(file);
      entries = StrSeq.get(tl).iter();
    }

    final String format = opts.get(ArchOptions.FORMAT);
    final int level = level(opts);
    if (!root.isDir()) throw FILE_NO_DIR_X.get(info, root);

    try (final ArchiveOut out = ArchiveOut.get(format.toLowerCase(Locale.ENGLISH), info)) {
      out.level(level);
      try {
        while (true) {
          Item en = entries.next();
          if (en == null) break;
          en = checkElemToken(en);
          final IOFile file = new IOFile(root, string(en.string(info)));
          if (!file.exists()) throw FILE_NOT_FOUND_X.get(info, file);
          if (file.isDir()) throw FILE_IS_DIR_X.get(info, file);
          add(en, new B64(file.read()), out, level, qc);
        }
      } catch (final IOException ex) {
        throw ARCH_FAIL_X.get(info, ex);
      }
      return new B64(out.finish());
    }
  }