/** * Creates an XQuery representation for the specified query. * * @param query query * @param ctx database context * @param root start from root node * @return query */ public static String find(final String query, final Context ctx, final boolean root) { // treat input as XQuery if (query.startsWith("/")) return query; final boolean r = root || ctx.root(); if (query.isEmpty()) return r ? "/" : "."; // parse user input final String qu = query.replaceAll(" \\+", " "); final String[] terms = split(qu); String pre = ""; String preds = ""; final String tag = "*"; for (String term : terms) { if (term.startsWith("@=")) { preds += "[@* = \"" + term.substring(2) + "\"]"; } else if (term.startsWith("=")) { preds += "[text() = \"" + term.substring(1) + "\"]"; } else if (term.startsWith("~")) { preds += "[text() contains text \"" + term.substring(1) + "\" using fuzzy]"; } else if (term.startsWith("@")) { if (term.length() == 1) continue; preds += "[@* contains text \"" + term.substring(1) + "\"]"; term = term.substring(1); // add valid name tests if (XMLToken.isName(token(term))) { pre += (r ? "" : ".") + "//@" + term + " | "; } } else { preds += "[text() contains text \"" + term + "\"]"; // add valid name tests if (XMLToken.isName(token(term))) { pre += (r ? "/" : "") + Axis.DESC + "::*:" + term + " | "; } } } if (pre.isEmpty() && preds.isEmpty()) return root ? "/" : "."; // create final string final TokenBuilder tb = new TokenBuilder(); tb.add(pre + (r ? "/" : "") + Axis.DESCORSELF + "::" + tag + preds); return tb.toString(); }
/** * Checks the specified template and adds a variable. * * @param tmp template string * @param type allowed type * @param declared variable declaration flags * @return resulting variable * @throws QueryException query exception */ private QNm checkVariable(final String tmp, final Type type, final boolean[] declared) throws QueryException { final Var[] args = function.args; final Matcher m = TEMPLATE.matcher(tmp); if (!m.find()) error(INV_TEMPLATE, tmp); final byte[] vn = token(m.group(1)); if (!XMLToken.isQName(vn)) error(INV_VARNAME, vn); final QNm qnm = new QNm(vn, context); int r = -1; while (++r < args.length && !args[r].name.eq(qnm)) ; if (r == args.length) error(UNKNOWN_VAR, vn); if (declared[r]) error(VAR_ASSIGNED, vn); final SeqType st = args[r].declaredType(); if (args[r].checksType() && !st.type.instanceOf(type)) error(INV_VARTYPE, vn, type); declared[r] = true; return qnm; }
/** * Splits the string and returns an array. * * @param str string to be split * @return array */ private static String[] split(final String str) { final int l = str.length(); final String[] split = new String[l]; int s = 0; char delim = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < l; ++i) { final char c = str.charAt(i); if (delim == 0) { if (c == '\'' || c == '"') { delim = c; } else if (!XMLToken.isChar(c) && c != '@' && c != '=' && c != '<' && c != '>' && c != '~') { if (sb.length() != 0) { split[s++] = sb.toString(); sb.setLength(0); } } else { sb.append(c); } } else { if (c == delim) { delim = 0; if (sb.length() != 0) { split[s++] = sb.toString(); sb.setLength(0); } } else { if (c != '\'' && c != '"') sb.append(c); } } } if (sb.length() != 0) split[s++] = sb.toString(); return Array.copyOf(split, s); }