/** * 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(); }
/** * 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); }