/** * Adds a local function. * * @param fun function instance * @param ii input info * @return function id * @throws QueryException query exception */ public int add(final UserFunc fun, final InputInfo ii) throws QueryException { final QNm name = fun.name; final byte[] uri = name.uri(); if (uri.length == 0) FUNNONS.thrw(ii, name.string()); if (NSGlobal.reserved(uri)) { if (fun.declared) NAMERES.thrw(ii, name.string()); funError(name, ii); } final byte[] ln = name.local(); for (int l = 0; l < funcs.length; ++l) { final QNm qn = funcs[l].name; final byte[] u = qn.uri(); final byte[] nm = qn.local(); if (eq(ln, nm) && eq(uri, u) && fun.args.length == funcs[l].args.length) { // declare function that has been called before if (!funcs[l].declared) { funcs[l] = fun; return l; } // duplicate declaration FUNCDEFINED.thrw(ii, fun.name.string()); } } // add function skeleton funcs = Array.add(funcs, fun); calls = Array.add(calls, new UserFuncCall[0]); return funcs.length - 1; }
/** * Finds similar function names and throws an error message. * * @param name function name * @param ii input info * @throws QueryException query exception */ public void funError(final QNm name, final InputInfo ii) throws QueryException { // find global function Functions.get().error(name, ii); // find similar local function final Levenshtein ls = new Levenshtein(); final byte[] nm = lc(name.local()); for (final UserFunc f : funcs) { if (ls.similar(nm, lc(f.name.local()), 0)) { FUNSIMILAR.thrw(ii, name.string(), f.name.string()); } } }
/** * Checks if the specified item is a string or element. * * @param it item to be checked * @return item * @throws QueryException query exception */ private Item checkElmStr(final Item it) throws QueryException { if (it instanceof AStr || TEST.eq(it)) return it; throw ELMSTRTYPE.thrw(info, Q_ENTRY.string(), it.type); }