Beispiel #1
0
        public override object Evaluate(XPathNodeIterator nodeIterator)
        {
            if (xsltContext == null)
            {
                throw XPathException.Create(SR.Xp_NoContext);
            }

            // calculate arguments:
            object[] argVals = new object[_args.Count];
            for (int i = 0; i < _args.Count; i++)
            {
                argVals[i] = _args[i].Evaluate(nodeIterator);
                if (argVals[i] is XPathNodeIterator)
                {// ForBack Compat. To protect our queries from users.
                    argVals[i] = new XPathSelectionIterator(nodeIterator.Current, _args[i]);
                }
            }
            try
            {
                return(ProcessResult(_function.Invoke(xsltContext, argVals, nodeIterator.Current)));
            }
            catch (Exception ex)
            {
                throw XPathException.Create(SR.Xp_FunctionFailed, QName, ex);
            }
        }
Beispiel #2
0
        public override object Evaluate(BaseIterator iter)
        {
            XPathResultType[]    argTypes            = this.GetArgTypes(iter);
            IXsltContextFunction xsltContextFunction = null;
            XsltContext          xsltContext         = iter.NamespaceManager as XsltContext;

            if (xsltContext != null)
            {
                if (this.resolvedName)
                {
                    xsltContextFunction = xsltContext.ResolveFunction(this._name, argTypes);
                }
                else
                {
                    xsltContextFunction = xsltContext.ResolveFunction(this._name.Namespace, this._name.Name, argTypes);
                }
            }
            if (xsltContextFunction == null)
            {
                throw new XPathException("function " + this._name.ToString() + " not found");
            }
            object[] array = new object[this._args.Count];
            if (xsltContextFunction.Maxargs != 0)
            {
                XPathResultType[] argTypes2 = xsltContextFunction.ArgTypes;
                for (int i = 0; i < this._args.Count; i++)
                {
                    XPathResultType type;
                    if (argTypes2 == null)
                    {
                        type = XPathResultType.Any;
                    }
                    else if (i < argTypes2.Length)
                    {
                        type = argTypes2[i];
                    }
                    else
                    {
                        type = argTypes2[argTypes2.Length - 1];
                    }
                    Expression expression = (Expression)this._args[i];
                    object     obj        = expression.EvaluateAs(iter, type);
                    array[i] = obj;
                }
            }
            return(xsltContextFunction.Invoke(xsltContext, array, iter.Current));
        }
Beispiel #3
0
        public override object Evaluate(XPathNodeIterator nodeIterator)
        {
            if (xsltContext == null)
            {
                throw XPathException.Create(Res.Xp_NoContext);
            }

            // calculate arguments:
            object[] argVals = new object[args.Count];
            for (int i = 0; i < args.Count; i++)
            {
                argVals[i] = args[i].Evaluate(nodeIterator);
                if (argVals[i] is XPathNodeIterator)
                {
                    argVals[i] = new XPathSelectionIterator(nodeIterator.Current, args[i]);
                }
            }
            try {
                return(ProcessResult(function.Invoke(xsltContext, argVals, nodeIterator.Current)));
            } catch (Exception ex) {
                throw XPathException.Create(Res.Xp_FunctionFailed, QName, ex);
            }
        }
Beispiel #4
0
        private void InvokeFunction(XPathNavigator qy)
        {
            IXsltContextFunction function = this.Function;

            // calculate arguments:
            Debug.Assert(_ArgArray != null && _ArgArray.Length == _ArgList.Count);
            for (int i = _ArgList.Count - 1; 0 <= i; i--)
            {
                IQuery arg = (IQuery)_ArgList[i];
                if (arg.ReturnType() == XPathResultType.NodeSet)
                {
                    _ArgArray[i] = new XPathQueryIterator(arg, qy.Clone());
                }
                else
                {
                    _ArgArray[i] = arg.getValue(qy, null);
                }
            }

            try {
                object result = function.Invoke(_XsltContext, _ArgArray, qy);

                if (result == null)
                {
                    _ResultQuery = new OperandQuery(String.Empty, XPathResultType.String);
                }
                else
                {
                    XPathResultType returnedType = function.ReturnType;
                    if (returnedType == XPathResultType.Any)
                    {
                        // If function is untyped get returned type from real result
                        returnedType = XsltCompileContext.GetXPathType(result.GetType());
                    }
                    switch (returnedType)
                    {
                    case XPathResultType.String:
                        // trick. As soon XPathResultType.Navigator will be distinct type rid of from it.
                        //_ResultQuery = new OperandQuery( result, XPathResultType.String );
                        if (result is XPathNavigator)
                        {
                            _ResultQuery = new NavigatorQuery((XPathNavigator)result);
                        }
                        else
                        {
                            _ResultQuery = new OperandQuery(result, XPathResultType.String);
                        }
                        break;

                    case XPathResultType.Boolean:
                        _ResultQuery = new OperandQuery(result, XPathResultType.Boolean);
                        break;

                    case XPathResultType.Number:
                        _ResultQuery = new OperandQuery(XmlConvert.ToXPathDouble(result), XPathResultType.Number);
                        break;

                    case XPathResultType.NodeSet:
                        if (result is ResetableIterator)
                        {
                            _ResultQuery = new XmlIteratorQuery((ResetableIterator)result);
                        }
                        else
                        {
                            Debug.Assert(false, "Unexpected type of XPathNodeIterator");
                            throw new NotSupportedException(Res.GetString(Res.XmlUnsupportedType, result.GetType().FullName));
                        }
                        break;

//                  case XPathResultType.Navigator :
//                      _ResultQuery = new NavigatorQuery((XPathNavigator)result);
//                      break;
                    default:
                        _ResultQuery = new OperandQuery(result.ToString(), XPathResultType.String);
                        break;
                    }
                }
            }
            catch (Exception ex) {
                string qname = _Prefix != string.Empty ? _Prefix + ":" + _Name : _Name;
                throw new XsltException(Res.Xslt_FunctionFailed, new string[] { qname }, ex);
            }
        }