Example #1
0
        ///<summary>
        /// Call a global user-defined function in the compiled query.
        ///</summary>
        ///<remarks>
        /// If this is called more than once (to evaluate the same function repeatedly with different arguments,
        /// or to evaluate different functions) then the sequence of evaluations uses the same values of global
        /// variables including external variables (query parameters); the effect of any changes made to query parameters
        /// between calls is undefined.
        /// </remarks>
        /// <param name="function">
        /// The name of the function to be called
        /// </param>
        /// <param name="arguments">
        /// The values of the arguments to be supplied to the function. These
        /// must be of the correct type as defined in the function signature (there is no automatic
        /// conversion to the required type).
        /// </param>
        /// <exception cref="ArgumentException">If no function has been defined with the given name and arity
        /// or if any of the arguments does not match its required type according to the function
        /// signature.</exception>
        /// <exception cref="DynamicError">If a dynamic error occurs in evaluating the function.
        /// </exception>

        public XdmValue CallFunction(QName function, XdmValue[] arguments)
        {
            JUserFunction fn = exp.getMainModule().getUserDefinedFunction(
                function.Uri, function.LocalName, arguments.Length);

            if (fn == null)
            {
                throw new ArgumentException("No function with name " + function.ClarkName +
                                            " and arity " + arguments.Length + " has been declared in the query");
            }
            try {
                // TODO: use the same controller in other interfaces such as run(), and expose it in a trapdoor API
                if (controller == null)
                {
                    controller = exp.newController(context);
                    context.initializeController(controller);
                    controller.initializeController(context.getParameters());
                }
                Sequence[] vr = new Sequence[arguments.Length];

                for (int i = 0; i < arguments.Length; i++)
                {
                    SequenceType type = fn.getParameterDefinitions()[i].getRequiredType();
                    vr[i] = arguments[i].Unwrap();
                    if (!type.matches(vr[i], controller.getConfiguration()))
                    {
                        throw new ArgumentException("Argument " + (i + 1) +
                                                    " of function " + function.ClarkName +
                                                    " does not match the required type " + type.toString());
                    }
                }
                Sequence result = fn.call(vr, controller);
                return(XdmValue.Wrap(result));
            } catch (JXPathException e) {
                throw new DynamicError(e);
            }
        }