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); }
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.GetType().GetTypeInfo(), 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(); }
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 (pis[request.args.Length - 1].GetCustomAttribute <ParamArrayAttribute>() != null) { 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); }
protected MappingActions GetMappingActions(TypeInfo 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); }
//#endif void Serialize( XmlWriter xtw, Object o, MappingActions mappingActions) { Serialize(xtw, o, mappingActions, new List <object>()); }
protected MappingActions GetTypeMappings(MethodInfo mi, MappingActions mappingActions) { if (mi != null) { var declaringType = mi != null ? mi.DeclaringType : null; foreach (Type itf in declaringType.GetInterfaces()) { mappingActions = GetMappingActions(itf.GetTypeInfo(), mappingActions); } mappingActions = GetMappingActions(declaringType.GetTypeInfo(), mappingActions); } return(mappingActions); }
private Object SerializeInt64(XmlWriter xtw, Object o, MappingActions mappingActions) { if (o.GetType().GetTypeInfo().IsEnum) { if (mappingActions.EnumMapping == EnumMapping.String) { SerializeString(xtw, o.ToString()); } else { o = Convert.ToInt64(o); } } WriteFullElementString(xtw, "i8", o.ToString()); return(o); }
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].GetType().GetTypeInfo(), 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 && pis[i].GetCustomAttribute <ParamArrayAttribute>() != null) { 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 string SerializeRequestToString(XmlRpcRequest request) { var ms = new MemoryStream(); XmlWriter xtw = XmlRpcXmlWriter.Create(ms, base.XmlRpcFormatSettings); xtw.WriteStartDocument(); xtw.WriteStartElement("", "methodCall", ""); { var mappingActions = new MappingActions(); mappingActions = GetTypeMappings(request.mi, mappingActions); mappingActions = GetMappingActions(request.mi.GetType().GetTypeInfo(), 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(); return(global::System.Text.Encoding.UTF8.GetString(ms.ToArray())); }
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); } var ret = GetMappingActions(mis[0].GetType().GetTypeInfo(), currentActions); return(ret); }
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 (pis[request.args.Length - 1].GetCustomAttribute<ParamArrayAttribute>() != null) { 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 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].GetType().GetTypeInfo(), 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 && pis[i].GetCustomAttribute<ParamArrayAttribute>() != null) { 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); } }
//#endif 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", ""); XmlRpcType xType = XmlRpcTypeInfo.GetXmlRpcType(o); if (xType == XmlRpcType.tArray) { xtw.WriteStartElement("", "array", ""); xtw.WriteStartElement("", "data", ""); Array 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); } else if (xType == XmlRpcType.tMultiDimArray) { Array mda = (Array)o; int[] indices = new int[mda.Rank]; BuildArrayXml(xtw, mda, 0, indices, mappingActions, nestedObjs); } else if (xType == XmlRpcType.tBase64) { byte[] buf = (byte[])o; xtw.WriteStartElement("", "base64", ""); xtw.WriteBase64(buf, 0, buf.Length); WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tBoolean) { bool boolVal = (bool)o; if (boolVal) { WriteFullElementString(xtw, "boolean", "1"); } else { WriteFullElementString(xtw, "boolean", "0"); } } else if (xType == XmlRpcType.tDateTime) { DateTime dt = (DateTime)o; string sdt = dt.ToString("yyyyMMdd'T'HH':'mm':'ss", DateTimeFormatInfo.InvariantInfo); WriteFullElementString(xtw, "dateTime.iso8601", sdt); } else if (xType == XmlRpcType.tDouble) { double doubleVal = (double)o; WriteFullElementString(xtw, "double", doubleVal.ToString(null, CultureInfo.InvariantCulture)); } else if (xType == XmlRpcType.tHashtable) { xtw.WriteStartElement("", "struct", ""); XmlRpcStruct 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); } else if (xType == XmlRpcType.tInt32) { o = SerializeInt32(xtw, o, mappingActions); } else if (xType == XmlRpcType.tInt64) { o = SerializeInt64(xtw, o, mappingActions); } else if (xType == XmlRpcType.tString) { SerializeString(xtw, o); } else if (xType == XmlRpcType.tStruct) { MappingActions structActions = GetMappingActions(o.GetType().GetTypeInfo(), mappingActions); xtw.WriteStartElement("", "struct", ""); MemberInfo[] mis = o.GetType().GetMembers(); foreach (MemberInfo mi in mis) { if (mi.GetCustomAttribute <IgnoreDataMemberAttribute>() != null) { continue; } if (mi is FieldInfo) { FieldInfo fi = (FieldInfo)mi; string member = fi.Name; Attribute attrchk = fi.GetCustomAttribute <XmlRpcMemberAttribute>(); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") { member = mmbr; } } MappingActions memberActions = MemberMappingActions(o.GetType(), fi.Name, structActions); if (fi.GetValue(o) == null) { if (memberActions.NullMappingAction == NullMappingAction.Ignore) { continue; } else 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 is PropertyInfo) { PropertyInfo pi = (PropertyInfo)mi; string member = pi.Name; Attribute attrchk = pi.GetCustomAttribute <XmlRpcMemberAttribute>(); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") { member = mmbr; } } MappingActions memberActions = MemberMappingActions(o.GetType(), pi.Name, structActions); if (pi.GetValue(o, null) == null) { if (memberActions.NullMappingAction == NullMappingAction.Ignore) { continue; } else 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); } else if (xType == XmlRpcType.tVoid) { WriteFullElementString(xtw, "string", ""); } else if (xType == XmlRpcType.tNil) { xtw.WriteStartElement("nil"); WriteFullEndElement(xtw); } else { throw new XmlRpcUnsupportedTypeException(o.GetType()); } WriteFullEndElement(xtw); } catch (global::System.NullReferenceException) { throw new XmlRpcNullReferenceException("Attempt to serialize data " + "containing null reference"); } finally { nestedObjs.RemoveAt(nestedObjs.Count - 1); } }