SoapReflectionImporter.cs 14 KB

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