示例#1
0
文件: ANode.java 项目: fpapai/basex
 @Override
 public final boolean eq(
     final Item it, final Collation coll, final StaticContext sc, final InputInfo ii)
     throws QueryException {
   return it.type.isUntyped()
       ? coll == null
           ? Token.eq(string(), it.string(ii))
           : coll.compare(string(), it.string(ii)) == 0
       : it.eq(this, coll, sc, ii);
 }
示例#2
0
文件: ANode.java 项目: fpapai/basex
 @Override
 public final int diff(final Item it, final Collation coll, final InputInfo ii)
     throws QueryException {
   return it.type.isUntyped()
       ? coll == null ? Token.diff(string(), it.string(ii)) : coll.compare(string(), it.string(ii))
       : -it.diff(this, coll, ii);
 }
示例#3
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final byte[] s;
   if (exprs.length == 0) {
     final Item it = ctxValue(qc).item(qc, info);
     if (it instanceof FItem) throw FISTRING_X.get(ii, it.type);
     s = it == null ? Token.EMPTY : it.string(ii);
   } else {
     s = toEmptyToken(arg(0, qc), qc);
   }
   return Int.get(Token.length(s));
 }
示例#4
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final String form = string(toToken(exprs[0], qc));
   final int es = exprs.length;
   final Object[] args = new Object[es - 1];
   for (int e = 1; e < es; e++) {
     final Item it = exprs[e].item(qc, info);
     args[e - 1] = it == null ? null : it.type.isUntyped() ? string(it.string(info)) : it.toJava();
   }
   try {
     return Str.get(String.format(form, args));
   } catch (final RuntimeException ex) {
     throw ERRFORMAT_X_X.get(info, Util.className(ex), ex);
   }
 }
示例#5
0
  /**
   * Adds the specified entry to the output stream.
   *
   * @param entry entry descriptor
   * @param con contents
   * @param out output archive
   * @param level default compression level
   * @throws QueryException query exception
   * @throws IOException I/O exception
   */
  private void add(final Item entry, final Item con, final ArchiveOut out, final int level)
      throws QueryException, IOException {

    // create new zip entry
    final String name = string(entry.string(info));
    if (name.isEmpty()) ARCH_EMPTY.thrw(info);
    final ZipEntry ze = new ZipEntry(name);
    String en = null;

    // compression level
    byte[] lvl = null;
    if (entry instanceof ANode) {
      final ANode el = (ANode) entry;
      lvl = el.attribute(Q_LEVEL);

      // last modified
      final byte[] mod = el.attribute(Q_LAST_MOD);
      if (mod != null) {
        try {
          ze.setTime(new Int(new Dtm(mod, info)).itr());
        } catch (final QueryException qe) {
          ARCH_DATETIME.thrw(info, mod);
        }
      }

      // encoding
      final byte[] enc = el.attribute(Q_ENCODING);
      if (enc != null) {
        en = string(enc);
        if (!Charset.isSupported(en)) ARCH_ENCODING.thrw(info, enc);
      }
    }

    // data to be compressed
    byte[] val = checkStrBin(con);
    if (con instanceof AStr && en != null && en != Token.UTF8) val = encode(val, en);

    try {
      out.level(lvl == null ? level : toInt(lvl));
    } catch (final IllegalArgumentException ex) {
      ARCH_LEVEL.thrw(info, lvl);
    }
    out.write(ze, val);
  }
示例#6
0
  /**
   * Creates annotation child elements.
   *
   * @param anns annotations
   * @param parent parent element
   * @param uri include uri
   * @throws QueryException query exception
   */
  final void annotation(final AnnList anns, final FElem parent, final boolean uri)
      throws QueryException {

    for (final Ann ann : anns) {
      final FElem annotation = elem("annotation", parent);
      if (ann.sig != null) {
        annotation.add("name", ann.sig.id());
        if (uri) annotation.add("uri", ann.sig.uri);
      } else {
        annotation.add("name", ann.name.string());
        if (uri) annotation.add("uri", ann.name.uri());
      }

      for (final Item it : ann.args) {
        final FElem literal = elem("literal", annotation);
        literal.add("type", it.type.toString()).add(it.string(null));
      }
    }
  }
示例#7
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());
    }
  }
示例#8
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final Item it = unparsedText(qc, false, false);
   return it == null ? null : parse(it.string(info), false, qc, info);
 }
示例#9
0
 /**
  * Checks if the specified item is a string or binary item.
  *
  * @param it item to be checked
  * @return byte array
  * @throws QueryException query exception
  */
 protected final byte[] toBytes(final Item it) throws QueryException {
   if (checkNoEmpty(it).type.isStringOrUntyped()) return it.string(info);
   if (it instanceof Bin) return ((Bin) it).binary(info);
   throw STRBIN_X_X.get(info, it.type, it);
 }
示例#10
0
 /**
  * Checks if the specified item is a boolean. Returns the boolean or throws an exception.
  *
  * @param it item be checked
  * @return boolean
  * @throws QueryException query exception
  */
 protected final boolean toBoolean(final Item it) throws QueryException {
   final Type ip = checkNoEmpty(it, AtomType.BLN).type;
   if (ip == AtomType.BLN) return it.bool(info);
   if (ip.isUntyped()) return Bln.parse(it.string(info), info);
   throw castError(it, AtomType.BLN, info);
 }
示例#11
0
 /**
  * Checks if the specified non-empty item is a string. Returns its value as token or throws an
  * exception.
  *
  * @param it item to be checked
  * @return token
  * @throws QueryException query exception
  */
 protected final byte[] toToken(final Item it) throws QueryException {
   final Type ip = it.type;
   if (ip.isStringOrUntyped()) return it.string(info);
   throw it instanceof FItem ? FIATOM_X.get(info, it.type) : castError(it, AtomType.STR, info);
 }
示例#12
0
文件: B64.java 项目: runeengh/basex
 @Override
 public final int diff(final Item it, final Collation coll, final InputInfo ii)
     throws QueryException {
   return Token.diff(
       binary(ii), it instanceof Bin ? ((Bin) it).binary(ii) : decode(it.string(ii), ii));
 }