XmlReflectionImporter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //
  2. // System.Xml.Serialization.XmlReflectionImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Erik LeBel ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2002
  10. // (C) 2003 Erik LeBel
  11. //
  12. using System.Reflection;
  13. using System.Collections;
  14. namespace System.Xml.Serialization {
  15. public class XmlReflectionImporter {
  16. string defaultNamespace;
  17. XmlAttributeOverrides attributeOverrides;
  18. ArrayList includedTypes;
  19. Hashtable clrTypes = new Hashtable ();
  20. Hashtable schemaTypes = new Hashtable ();
  21. int arrayChoiceCount = 1;
  22. #region Constructors
  23. public XmlReflectionImporter ()
  24. : this (null, null)
  25. {
  26. }
  27. public XmlReflectionImporter (string defaultNamespace)
  28. : this (null, defaultNamespace)
  29. {
  30. }
  31. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
  32. : this (attributeOverrides, null)
  33. {
  34. }
  35. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
  36. {
  37. if (defaultNamespace == null)
  38. this.defaultNamespace = String.Empty;
  39. else
  40. this.defaultNamespace = defaultNamespace;
  41. if (attributeOverrides == null)
  42. this.attributeOverrides = new XmlAttributeOverrides();
  43. else
  44. this.attributeOverrides = attributeOverrides;
  45. }
  46. #endregion // Constructors
  47. #region Methods
  48. public XmlTypeMapping ImportTypeMapping (Type type)
  49. {
  50. return ImportTypeMapping (type, null, "");
  51. }
  52. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  53. {
  54. return ImportTypeMapping (type, null, defaultNamespace);
  55. }
  56. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
  57. {
  58. return ImportTypeMapping (type, group, "");
  59. }
  60. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  61. {
  62. if (type == null)
  63. throw new ArgumentNullException ("type");
  64. if (type == typeof (void))
  65. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  66. switch (TypeTranslator.GetTypeData(type).SchemaType)
  67. {
  68. case SchemaTypes.Class: return ImportClassMapping (type, root, defaultNamespace);
  69. case SchemaTypes.Array: return ImportListMapping (type, root, defaultNamespace, null, 0);
  70. case SchemaTypes.XmlNode: return ImportXmlNodeMapping (type, root, defaultNamespace);
  71. case SchemaTypes.DataSet:
  72. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  73. }
  74. }
  75. XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
  76. {
  77. string membersNamespace = defaultNamespace;
  78. string elementName;
  79. XmlAttributes atts = null;
  80. if (defaultXmlType == null) defaultXmlType = typeData.ElementName;
  81. if (!typeData.IsListType)
  82. {
  83. if (attributeOverrides != null)
  84. atts = attributeOverrides[typeData.Type];
  85. }
  86. if (atts == null)
  87. atts = new XmlAttributes (typeData.Type);
  88. if (atts.XmlRoot != null && root == null)
  89. root = atts.XmlRoot;
  90. if (atts.XmlType != null)
  91. {
  92. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
  93. membersNamespace = atts.XmlType.Namespace;
  94. if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
  95. defaultXmlType = atts.XmlType.TypeName;
  96. }
  97. elementName = defaultXmlType;
  98. if (root != null)
  99. {
  100. if (root.ElementName != null && root.ElementName != String.Empty)
  101. elementName = root.ElementName;
  102. if (root.Namespace != null && root.Namespace != String.Empty)
  103. membersNamespace = root.Namespace;
  104. }
  105. if (membersNamespace == null) membersNamespace = "";
  106. XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType);
  107. return map;
  108. }
  109. XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  110. {
  111. TypeData typeData = TypeTranslator.GetTypeData (type);
  112. XmlTypeMapping map = GetRegisteredClrType (type, defaultNamespace);
  113. if (map != null) return map;
  114. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  115. RegisterClrType (map, type, defaultNamespace);
  116. RegisterSchemaType (map, map.XmlType, defaultNamespace);
  117. map.ObjectMap = new ClassMap ();
  118. // Import members
  119. ICollection members = GetReflectionMembers (type);
  120. foreach (XmlReflectionMember rmember in members)
  121. {
  122. if (rmember.XmlAttributes.XmlIgnore) continue;
  123. AddMember (map, rmember, map.Namespace);
  124. }
  125. // Import derived classes
  126. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  127. for (int n=0; n<includes.Length; n++)
  128. {
  129. Type includedType = includes[n].Type;
  130. if (!includedType.IsSubclassOf(type)) throw CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  131. map.DerivedTypes.Add (ImportTypeMapping (includedType, root, defaultNamespace));
  132. }
  133. if (type == typeof (object) && includedTypes != null)
  134. {
  135. foreach (Type intype in includedTypes)
  136. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  137. }
  138. // Register this map as a derived class of object
  139. if (typeData.Type != typeof(object))
  140. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  141. return map;
  142. }
  143. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  144. {
  145. TypeData typeData = TypeTranslator.GetTypeData (type);
  146. ListMap obmap = new ListMap ();
  147. if (atts == null) atts = new XmlAttributes();
  148. Type itemType = typeData.ListItemType;
  149. bool isMultiArray = (type.IsArray && itemType.IsArray);
  150. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  151. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  152. {
  153. if (att.NestingLevel != nestingLevel) continue;
  154. Type elemType = (att.Type != null) ? att.Type : itemType;
  155. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType));
  156. elem.DataType = att.DataType;
  157. elem.Namespace = att.Namespace != null ? att.Namespace : "";
  158. elem.Form = att.Form;
  159. elem.IsNullable = att.IsNullable;
  160. elem.NestingLevel = att.NestingLevel;
  161. if (isMultiArray)
  162. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  163. else if (elem.TypeData.IsComplexType)
  164. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  165. if (att.ElementName != null) elem.ElementName = att.ElementName;
  166. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  167. else elem.ElementName = TypeTranslator.GetTypeData(elemType).ElementName;
  168. list.Add (elem);
  169. }
  170. if (list.Count == 0)
  171. {
  172. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  173. if (isMultiArray)
  174. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  175. else if (elem.TypeData.IsComplexType)
  176. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  177. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  178. else elem.ElementName = TypeTranslator.GetTypeData(itemType).ElementName ;
  179. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  180. elem.IsNullable = true; // By default, items are nullable
  181. list.Add (elem);
  182. }
  183. obmap.ItemInfo = list;
  184. // If there can be different element names (types) in the array, then its name cannot
  185. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  186. string baseName;
  187. if (list.Count > 1)
  188. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  189. else
  190. {
  191. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  192. if (elem.MappedType != null) baseName = "ArrayOf" + elem.MappedType.ElementName;
  193. else baseName = "ArrayOf" + elem.ElementName;
  194. }
  195. // Avoid name colisions
  196. int nameCount = 1;
  197. string name = baseName;
  198. do {
  199. XmlTypeMapping foundMap = GetRegisteredSchemaType (name, defaultNamespace);
  200. if (foundMap == null) nameCount = -1;
  201. else if (obmap.Equals (foundMap.ObjectMap)) return foundMap;
  202. else name = baseName + (nameCount++);
  203. }
  204. while (nameCount != -1);
  205. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  206. map.ObjectMap = obmap;
  207. // Register this map as a derived class of object
  208. RegisterSchemaType (map, name, defaultNamespace);
  209. if (typeData.Type != typeof(object))
  210. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  211. return map;
  212. }
  213. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  214. {
  215. XmlTypeMapping map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  216. return map;
  217. }
  218. public ICollection GetReflectionMembers (Type type)
  219. {
  220. ArrayList members = new ArrayList();
  221. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  222. foreach (PropertyInfo prop in properties)
  223. {
  224. if (!prop.CanRead || !prop.CanWrite) continue;
  225. XmlAttributes atts = attributeOverrides[type, prop.Name];
  226. if (atts == null) atts = new XmlAttributes (prop);
  227. if (atts.XmlIgnore) continue;
  228. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  229. members.Add (member);
  230. }
  231. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  232. foreach (FieldInfo field in fields)
  233. {
  234. XmlAttributes atts = attributeOverrides[type, field.Name];
  235. if (atts == null) atts = new XmlAttributes (field);
  236. if (atts.XmlIgnore) continue;
  237. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  238. members.Add (member);
  239. }
  240. return members;
  241. }
  242. private void AddMember (XmlTypeMapping map, XmlReflectionMember rmember, string defaultNamespace)
  243. {
  244. XmlTypeMapMember mapMember;
  245. XmlAttributes atts = rmember.XmlAttributes;
  246. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  247. if (atts.XmlAnyAttribute != null)
  248. {
  249. }
  250. else if (atts.XmlAnyElements != null)
  251. {
  252. }
  253. if (atts.XmlAttribute != null)
  254. {
  255. // An attribute
  256. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  257. throw CreateError (map, "XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  258. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  259. if (atts.XmlAttribute.AttributeName == null)
  260. mapAttribute.AttributeName = rmember.MemberName;
  261. else
  262. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  263. mapAttribute.DataType = atts.XmlAttribute.DataType;
  264. mapAttribute.Form = atts.XmlAttribute.Form;
  265. mapAttribute.Namespace = (atts.XmlAttribute != null) ? atts.XmlAttribute.Namespace : "";
  266. mapMember = mapAttribute;
  267. }
  268. else if (typeData.SchemaType == SchemaTypes.Array)
  269. {
  270. if (atts.XmlElements.Count > 0)
  271. {
  272. // A flat list
  273. // TODO: check that it does not have XmlArrayAttribute
  274. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  275. member.ListMap = new ListMap ();
  276. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  277. member.ElementInfo = member.ListMap.ItemInfo;
  278. mapMember = member;
  279. }
  280. else
  281. {
  282. // A list
  283. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  284. member.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  285. member.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  286. member.ListTypeMapping = ImportListMapping (rmember.MemberType, null, member.Namespace, atts, 0);
  287. // Creates an ElementInfo that identifies the array instance.
  288. member.ElementInfo = new XmlTypeMapElementInfoList();
  289. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  290. elem.ElementName = member.ElementName;
  291. elem.Namespace = member.Namespace;
  292. elem.MappedType = member.ListTypeMapping;
  293. member.ElementInfo.Add (elem);
  294. mapMember = member;
  295. }
  296. }
  297. else
  298. {
  299. // An element
  300. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  301. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  302. mapMember = member;
  303. }
  304. mapMember.TypeData = typeData;
  305. mapMember.Name = rmember.MemberName;
  306. ((ClassMap)map.ObjectMap).AddMember (mapMember);
  307. }
  308. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  309. {
  310. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  311. if (atts.XmlElements.Count == 0)
  312. {
  313. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  314. elem.ElementName = defaultName;
  315. elem.Namespace = defaultNamespace;
  316. if (elem.TypeData.IsComplexType)
  317. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  318. list.Add (elem);
  319. }
  320. foreach (XmlElementAttribute att in atts.XmlElements)
  321. {
  322. Type elemType = (att.Type != null) ? att.Type : defaultType;
  323. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType));
  324. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  325. elem.DataType = att.DataType;
  326. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  327. elem.Form = att.Form;
  328. elem.IsNullable = att.IsNullable;
  329. if (elem.TypeData.IsComplexType)
  330. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  331. list.Add (elem);
  332. }
  333. return list;
  334. }
  335. public void IncludeType (Type type)
  336. {
  337. if (type == null)
  338. throw new ArgumentNullException ("type");
  339. if (includedTypes == null) includedTypes = new ArrayList ();
  340. includedTypes.Add (type);
  341. }
  342. void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
  343. {
  344. string mapKey = xmlType + "/" + ns;
  345. if (!schemaTypes.ContainsKey (xmlType))
  346. schemaTypes.Add (mapKey, map);
  347. }
  348. XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
  349. {
  350. string mapKey = xmlType + "/" + ns;
  351. return schemaTypes[mapKey] as XmlTypeMapping;
  352. }
  353. void RegisterClrType (XmlTypeMapping map, Type type, string ns)
  354. {
  355. if (type == typeof(object)) ns = "";
  356. string mapKey = type.FullName + "/" + ns;
  357. if (!clrTypes.ContainsKey (mapKey))
  358. clrTypes.Add (mapKey, map);
  359. }
  360. XmlTypeMapping GetRegisteredClrType (Type type, string ns)
  361. {
  362. if (type == typeof(object)) ns = "";
  363. string mapKey = type.FullName + "/" + ns;
  364. return clrTypes[mapKey] as XmlTypeMapping;
  365. }
  366. Exception CreateError (XmlTypeMapping map, string message)
  367. {
  368. throw new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
  369. }
  370. #endregion // Methods
  371. }
  372. }