SoapReflectionImporter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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, mapMem);
  54. }
  55. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, mapping);
  56. mps.RelatedMaps = relatedMaps;
  57. mps.Format = SerializationFormat.Encoded;
  58. return mps;
  59. }
  60. public XmlTypeMapping ImportTypeMapping (Type type)
  61. {
  62. return ImportTypeMapping (type, null);
  63. }
  64. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  65. {
  66. if (type == null)
  67. throw new ArgumentNullException ("type");
  68. if (type == typeof (void))
  69. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  70. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  71. if (defaultNamespace == null) defaultNamespace = string.Empty;
  72. XmlTypeMapping map;
  73. switch (TypeTranslator.GetTypeData(type).SchemaType)
  74. {
  75. case SchemaTypes.Class: map = ImportClassMapping (type, defaultNamespace); break;
  76. case SchemaTypes.Array: map = ImportListMapping (type, defaultNamespace); break;
  77. case SchemaTypes.XmlNode: throw CreateTypeException (type);
  78. case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, defaultNamespace); break;
  79. case SchemaTypes.Enum: map = ImportEnumMapping (type, defaultNamespace); break;
  80. case SchemaTypes.XmlSerializable:
  81. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  82. }
  83. map.RelatedMaps = relatedMaps;
  84. map.Format = SerializationFormat.Encoded;
  85. return map;
  86. }
  87. XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace)
  88. {
  89. string membersNamespace = defaultNamespace;
  90. bool includeInSchema = true;
  91. SoapAttributes atts = null;
  92. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  93. if (!typeData.IsListType)
  94. {
  95. if (attributeOverrides != null)
  96. atts = attributeOverrides[typeData.Type];
  97. if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
  98. throw new InvalidOperationException ("SoapType attribute may not be specified for the type " + typeData.FullTypeName);
  99. }
  100. if (atts == null)
  101. atts = new SoapAttributes (typeData.Type);
  102. if (atts.SoapType != null)
  103. {
  104. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  105. membersNamespace = atts.SoapType.Namespace;
  106. if (atts.SoapType.TypeName != null && atts.SoapType.TypeName != string.Empty)
  107. defaultXmlType = atts.SoapType.TypeName;
  108. includeInSchema = atts.SoapType.IncludeInSchema;
  109. }
  110. if (membersNamespace == null) membersNamespace = "";
  111. XmlTypeMapping map = new XmlTypeMapping (defaultXmlType, membersNamespace, typeData, defaultXmlType, membersNamespace);
  112. map.IncludeInSchema = includeInSchema;
  113. relatedMaps.Add (map);
  114. return map;
  115. }
  116. XmlTypeMapping ImportClassMapping (Type type, string defaultNamespace)
  117. {
  118. if (type.IsValueType) throw CreateStructException (type);
  119. if (type == typeof (object)) defaultNamespace = XmlSchema.Namespace;
  120. if (type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract)
  121. throw new InvalidOperationException (type.Name + " cannot be serialized because it does not have a default public constructor");
  122. TypeData typeData = TypeTranslator.GetTypeData (type);
  123. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  124. if (map != null) return map;
  125. map = CreateTypeMapping (typeData, null, defaultNamespace);
  126. helper.RegisterClrType (map, type, map.Namespace);
  127. map.MultiReferenceType = true;
  128. ClassMap classMap = new ClassMap ();
  129. map.ObjectMap = classMap;
  130. // Import members
  131. try
  132. {
  133. ICollection members = GetReflectionMembers (type);
  134. foreach (XmlReflectionMember rmember in members)
  135. {
  136. if (rmember.SoapAttributes.SoapIgnore) continue;
  137. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. throw helper.CreateError (map, ex.Message);
  143. }
  144. // Import derived classes
  145. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  146. for (int n=0; n<includes.Length; n++)
  147. {
  148. Type includedType = includes[n].Type;
  149. if (!includedType.IsSubclassOf(type)) throw helper.CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  150. XmlTypeMapping derived = ImportTypeMapping (includedType, defaultNamespace);
  151. map.DerivedTypes.Add (derived);
  152. map.DerivedTypes.AddRange (derived.DerivedTypes);
  153. }
  154. if (type == typeof (object) && includedTypes != null)
  155. {
  156. foreach (Type intype in includedTypes)
  157. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  158. }
  159. // Register this map as a derived class of object
  160. if (typeData.Type != typeof(object))
  161. ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
  162. if (type.BaseType != null && type.BaseType != typeof(object))
  163. map.BaseMap = ImportClassMapping (type.BaseType, defaultNamespace);
  164. return map;
  165. }
  166. string GetTypeNamespace (TypeData typeData, string defaultNamespace)
  167. {
  168. string membersNamespace = defaultNamespace;
  169. SoapAttributes atts = null;
  170. if (!typeData.IsListType)
  171. {
  172. if (attributeOverrides != null)
  173. atts = attributeOverrides[typeData.Type];
  174. }
  175. if (atts == null)
  176. atts = new SoapAttributes (typeData.Type);
  177. if (atts.SoapType != null)
  178. {
  179. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  180. membersNamespace = atts.SoapType.Namespace;
  181. }
  182. if (membersNamespace == null) return "";
  183. else return membersNamespace;
  184. }
  185. XmlTypeMapping ImportListMapping (Type type, string defaultNamespace)
  186. {
  187. TypeData typeData = TypeTranslator.GetTypeData (type);
  188. XmlTypeMapping map = helper.GetRegisteredClrType (type, XmlSerializer.EncodingNamespace);
  189. if (map != null) return map;
  190. ListMap obmap = new ListMap ();
  191. TypeData itemTypeData = typeData.ListItemTypeData;
  192. map = CreateTypeMapping (typeData, "Array", XmlSerializer.EncodingNamespace);
  193. helper.RegisterClrType (map, type, XmlSerializer.EncodingNamespace);
  194. map.MultiReferenceType = true;
  195. map.ObjectMap = obmap;
  196. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, itemTypeData);
  197. if (elem.TypeData.IsComplexType) {
  198. elem.MappedType = ImportTypeMapping (typeData.ListItemType, defaultNamespace);
  199. elem.TypeData = elem.MappedType.TypeData;
  200. }
  201. elem.ElementName = "Item";
  202. elem.Namespace = string.Empty;
  203. elem.IsNullable = true; // By default, items are nullable
  204. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  205. list.Add (elem);
  206. obmap.ItemInfo = list;
  207. ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
  208. return map;
  209. }
  210. XmlTypeMapping ImportPrimitiveMapping (Type type, string defaultNamespace)
  211. {
  212. TypeData typeData = TypeTranslator.GetTypeData (type);
  213. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  214. if (map != null) return map;
  215. map = CreateTypeMapping (typeData, null, defaultNamespace);
  216. helper.RegisterClrType (map, type, map.Namespace);
  217. return map;
  218. }
  219. XmlTypeMapping ImportEnumMapping (Type type, string defaultNamespace)
  220. {
  221. TypeData typeData = TypeTranslator.GetTypeData (type);
  222. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  223. if (map != null) return map;
  224. map = CreateTypeMapping (typeData, null, defaultNamespace);
  225. helper.RegisterClrType (map, type, map.Namespace);
  226. string [] names = Enum.GetNames (type);
  227. EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length];
  228. for (int n=0; n<names.Length; n++)
  229. {
  230. MemberInfo[] mem = type.GetMember (names[n]);
  231. string xmlName = names[n];
  232. object[] atts = mem[0].GetCustomAttributes (typeof(SoapEnumAttribute), false);
  233. if (atts.Length > 0) xmlName = ((SoapEnumAttribute)atts[0]).Name;
  234. members[n] = new EnumMap.EnumMapMember (xmlName, names[n]);
  235. }
  236. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  237. map.ObjectMap = new EnumMap (members, isFlags);
  238. ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
  239. return map;
  240. }
  241. public ICollection GetReflectionMembers (Type type)
  242. {
  243. ArrayList members = new ArrayList();
  244. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  245. foreach (PropertyInfo prop in properties)
  246. {
  247. if (!prop.CanRead) continue;
  248. SoapAttributes atts = attributeOverrides[type, prop.Name];
  249. if (atts == null) atts = new SoapAttributes (prop);
  250. if (atts.SoapIgnore) continue;
  251. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  252. members.Add (member);
  253. }
  254. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  255. foreach (FieldInfo field in fields)
  256. {
  257. SoapAttributes atts = attributeOverrides[type, field.Name];
  258. if (atts == null) atts = new SoapAttributes (field);
  259. if (atts.SoapIgnore) continue;
  260. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  261. members.Add (member);
  262. }
  263. return members;
  264. }
  265. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  266. {
  267. XmlTypeMapMember mapMember;
  268. SoapAttributes atts = rmember.SoapAttributes;
  269. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  270. if (atts.SoapAttribute != null)
  271. {
  272. // An attribute
  273. if (atts.SoapElement != null)
  274. throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
  275. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  276. if (atts.SoapAttribute.AttributeName == null)
  277. mapAttribute.AttributeName = rmember.MemberName;
  278. else
  279. mapAttribute.AttributeName = atts.SoapAttribute.AttributeName;
  280. mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
  281. if (typeData.IsComplexType)
  282. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  283. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType);
  284. mapMember = mapAttribute;
  285. }
  286. else
  287. {
  288. if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList ();
  289. else mapMember = new XmlTypeMapMemberElement ();
  290. if (atts.SoapElement != null && atts.SoapElement.DataType != null)
  291. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType);
  292. // Creates an ElementInfo that identifies the element
  293. XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
  294. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData);
  295. elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName;
  296. elem.Namespace = string.Empty;
  297. elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
  298. if (typeData.IsComplexType)
  299. elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  300. infoList.Add (elem);
  301. ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
  302. }
  303. mapMember.TypeData = typeData;
  304. mapMember.Name = rmember.MemberName;
  305. return mapMember;
  306. }
  307. public void IncludeType (Type type)
  308. {
  309. if (type == null)
  310. throw new ArgumentNullException ("type");
  311. if (includedTypes == null) includedTypes = new ArrayList ();
  312. includedTypes.Add (type);
  313. }
  314. [MonoTODO]
  315. public void IncludeTypes (ICustomAttributeProvider provider)
  316. {
  317. throw new NotImplementedException ();
  318. }
  319. Exception CreateTypeException (Type type)
  320. {
  321. return new NotSupportedException ("The type " + type.FullName + " may not be serialized with SOAP-encoded messages. Set the Use for your message to Literal");
  322. }
  323. Exception CreateStructException (Type type)
  324. {
  325. return new NotSupportedException ("Cannot serialize " + type.FullName + ". Nested structs are not supported with encoded SOAP");
  326. }
  327. #endregion // Methods
  328. }
  329. }