Esempio n. 1
0
 /**
  * Checks if the specified item is a QName. Returns the item or throws an exception.
  *
  * @param it item
  * @param empty allow empty result
  * @return QNm item
  * @throws QueryException query exception
  */
 protected final QNm toQNm(final Item it, final boolean empty) throws QueryException {
   if (empty && it == null) return null;
   final Type ip = checkNoEmpty(it, AtomType.QNM).type;
   if (ip == AtomType.QNM) return (QNm) it;
   if (ip.isUntyped()) throw NSSENS_X_X.get(info, ip, AtomType.QNM);
   throw castError(it, AtomType.QNM, info);
 }
Esempio n. 2
0
 /**
  * Returns the new target type, or {@code null} if conversion is not necessary.
  *
  * @param curr old item
  * @param it new item
  * @return result (or {@code null})
  * @throws QueryException query exception
  */
 private AtomType numType(final Item curr, final Item it) throws QueryException {
   final Type ti = it.type;
   if (ti.isUntyped()) return DBL;
   final Type tc = curr.type;
   if (!(it instanceof ANum)) throw EXPTYPE_X_X_X.get(info, tc, ti, it);
   return tc == ti ? null : tc == DBL || ti == DBL ? DBL : tc == FLT || ti == FLT ? FLT : null;
 }
Esempio n. 3
0
  /**
   * Converts the specified result to an XQuery value.
   *
   * @param obj result object
   * @param qc query context
   * @param sc static context
   * @return value
   * @throws QueryException query exception
   */
  public static Value toValue(final Object obj, final QueryContext qc, final StaticContext sc)
      throws QueryException {

    if (obj == null) return Empty.SEQ;
    if (obj instanceof Value) return (Value) obj;
    if (obj instanceof Iter) return ((Iter) obj).value();
    // find XQuery mapping for specified type
    final Type type = type(obj);
    if (type != null) return type.cast(obj, qc, sc, null);

    // primitive arrays
    if (obj instanceof byte[]) return BytSeq.get((byte[]) obj);
    if (obj instanceof long[]) return IntSeq.get((long[]) obj, AtomType.ITR);
    if (obj instanceof char[]) return Str.get(new String((char[]) obj));
    if (obj instanceof boolean[]) return BlnSeq.get((boolean[]) obj);
    if (obj instanceof double[]) return DblSeq.get((double[]) obj);
    if (obj instanceof float[]) return FltSeq.get((float[]) obj);

    // no array: return Java type
    if (!obj.getClass().isArray()) return new Jav(obj, qc);

    // empty array
    final int s = Array.getLength(obj);
    if (s == 0) return Empty.SEQ;
    // string array
    if (obj instanceof String[]) {
      final String[] r = (String[]) obj;
      final byte[][] b = new byte[r.length][];
      for (int v = 0; v < s; v++) b[v] = token(r[v]);
      return StrSeq.get(b);
    }
    // character array
    if (obj instanceof char[][]) {
      final char[][] r = (char[][]) obj;
      final byte[][] b = new byte[r.length][];
      for (int v = 0; v < s; v++) b[v] = token(new String(r[v]));
      return StrSeq.get(b);
    }
    // short array
    if (obj instanceof short[]) {
      final short[] r = (short[]) obj;
      final long[] b = new long[r.length];
      for (int v = 0; v < s; v++) b[v] = r[v];
      return IntSeq.get(b, AtomType.SHR);
    }
    // integer array
    if (obj instanceof int[]) {
      final int[] r = (int[]) obj;
      final long[] b = new long[r.length];
      for (int v = 0; v < s; v++) b[v] = r[v];
      return IntSeq.get(b, AtomType.INT);
    }
    // any other array (also nested ones)
    final Object[] objs = (Object[]) obj;
    final ValueBuilder vb = new ValueBuilder(objs.length);
    for (final Object o : objs) vb.add(toValue(o, qc, sc));
    return vb.value();
  }
Esempio n. 4
0
 @Override
 public Expr optimize(final QueryContext ctx, final VarScope scp) throws QueryException {
   final SeqType s0 = expr[0].type();
   final SeqType s1 = expr[1].type();
   final Type t0 = s0.type;
   final Type t1 = s1.type;
   if (t0.isNumberOrUntyped() && t1.isNumberOrUntyped()) {
     final Occ occ = s0.one() && s1.one() ? Occ.ONE : Occ.ZERO_ONE;
     type = SeqType.get(Calc.type(t0, t1), occ);
   } else if (s0.one() && s1.one()) {
     type = SeqType.ITEM;
   }
   return optPre(oneIsEmpty() ? null : allAreValues() ? item(ctx, info) : this, ctx);
 }
Esempio n. 5
0
  /**
   * Returns a minimum or maximum item.
   *
   * @param cmp comparator
   * @param qc query context
   * @return resulting item
   * @throws QueryException query exception
   */
  Item minmax(final OpV cmp, final QueryContext qc) throws QueryException {
    final Collation coll = toCollation(1, qc);

    final Iter iter = exprs[0].atomIter(qc, info);
    Item curr = iter.next();
    if (curr == null) return null;

    // check if first item is comparable
    cmp.eval(curr, curr, coll, sc, info);

    // strings
    if (curr instanceof AStr) {
      for (Item it; (it = iter.next()) != null; ) {
        if (!(it instanceof AStr)) throw EXPTYPE_X_X_X.get(info, curr.type, it.type, it);
        final Type rt = curr.type, ri = it.type;
        if (cmp.eval(curr, it, coll, sc, info)) curr = it;
        if (rt != ri && curr.type == URI) curr = STR.cast(curr, qc, sc, info);
      }
      return curr;
    }
    // dates, durations, booleans, binary values
    if (curr instanceof ADate || curr instanceof Dur || curr instanceof Bin || curr.type == BLN) {
      for (Item it; (it = iter.next()) != null; ) {
        if (curr.type != it.type) throw EXPTYPE_X_X_X.get(info, curr.type, it.type, it);
        if (cmp.eval(curr, it, coll, sc, info)) curr = it;
      }
      return curr;
    }
    // numbers
    if (curr.type.isUntyped()) curr = DBL.cast(curr, qc, sc, info);
    for (Item it; (it = iter.next()) != null; ) {
      final Type type = numType(curr, it);
      if (cmp.eval(curr, it, coll, sc, info) || Double.isNaN(it.dbl(info))) curr = it;
      if (type != null) curr = (Item) type.cast(curr, qc, sc, info);
    }
    return curr;
  }
Esempio n. 6
0
 @Override
 public Test intersect(final Test other) {
   if (other instanceof NodeTest) {
     final NodeTest o = (NodeTest) other;
     if (type != null && o.type != null && type != o.type) return null;
     final NodeType nt = type != null ? type : o.type;
     if (name != null && o.name != null && !name.eq(o.name)) return null;
     final QNm n = name != null ? name : o.name;
     final boolean both = ext != null && o.ext != null;
     final Type e = ext == null ? o.ext : o.ext == null ? ext : ext.intersect(o.ext);
     return both && e == null ? null : new NodeTest(nt, n, e, strip || o.strip);
   }
   if (other instanceof KindTest) {
     return type.instanceOf(other.type) ? this : null;
   }
   if (other instanceof NameTest) {
     throw Util.notExpected(other);
   }
   return null;
 }
Esempio n. 7
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);
 }
Esempio n. 8
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);
 }
Esempio n. 9
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);
 }