/** * Evaluates the replace function. * * @param val input value * @param ctx query context * @return function result * @throws QueryException query exception */ private Item replace(final byte[] val, final QueryContext ctx) throws QueryException { final byte[] rep = checkStr(expr[2], ctx); for (int i = 0; i < rep.length; ++i) { if (rep[i] == '\\') { if (i + 1 == rep.length || rep[i + 1] != '\\' && rep[i + 1] != '$') FUNREPBS.thrw(info); ++i; } if (rep[i] == '$' && (i == 0 || rep[i - 1] != '\\') && (i + 1 == rep.length || !digit(rep[i + 1]))) FUNREPDOL.thrw(info); } final Pattern p = pattern(expr[1], expr.length == 4 ? expr[3] : null, ctx); if (p.pattern().isEmpty()) REGROUP.thrw(info); String r = string(rep); if ((p.flags() & Pattern.LITERAL) != 0) { r = SLASH.matcher(BSLASH.matcher(r).replaceAll("\\\\\\\\")).replaceAll("\\\\\\$"); } try { return Str.get(p.matcher(string(val)).replaceAll(r)); } catch (final Exception ex) { if (ex.getMessage().contains("No group")) REGROUP.thrw(info); throw REGPAT.thrw(info, ex); } }
/** * 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(); }
@Override public String toString() { final boolean text = index.type() == IndexType.TEXT; final Function func = text ? Function._DB_TEXT_RANGE : Function._DB_ATTRIBUTE_RANGE; return func.toString(Str.get(ictx.data.meta.name), Str.get(index.min), Str.get(index.max)); }
/** * Extracts text entries. * * @param ctx query context * @return text entry * @throws QueryException query exception */ private ValueBuilder extractText(final QueryContext ctx) throws QueryException { final String enc = encoding(2, ARCH_ENCODING, ctx); final ValueBuilder vb = new ValueBuilder(); for (final byte[] b : extract(ctx)) vb.add(Str.get(encode(b, enc))); return vb; }