/** * Performs the test-libraries function (still experimental). * * @param ctx query context * @return resulting value * @throws QueryException query exception */ private Item testLibraries(final QueryContext ctx) throws QueryException { checkCreate(ctx); final TokenList tl = new TokenList(); final Iter ir = ctx.iter(expr[0]); for (Item it; (it = ir.next()) != null; ) tl.add(checkStr(it)); return new Suite(ctx, info).test(tl); }
@Override public Value value(final QueryContext qc) throws QueryException { final ArrayList<User> users = qc.context.users.users(null, qc.context); final TokenList tl = new TokenList(users.size()); for (final User user : users) tl.add(user.name()); return StrSeq.get(tl); }
@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; } }; }
/** * 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; }
/** * Returns all tokens of the query. * * @param qc query context * @return token list * @throws QueryException query exception */ private TokenList tokens(final QueryContext qc) throws QueryException { final TokenList tl = new TokenList(); final Iter ir = qc.iter(query); for (byte[] qu; (qu = nextToken(ir)) != null; ) { // skip empty tokens if not all results are needed if (qu.length != 0 || mode == FTMode.ALL || mode == FTMode.ALL_WORDS) tl.add(qu); } return tl; }
/** * 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); }
@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()); } }