SoapReflectionImporter.cs 13 KB

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