void SerializeStructParams(XmlWriter xtw, XmlRpcRequest request, MappingActions mappingActions)
 {
     ParameterInfo[] pis = request.mi.GetParameters();
     if (request.args.Length > pis.Length)
     {
         throw new XmlRpcInvalidParametersException("Number of request "
                                                    + "parameters greater than number of proxy method parameters.");
     }
     if (Attribute.IsDefined(pis[request.args.Length - 1],
                             typeof(ParamArrayAttribute)))
     {
         throw new XmlRpcInvalidParametersException("params parameter cannot "
                                                    + "be used with StructParams.");
     }
     xtw.WriteStartElement("", "param", "");
     xtw.WriteStartElement("", "value", "");
     xtw.WriteStartElement("", "struct", "");
     for (int i = 0; i < request.args.Length; i++)
     {
         if (request.args[i] == null)
         {
             throw new XmlRpcNullParameterException(String.Format(
                                                        "Null method parameter #{0}", i + 1));
         }
         xtw.WriteStartElement("", "member", "");
         WriteFullElementString(xtw, "name", pis[i].Name);
         Serialize(xtw, request.args[i], mappingActions);
         WriteFullEndElement(xtw);
     }
     WriteFullEndElement(xtw);
     WriteFullEndElement(xtw);
     WriteFullEndElement(xtw);
 }
 void BuildArrayXml(XmlWriter xtw, Array ary, int CurRank, int[] indices, MappingActions mappingActions, List <object> nestedObjs)
 {
     xtw.WriteStartElement("", "array", "");
     xtw.WriteStartElement("", "data", "");
     if (CurRank < (ary.Rank - 1))
     {
         for (int i = 0; i < ary.GetLength(CurRank); i++)
         {
             indices[CurRank] = i;
             xtw.WriteStartElement("", "value", "");
             BuildArrayXml(xtw, ary, CurRank + 1, indices, mappingActions, nestedObjs);
             WriteFullEndElement(xtw);
         }
     }
     else
     {
         for (int i = 0; i < ary.GetLength(CurRank); i++)
         {
             indices[CurRank] = i;
             Serialize(xtw, ary.GetValue(indices), mappingActions, nestedObjs);
         }
     }
     WriteFullEndElement(xtw);
     WriteFullEndElement(xtw);
 }
 private object SerializeInt64(XmlWriter xtw, object o, MappingActions mappingActions)
 {
     if (o.GetType().IsEnum)
     {
         if (mappingActions.EnumMapping == EnumMapping.String)
         {
             SerializeString(xtw, o.ToString());
         }
         else
         {
             o = Convert.ToInt64(o);
         }
     }
     WriteFullElementString(xtw, "i8", o.ToString());
     return(o);
 }
        protected MappingActions GetTypeMappings(MethodInfo mi, MappingActions mappingActions)
        {
            if (mi == null)
            {
                return(mappingActions);
            }

            var declaringType = mi?.DeclaringType;

            foreach (var itf in declaringType.GetInterfaces())
            {
                mappingActions = GetMappingActions(itf, mappingActions);
            }
            mappingActions = GetMappingActions(declaringType, mappingActions);

            return(mappingActions);
        }
        private MappingActions MemberMappingActions(Type type, string memberName, MappingActions currentActions)
        {
            // if struct member has mapping action attribute, override the current
            // mapping action else just return the current action
            if (type == null)
            {
                return(currentActions);
            }

            MemberInfo[] mis = type.GetMember(memberName);
            if (mis == null || mis.Length == 0)
            {
                return(currentActions);
            }

            return(GetMappingActions(mis[0], currentActions));
        }
 void SerializeParams(XmlWriter xtw, XmlRpcRequest request, MappingActions mappingActions)
 {
     ParameterInfo[] pis = null;
     if (request.mi != null)
     {
         pis = request.mi.GetParameters();
     }
     for (int i = 0; i < request.args.Length; i++)
     {
         var paramMappingActions = pis == null ? mappingActions
           : GetMappingActions(pis[i], mappingActions);
         if (pis != null)
         {
             if (i >= pis.Length)
             {
                 throw new XmlRpcInvalidParametersException("Number of request "
                                                            + "parameters greater than number of proxy method parameters.");
             }
             if (i == pis.Length - 1 &&
                 Attribute.IsDefined(pis[i], typeof(ParamArrayAttribute)))
             {
                 Array ary = (Array)request.args[i];
                 foreach (object o in ary)
                 {
                     //if (o == null)
                     //  throw new XmlRpcNullParameterException(
                     //    "Null parameter in params array");
                     xtw.WriteStartElement("", "param", "");
                     Serialize(xtw, o, paramMappingActions);
                     WriteFullEndElement(xtw);
                 }
                 break;
             }
         }
         //if (request.args[i] == null)
         //{
         //  throw new XmlRpcNullParameterException(String.Format(
         //    "Null method parameter #{0}", i + 1));
         //}
         xtw.WriteStartElement("", "param", "");
         Serialize(xtw, request.args[i], paramMappingActions);
         WriteFullEndElement(xtw);
     }
 }
        public void SerializeRequest(Stream stm, XmlRpcRequest request)
        {
            XmlWriter xtw = XmlRpcXmlWriter.Create(stm, base.XmlRpcFormatSettings);

            xtw.WriteStartDocument();
            xtw.WriteStartElement("", "methodCall", "");
            {
                var mappingActions = new MappingActions();
                mappingActions = GetTypeMappings(request.mi, mappingActions);
                mappingActions = GetMappingActions(request.mi, mappingActions);
                WriteFullElementString(xtw, "methodName", request.method);
                if (request.args.Length > 0 || UseEmptyParamsTag)
                {
                    xtw.WriteStartElement("params");
                    try
                    {
                        if (!IsStructParamsMethod(request.mi))
                        {
                            SerializeParams(xtw, request, mappingActions);
                        }
                        else
                        {
                            SerializeStructParams(xtw, request, mappingActions);
                        }
                    }
                    catch (XmlRpcUnsupportedTypeException ex)
                    {
                        throw new XmlRpcUnsupportedTypeException(ex.UnsupportedType,
                                                                 String.Format("A parameter is of, or contains an instance of, "
                                                                               + "type {0} which cannot be mapped to an XML-RPC type",
                                                                               ex.UnsupportedType));
                    }
                    WriteFullEndElement(xtw);
                }
            }
            WriteFullEndElement(xtw);
            xtw.Flush();
        }
        private object SerializeInt32(XmlWriter xtw, object o, MappingActions mappingActions)
        {
            if (o.GetType().IsEnum)
            {
                if (mappingActions.EnumMapping == EnumMapping.String)
                {
                    SerializeString(xtw, o.ToString());
                    return(o);
                }

                o = Convert.ToInt32(o);
            }
            if (UseIntTag)
            {
                WriteFullElementString(xtw, "int", o.ToString());
            }
            else
            {
                WriteFullElementString(xtw, "i4", o.ToString());
            }

            return(o);
        }
        protected MappingActions GetMappingActions(ICustomAttributeProvider cap, MappingActions mappingActions)
        {
            if (cap == null)
            {
                return(mappingActions);
            }

            var ret = new MappingActions
            {
                EnumMapping       = mappingActions.EnumMapping,
                NullMappingAction = mappingActions.NullMappingAction
            };

            var nullMappingAttr = GetAttribute <XmlRpcNullMappingAttribute>(cap);

            if (nullMappingAttr != null)
            {
                ret.NullMappingAction = nullMappingAttr.Action;
            }
            else
            {
                // check for missing mapping attribute for backwards compatibility
                var missingAttr = GetAttribute <XmlRpcMissingMappingAttribute>(cap);
                if (missingAttr != null)
                {
                    ret.NullMappingAction = MapToNullMappingAction(missingAttr.Action);
                }
            }
            var enumAttr = GetAttribute <XmlRpcEnumMappingAttribute>(cap);

            if (enumAttr != null)
            {
                ret.EnumMapping = ((XmlRpcEnumMappingAttribute)enumAttr).Mapping;
            }
            return(ret);
        }
 public void Serialize(XmlWriter xtw, object o, MappingActions mappingActions)
 {
     Serialize(xtw, o, mappingActions, new List <object>());
 }
        public void Serialize(XmlWriter xtw, object o, MappingActions mappingActions, List <object> nestedObjs)
        {
            if (nestedObjs.Contains(o))
            {
                throw new XmlRpcUnsupportedTypeException(nestedObjs[0].GetType(),
                                                         "Cannot serialize recursive data structure");
            }
            nestedObjs.Add(o);
            try
            {
                xtw.WriteStartElement("", "value", "");
                var xType = XmlRpcTypeInfo.GetXmlRpcType(o);
                switch (xType)
                {
                case XmlRpcType.tArray:
                    xtw.WriteStartElement("", "array", "");
                    xtw.WriteStartElement("", "data", "");
                    var a = (Array)o;
                    foreach (object aobj in a)
                    {
                        //if (aobj == null)
                        //  throw new XmlRpcMappingSerializeException(String.Format(
                        //    "Items in array cannot be null ({0}[]).",
                        //o.GetType().GetElementType()));
                        Serialize(xtw, aobj, mappingActions, nestedObjs);
                    }
                    WriteFullEndElement(xtw);
                    WriteFullEndElement(xtw);
                    break;

                case XmlRpcType.tMultiDimArray:
                    var mda     = (Array)o;
                    var indices = new int[mda.Rank];
                    BuildArrayXml(xtw, mda, 0, indices, mappingActions, nestedObjs);
                    break;

                case XmlRpcType.tBase64:
                    var buf = (byte[])o;
                    xtw.WriteStartElement("", "base64", "");
                    xtw.WriteBase64(buf, 0, buf.Length);
                    WriteFullEndElement(xtw);
                    break;

                case XmlRpcType.tBoolean:
                    var strBoolVal = (bool)o ? "1" : "0";
                    WriteFullElementString(xtw, "boolean", strBoolVal);
                    break;

                case XmlRpcType.tDateTime:
                    var dt  = (DateTime)o;
                    var sdt = dt.ToString("yyyyMMdd'T'HH':'mm':'ss",
                                          DateTimeFormatInfo.InvariantInfo);
                    WriteFullElementString(xtw, "dateTime.iso8601", sdt);
                    break;

                case XmlRpcType.tDouble:
                    double doubleVal = (double)o;
                    WriteFullElementString(xtw, "double", doubleVal.ToString(null,
                                                                             CultureInfo.InvariantCulture));
                    break;

                case XmlRpcType.tHashtable:
                    xtw.WriteStartElement("", "struct", "");
                    var xrs = o as XmlRpcStruct;
                    foreach (object obj in xrs.Keys)
                    {
                        string skey = obj as string;
                        xtw.WriteStartElement("", "member", "");
                        WriteFullElementString(xtw, "name", skey);
                        Serialize(xtw, xrs[skey], mappingActions, nestedObjs);
                        WriteFullEndElement(xtw);
                    }
                    WriteFullEndElement(xtw);
                    break;

                case XmlRpcType.tInt32:
                    o = SerializeInt32(xtw, o, mappingActions);
                    break;

                case XmlRpcType.tInt64:
                    o = SerializeInt64(xtw, o, mappingActions);
                    break;

                case XmlRpcType.tString:
                    SerializeString(xtw, o);
                    break;

                case XmlRpcType.tStruct:
                    MappingActions structActions = GetMappingActions(o.GetType(), mappingActions);
                    xtw.WriteStartElement("", "struct", "");
                    MemberInfo[] mis = o.GetType().GetMembers();
                    foreach (var mi in mis)
                    {
                        if (Attribute.IsDefined(mi, typeof(NonSerializedAttribute)))
                        {
                            continue;
                        }

                        if (mi.MemberType == MemberTypes.Field)
                        {
                            var fi      = (FieldInfo)mi;
                            var member  = fi.Name;
                            var attrchk = Attribute.GetCustomAttribute(fi, typeof(XmlRpcMemberAttribute));
                            if (attrchk != null && attrchk is XmlRpcMemberAttribute)
                            {
                                var mmbr = ((XmlRpcMemberAttribute)attrchk).Member;
                                if (mmbr != "")
                                {
                                    member = mmbr;
                                }
                            }
                            var memberActions = MemberMappingActions(o.GetType(), fi.Name, structActions);
                            if (fi.GetValue(o) == null)
                            {
                                if (memberActions.NullMappingAction == NullMappingAction.Ignore)
                                {
                                    continue;
                                }

                                if (memberActions.NullMappingAction == NullMappingAction.Error)
                                {
                                    throw new XmlRpcMappingSerializeException(@"Member """ + member +
                                                                              @""" of struct """ + o.GetType().Name + @""" cannot be null.");
                                }
                            }
                            xtw.WriteStartElement("", "member", "");
                            WriteFullElementString(xtw, "name", member);
                            Serialize(xtw, fi.GetValue(o), memberActions, nestedObjs);
                            WriteFullEndElement(xtw);
                        }
                        else if (mi.MemberType == MemberTypes.Property)
                        {
                            var pi      = (PropertyInfo)mi;
                            var member  = pi.Name;
                            var attrchk = Attribute.GetCustomAttribute(pi, typeof(XmlRpcMemberAttribute));
                            if (attrchk != null && attrchk is XmlRpcMemberAttribute)
                            {
                                var mmbr = ((XmlRpcMemberAttribute)attrchk).Member;
                                if (mmbr != "")
                                {
                                    member = mmbr;
                                }
                            }
                            var memberActions = MemberMappingActions(o.GetType(), pi.Name, structActions);
                            if (pi.GetValue(o, null) == null)
                            {
                                if (memberActions.NullMappingAction == NullMappingAction.Ignore)
                                {
                                    continue;
                                }

                                if (memberActions.NullMappingAction == NullMappingAction.Error)
                                {
                                    throw new XmlRpcMappingSerializeException(@"Member """ + member +
                                                                              @""" of struct """ + o.GetType().Name + @""" cannot be null.");
                                }
                            }
                            xtw.WriteStartElement("", "member", "");
                            WriteFullElementString(xtw, "name", member);
                            Serialize(xtw, pi.GetValue(o, null), memberActions, nestedObjs);
                            WriteFullEndElement(xtw);
                        }
                    }
                    WriteFullEndElement(xtw);
                    break;

                case XmlRpcType.tVoid:
                    WriteFullElementString(xtw, "string", "");
                    break;

                case XmlRpcType.tNil:
                    xtw.WriteStartElement("nil");
                    WriteFullEndElement(xtw);
                    break;

                default:
                    throw new XmlRpcUnsupportedTypeException(o.GetType());
                }

                WriteFullEndElement(xtw);
            }
            catch (NullReferenceException)
            {
                throw new XmlRpcNullReferenceException("Attempt to serialize data "
                                                       + "containing null reference");
            }
            finally
            {
                nestedObjs.RemoveAt(nestedObjs.Count - 1);
            }
        }