SoapReflectionImporter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #region Constructors
  21. public SoapReflectionImporter (): this (null, null)
  22. {
  23. }
  24. public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides): this (attributeOverrides, null)
  25. {
  26. }
  27. public SoapReflectionImporter (string defaultNamespace): this (null, defaultNamespace)
  28. {
  29. }
  30. public SoapReflectionImporter (SoapAttributeOverrides attributeOverrides, string defaultNamespace)
  31. {
  32. if (defaultNamespace == null) initialDefaultNamespace = String.Empty;
  33. else initialDefaultNamespace = defaultNamespace;
  34. if (attributeOverrides == null) this.attributeOverrides = new SoapAttributeOverrides();
  35. else this.attributeOverrides = attributeOverrides;
  36. }
  37. #endregion // Constructors
  38. #region Methods
  39. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members)
  40. {
  41. return ImportMembersMapping (elementName, ns, members, true, true, false);
  42. }
  43. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors)
  44. {
  45. return ImportMembersMapping (elementName, ns, members, hasWrapperElement, writeAccessors, false);
  46. }
  47. public XmlMembersMapping ImportMembersMapping (string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate)
  48. {
  49. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  50. for (int n=0; n<members.Length; n++)
  51. {
  52. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  53. mapping[n] = new XmlMemberMapping (members[n].MemberName, ns, mapMem, true);
  54. }
  55. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, writeAccessors, mapping);
  56. mps.RelatedMaps = relatedMaps;
  57. mps.Format = SerializationFormat.Encoded;
  58. mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, writeAccessors, false, null, includedTypes);
  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.XmlSerializable:
  82. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  83. }
  84. map.RelatedMaps = relatedMaps;
  85. map.Format = SerializationFormat.Encoded;
  86. map.Source = new SoapTypeSerializationSource (type, attributeOverrides, defaultNamespace, includedTypes);
  87. return map;
  88. }
  89. XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace)
  90. {
  91. string membersNamespace = defaultNamespace;
  92. bool includeInSchema = true;
  93. SoapAttributes atts = null;
  94. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  95. if (!typeData.IsListType)
  96. {
  97. if (attributeOverrides != null)
  98. atts = attributeOverrides[typeData.Type];
  99. if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
  100. throw new InvalidOperationException ("SoapType attribute may not be specified for the type " + typeData.FullTypeName);
  101. }
  102. if (atts == null)
  103. atts = new SoapAttributes (typeData.Type);
  104. if (atts.SoapType != null)
  105. {
  106. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  107. membersNamespace = atts.SoapType.Namespace;
  108. if (atts.SoapType.TypeName != null && atts.SoapType.TypeName != string.Empty)
  109. defaultXmlType = atts.SoapType.TypeName;
  110. includeInSchema = atts.SoapType.IncludeInSchema;
  111. }
  112. if (membersNamespace == null) membersNamespace = "";
  113. XmlTypeMapping map = new XmlTypeMapping (defaultXmlType, membersNamespace, typeData, defaultXmlType, membersNamespace);
  114. map.IncludeInSchema = includeInSchema;
  115. relatedMaps.Add (map);
  116. return map;
  117. }
  118. XmlTypeMapping ImportClassMapping (Type type, string defaultNamespace)
  119. {
  120. if (type.IsValueType) throw CreateStructException (type);
  121. if (type == typeof (object)) defaultNamespace = XmlSchema.Namespace;
  122. ReflectionHelper.CheckSerializableType (type);
  123. TypeData typeData = TypeTranslator.GetTypeData (type);
  124. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  125. if (map != null) return map;
  126. map = CreateTypeMapping (typeData, null, defaultNamespace);
  127. helper.RegisterClrType (map, type, map.Namespace);
  128. map.MultiReferenceType = true;
  129. ClassMap classMap = new ClassMap ();
  130. map.ObjectMap = classMap;
  131. // Import members
  132. try
  133. {
  134. ICollection members = GetReflectionMembers (type);
  135. foreach (XmlReflectionMember rmember in members)
  136. {
  137. if (rmember.SoapAttributes.SoapIgnore) continue;
  138. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. throw helper.CreateError (map, ex.Message);
  144. }
  145. // Import included classes
  146. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  147. for (int n=0; n<includes.Length; n++)
  148. {
  149. Type includedType = includes[n].Type;
  150. ImportTypeMapping (includedType);
  151. }
  152. if (type == typeof (object) && includedTypes != null)
  153. {
  154. foreach (Type intype in includedTypes)
  155. map.DerivedTypes.Add (ImportTypeMapping (intype));
  156. }
  157. // Register inheritance relations
  158. if (type.BaseType != null)
  159. {
  160. XmlTypeMapping bmap = ImportClassMapping (type.BaseType, defaultNamespace);
  161. if (type.BaseType != typeof (object))
  162. map.BaseMap = bmap;
  163. // At this point, derived classes of this map must be already registered
  164. RegisterDerivedMap (bmap, map);
  165. }
  166. return map;
  167. }
  168. void RegisterDerivedMap (XmlTypeMapping map, XmlTypeMapping derivedMap)
  169. {
  170. map.DerivedTypes.Add (derivedMap);
  171. map.DerivedTypes.AddRange (derivedMap.DerivedTypes);
  172. if (map.BaseMap != null)
  173. RegisterDerivedMap (map.BaseMap, derivedMap);
  174. }
  175. string GetTypeNamespace (TypeData typeData, string defaultNamespace)
  176. {
  177. string membersNamespace = defaultNamespace;
  178. SoapAttributes atts = null;
  179. if (!typeData.IsListType)
  180. {
  181. if (attributeOverrides != null)
  182. atts = attributeOverrides[typeData.Type];
  183. }
  184. if (atts == null)
  185. atts = new SoapAttributes (typeData.Type);
  186. if (atts.SoapType != null)
  187. {
  188. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  189. membersNamespace = atts.SoapType.Namespace;
  190. }
  191. if (membersNamespace == null) return "";
  192. else return membersNamespace;
  193. }
  194. XmlTypeMapping ImportListMapping (Type type, string defaultNamespace)
  195. {
  196. TypeData typeData = TypeTranslator.GetTypeData (type);
  197. XmlTypeMapping map = helper.GetRegisteredClrType (type, XmlSerializer.EncodingNamespace);
  198. if (map != null) return map;
  199. ListMap obmap = new ListMap ();
  200. TypeData itemTypeData = typeData.ListItemTypeData;
  201. map = CreateTypeMapping (typeData, "Array", XmlSerializer.EncodingNamespace);
  202. helper.RegisterClrType (map, type, XmlSerializer.EncodingNamespace);
  203. map.MultiReferenceType = true;
  204. map.ObjectMap = obmap;
  205. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, itemTypeData);
  206. if (elem.TypeData.IsComplexType) {
  207. elem.MappedType = ImportTypeMapping (typeData.ListItemType, defaultNamespace);
  208. elem.TypeData = elem.MappedType.TypeData;
  209. }
  210. elem.ElementName = "Item";
  211. elem.Namespace = string.Empty;
  212. elem.IsNullable = true; // By default, items are nullable
  213. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  214. list.Add (elem);
  215. obmap.ItemInfo = list;
  216. XmlTypeMapping objMap = ImportTypeMapping (typeof(object), defaultNamespace);
  217. objMap.DerivedTypes.Add (map);
  218. // Register any of the including types as a derived class of object
  219. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  220. for (int i = 0; i < includes.Length; i++)
  221. {
  222. Type includedType = includes[i].Type;
  223. objMap.DerivedTypes.Add(ImportTypeMapping (includedType, defaultNamespace));
  224. }
  225. return map;
  226. }
  227. XmlTypeMapping ImportPrimitiveMapping (Type type, string defaultNamespace)
  228. {
  229. TypeData typeData = TypeTranslator.GetTypeData (type);
  230. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  231. if (map != null) return map;
  232. map = CreateTypeMapping (typeData, null, defaultNamespace);
  233. helper.RegisterClrType (map, type, map.Namespace);
  234. return map;
  235. }
  236. XmlTypeMapping ImportEnumMapping (Type type, string defaultNamespace)
  237. {
  238. TypeData typeData = TypeTranslator.GetTypeData (type);
  239. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  240. if (map != null) return map;
  241. map = CreateTypeMapping (typeData, null, defaultNamespace);
  242. helper.RegisterClrType (map, type, map.Namespace);
  243. map.MultiReferenceType = true;
  244. string [] names = Enum.GetNames (type);
  245. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
  246. for (int n=0; n<names.Length; n++)
  247. {
  248. MemberInfo[] mem = type.GetMember (names[n]);
  249. string xmlName = names[n];
  250. object[] atts = mem[0].GetCustomAttributes (typeof(SoapEnumAttribute), false);
  251. if (atts.Length > 0) xmlName = ((SoapEnumAttribute)atts[0]).Name;
  252. members[n] = new EnumMap.EnumMapMember (xmlName, names[n]);
  253. }
  254. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  255. map.ObjectMap = new EnumMap (members, isFlags);
  256. ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
  257. return map;
  258. }
  259. ICollection GetReflectionMembers (Type type)
  260. {
  261. ArrayList members = new ArrayList();
  262. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  263. foreach (PropertyInfo prop in properties)
  264. {
  265. if (!prop.CanRead) continue;
  266. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  267. continue;
  268. SoapAttributes atts = attributeOverrides[type, prop.Name];
  269. if (atts == null) atts = new SoapAttributes (prop);
  270. if (atts.SoapIgnore) continue;
  271. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  272. members.Add (member);
  273. }
  274. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  275. foreach (FieldInfo field in fields)
  276. {
  277. SoapAttributes atts = attributeOverrides[type, field.Name];
  278. if (atts == null) atts = new SoapAttributes (field);
  279. if (atts.SoapIgnore) continue;
  280. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  281. members.Add (member);
  282. }
  283. return members;
  284. }
  285. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  286. {
  287. XmlTypeMapMember mapMember;
  288. SoapAttributes atts = rmember.SoapAttributes;
  289. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  290. if (atts.SoapAttribute != null)
  291. {
  292. // An attribute
  293. if (atts.SoapElement != null)
  294. throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
  295. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  296. if (atts.SoapAttribute.AttributeName == null)
  297. mapAttribute.AttributeName = rmember.MemberName;
  298. else
  299. mapAttribute.AttributeName = atts.SoapAttribute.AttributeName;
  300. mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
  301. if (typeData.IsComplexType)
  302. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  303. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType);
  304. mapMember = mapAttribute;
  305. }
  306. else
  307. {
  308. if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList ();
  309. else mapMember = new XmlTypeMapMemberElement ();
  310. if (atts.SoapElement != null && atts.SoapElement.DataType != null)
  311. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType);
  312. // Creates an ElementInfo that identifies the element
  313. XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
  314. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData);
  315. elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName;
  316. elem.Namespace = string.Empty;
  317. elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
  318. if (typeData.IsComplexType)
  319. elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  320. infoList.Add (elem);
  321. ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
  322. }
  323. mapMember.TypeData = typeData;
  324. mapMember.Name = rmember.MemberName;
  325. return mapMember;
  326. }
  327. public void IncludeType (Type type)
  328. {
  329. if (type == null)
  330. throw new ArgumentNullException ("type");
  331. if (includedTypes == null) includedTypes = new ArrayList ();
  332. if (!includedTypes.Contains (type))
  333. includedTypes.Add (type);
  334. }
  335. public void IncludeTypes (ICustomAttributeProvider provider)
  336. {
  337. object[] ats = provider.GetCustomAttributes (typeof(SoapIncludeAttribute), true);
  338. foreach (SoapIncludeAttribute at in ats)
  339. IncludeType (at.Type);
  340. }
  341. Exception CreateTypeException (Type type)
  342. {
  343. return new NotSupportedException ("The type " + type.FullName + " may not be serialized with SOAP-encoded messages. Set the Use for your message to Literal");
  344. }
  345. Exception CreateStructException (Type type)
  346. {
  347. return new NotSupportedException ("Cannot serialize " + type.FullName + ". Nested structs are not supported with encoded SOAP");
  348. }
  349. #endregion // Methods
  350. }
  351. }