SoapReflectionImporter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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, 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. ReflectionHelper.CheckSerializableType (type);
  121. TypeData typeData = TypeTranslator.GetTypeData (type);
  122. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, defaultNamespace));
  123. if (map != null) return map;
  124. map = CreateTypeMapping (typeData, null, defaultNamespace);
  125. helper.RegisterClrType (map, type, map.Namespace);
  126. map.MultiReferenceType = true;
  127. ClassMap classMap = new ClassMap ();
  128. map.ObjectMap = classMap;
  129. // Import members
  130. try
  131. {
  132. ICollection members = GetReflectionMembers (type);
  133. foreach (XmlReflectionMember rmember in members)
  134. {
  135. if (rmember.SoapAttributes.SoapIgnore) continue;
  136. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. throw helper.CreateError (map, ex.Message);
  142. }
  143. // Import derived classes
  144. SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes (typeof (SoapIncludeAttribute), false);
  145. for (int n=0; n<includes.Length; n++)
  146. {
  147. Type includedType = includes[n].Type;
  148. if (!includedType.IsSubclassOf(type)) throw helper.CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  149. XmlTypeMapping derived = ImportTypeMapping (includedType, defaultNamespace);
  150. map.DerivedTypes.Add (derived);
  151. map.DerivedTypes.AddRange (derived.DerivedTypes);
  152. }
  153. if (type == typeof (object) && includedTypes != null)
  154. {
  155. foreach (Type intype in includedTypes)
  156. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  157. }
  158. // Register this map as a derived class of object
  159. if (typeData.Type != typeof(object))
  160. ImportTypeMapping (typeof(object), defaultNamespace).DerivedTypes.Add (map);
  161. if (type.BaseType != null && type.BaseType != typeof(object))
  162. map.BaseMap = ImportClassMapping (type.BaseType, defaultNamespace);
  163. return map;
  164. }
  165. string GetTypeNamespace (TypeData typeData, string defaultNamespace)
  166. {
  167. string membersNamespace = defaultNamespace;
  168. SoapAttributes atts = null;
  169. if (!typeData.IsListType)
  170. {
  171. if (attributeOverrides != null)
  172. atts = attributeOverrides[typeData.Type];
  173. }
  174. if (atts == null)
  175. atts = new SoapAttributes (typeData.Type);
  176. if (atts.SoapType != null)
  177. {
  178. if (atts.SoapType.Namespace != null && atts.SoapType.Namespace != string.Empty)
  179. membersNamespace = atts.SoapType.Namespace;
  180. }
  181. if (membersNamespace == null) return "";
  182. else return membersNamespace;
  183. }
  184. XmlTypeMapping ImportListMapping (Type type, string defaultNamespace)
  185. {
  186. TypeData typeData = TypeTranslator.GetTypeData (type);
  187. XmlTypeMapping map = helper.GetRegisteredClrType (type, XmlSerializer.EncodingNamespace);
  188. if (map != null) return map;
  189. ListMap obmap = new ListMap ();
  190. TypeData itemTypeData = typeData.ListItemTypeData;
  191. map = CreateTypeMapping (typeData, "Array", XmlSerializer.EncodingNamespace);
  192. helper.RegisterClrType (map, type, XmlSerializer.EncodingNamespace);
  193. map.MultiReferenceType = true;
  194. map.ObjectMap = obmap;
  195. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, itemTypeData);
  196. if (elem.TypeData.IsComplexType) {
  197. elem.MappedType = ImportTypeMapping (typeData.ListItemType, defaultNamespace);
  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), defaultNamespace).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. map.MultiReferenceType = true;
  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. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  249. continue;
  250. SoapAttributes atts = attributeOverrides[type, prop.Name];
  251. if (atts == null) atts = new SoapAttributes (prop);
  252. if (atts.SoapIgnore) continue;
  253. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  254. members.Add (member);
  255. }
  256. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  257. foreach (FieldInfo field in fields)
  258. {
  259. SoapAttributes atts = attributeOverrides[type, field.Name];
  260. if (atts == null) atts = new SoapAttributes (field);
  261. if (atts.SoapIgnore) continue;
  262. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  263. members.Add (member);
  264. }
  265. return members;
  266. }
  267. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  268. {
  269. XmlTypeMapMember mapMember;
  270. SoapAttributes atts = rmember.SoapAttributes;
  271. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  272. if (atts.SoapAttribute != null)
  273. {
  274. // An attribute
  275. if (atts.SoapElement != null)
  276. throw new Exception ("SoapAttributeAttribute and SoapElementAttribute cannot be applied to the same member");
  277. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  278. if (atts.SoapAttribute.AttributeName == null)
  279. mapAttribute.AttributeName = rmember.MemberName;
  280. else
  281. mapAttribute.AttributeName = atts.SoapAttribute.AttributeName;
  282. mapAttribute.Namespace = (atts.SoapAttribute.Namespace != null) ? atts.SoapAttribute.Namespace : "";
  283. if (typeData.IsComplexType)
  284. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  285. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapAttribute.DataType);
  286. mapMember = mapAttribute;
  287. }
  288. else
  289. {
  290. if (typeData.SchemaType == SchemaTypes.Array) mapMember = new XmlTypeMapMemberList ();
  291. else mapMember = new XmlTypeMapMemberElement ();
  292. if (atts.SoapElement != null && atts.SoapElement.DataType != null)
  293. typeData = TypeTranslator.GetTypeData (rmember.MemberType, atts.SoapElement.DataType);
  294. // Creates an ElementInfo that identifies the element
  295. XmlTypeMapElementInfoList infoList = new XmlTypeMapElementInfoList();
  296. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (mapMember, typeData);
  297. elem.ElementName = (atts.SoapElement != null && atts.SoapElement.ElementName != null) ? atts.SoapElement.ElementName : rmember.MemberName;
  298. elem.Namespace = string.Empty;
  299. elem.IsNullable = (atts.SoapElement != null) ? atts.SoapElement.IsNullable : false;
  300. if (typeData.IsComplexType)
  301. elem.MappedType = ImportTypeMapping (typeData.Type, defaultNamespace);
  302. infoList.Add (elem);
  303. ((XmlTypeMapMemberElement)mapMember).ElementInfo = infoList;
  304. }
  305. mapMember.TypeData = typeData;
  306. mapMember.Name = rmember.MemberName;
  307. return mapMember;
  308. }
  309. public void IncludeType (Type type)
  310. {
  311. if (type == null)
  312. throw new ArgumentNullException ("type");
  313. if (includedTypes == null) includedTypes = new ArrayList ();
  314. includedTypes.Add (type);
  315. }
  316. [MonoTODO]
  317. public void IncludeTypes (ICustomAttributeProvider provider)
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. Exception CreateTypeException (Type type)
  322. {
  323. return new NotSupportedException ("The type " + type.FullName + " may not be serialized with SOAP-encoded messages. Set the Use for your message to Literal");
  324. }
  325. Exception CreateStructException (Type type)
  326. {
  327. return new NotSupportedException ("Cannot serialize " + type.FullName + ". Nested structs are not supported with encoded SOAP");
  328. }
  329. #endregion // Methods
  330. }
  331. }