Exemple #1
0
  /**
   * Returns a formatted number.
   *
   * @param info input info
   * @param number number to be formatted
   * @param picture picture
   * @return string representation
   * @throws QueryException query exception
   */
  public byte[] format(final InputInfo info, final ANum number, final byte[] picture)
      throws QueryException {

    // find pattern separator and sub-patterns
    final TokenList tl = new TokenList();
    byte[] pic = picture;
    // "A picture-string consists either of a sub-picture, or of two sub-pictures separated by
    // a pattern-separator-sign"
    final int i = indexOf(pic, pattern);
    if (i == -1) {
      tl.add(pic);
    } else {
      tl.add(substring(pic, 0, i));
      pic = substring(pic, i + cl(pic, i));
      // "A picture-string must not contain more than one pattern-separator-sign"
      if (contains(pic, pattern)) throw PICNUM_X.get(info, picture);
      tl.add(pic);
    }
    final byte[][] patterns = tl.finish();

    // check and analyze patterns
    if (!checkSyntax(patterns)) throw PICNUM_X.get(info, picture);
    final Picture[] pics = analyze(patterns);

    // return formatted string
    return format(number, pics, info);
  }
Exemple #2
0
  @Override
  public Iter iter(final QueryContext qc) throws QueryException {
    final int el = exprs.length;
    if (el == 0) {
      final TokenList tl = new TokenList();
      for (final String s : qc.context.databases.listDBs()) tl.add(s);
      return StrSeq.get(tl).iter();
    }

    final Data data = checkData(qc);
    final String path = string(exprs.length == 1 ? Token.EMPTY : toToken(exprs[1], qc));
    final IntList il = data.resources.docs(path);
    final TokenList tl = data.resources.binaries(path);
    return new Iter() {
      final int is = il.size(), ts = tl.size();
      int ip, tp;

      @Override
      public Str get(final long i) {
        return i < is
            ? Str.get(data.text(il.get((int) i), true))
            : i < is + ts ? Str.get(tl.get((int) i - is)) : null;
      }

      @Override
      public Str next() {
        return ip < is ? get(ip++) : tp < ts ? get(ip + tp++) : null;
      }

      @Override
      public long size() {
        return is + ts;
      }
    };
  }
Exemple #3
0
  /**
   * Extracts entries from the archive.
   *
   * @param ctx query context
   * @return text entries
   * @throws QueryException query exception
   */
  private TokenList extract(final QueryContext ctx) throws QueryException {
    final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64);
    TokenSet hs = null;
    if (expr.length > 1) {
      // filter result to specified entries
      hs = new TokenSet();
      final Iter names = ctx.iter(expr[1]);
      for (Item en; (en = names.next()) != null; ) {
        hs.add(checkElmStr(en).string(info));
      }
    }

    final TokenList tl = new TokenList();
    final ArchiveIn in = ArchiveIn.get(archive.input(info), info);
    try {
      while (in.more()) {
        final ZipEntry ze = in.entry();
        if (ze.isDirectory()) continue;
        if (hs == null || hs.delete(token(ze.getName())) != 0) tl.add(in.read());
      }
    } catch (final IOException ex) {
      Util.debug(ex);
      ARCH_FAIL.thrw(info, ex);
    } finally {
      in.close();
    }
    return tl;
  }
Exemple #4
0
 /**
  * Lists the table contents.
  *
  * @param table table reference
  * @param root root node
  * @param header table header
  * @param skip number of columns to skip
  */
 static void list(final Table table, final FElem root, final QNm header, final int skip) {
   for (final TokenList list : table.contents) {
     final FElem el = new FElem(header);
     // don't show last attribute (input path)
     final int ll = list.size() - skip;
     for (int l = 1; l < ll; l++) {
       el.add(new QNm(lc(table.header.get(l))), list.get(l));
     }
     el.add(list.get(0));
     root.add(el);
   }
 }
Exemple #5
0
  /**
   * Recursively collects relevant thesaurus terms.
   *
   * @param list result list
   * @param node input node
   * @param level current level
   */
  private void find(final TokenList list, final ThesNode node, final long level) {
    if (level > max || node == null) return;

    for (int n = 0; n < node.size; ++n) {
      if (rel.length == 0 || eq(node.rs[n], rel)) {
        final byte[] term = node.nodes[n].term;
        if (!list.contains(term)) {
          list.add(term);
          find(list, node.nodes[n], level + 1);
        }
      }
    }
  }
Exemple #6
0
  /**
   * Creates an XQuery representation for the specified table query.
   *
   * @param filter filter terms
   * @param cols filter columns
   * @param elem element flag
   * @param name name of root element
   * @param root root flag
   * @return query
   */
  public static String findTable(
      final StringList filter,
      final TokenList cols,
      final BoolList elem,
      final byte[] name,
      final boolean root) {

    final TokenBuilder tb = new TokenBuilder();
    final int is = filter.size();
    for (int i = 0; i < is; ++i) {
      final String[] spl = split(filter.get(i));
      for (final String s : spl) {
        final byte[] term = trim(replace(token(s), '"', ' '));
        if (term.length == 0) continue;
        tb.add('[');

        final boolean elm = elem.get(i);
        tb.add(elm ? ".//" : "@");
        tb.add("*:");
        tb.add(cols.get(i));

        if (term[0] == '<' || term[0] == '>') {
          tb.add(term[0]);
          tb.addLong(calcNum(substring(term, 1)));
        } else {
          tb.add(" contains text \"");
          tb.add(term);
          tb.add('"');
        }
        tb.add(']');
      }
    }
    return tb.isEmpty() ? "/" : (root ? "/" : "") + Axis.DESCORSELF + "::*:" + string(name) + tb;
  }
Exemple #7
0
  /**
   * Lists resources of the specified database.
   *
   * @return success flag
   * @throws IOException I/O exception
   */
  private boolean listDB() throws IOException {
    final String db = args[0];
    final String path = args[1] != null ? args[1] : "";
    if (!Databases.validName(db)) return error(NAME_INVALID_X, db);

    final Table table = new Table();
    table.description = RESOURCES;
    table.header.add(INPUT_PATH);
    table.header.add(TYPE);
    table.header.add(MimeTypes.CONTENT_TYPE);
    table.header.add(SIZE);

    try {
      // add xml documents
      final Data data = Open.open(db, context);
      final Resources res = data.resources;
      final IntList il = res.docs(path);
      final int ds = il.size();
      for (int i = 0; i < ds; i++) {
        final int pre = il.get(i);
        final TokenList tl = new TokenList(3);
        final byte[] file = data.text(pre, true);
        tl.add(file);
        tl.add(DataText.M_XML);
        tl.add(MimeTypes.APP_XML);
        tl.add(data.size(pre, Data.DOC));
        table.contents.add(tl);
      }
      // add binary resources
      for (final byte[] file : res.binaries(path)) {
        final String f = string(file);
        final TokenList tl = new TokenList(3);
        tl.add(file);
        tl.add(DataText.M_RAW);
        tl.add(MimeTypes.get(f));
        tl.add(data.meta.binary(f).length());
        table.contents.add(tl);
      }
      Close.close(data, context);
    } catch (final IOException ex) {
      return error(Util.message(ex));
    }
    out.println(table.sort().finish());
    return true;
  }
Exemple #8
0
  /**
   * Evaluates the tokenize function.
   *
   * @param ctx query context
   * @return function result
   * @throws QueryException query exception
   */
  private Value tokenize(final QueryContext ctx) throws QueryException {
    final byte[] val = checkEStr(expr[0], ctx);
    final Pattern p = pattern(expr[1], expr.length == 3 ? expr[2] : null, ctx);
    if (p.matcher("").matches()) REGROUP.thrw(info);

    final TokenList tl = new TokenList();
    final String str = string(val);
    if (!str.isEmpty()) {
      final Matcher m = p.matcher(str);
      int s = 0;
      while (m.find()) {
        tl.add(str.substring(s, m.start()));
        s = m.end();
      }
      tl.add(str.substring(s, str.length()));
    }
    return StrSeq.get(tl);
  }
Exemple #9
0
  /**
   * Lists all databases.
   *
   * @return success flag
   * @throws IOException I/O exception
   */
  private boolean list() throws IOException {
    final Table table = new Table();
    table.description = DATABASES_X;

    final boolean create = context.user.has(Perm.CREATE);
    table.header.add(T_NAME);
    table.header.add(RESOURCES);
    table.header.add(SIZE);
    if (create) table.header.add(INPUT_PATH);

    for (final String name : context.databases.listDBs()) {
      String file = null;
      long size = 0;
      int docs = 0;
      final MetaData meta = new MetaData(name, context);
      try {
        meta.read();
        size = meta.dbsize();
        docs = meta.ndocs;
        if (context.perm(Perm.READ, meta)) file = meta.original;
      } catch (final IOException ex) {
        file = ERROR;
      }

      // count number of raw files
      final IOFile dir = new IOFile(mprop.dbpath(name), M_RAW);
      final int bin = dir.descendants().size();

      // create entry
      if (file != null) {
        final TokenList tl = new TokenList(4);
        tl.add(name);
        tl.add(docs + bin);
        tl.add(size);
        if (create) tl.add(file);
        table.contents.add(tl);
      }
    }
    out.println(table.sort().finish());
    return true;
  }
Exemple #10
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());
    }
  }