/// <summary>
 /// Deserializes an XML-RPC request from a request stream.
 /// </summary>
 /// <param name="requestStream">the <see cref="System.IO.Stream"/> to read</param>
 /// <param name="config">the context configuration</param>
 /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
 /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> read from the reader</returns>
 /// <exception cref="System.Xml.XmlException">failed parsing the request XML</exception>
 public IXmlRpcRequest ReadRequest(Stream requestStream, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     using (XmlReader reader = new XmlTextReader(requestStream))
     {
         return ReadRequest(reader, config, typeSerializerFactory);
     }
 }
        private void WriteMember(XmlWriter writer, Object obj, MemberInfo mi,
            IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            if (Attribute.IsDefined(mi, typeof(XmlRpcIgnoreAttribute)))
                return;

            Object value = null;
            if (mi is PropertyInfo)
                value = ((PropertyInfo)mi).GetValue(obj, null);
            else if (mi is FieldInfo)
                value = ((FieldInfo)mi).GetValue(obj);

            MissingMemberAction action = config.MissingMemberAction;
            // TODO check member attribute

            XmlRpcMemberAttribute ma = (XmlRpcMemberAttribute)Attribute.GetCustomAttribute(mi, typeof(XmlRpcMemberAttribute));
            String memberName = (ma != null && !String.IsNullOrEmpty(ma.Name)) ? ma.Name : mi.Name;

            if (value == null)
            {
                if (action == MissingMemberAction.Ignore)
                    return;
                else if (action == MissingMemberAction.Error)
                    throw new XmlRpcException("Missing non-optional member: " + memberName);
            }

            writer.WriteStartElement(XmlRpcSpec.MEMBER_TAG);
            writer.WriteElementString(XmlRpcSpec.MEMBER_NAME_TAG, memberName);
            WriteValue(writer, value, config, typeSerializerFactory, nestedObjs);
            writer.WriteEndElement();
        }
Beispiel #3
0
 /// <summary>
 /// Deserializes an XML-RPC request from a request stream.
 /// </summary>
 /// <param name="requestStream">the <see cref="System.IO.Stream"/> to read</param>
 /// <param name="config">the context configuration</param>
 /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
 /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> read from the reader</returns>
 /// <exception cref="System.Xml.XmlException">failed parsing the request XML</exception>
 public IXmlRpcRequest ReadRequest(Stream requestStream, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     using (XmlReader reader = new XmlTextReader(requestStream))
     {
         return(ReadRequest(reader, config, typeSerializerFactory));
     }
 }
Beispiel #4
0
 /// <summary>
 /// Serializes an XML-RPC request to a request stream.
 /// </summary>
 /// <param name="requestStream">the <see cref="System.IO.Stream"/> to write</param>
 /// <param name="request">the <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> to serialize</param>
 /// <param name="config">the context configuration</param>
 /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
 /// <exception cref="System.Xml.XmlException">failed writing the request XML</exception>
 public void WriteRequest(Stream requestStream, IXmlRpcRequest request, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     using (XmlWriter writer = _xmlWriterFactory.GetXmlWriter(config, requestStream))
     {
         WriteRequest(writer, request, config, typeSerializerFactory);
     }
 }
Beispiel #5
0
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
            writer.WriteStartElement(XmlRpcSpec.STRUCT_TAG);

            foreach (DictionaryEntry entry in (IDictionary)obj)
            {
                writer.WriteStartElement(XmlRpcSpec.MEMBER_TAG);

                writer.WriteStartElement(XmlRpcSpec.MEMBER_NAME_TAG);
                if (config.EnabledForExtensions && !(entry.Key is String))
                {
                    WriteValue(writer, entry.Key, config, typeSerializerFactory, nestedObjs);
                }
                else
                {
                    writer.WriteString(entry.Key.ToString());
                }
                writer.WriteEndElement();

                WriteValue(writer, entry.Value, config, typeSerializerFactory, nestedObjs);

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
        }
Beispiel #6
0
 protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
     writer.WriteStartElement(XmlRpcSpec.EXTENSIONS_PREFIX, XmlRpcSpec.NIL_TAG, XmlRpcSpec.EXTENSIONS_URI);
     writer.WriteEndElement();
     writer.WriteEndElement();
 }
Beispiel #7
0
        private static IXmlRpcResponse ReadParamReponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IList          list     = RecursiveTypeSerializer.ReadParams(reader, config, typeSerializerFactory);
            XmlRpcResponse response = new XmlRpcResponse(list.Count > 0 ? list[0] : null);

            return(response);
        }
 protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
     writer.WriteStartElement(XmlRpcSpec.EXTENSIONS_PREFIX, XmlRpcSpec.NIL_TAG, XmlRpcSpec.EXTENSIONS_URI);
     writer.WriteEndElement();
     writer.WriteEndElement();
 }
        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="System.Xml.XmlException"></exception>
        public static Object ReadValue(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeFactory)
        {
            CheckTag(reader, XmlRpcSpec.VALUE_TAG);

            Object value = null;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.IsEmptyElement)
                        continue;
                    ITypeSerializer parser = typeFactory.GetSerializer(config, reader.NamespaceURI, reader.LocalName);
                    if (parser == null)
                        throw new XmlException("Unknown type: " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI));
                    else
                        value = parser.Read(reader, config, typeFactory);
                }
                else if (reader.NodeType == XmlNodeType.Text)
                {
                    ITypeSerializer parser = typeFactory.GetSerializer(config, reader.NamespaceURI, String.Empty);
                    if (parser != null)
                        value = parser.Read(reader, config, typeFactory);
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.VALUE_TAG.Equals(reader.LocalName));

            return value;
        }
Beispiel #10
0
        /// <summary>
        /// Deserializes an XML-RPC response from a <see cref="System.Xml.XmlReader"/>.
        /// </summary>
        /// <param name="reader">the <see cref="System.Xml.XmlReader"/> to read</param>
        /// <param name="config">the context configuration</param>
        /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
        /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcResponse"/> read from the reader</returns>
        /// <exception cref="System.Xml.XmlException">failed parsing the response XML</exception>
        public IXmlRpcResponse ReadResponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IXmlRpcResponse response = null;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (!String.IsNullOrEmpty(reader.NamespaceURI) || !XmlRpcSpec.METHOD_RESPONSE_TAG.Equals(reader.LocalName))
                    {
                        throw new XmlException("Expected root element methodResponse, got " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI));
                    }

                    RecursiveTypeSerializer.ReadToElement(reader);
                    if (String.IsNullOrEmpty(reader.NamespaceURI) && XmlRpcSpec.FAULT_TAG.Equals(reader.LocalName))
                    {
                        response = ReadFaultResponse(reader, config, typeSerializerFactory);
                    }
                    else if (String.IsNullOrEmpty(reader.NamespaceURI) && XmlRpcSpec.PARAMS_TAG.Equals(reader.LocalName))
                    {
                        response = ReadParamReponse(reader, config, typeSerializerFactory);
                    }
                }
            } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.METHOD_RESPONSE_TAG.Equals(reader.LocalName)));

            if (response == null)
            {
                throw new XmlRpcException("Invalid XML-RPC response.");
            }

            return(response);
        }
        public XmlWriter GetXmlWriter(IXmlRpcStreamConfig config, Stream stream)
        {
            Encoding      encoding = String.IsNullOrEmpty(config.Encoding) ? XmlRpcSpec.DEFAULT_ENCODING : Encoding.GetEncoding(config.Encoding);
            XmlTextWriter writer   = new XmlTextWriter(stream, encoding);

            return(writer);
        }
Beispiel #12
0
 protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     String result = reader.ReadElementContentAsString();
     if (String.IsNullOrEmpty(result))
         return null;
     else
         throw new XmlRpcException("Unexpected characters in nil element.");
 }
Beispiel #13
0
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
#if DEBUG
            throw new NotSupportedException("Call DoWrite(writer, value, config, typeSerializerFactory, nestedObjs) instead");
#else
            WriteValue(writer, obj, config, typeSerializerFactory, new ArrayList());
#endif
        }
Beispiel #14
0
        /// <summary>
        /// Serializes an XML-RPC request to a <see cref="System.Xml.XmlWriter"/>.
        /// </summary>
        /// <param name="writer">the <see cref="System.Xml.XmlWriter"/> to write</param>
        /// <param name="request">the <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> to serialize</param>
        /// <param name="config">the context configuration</param>
        /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
        /// <exception cref="System.Xml.XmlException">failed writing the request XML</exception>
        public void WriteRequest(XmlWriter writer, IXmlRpcRequest request, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement(XmlRpcSpec.METHOD_CALL_TAG);

            writer.WriteElementString(XmlRpcSpec.METHOD_NAME_TAG, request.MethodName);
            RecursiveTypeSerializer.WriteParams(writer, config, typeSerializerFactory, request.Parameters);

            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
Beispiel #15
0
        protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            String result = reader.ReadElementContentAsString();

            if (String.IsNullOrEmpty(result))
            {
                return(null);
            }
            else
            {
                throw new XmlRpcException("Unexpected characters in nil element.");
            }
        }
 public static void WriteValue(XmlWriter writer, Object value, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
 {
     if (nestedObjs.Contains(value))
         return;
     nestedObjs.Add(value);
     ITypeSerializer serializer = typeSerializerFactory.GetSerializer(config, value);
     if (serializer == null)
         throw new XmlRpcException("Unsupported type: " + value.GetType().Name);
     if (serializer is RecursiveTypeSerializer)
         ((RecursiveTypeSerializer)serializer).DoWrite(writer, value, config, typeSerializerFactory, nestedObjs);
     else
         serializer.Write(writer, value, config, typeSerializerFactory);
     nestedObjs.RemoveAt(nestedObjs.Count - 1);
 }
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
            writer.WriteStartElement(XmlRpcSpec.ARRAY_TAG);
            writer.WriteStartElement(XmlRpcSpec.DATA_TAG);

            foreach (var value in (IEnumerable)obj)
            {
                WriteValue(writer, value, config, typeSerializerFactory, nestedObjs);
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
Beispiel #18
0
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
            writer.WriteStartElement(XmlRpcSpec.ARRAY_TAG);
            writer.WriteStartElement(XmlRpcSpec.DATA_TAG);

            foreach (var value in (IEnumerable)obj)
            {
                WriteValue(writer, value, config, typeSerializerFactory, nestedObjs);
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
Beispiel #19
0
        public static void WriteParams(XmlWriter writer, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, params Object[] parameters)
        {
            writer.WriteStartElement(XmlRpcSpec.PARAMS_TAG);

            ArrayList nestedObjs = new ArrayList();

            foreach (var item in parameters)
            {
                writer.WriteStartElement(XmlRpcSpec.PARAM_TAG);
                WriteValue(writer, item, config, typeSerializerFactory, nestedObjs);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
 public static IList ReadParams(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     if (reader.EOF)
         return null;
     CheckTag(reader, XmlRpcSpec.PARAMS_TAG);
     IList args = new ArrayList();
     do
     {
         reader.Read();
         if (reader.NodeType == XmlNodeType.Element)
         {
             args.Add(ReadParam(reader, config, typeSerializerFactory));
         }
     } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.PARAMS_TAG.Equals(reader.LocalName)));
     return args;
 }
Beispiel #21
0
        public static IList ReadParams(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            if (reader.EOF)
            {
                return(null);
            }
            CheckTag(reader, XmlRpcSpec.PARAMS_TAG);
            IList args = new ArrayList();

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    args.Add(ReadParam(reader, config, typeSerializerFactory));
                }
            } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.PARAMS_TAG.Equals(reader.LocalName)));
            return(args);
        }
        private void WriteMember(XmlWriter writer, Object obj, MemberInfo mi,
                                 IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            if (Attribute.IsDefined(mi, typeof(XmlRpcIgnoreAttribute)))
            {
                return;
            }

            Object value = null;

            if (mi is PropertyInfo)
            {
                value = ((PropertyInfo)mi).GetValue(obj, null);
            }
            else if (mi is FieldInfo)
            {
                value = ((FieldInfo)mi).GetValue(obj);
            }

            MissingMemberAction action = config.MissingMemberAction;
            // TODO check member attribute

            XmlRpcMemberAttribute ma = (XmlRpcMemberAttribute)Attribute.GetCustomAttribute(mi, typeof(XmlRpcMemberAttribute));
            String memberName        = (ma != null && !String.IsNullOrEmpty(ma.Name)) ? ma.Name : mi.Name;

            if (value == null)
            {
                if (action == MissingMemberAction.Ignore)
                {
                    return;
                }
                else if (action == MissingMemberAction.Error)
                {
                    throw new XmlRpcException("Missing non-optional member: " + memberName);
                }
            }

            writer.WriteStartElement(XmlRpcSpec.MEMBER_TAG);
            writer.WriteElementString(XmlRpcSpec.MEMBER_NAME_TAG, memberName);
            WriteValue(writer, value, config, typeSerializerFactory, nestedObjs);
            writer.WriteEndElement();
        }
Beispiel #23
0
        protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IDictionary <String, Object> dic = new Dictionary <String, Object>();

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    CheckTag(reader, XmlRpcSpec.MEMBER_TAG);
                    do
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            CheckTag(reader, XmlRpcSpec.MEMBER_NAME_TAG);

                            String name = reader.ReadElementContentAsString();
                            if (name == null)
                            {
                                throw new XmlException("Name is expected for struct members.");
                            }

                            while (reader.NodeType != XmlNodeType.Element && reader.Read())
                            {
                                ;
                            }

                            if (dic.ContainsKey(name))
                            {
                                throw new XmlException("Duplicate struct member name: " + name);
                            }

                            Object value = ReadValue(reader, config, typeSerializerFactory);
                            dic.Add(name, value);
                        }
                    } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.MEMBER_TAG.Equals(reader.LocalName));
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.STRUCT_TAG.Equals(reader.LocalName));

            return(dic);
        }
        /// <summary>
        /// Deserializes an XML-RPC request from a <see cref="System.Xml.XmlReader"/>.
        /// </summary>
        /// <param name="reader">the <see cref="System.Xml.XmlReader"/> to read</param>
        /// <param name="config">the context configuration</param>
        /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
        /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> read from the reader</returns>
        /// <exception cref="System.Xml.XmlException">failed parsing the request XML</exception>
        public IXmlRpcRequest ReadRequest(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            String methodName = null;
            IList args = null;
            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (!String.IsNullOrEmpty(reader.NamespaceURI) || !XmlRpcSpec.METHOD_CALL_TAG.Equals(reader.LocalName))
                        throw new XmlException("Expected root element methodCall, got " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI));

                    RecursiveTypeSerializer.ReadToElement(reader);
                    methodName = ReadMethodName(reader);
                    RecursiveTypeSerializer.ReadToElement(reader);
                    args = RecursiveTypeSerializer.ReadParams(reader, config, typeSerializerFactory);
                }
            } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.METHOD_CALL_TAG.Equals(reader.LocalName)));
            return new XmlRpcRequest(methodName, args == null ? null : ToArray(args));
        }
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
            writer.WriteStartElement(XmlRpcSpec.STRUCT_TAG);

            Type type = obj.GetType();

            foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                WriteMember(writer, obj, pi, config, typeSerializerFactory, nestedObjs);
            }

            foreach (FieldInfo fi in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                WriteMember(writer, obj, fi, config, typeSerializerFactory, nestedObjs);
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
        }
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
            writer.WriteStartElement(XmlRpcSpec.STRUCT_TAG);

            Type type = obj.GetType();

            foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                WriteMember(writer, obj, pi, config, typeSerializerFactory, nestedObjs);
            }

            foreach (FieldInfo fi in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                WriteMember(writer, obj, fi, config, typeSerializerFactory, nestedObjs);
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
        }
 /// <summary>
 /// Gets a <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializer"/> for a parameter or result object.
 /// </summary>
 /// <param name="config">the request configuration</param>
 /// <param name="namespaceURI">the namespace URI of the element containing the parameter or result</param>
 /// <param name="localName">the local name of the element containing the parameter or result</param>
 /// <returns>a <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializer"/></returns>
 public ITypeSerializer GetSerializer(IXmlRpcStreamConfig config, String namespaceURI, String localName)
 {
     if (XmlRpcSpec.EXTENSIONS_URI.Equals(namespaceURI))
     {
         if (!config.EnabledForExtensions)
             return null;
         else if (XmlRpcSpec.NIL_TAG.Equals(localName))
             return nullSerializer;
         else if (XmlRpcSpec.I1_TAG.Equals(localName))
             return byteSerializer;
         else if (XmlRpcSpec.I2_TAG.Equals(localName))
             return int16Serializer;
         else if (XmlRpcSpec.I8_TAG.Equals(localName))
             return int64Serializer;
         else if (XmlRpcSpec.FLOAT_TAG.Equals(localName))
             return floatSerializer;
     }
     else
     {
         if (XmlRpcSpec.INT_TAG.Equals(localName) || XmlRpcSpec.I4_TAG.Equals(localName))
             return i4Serializer;
         else if (XmlRpcSpec.BOOLEAN_TAG.Equals(localName))
             return boolSerializer;
         else if (XmlRpcSpec.DOUBLE_TAG.Equals(localName))
             return doubleSerializer;
         else if (XmlRpcSpec.STRING_TAG.Equals(localName))
             return stringSerializer;
         else if (XmlRpcSpec.DATETIME_ISO8601_TAG.Equals(localName))
             return dateTimeSerializer;
         else if (XmlRpcSpec.BASE64_TAG.Equals(localName))
             return base64Serializer;
         else if (XmlRpcSpec.ARRAY_TAG.Equals(localName))
             return arraySerializer;
         else if (XmlRpcSpec.STRUCT_TAG.Equals(localName))
             return structSerializer;
         else if (String.Empty.Equals(localName))
             return stringSerializer;
     }
     return null;
 }
Beispiel #28
0
        private static IXmlRpcResponse ReadFaultResponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IDictionary dict = null;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    dict = (IDictionary)RecursiveTypeSerializer.ReadValue(reader, config, typeSerializerFactory);
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.FAULT_TAG.Equals(reader.LocalName));

            if (dict != null)
            {
                if (!dict.Contains(XmlRpcSpec.FAULT_CODE_TAG))
                {
                    throw new XmlRpcException("Invalid XML-RPC response: missing fault code");
                }
                if (!dict.Contains(XmlRpcSpec.FAULT_STRING_TAG))
                {
                    throw new XmlRpcException("Invalid XML-RPC response: missing fault string");
                }

                try
                {
                    XmlRpcFault    fault    = new XmlRpcFault((Int32)dict[XmlRpcSpec.FAULT_CODE_TAG], (String)dict[XmlRpcSpec.FAULT_STRING_TAG]);
                    XmlRpcResponse response = new XmlRpcResponse(null);
                    response.Fault = fault;
                    return(response);
                }
                catch (Exception ex)
                {
                    throw new XmlRpcException("Invalid XML-RPC response: " + ex.Message);
                }
            }

            throw new XmlRpcException("Invalid XML-RPC response.");
        }
Beispiel #29
0
        /// <summary>
        /// Deserializes an XML-RPC request from a <see cref="System.Xml.XmlReader"/>.
        /// </summary>
        /// <param name="reader">the <see cref="System.Xml.XmlReader"/> to read</param>
        /// <param name="config">the context configuration</param>
        /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
        /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> read from the reader</returns>
        /// <exception cref="System.Xml.XmlException">failed parsing the request XML</exception>
        public IXmlRpcRequest ReadRequest(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            String methodName = null;
            IList  args       = null;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (!String.IsNullOrEmpty(reader.NamespaceURI) || !XmlRpcSpec.METHOD_CALL_TAG.Equals(reader.LocalName))
                    {
                        throw new XmlException("Expected root element methodCall, got " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI));
                    }

                    RecursiveTypeSerializer.ReadToElement(reader);
                    methodName = ReadMethodName(reader);
                    RecursiveTypeSerializer.ReadToElement(reader);
                    args = RecursiveTypeSerializer.ReadParams(reader, config, typeSerializerFactory);
                }
            } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.METHOD_CALL_TAG.Equals(reader.LocalName)));
            return(new XmlRpcRequest(methodName, args == null ? null : ToArray(args)));
        }
        protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            List<Object> list = new List<Object>();

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    CheckTag(reader, XmlRpcSpec.DATA_TAG);
                    do
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            list.Add(ReadValue(reader, config, typeSerializerFactory));
                        }
                    } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.DATA_TAG.Equals(reader.LocalName));
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.ARRAY_TAG.Equals(reader.LocalName));

            return list.ToArray();
        }
Beispiel #31
0
        protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            List <Object> list = new List <Object>();

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    CheckTag(reader, XmlRpcSpec.DATA_TAG);
                    do
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            list.Add(ReadValue(reader, config, typeSerializerFactory));
                        }
                    } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.DATA_TAG.Equals(reader.LocalName));
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.ARRAY_TAG.Equals(reader.LocalName));

            return(list.ToArray());
        }
Beispiel #32
0
        public static void WriteValue(XmlWriter writer, Object value, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            if (nestedObjs.Contains(value))
            {
                return;
            }
            nestedObjs.Add(value);
            ITypeSerializer serializer = typeSerializerFactory.GetSerializer(config, value);

            if (serializer == null)
            {
                throw new XmlRpcException("Unsupported type: " + value.GetType().Name);
            }
            if (serializer is RecursiveTypeSerializer)
            {
                ((RecursiveTypeSerializer)serializer).DoWrite(writer, value, config, typeSerializerFactory, nestedObjs);
            }
            else
            {
                serializer.Write(writer, value, config, typeSerializerFactory);
            }
            nestedObjs.RemoveAt(nestedObjs.Count - 1);
        }
Beispiel #33
0
        /// <summary>
        ///
        /// </summary>
        /// <exception cref="System.Xml.XmlException"></exception>
        public static Object ReadValue(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeFactory)
        {
            CheckTag(reader, XmlRpcSpec.VALUE_TAG);

            Object value = null;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.IsEmptyElement)
                    {
                        continue;
                    }
                    ITypeSerializer parser = typeFactory.GetSerializer(config, reader.NamespaceURI, reader.LocalName);
                    if (parser == null)
                    {
                        throw new XmlException("Unknown type: " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI));
                    }
                    else
                    {
                        value = parser.Read(reader, config, typeFactory);
                    }
                }
                else if (reader.NodeType == XmlNodeType.Text)
                {
                    ITypeSerializer parser = typeFactory.GetSerializer(config, reader.NamespaceURI, String.Empty);
                    if (parser != null)
                    {
                        value = parser.Read(reader, config, typeFactory);
                    }
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.VALUE_TAG.Equals(reader.LocalName));

            return(value);
        }
        /// <summary>
        /// Deserializes an XML-RPC response from a <see cref="System.Xml.XmlReader"/>.
        /// </summary>
        /// <param name="reader">the <see cref="System.Xml.XmlReader"/> to read</param>
        /// <param name="config">the context configuration</param>
        /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
        /// <returns>a <see cref="LX.EasyWeb.XmlRpc.IXmlRpcResponse"/> read from the reader</returns>
        /// <exception cref="System.Xml.XmlException">failed parsing the response XML</exception>
        public IXmlRpcResponse ReadResponse(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IXmlRpcResponse response = null;
            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (!String.IsNullOrEmpty(reader.NamespaceURI) || !XmlRpcSpec.METHOD_RESPONSE_TAG.Equals(reader.LocalName))
                        throw new XmlException("Expected root element methodResponse, got " + new XmlQualifiedName(reader.LocalName, reader.NamespaceURI));

                    RecursiveTypeSerializer.ReadToElement(reader);
                    if (String.IsNullOrEmpty(reader.NamespaceURI) && XmlRpcSpec.FAULT_TAG.Equals(reader.LocalName))
                        response = ReadFaultResponse(reader, config, typeSerializerFactory);
                    else if (String.IsNullOrEmpty(reader.NamespaceURI) && XmlRpcSpec.PARAMS_TAG.Equals(reader.LocalName))
                        response = ReadParamReponse(reader, config, typeSerializerFactory);
                }
            } while (!reader.EOF && (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.METHOD_RESPONSE_TAG.Equals(reader.LocalName)));

            if (response == null)
                throw new XmlRpcException("Invalid XML-RPC response.");

            return response;
        }
        protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs)
        {
            writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
            writer.WriteStartElement(XmlRpcSpec.STRUCT_TAG);

            foreach (DictionaryEntry entry in (IDictionary)obj)
            {
                writer.WriteStartElement(XmlRpcSpec.MEMBER_TAG);

                writer.WriteStartElement(XmlRpcSpec.MEMBER_NAME_TAG);
                if (config.EnabledForExtensions && !(entry.Key is String))
                    WriteValue(writer, entry.Key, config, typeSerializerFactory, nestedObjs);
                else
                    writer.WriteString(entry.Key.ToString());
                writer.WriteEndElement();

                WriteValue(writer, entry.Value, config, typeSerializerFactory, nestedObjs);

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
        }
Beispiel #36
0
        private static Object ReadParam(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            CheckTag(reader, XmlRpcSpec.PARAM_TAG);
            Object param = null;
            Int32  count = 0;

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element && count++ == 0)
                {
                    param = ReadValue(reader, config, typeSerializerFactory);
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.PARAM_TAG.Equals(reader.LocalName));
            if (count == 0)
            {
                throw new XmlException("No value element in the param element.");
            }
            else if (count > 1)
            {
                throw new XmlException("More than one value element in the param element.");
            }
            return(param);
        }
        protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            IDictionary<String, Object> dic = new Dictionary<String, Object>();

            do
            {
                reader.Read();
                if (reader.NodeType == XmlNodeType.Element)
                {
                    CheckTag(reader, XmlRpcSpec.MEMBER_TAG);
                    do
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            CheckTag(reader, XmlRpcSpec.MEMBER_NAME_TAG);

                            String name = reader.ReadElementContentAsString();
                            if (name == null)
                                throw new XmlException("Name is expected for struct members.");

                            while (reader.NodeType != XmlNodeType.Element && reader.Read())
                                ;

                            if (dic.ContainsKey(name))
                                throw new XmlException("Duplicate struct member name: " + name);

                            Object value = ReadValue(reader, config, typeSerializerFactory);
                            dic.Add(name, value);
                        }
                    } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.MEMBER_TAG.Equals(reader.LocalName));
                }
            } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.STRUCT_TAG.Equals(reader.LocalName));

            return dic;
        }
 protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     return(DoRead(reader));
 }
 /// <summary>
 /// Gets a <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializer"/> for a parameter or result object.
 /// </summary>
 /// <param name="config">the request configuration</param>
 /// <param name="obj">the object to serialize</param>
 /// <returns>a <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializer"/></returns>
 public ITypeSerializer GetSerializer(IXmlRpcStreamConfig config, Object obj)
 {
     if (obj == null)
     {
         if (config.EnabledForExtensions)
         {
             return(nullSerializer);
         }
         else
         {
             throw new XmlRpcException("Null values aren't supported");
         }
     }
     else if (obj is String)
     {
         return(stringSerializer);
     }
     else if (obj is Byte)
     {
         if (config.EnabledForExtensions)
         {
             return(byteSerializer);
         }
         else
         {
             return(i4Serializer);
         }
     }
     else if (obj is Int16)
     {
         if (config.EnabledForExtensions)
         {
             return(int16Serializer);
         }
         else
         {
             return(i4Serializer);
         }
     }
     else if (obj is Int32)
     {
         return(i4Serializer);
     }
     else if (obj is Boolean)
     {
         return(boolSerializer);
     }
     else if (obj is Double)
     {
         return(doubleSerializer);
     }
     else if (obj is Single)
     {
         if (config.EnabledForExtensions)
         {
             return(floatSerializer);
         }
         else
         {
             return(doubleSerializer);
         }
     }
     else if (obj is Int64)
     {
         if (config.EnabledForExtensions)
         {
             return(int64Serializer);
         }
         else
         {
             return(stringSerializer);
         }
     }
     else if (obj is DateTime)
     {
         return(dateTimeSerializer);
     }
     else if (obj is Byte[])
     {
         return(base64Serializer);
     }
     else if (obj is IDictionary)
     {
         return(structSerializer);
     }
     else if (obj is IEnumerable)
     {
         return(arraySerializer);
     }
     else if (obj is Enum)
     {
         return(stringSerializer);
     }
     else
     {
         return(objectSerializer);
     }
 }
 protected override String GetTag(IXmlRpcStreamConfig config)
 {
     return config == null || config.UseStringTag ? XmlRpcSpec.STRING_TAG : null;
 }
 /// <summary>
 /// Serializes an XML-RPC request to a request stream.
 /// </summary>
 /// <param name="requestStream">the <see cref="System.IO.Stream"/> to write</param>
 /// <param name="request">the <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> to serialize</param>
 /// <param name="config">the context configuration</param>
 /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
 /// <exception cref="System.Xml.XmlException">failed writing the request XML</exception>
 public void WriteRequest(Stream requestStream, IXmlRpcRequest request, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     using (XmlWriter writer = _xmlWriterFactory.GetXmlWriter(config, requestStream))
     {
         WriteRequest(writer, request, config, typeSerializerFactory);
     }
 }
 protected override String GetTag(IXmlRpcStreamConfig config)
 {
     return(config == null || config.UseStringTag ? XmlRpcSpec.STRING_TAG : null);
 }
 protected override String GetTag(IXmlRpcStreamConfig config)
 {
     return(config != null && config.UseIntTag ? XmlRpcSpec.INT_TAG : XmlRpcSpec.I4_TAG);;
 }
 protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     throw new InvalidOperationException("Cannot deserialize objects directly, using StructSerializer instead.");
 }
 protected abstract void DoWrite(XmlWriter writer, Object value, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, IList nestedObjs);
 protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
     WriteString(writer, GetTag(config), GetString(obj));
     writer.WriteEndElement();
 }
 protected abstract String GetTag(IXmlRpcStreamConfig config);
 void ITypeSerializer.Write(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     WriteRequest(writer, (IXmlRpcRequest)obj, config, typeSerializerFactory);
 }
 protected override String GetTag(IXmlRpcStreamConfig config)
 {
     return(XmlRpcSpec.DOUBLE_TAG);
 }
        public static void WriteParams(XmlWriter writer, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory, params Object[] parameters)
        {
            writer.WriteStartElement(XmlRpcSpec.PARAMS_TAG);

            ArrayList nestedObjs = new ArrayList();
            foreach (var item in parameters)
            {
                writer.WriteStartElement(XmlRpcSpec.PARAM_TAG);
                WriteValue(writer, item, config, typeSerializerFactory, nestedObjs);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
 protected override String GetTag(IXmlRpcStreamConfig config)
 {
     return XmlRpcSpec.BASE64_TAG;
 }
 public XmlWriter GetXmlWriter(IXmlRpcStreamConfig config, Stream stream)
 {
     Encoding encoding = String.IsNullOrEmpty(config.Encoding) ? XmlRpcSpec.DEFAULT_ENCODING : Encoding.GetEncoding(config.Encoding);
     XmlTextWriter writer = new XmlTextWriter(stream, encoding);
     return writer;
 }
 Object ITypeSerializer.Read(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     return ReadRequest(reader, config, typeSerializerFactory);
 }
 private static Object ReadParam(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     CheckTag(reader, XmlRpcSpec.PARAM_TAG);
     Object param = null;
     Int32 count = 0;
     do
     {
         reader.Read();
         if (reader.NodeType == XmlNodeType.Element && count++ == 0)
         {
             param = ReadValue(reader, config, typeSerializerFactory);
         }
     } while (reader.NodeType != XmlNodeType.EndElement || !XmlRpcSpec.PARAM_TAG.Equals(reader.LocalName));
     if (count == 0)
         throw new XmlException("No value element in the param element.");
     else if (count > 1)
         throw new XmlException("More than one value element in the param element.");
     return param;
 }
 protected override void DoWrite(XmlWriter writer, Object obj, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     writer.WriteStartElement(XmlRpcSpec.VALUE_TAG);
     WriteString(writer, GetTag(config), GetString(obj));
     writer.WriteEndElement();
 }
 protected override Object DoRead(XmlReader reader, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
 {
     return DoRead(reader);
 }
        /// <summary>
        /// Serializes an XML-RPC request to a <see cref="System.Xml.XmlWriter"/>.
        /// </summary>
        /// <param name="writer">the <see cref="System.Xml.XmlWriter"/> to write</param>
        /// <param name="request">the <see cref="LX.EasyWeb.XmlRpc.IXmlRpcRequest"/> to serialize</param>
        /// <param name="config">the context configuration</param>
        /// <param name="typeSerializerFactory">the <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializerFactory"/> to get type serializers</param>
        /// <exception cref="System.Xml.XmlException">failed writing the request XML</exception>
        public void WriteRequest(XmlWriter writer, IXmlRpcRequest request, IXmlRpcStreamConfig config, ITypeSerializerFactory typeSerializerFactory)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement(XmlRpcSpec.METHOD_CALL_TAG);

            writer.WriteElementString(XmlRpcSpec.METHOD_NAME_TAG, request.MethodName);
            RecursiveTypeSerializer.WriteParams(writer, config, typeSerializerFactory, request.Parameters);

            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
 protected abstract String GetTag(IXmlRpcStreamConfig config);
 /// <summary>
 /// Gets a <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializer"/> for a parameter or result object.
 /// </summary>
 /// <param name="config">the request configuration</param>
 /// <param name="namespaceURI">the namespace URI of the element containing the parameter or result</param>
 /// <param name="localName">the local name of the element containing the parameter or result</param>
 /// <returns>a <see cref="LX.EasyWeb.XmlRpc.Serializer.ITypeSerializer"/></returns>
 public ITypeSerializer GetSerializer(IXmlRpcStreamConfig config, String namespaceURI, String localName)
 {
     if (XmlRpcSpec.EXTENSIONS_URI.Equals(namespaceURI))
     {
         if (!config.EnabledForExtensions)
         {
             return(null);
         }
         else if (XmlRpcSpec.NIL_TAG.Equals(localName))
         {
             return(nullSerializer);
         }
         else if (XmlRpcSpec.I1_TAG.Equals(localName))
         {
             return(byteSerializer);
         }
         else if (XmlRpcSpec.I2_TAG.Equals(localName))
         {
             return(int16Serializer);
         }
         else if (XmlRpcSpec.I8_TAG.Equals(localName))
         {
             return(int64Serializer);
         }
         else if (XmlRpcSpec.FLOAT_TAG.Equals(localName))
         {
             return(floatSerializer);
         }
     }
     else
     {
         if (XmlRpcSpec.INT_TAG.Equals(localName) || XmlRpcSpec.I4_TAG.Equals(localName))
         {
             return(i4Serializer);
         }
         else if (XmlRpcSpec.BOOLEAN_TAG.Equals(localName))
         {
             return(boolSerializer);
         }
         else if (XmlRpcSpec.DOUBLE_TAG.Equals(localName))
         {
             return(doubleSerializer);
         }
         else if (XmlRpcSpec.STRING_TAG.Equals(localName))
         {
             return(stringSerializer);
         }
         else if (XmlRpcSpec.DATETIME_ISO8601_TAG.Equals(localName))
         {
             return(dateTimeSerializer);
         }
         else if (XmlRpcSpec.BASE64_TAG.Equals(localName))
         {
             return(base64Serializer);
         }
         else if (XmlRpcSpec.ARRAY_TAG.Equals(localName))
         {
             return(arraySerializer);
         }
         else if (XmlRpcSpec.STRUCT_TAG.Equals(localName))
         {
             return(structSerializer);
         }
         else if (String.Empty.Equals(localName))
         {
             return(stringSerializer);
         }
     }
     return(null);
 }
 protected override String GetTag(IXmlRpcStreamConfig config)
 {
     return config != null && config.UseIntTag ? XmlRpcSpec.INT_TAG : XmlRpcSpec.I4_TAG; ;
 }