Example #1
0
        //============================================================
        //	PUBLIC METHODS
        //============================================================
        #region Load(XPathNavigator source)
        /// <summary>
        /// Loads this <see cref="XmlRpcResponse"/> using the supplied <see cref="XPathNavigator"/>.
        /// </summary>
        /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param>
        /// <returns><b>true</b> if the <see cref="XmlRpcResponse"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns>
        /// <remarks>
        ///     <para>This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="XmlRpcResponse"/>.</para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public bool Load(XPathNavigator source)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            bool wasLoaded = false;

            //------------------------------------------------------------
            //	Validate parameter
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Attempt to extract response information
            //------------------------------------------------------------
            if (source.HasChildren)
            {
                XPathNavigator parametersNavigator = source.SelectSingleNode("params");
                XPathNavigator faultNavigator      = source.SelectSingleNode("fault");

                if (parametersNavigator != null)
                {
                    XPathNavigator valueNavigator = parametersNavigator.SelectSingleNode("param/value");
                    if (valueNavigator != null)
                    {
                        IXmlRpcValue value;
                        if (XmlRpcClient.TryParseValue(valueNavigator, out value))
                        {
                            responseParameter = value;
                            wasLoaded         = true;
                        }
                    }
                }

                if (faultNavigator != null)
                {
                    XPathNavigator structNavigator = faultNavigator.SelectSingleNode("value");
                    if (structNavigator != null)
                    {
                        XmlRpcStructureValue structure = new XmlRpcStructureValue();
                        if (structure.Load(structNavigator))
                        {
                            responseFault = structure;
                            wasLoaded     = true;
                        }
                    }
                }
            }

            return(wasLoaded);
        }
        /// <summary>
        /// Loads this <see cref="XmlRpcResponse"/> using the supplied <see cref="XPathNavigator"/>.
        /// </summary>
        /// <param name="source">The <see cref="XPathNavigator"/> to extract information from.</param>
        /// <returns><b>true</b> if the <see cref="XmlRpcResponse"/> was initialized using the supplied <paramref name="source"/>, otherwise <b>false</b>.</returns>
        /// <remarks>
        ///     <para>This method expects the supplied <paramref name="source"/> to be positioned on the XML element that represents a <see cref="XmlRpcResponse"/>.</para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        public bool Load(XPathNavigator source)
        {
            bool wasLoaded = false;

            Guard.ArgumentNotNull(source, "source");

            if (source.HasChildren)
            {
                XPathNavigator parametersNavigator = source.SelectSingleNode("params");
                XPathNavigator faultNavigator      = source.SelectSingleNode("fault");

                if (parametersNavigator != null)
                {
                    XPathNavigator valueNavigator = parametersNavigator.SelectSingleNode("param/value");
                    if (valueNavigator != null)
                    {
                        IXmlRpcValue value;
                        if (XmlRpcClient.TryParseValue(valueNavigator, out value))
                        {
                            responseParameter = value;
                            wasLoaded         = true;
                        }
                    }
                }

                if (faultNavigator != null)
                {
                    XPathNavigator structNavigator = faultNavigator.SelectSingleNode("value");
                    if (structNavigator != null)
                    {
                        XmlRpcStructureValue structure = new XmlRpcStructureValue();
                        if (structure.Load(structNavigator))
                        {
                            responseFault = structure;
                            wasLoaded     = true;
                        }
                    }
                }
            }

            return(wasLoaded);
        }
        /// <summary>
        /// Constructs a new <see cref="IXmlRpcValue"/> object from the specified <see cref="XPathNavigator"/>.
        /// Parameters specify the XML data source and the variable where the new <see cref="IXmlRpcValue"/> object is returned.
        /// </summary>
        /// <param name="source">A <see cref="XPathNavigator"/> that represents the XML data source to be parsed.</param>
        /// <param name="value">
        ///     When this method returns, contains an object that represents the <see cref="IXmlRpcValue"/> specified by the <paramref name="source"/>, or <b>null</b> if the conversion failed.
        ///     This parameter is passed uninitialized.
        /// </param>
        /// <returns>
        ///     <b>true</b> if <paramref name="source"/> was converted successfully; otherwise, <b>false</b>.
        ///     This operation returns <b>false</b> if the <paramref name="source"/> parameter is a null reference (Nothing in Visual Basic),
        ///     or represents XML data that is not in the expected format.
        /// </returns>
        /// <remarks>
        ///     The <paramref name="source"/> is expected to represent an XML-RPC <b>value</b> node.
        /// </remarks>
        public static bool TryParseValue(XPathNavigator source, out IXmlRpcValue value)
        {
            if (source == null || String.Compare(source.Name, "value", StringComparison.OrdinalIgnoreCase) != 0)
            {
                value = null;
                return(false);
            }

            if (source.HasChildren)
            {
                XPathNavigator navigator = source.CreateNavigator();
                if (navigator.MoveToFirstChild())
                {
                    if (String.Compare(navigator.Name, "i4", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        int scalar;
                        if (Int32.TryParse(navigator.Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out scalar))
                        {
                            value = new XmlRpcScalarValue(scalar);
                            return(true);
                        }
                    }
                    else if (String.Compare(navigator.Name, "int", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        int scalar;
                        if (Int32.TryParse(navigator.Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out scalar))
                        {
                            value = new XmlRpcScalarValue(scalar);
                            return(true);
                        }
                    }
                    else if (String.Compare(navigator.Name, "boolean", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        bool scalar;
                        if (XmlRpcClient.TryParseBoolean(navigator.Value, out scalar))
                        {
                            value = new XmlRpcScalarValue(scalar);
                            return(true);
                        }
                    }
                    else if (String.Compare(navigator.Name, "string", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        value = new XmlRpcScalarValue(navigator.Value);
                        return(true);
                    }
                    else if (String.Compare(navigator.Name, "double", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        double scalar;
                        if (Double.TryParse(navigator.Value, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out scalar))
                        {
                            value = new XmlRpcScalarValue(scalar);
                            return(true);
                        }
                    }
                    else if (String.Compare(navigator.Name, "dateTime.iso8601", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        DateTime scalar;
                        if (SyndicationDateTimeUtility.TryParseRfc3339DateTime(navigator.Value, out scalar))
                        {
                            value = new XmlRpcScalarValue(scalar);
                            return(true);
                        }
                    }
                    else if (String.Compare(navigator.Name, "base64", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (!String.IsNullOrEmpty(navigator.Value))
                        {
                            try
                            {
                                byte[] data = Convert.FromBase64String(navigator.Value);
                                value = new XmlRpcScalarValue(data);
                                return(true);
                            }
                            catch (FormatException)
                            {
                                value = null;
                                return(false);
                            }
                        }
                    }
                    else if (String.Compare(navigator.Name, "struct", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        XmlRpcStructureValue structure = new XmlRpcStructureValue();
                        if (structure.Load(source))
                        {
                            value = structure;
                            return(true);
                        }
                    }
                    else if (String.Compare(navigator.Name, "array", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        XmlRpcArrayValue array = new XmlRpcArrayValue();
                        if (array.Load(source))
                        {
                            value = array;
                            return(true);
                        }
                    }
                }
            }
            else if (!String.IsNullOrEmpty(source.Value))
            {
                value = new XmlRpcScalarValue(source.Value);
                return(true);
            }

            value = null;
            return(false);
        }