SoapReflectionImporter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //
  2. // System.Xml.Serialization.SoapReflectionImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System.Reflection;
  10. using System.Xml;
  11. using System.Xml.Schema;
  12. using System.Collections;
  13. namespace System.Xml.Serialization {
  14. public class SoapReflectionImporter {
  15. SoapAttributeOverrides attributeOverrides;
  16. string initialDefaultNamespace;
  17. ArrayList includedTypes;
  18. ArrayList relatedMaps = new ArrayList ();
  19. ReflectionHelper helper = new ReflectionHelper();
  20. internal const string EncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  21. #region Constructors
  22. public SoapReflectionImporter (): this (null, null)
  23. {
  24. }
  25. public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides): this (attributeOverrides, null)
  26. {
  27. }
  28. public SoapReflectionImporter (string defaultNamespace): this (null, defaultNamespace)
  29. {
  30. }
  31. public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides, string defaultNamespace)
  32. {
  33. if (defaultNamespace == null) initialDefaultNamespace = String.Empty;
  34. else initialDefaultNamespace = defaultNamespace;
  35. if (attributeOverrides == null) this.attributeOverrides = new SoapAttributeOverrides();
  36. else this.attributeOverrides = attributeOverrides;
  37. }
  38. #endregion // Constructors
  39. #region Methods
  40. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members)
  41. {
  42. return ImportMembersMapping (elementName, ns, members, true, true, false);
  43. }
  44. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors)
  45. {
  46. return ImportMembersMapping (elementName, ns, members, hasWrapperElement, writeAccessors, false);
  47. }
  48. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate)
  49. {
  50. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  51. for (int n=0; n<members.Length; n++)
  52. {
  53. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  54. mapping[n] = new XmlMemberMapping (members[n], mapMem);
  55. }
  56. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, mapping);
  57. mps.RelatedMaps = relatedMaps;
  58. mps.Format = SerializationFormat.Encoded;
  59. return mps;
  60. }
  61. public XmlTypeMapping ImportTypeMapping (Type type)
  62. {
  63. return ImportTypeMapping (type, null);
  64. }
  65. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  66. {
  67. if (type == null)
  68. throw new ArgumentNullException ("type");
  69. if (type == typeof (void))
  70. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  71. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  72. if (defaultNamespace == null) defaultNamespace = string.Empty;
  73. XmlTypeMapping map;
  74. switch (TypeTranslator.GetTypeData(type).SchemaType)
  75. {
  76. case SchemaTypes.Class: map = ImportClassMapping (type, defaultNamespace); break;
  77. case SchemaTypes.Array: map = ImportListMapping (type, defaultNamespace); break;
  78. case SchemaTypes.XmlNode: throw CreateTypeException (type);
  79. case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, defaultNamespace); break;
  80. case SchemaTypes.Enum: map = ImportEnumMapping (type, defaultNamespace); break;
  81. case SchemaTypes.DataSet:
  82. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  83. }
  84. map.RelatedMaps = relatedMaps;
  85. map.Format = SerializationFormat.Encoded;
  86. return map;
  87. }
  88. XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace)
  89. {
  90. string membersNamespace = defaultNamespace;
  91. string elementName;
  92. SoapAttributes atts = null;
  93. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  94. if (!typeData.IsListType)
  95. {
  96. if (attributeOverrides != null)
  97. atts = attributeOverrides[typeData.Type];
  98. }
  99. if (atts == null)
  100. atts = new SoapAttributes (typeData.Type);
  101. if (atts.SoapType != null)
  102. {
  103. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  104. membersNamespace = atts.SoapType.Namespace;
  105. if (atts.SoapType.TypeName != null && atts.SoapType.TypeName != string.Empty)
  106. defaultXmlType = atts.SoapType.TypeName;
  107. }
  108. elementName = defaultXmlType;
  109. if (membersNamespace == null) membersNamespace = "";
  110. XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType);
  111. relatedMaps.Add (map);
  112. return map;
  113. }
  114. XmlTypeMapping ImportClassMapping (Type type, string defaultNamespace)
  115. {
  116. if (type.IsValueType) throw CreateStructException (type);
  117. if (type == typeof (object)) defaultNamespace = XmlSchema.Namespace;
  118. TypeData typeData = TypeTranslator.GetTypeData (type);
  119. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  120. if (map != null) return map;
  121. map = CreateTypeMapping (typeData, null, defaultNamespace);
  122. helper.RegisterClrType (map, type, defaultNamespace);
  123. map.MultiReferenceType = true;
  124. ClassMap classMap = new ClassMap ();
  125. map.ObjectMap = classMap;
  126. // Import members
  127. try
  128. {
  129. ICollection members = GetReflectionMembers (type);
  130. foreach (XmlReflectionMember rmember in members)
  131. {
  132. if (rmember.SoapAttributes.SoapIgnore) continue;
  133. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. throw helper.CreateError (map, ex.Message);
  139. }
  140. // Import derived classes
  141. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  142. for (int n=0; n<includes.Length; n++)
  143. {
  144. Type includedType = includes[n].Type;
  145. if (!includedType.IsSubclassOf(type)) throw helper.CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  146. map.DerivedTypes.Add (ImportTypeMapping (includedType, defaultNamespace));
  147. }
  148. if (type == typeof (object) && includedTypes != null)
  149. {
  150. foreach (Type intype in includedTypes)
  151. map.DerivedTypes.Add (ImportTypeMapping (intype));
  152. }
  153. // Register this map as a derived class of object
  154. if (typeData.Type != typeof(object))
  155. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  156. return map;
  157. }
  158. XmlTypeMapping ImportListMapping (Type type, string defaultNamespace)
  159. {
  160. TypeData typeData = TypeTranslator.GetTypeData (type);
  161. XmlTypeMapping map = helper.GetRegisteredClrType (type, EncodingNamespace);
  162. if (map != null) return map;
  163. ListMap obmap = new ListMap ();
  164. map = CreateTypeMapping (typeData, "Array", EncodingNamespace);
  165. helper.RegisterClrType (map, type, EncodingNamespace);
  166. map.MultiReferenceType = true;
  167. map.ObjectMap = obmap;
  168. Type itemType = typeData.ListItemType;
  169. TypeData itemTypeData = TypeTranslator.GetTypeData (itemType);
  170. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, itemTypeData);
  171. if (elem.TypeData.IsComplexType) {
  172. elem.MappedType = ImportTypeMapping (itemType);
  173. elem.DataType = elem.MappedType.XmlType;
  174. }
  175. else
  176. elem.DataType = itemTypeData.XmlType;
  177. elem.ElementName = "Item";
  178. elem.Namespace = string.Empty;
  179. elem.IsNullable = true; // By default, items are nullable
  180. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  181. list.Add (elem);
  182. obmap.ItemInfo = list;
  183. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  184. return map;
  185. }
  186. XmlTypeMapping ImportPrimitiveMapping (Type type, string defaultNamespace)
  187. {
  188. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  189. if (map != null) return map;
  190. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), null, defaultNamespace);
  191. helper.RegisterClrType (map, type, defaultNamespace);
  192. return map;
  193. }
  194. XmlTypeMapping ImportEnumMapping (Type type, string defaultNamespace)
  195. {
  196. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  197. if (map != null) return map;
  198. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), null, defaultNamespace);
  199. helper.RegisterClrType (map, type, defaultNamespace);
  200. string [] names = Enum.GetNames (type);
  201. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
  202. for (int n=0; n<names.Length; n++)
  203. {
  204. MemberInfo[] mem = type.GetMember (names[n]);
  205. string xmlName = names[n];
  206. object[] atts = mem[0].GetCustomAttributes (typeof(SoapEnumAttribute), false);
  207. if (atts.Length > 0) xmlName = ((SoapEnumAttribute)atts[0]).Name;
  208. members[n] = new EnumMap.EnumMapMember (xmlName, names[n]);
  209. }
  210. map.ObjectMap = new EnumMap (members);
  211. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  212. return map;
  213. }
  214. public ICollection GetReflectionMembers (Type type)
  215. {
  216. ArrayList members = new ArrayList();
  217. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  218. foreach (PropertyInfo prop in properties)
  219. {
  220. if (!prop.CanRead) continue;
  221. SoapAttributes atts = attributeOverrides[type, prop.Name];
  222. if (atts == null) atts = new SoapAttributes (prop);
  223. if (atts.SoapIgnore) continue;
  224. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  225. members.Add (member);
  226. }
  227. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  228. foreach (FieldInfo field in fields)
  229. {
  230. SoapAttributes atts = attributeOverrides[type, field.Name];
  231. if (atts == null) atts = new SoapAttributes (field);
  232. if (atts.SoapIgnore) continue;
  233. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  234. members.Add (member);
  235. }
  236. return members;
  237. }
  238. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  239. {
  240. XmlTypeMapMember mapMember;
  241. SoapAttributes atts = rmember.SoapAttributes;
  242. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  243. if (atts.SoapAttribute != null)
  244. {
  245. // An attribute
  246. if (atts.SoapElement != null)
  247. throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
  248. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  249. if (atts.SoapAttribute.AttributeName == null)
  250. mapAttribute.AttributeName = rmember.MemberName;
  251. else
  252. mapAttribute.AttributeName = atts.SoapAttribute.AttributeName;
  253. mapAttribute.DataType = atts.SoapAttribute.DataType;
  254. mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
  255. if (typeData.IsComplexType)
  256. mapAttribute.MappedType = ImportTypeMapping (typeData.Type);
  257. mapMember = mapAttribute;
  258. }
  259. else
  260. {
  261. if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList ();
  262. else mapMember = new XmlTypeMapMemberElement ();
  263. // Creates an ElementInfo that identifies the element
  264. XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
  265. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData);
  266. elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName;
  267. elem.Namespace = string.Empty;
  268. elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
  269. elem.DataType = (atts.SoapElement != null && atts.SoapElement.DataType != null) ? atts.SoapElement.DataType : typeData.XmlType;
  270. if (typeData.IsComplexType)
  271. elem.MappedType = ImportTypeMapping (typeData.Type);
  272. infoList.Add (elem);
  273. ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
  274. }
  275. mapMember.TypeData = typeData;
  276. mapMember.Name = rmember.MemberName;
  277. return mapMember;
  278. }
  279. public void IncludeType (Type type)
  280. {
  281. if (type == null)
  282. throw new ArgumentNullException ("type");
  283. if (includedTypes == null) includedTypes = new ArrayList ();
  284. includedTypes.Add (type);
  285. }
  286. [MonoTODO]
  287. public void IncludeTypes (ICustomAttributeProvider provider)
  288. {
  289. throw new NotImplementedException ();
  290. }
  291. Exception CreateTypeException (Type type)
  292. {
  293. return new NotSupportedException ("The type " + type.FullName + " may not be serialized with SOAP-encoded messages. Set the Use for your message to Literal");
  294. }
  295. Exception CreateStructException (Type type)
  296. {
  297. return new NotSupportedException ("Cannot serialize " + type.FullName + ". Nested structs are not supported with encoded SOAP");
  298. }
  299. #endregion // Methods
  300. }
  301. }