SoapReflectionImporter.cs 14 KB

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