Beispiel #1
0
        /// <summary>
        /// Evaluate the expression, returning the result as an <c>XdmValue</c> (that is,
        /// a sequence of nodes and/or atomic values).
        /// </summary>
        /// <remarks>
        /// Although a singleton result <i>may</i> be represented as an <c>XdmItem</c>, there is
        /// no guarantee that this will always be the case. If you know that the expression will return at
        /// most one node or atomic value, it is best to use the <c>EvaluateSingle</c> method, which
        /// does guarantee that an <c>XdmItem</c> (or null) will be returned.
        /// </remarks>
        /// <returns>
        /// An <c>XdmValue</c> representing the results of the expression.
        /// </returns>
        /// <exception cref="DynamicError">
        /// Throws <c>Saxon.Api.DynamicError</c> if the evaluation of the XPath expression fails
        /// with a dynamic error.
        /// </exception>

        public XdmValue Evaluate()
        {
            try {
                JSequence value = (JSequence)JSequenceExtent.makeSequenceExtent(
                    exp.iterate(dynamicContext));
                return(XdmValue.Wrap(value));
            } catch (net.sf.saxon.trans.XPathException err) {
                throw new DynamicError(err);
            }
        }
Beispiel #2
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)
        {
            try {
                JXdmValue[] vr = new JXdmValue[arguments.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    vr[i] = XdmValue.FromGroundedValueToJXdmValue(arguments[i].value);
                }
                JSequence result = evaluator.callFunction(function.UnderlyingQName(), vr).getUnderlyingValue();
                return(XdmValue.Wrap(result));
            } catch (JSaxonApiException e) {
                throw new DynamicError(e);
            }
        }
Beispiel #3
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());
                }
                JSequence[] vr = new JSequence[arguments.Length];

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