Example #1
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();
  }
Example #2
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;
  }