Exemple #1
0
  /**
   * Checks if the predicates are successful for the specified item.
   *
   * @param it item to be checked
   * @param qc query context
   * @return result of check
   * @throws QueryException query exception
   */
  protected final boolean preds(final Item it, final QueryContext qc) throws QueryException {
    if (preds.length == 0) return true;

    // set context value and position
    final Value cv = qc.value;
    try {
      if (qc.scoring) {
        double s = 0;
        for (final Expr p : preds) {
          qc.value = it;
          final Item i = p.test(qc, info);
          if (i == null) return false;
          s += i.score();
        }
        it.score(Scoring.avg(s, preds.length));
      } else {
        for (final Expr p : preds) {
          qc.value = it;
          if (p.test(qc, info) == null) return false;
        }
      }
      return true;
    } finally {
      qc.value = cv;
    }
  }
Exemple #2
0
  @Override
  public Value value(final QueryContext ctx) throws QueryException {
    final int o = (int) ctx.resources.output.size();
    final Updates updates = ctx.resources.updates();
    final ContextModifier tmp = updates.mod;
    final TransformModifier pu = new TransformModifier();
    updates.mod = pu;

    try {
      for (final Let fo : copies) {
        final Iter ir = ctx.iter(fo.expr);
        Item i = ir.next();
        if (!(i instanceof ANode) || ir.next() != null) throw UPCOPYMULT.get(fo.info, fo.var.name);

        // copy node to main memory data instance
        i = ((ANode) i).dbCopy(ctx.context.options);
        // add resulting node to variable
        ctx.set(fo.var, i, info);
        pu.addData(i.data());
      }
      final Value v = ctx.value(expr[0]);
      if (!v.isEmpty()) throw BASEX_MOD.get(info);

      updates.prepare();
      updates.apply();
      return ctx.value(expr[1]);
    } finally {
      ctx.resources.output.size(o);
      updates.mod = tmp;
    }
  }
Exemple #3
0
 /**
  * Checks if the items can be compared. Items are comparable
  *
  * @param b second item
  * @return result of check
  */
 public final boolean comparable(final Item b) {
   return type == b.type
       || num() && b.num()
       || (unt() || str()) && (b.str() || b.unt())
       || dur() && b.dur()
       || func();
 }
Exemple #4
0
  /**
   * Checks two keys for equality.
   *
   * @param its1 first keys
   * @param its2 second keys
   * @param coll collations
   * @return {@code true} if the compare as equal, {@code false} otherwise
   * @throws QueryException query exception
   */
  private boolean eq(final Item[] its1, final Item[] its2, final Collation[] coll)
      throws QueryException {

    final int il = its1.length;
    for (int i = 0; i < il; i++) {
      final Item it1 = its1[i], it2 = its2[i];
      if (it1 == null ^ it2 == null || it1 != null && !it1.equiv(it2, coll[i], info)) return false;
    }
    return true;
  }
  /**
   * 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));
      }
    }
  }
Exemple #6
0
  @Override
  public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
    // compute scoring
    if (qc.scoring) {
      double s = 0;
      boolean f = false;
      for (final Expr expr : exprs) {
        final Item it = expr.ebv(qc, info);
        f |= it.bool(ii);
        s += it.score();
      }
      return Bln.get(f, Scoring.avg(s, exprs.length));
    }

    // standard evaluation
    for (final Expr expr : exprs) {
      if (expr.ebv(qc, info).bool(ii)) return Bln.TRUE;
    }
    return Bln.FALSE;
  }
Exemple #7
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);
  }
Exemple #8
0
 /**
  * Checks if the specified, non-empty item is a double. Returns the double or throws an exception.
  *
  * @param it item to be checked
  * @return number
  * @throws QueryException query exception
  */
 private ANum toNumber(final Item it) throws QueryException {
   if (it.type.isUntyped()) return Dbl.get(it.dbl(info));
   if (it instanceof ANum) return (ANum) it;
   throw numberError(this, it);
 }
Exemple #9
0
 @Override
 public final Item atomItem(final QueryContext qc, final InputInfo ii) throws QueryException {
   final Item it1 = item(qc, info);
   return it1 == null ? null : it1.atomItem(info);
 }
Exemple #10
0
 @Override
 public final Item test(final QueryContext qc, final InputInfo ii) throws QueryException {
   final Item it = ebv(qc, info);
   return (it instanceof ANum ? it.dbl(info) == qc.pos : it.bool(info)) ? it : null;
 }
Exemple #11
0
 /**
  * Checks if the specified item yields a node or item. Returns the item or throws an exception.
  *
  * @param it item to be checked (can be {@code null})
  * @return node or atomized item
  * @throws QueryException query exception
  */
 protected final Item toNodeOrAtomItem(final Item it) throws QueryException {
   return it == null || it instanceof ANode ? it : it.atomItem(info);
 }
Exemple #12
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);
 }
Exemple #13
0
 @Override
 public Iter iter(final QueryContext qc) throws QueryException {
   final Item it = item(qc, info);
   return it != null ? it.iter() : Empty.ITER;
 }
Exemple #14
0
 /**
  * Checks if the specified item is a number. Returns a token representation or throws an
  * exception.
  *
  * @param it item to be checked
  * @return number
  * @throws QueryException query exception
  */
 protected final long toLong(final Item it) throws QueryException {
   final Type ip = checkNoEmpty(it, AtomType.ITR).type;
   if (ip.instanceOf(AtomType.ITR) || ip.isUntyped()) return it.itr(info);
   throw castError(it, AtomType.ITR, info);
 }
Exemple #15
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);
 }
Exemple #16
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);
 }
Exemple #17
0
 /**
  * Checks if the specified item is a double. Returns the double or throws an exception.
  *
  * @param it item
  * @return double
  * @throws QueryException query exception
  */
 protected final double toDouble(final Item it) throws QueryException {
   if (checkNoEmpty(it, AtomType.DBL).type.isNumberOrUntyped()) return it.dbl(info);
   throw numberError(this, it);
 }
Exemple #18
0
  /**
   * Checks the items for equivalence.
   *
   * @param ii input info
   * @param it item to be compared
   * @return result of check
   * @throws QueryException query exception
   */
  public final boolean equiv(final InputInfo ii, final Item it) throws QueryException {

    // check if both values are NaN, or if values are equal..
    return (this == Dbl.NAN || this == Flt.NAN) && it.num() && Double.isNaN(it.dbl(ii))
        || comparable(it) && eq(ii, it);
  }
Exemple #19
0
 /**
  * Checks if the specified expression yields a float. Returns the float or throws an exception.
  *
  * @param ex expression to be evaluated
  * @param qc query context
  * @return float
  * @throws QueryException query exception
  */
 protected final float toFloat(final Expr ex, final QueryContext qc) throws QueryException {
   final Item it = ex.atomItem(qc, info);
   if (checkNoEmpty(it, AtomType.FLT).type.isNumberOrUntyped()) return it.flt(info);
   throw numberError(this, it);
 }