NullMappingAction MemberNullMappingAction( Type type, string memberName, NullMappingAction currentAction) { // if struct member has mapping action attribute, override the current // mapping action else just return the current action if (type == null) { return(currentAction); } Attribute attr = null; MemberInfo[] mis = type.GetMember(memberName); if (mis.Length > 0 && mis != null) { attr = Attribute.GetCustomAttribute(mis[0], typeof(XmlRpcNullMappingAttribute)); if (attr != null) { return(((XmlRpcNullMappingAttribute)attr).Action); } // check for missing mapping attribute for backwards compatibility attr = Attribute.GetCustomAttribute(mis[0], typeof(XmlRpcMissingMappingAttribute)); if (attr != null) { return(MapToNullMappingAction(((XmlRpcMissingMappingAttribute)attr).Action)); } } return(currentAction); }
void SerializeStructParams(XmlWriter xtw, XmlRpcRequest request, NullMappingAction mappingAction) { 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], mappingAction); WriteFullEndElement(xtw); } WriteFullEndElement(xtw); WriteFullEndElement(xtw); WriteFullEndElement(xtw); }
void BuildArrayXml( XmlWriter xtw, Array ary, int CurRank, int[] indices, NullMappingAction mappingAction, 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, mappingAction, nestedObjs); WriteFullEndElement(xtw); } } else { for (int i = 0; i < ary.GetLength(CurRank); i++) { indices[CurRank] = i; Serialize(xtw, ary.GetValue(indices), mappingAction, nestedObjs); } } WriteFullEndElement(xtw); WriteFullEndElement(xtw); }
void Serialize( XmlWriter xtw, Object o, NullMappingAction mappingAction) { Serialize(xtw, o, mappingAction, new List <object>()); }
void SerializeParams(XmlWriter xtw, XmlRpcRequest request, NullMappingAction mappingAction) { ParameterInfo[] pis = null; if (request.mi != null) { pis = request.mi.GetParameters(); } for (int i = 0; i < request.args.Length; i++) { 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, mappingAction); 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], mappingAction); WriteFullEndElement(xtw); } }
NullMappingAction StructMappingAction( Type type, NullMappingAction currentAction) { // if struct member has mapping action attribute, override the current // mapping action else just return the current action if (type == null) { return(currentAction); } Attribute attr = Attribute.GetCustomAttribute(type, typeof(XmlRpcNullMappingAttribute)); if (attr != null) { return(((XmlRpcNullMappingAttribute)attr).Action); } attr = Attribute.GetCustomAttribute(type, typeof(XmlRpcMissingMappingAttribute)); if (attr != null) { return(MapToNullMappingAction(((XmlRpcMissingMappingAttribute)attr).Action)); } return(currentAction); }
/// <summary> /// /// </summary> /// <param name="stm"></param> /// <param name="request"></param> public void SerializeRequest(Stream stm, XmlRpcRequest request) { XmlWriter xtw = XmlRpcXmlWriter.Create(stm, base.XmlRpcFormatSettings); xtw.WriteStartDocument(); xtw.WriteStartElement("", "methodCall", ""); { // TODO: use global action setting NullMappingAction mappingAction = NullMappingAction.Error; WriteFullElementString(xtw, "methodName", request.method); if (request.args.Length > 0 || UseEmptyParamsTag) { xtw.WriteStartElement("params"); try { if (!IsStructParamsMethod(request.mi)) { SerializeParams(xtw, request, mappingAction); } else { SerializeStructParams(xtw, request, mappingAction); } } 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 SerializeParams(XmlWriter xtw, XmlRpcRequest request, NullMappingAction mappingAction) { ParameterInfo[] pis = null; if (request.mi != null) { pis = request.mi.GetParameters(); } for (int i = 0; i < request.args.Length; i++) { 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, mappingAction); 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], mappingAction); WriteFullEndElement(xtw); } }
void SerializeStructParams(XmlWriter xtw, XmlRpcRequest request, NullMappingAction mappingAction) { 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], mappingAction); WriteFullEndElement(xtw); } WriteFullEndElement(xtw); WriteFullEndElement(xtw); WriteFullEndElement(xtw); }
NullMappingAction MemberNullMappingAction( Type type, string memberName, NullMappingAction currentAction) { // if struct member has mapping action attribute, override the current // mapping action else just return the current action if (type == null) return currentAction; Attribute attr = null; MemberInfo[] mis = type.GetMember(memberName); if (mis.Length > 0 && mis != null) { attr = Attribute.GetCustomAttribute(mis[0], typeof(XmlRpcNullMappingAttribute)); if (attr != null) return ((XmlRpcNullMappingAttribute)attr).Action; // check for missing mapping attribute for backwards compatibility attr = Attribute.GetCustomAttribute(mis[0], typeof(XmlRpcMissingMappingAttribute)); if (attr != null) return MapToNullMappingAction(((XmlRpcMissingMappingAttribute)attr).Action); } return currentAction; }
NullMappingAction StructMappingAction( Type type, NullMappingAction currentAction) { // if struct member has mapping action attribute, override the current // mapping action else just return the current action if (type == null) return currentAction; Attribute attr = Attribute.GetCustomAttribute(type, typeof(XmlRpcNullMappingAttribute)); if (attr != null) return ((XmlRpcNullMappingAttribute)attr).Action; attr = Attribute.GetCustomAttribute(type, typeof(XmlRpcMissingMappingAttribute)); if (attr != null) return MapToNullMappingAction(((XmlRpcMissingMappingAttribute)attr).Action); return currentAction; }
void BuildArrayXml( XmlWriter xtw, Array ary, int CurRank, int[] indices, NullMappingAction mappingAction, 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, mappingAction, nestedObjs); WriteFullEndElement(xtw); } } else { for (int i = 0; i < ary.GetLength(CurRank); i++) { indices[CurRank] = i; Serialize(xtw, ary.GetValue(indices), mappingAction, nestedObjs); } } WriteFullEndElement(xtw); WriteFullEndElement(xtw); }
/// <summary> /// /// </summary> /// <param name="xtw"></param> /// <param name="o"></param> /// <param name="mappingAction"></param> /// <param name="nestedObjs"></param> public void Serialize( XmlWriter xtw, Object o, NullMappingAction mappingAction, 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, mappingAction, 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, mappingAction, 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], mappingAction, nestedObjs); WriteFullEndElement(xtw); } WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tInt32) { if (o.GetType().IsEnum) o = Convert.ToInt32(o); if (UseIntTag) WriteFullElementString(xtw, "int", o.ToString()); else WriteFullElementString(xtw, "i4", o.ToString()); } else if (xType == XmlRpcType.tInt64) { if (o.GetType().IsEnum) o = Convert.ToInt64(o); WriteFullElementString(xtw, "i8", o.ToString()); } else if (xType == XmlRpcType.tString) { if (UseStringTag) { WriteFullElementString(xtw, "string", (string)o); } else xtw.WriteString((string)o); } else if (xType == XmlRpcType.tStruct) { NullMappingAction structAction = StructMappingAction(o.GetType(), mappingAction); xtw.WriteStartElement("", "struct", ""); MemberInfo[] mis = o.GetType().GetMembers(); foreach (MemberInfo mi in mis) { if (Attribute.IsDefined(mi, typeof(NonSerializedAttribute))) continue; if (mi.MemberType == MemberTypes.Field) { FieldInfo fi = (FieldInfo)mi; string member = fi.Name; Attribute attrchk = Attribute.GetCustomAttribute(fi, typeof(XmlRpcMemberAttribute)); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") member = mmbr; } if (fi.GetValue(o) == null) { NullMappingAction memberAction = MemberNullMappingAction(o.GetType(), fi.Name, structAction); if (memberAction == NullMappingAction.Ignore) continue; else if (memberAction == 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), mappingAction, nestedObjs); WriteFullEndElement(xtw); } else if (mi.MemberType == MemberTypes.Property) { PropertyInfo pi = (PropertyInfo)mi; string member = pi.Name; Attribute attrchk = Attribute.GetCustomAttribute(pi, typeof(XmlRpcMemberAttribute)); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") member = mmbr; } if (pi.GetValue(o, null) == null) { NullMappingAction memberAction = MemberNullMappingAction(o.GetType(), pi.Name, structAction); if (memberAction == NullMappingAction.Ignore) continue; else if (memberAction == 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), mappingAction, 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 (System.NullReferenceException) { throw new XmlRpcNullReferenceException("Attempt to serialize data " + "containing null reference"); } finally { nestedObjs.RemoveAt(nestedObjs.Count - 1); } }
/// <summary> /// /// </summary> /// <param name="xtw"></param> /// <param name="o"></param> /// <param name="mappingAction"></param> /// <param name="nestedObjs"></param> public void Serialize( XmlWriter xtw, Object o, NullMappingAction mappingAction, 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, mappingAction, 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, mappingAction, 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], mappingAction, nestedObjs); WriteFullEndElement(xtw); } WriteFullEndElement(xtw); } else if (xType == XmlRpcType.tInt32) { if (o.GetType().IsEnum) { o = Convert.ToInt32(o); } if (UseIntTag) { WriteFullElementString(xtw, "int", o.ToString()); } else { WriteFullElementString(xtw, "i4", o.ToString()); } } else if (xType == XmlRpcType.tInt64) { if (o.GetType().IsEnum) { o = Convert.ToInt64(o); } WriteFullElementString(xtw, "i8", o.ToString()); } else if (xType == XmlRpcType.tString) { if (UseStringTag) { WriteFullElementString(xtw, "string", (string)o); } else { xtw.WriteString((string)o); } } else if (xType == XmlRpcType.tStruct) { NullMappingAction structAction = StructMappingAction(o.GetType(), mappingAction); xtw.WriteStartElement("", "struct", ""); MemberInfo[] mis = o.GetType().GetMembers(); foreach (MemberInfo mi in mis) { if (Attribute.IsDefined(mi, typeof(NonSerializedAttribute))) { continue; } if (mi.MemberType == MemberTypes.Field) { FieldInfo fi = (FieldInfo)mi; string member = fi.Name; Attribute attrchk = Attribute.GetCustomAttribute(fi, typeof(XmlRpcMemberAttribute)); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") { member = mmbr; } } if (fi.GetValue(o) == null) { NullMappingAction memberAction = MemberNullMappingAction(o.GetType(), fi.Name, structAction); if (memberAction == NullMappingAction.Ignore) { continue; } else if (memberAction == 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), mappingAction, nestedObjs); WriteFullEndElement(xtw); } else if (mi.MemberType == MemberTypes.Property) { PropertyInfo pi = (PropertyInfo)mi; string member = pi.Name; Attribute attrchk = Attribute.GetCustomAttribute(pi, typeof(XmlRpcMemberAttribute)); if (attrchk != null && attrchk is XmlRpcMemberAttribute) { string mmbr = ((XmlRpcMemberAttribute)attrchk).Member; if (mmbr != "") { member = mmbr; } } if (pi.GetValue(o, null) == null) { NullMappingAction memberAction = MemberNullMappingAction(o.GetType(), pi.Name, structAction); if (memberAction == NullMappingAction.Ignore) { continue; } else if (memberAction == 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), mappingAction, 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 (System.NullReferenceException) { throw new XmlRpcNullReferenceException("Attempt to serialize data " + "containing null reference"); } finally { nestedObjs.RemoveAt(nestedObjs.Count - 1); } }
public XmlRpcNullMappingAttribute(NullMappingAction action) { _action = action; }
void Serialize( XmlWriter xtw, Object o, NullMappingAction mappingAction) { Serialize(xtw, o, mappingAction, new List<object>()); }