XmlReflectionImporter.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 initialDefaultNamespace;
  17. XmlAttributeOverrides attributeOverrides;
  18. ArrayList includedTypes;
  19. ReflectionHelper helper = new ReflectionHelper();
  20. int arrayChoiceCount = 1;
  21. ArrayList relatedMaps = new ArrayList ();
  22. static readonly string errSimple = "Cannot serialize object of type '{0}'. Base " +
  23. "type '{1}' has simpleContent and can be only extended by adding XmlAttribute " +
  24. "elements. Please consider changing XmlTextMember of the base class to string array";
  25. #region Constructors
  26. public XmlReflectionImporter ()
  27. : this (null, null)
  28. {
  29. }
  30. public XmlReflectionImporter (string defaultNamespace)
  31. : this (null, defaultNamespace)
  32. {
  33. }
  34. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
  35. : this (attributeOverrides, null)
  36. {
  37. }
  38. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
  39. {
  40. if (defaultNamespace == null)
  41. this.initialDefaultNamespace = String.Empty;
  42. else
  43. this.initialDefaultNamespace = defaultNamespace;
  44. if (attributeOverrides == null)
  45. this.attributeOverrides = new XmlAttributeOverrides();
  46. else
  47. this.attributeOverrides = attributeOverrides;
  48. }
  49. void Reset ()
  50. {
  51. helper = new ReflectionHelper();
  52. arrayChoiceCount = 1;
  53. }
  54. #endregion // Constructors
  55. #region Methods
  56. public XmlMembersMapping ImportMembersMapping (string elementName,
  57. string ns,
  58. XmlReflectionMember [] members,
  59. bool hasWrapperElement)
  60. {
  61. Reset ();
  62. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  63. for (int n=0; n<members.Length; n++)
  64. {
  65. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  66. mapping[n] = new XmlMemberMapping (members[n].MemberName, mapMem);
  67. }
  68. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, mapping);
  69. mps.RelatedMaps = relatedMaps;
  70. mps.Format = SerializationFormat.Literal;
  71. return mps;
  72. }
  73. public XmlTypeMapping ImportTypeMapping (Type type)
  74. {
  75. return ImportTypeMapping (type, null, null);
  76. }
  77. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  78. {
  79. return ImportTypeMapping (type, null, defaultNamespace);
  80. }
  81. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
  82. {
  83. return ImportTypeMapping (type, group, null);
  84. }
  85. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  86. {
  87. if (type == null)
  88. throw new ArgumentNullException ("type");
  89. if (type == typeof (void))
  90. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  91. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  92. if (defaultNamespace == null) defaultNamespace = string.Empty;
  93. XmlTypeMapping map;
  94. switch (TypeTranslator.GetTypeData(type).SchemaType)
  95. {
  96. case SchemaTypes.Class: map = ImportClassMapping (type, root, defaultNamespace); break;
  97. case SchemaTypes.Array: map = ImportListMapping (type, root, defaultNamespace, null, 0); break;
  98. case SchemaTypes.XmlNode: map = ImportXmlNodeMapping (type, root, defaultNamespace); break;
  99. case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, root, defaultNamespace); break;
  100. case SchemaTypes.Enum: map = ImportEnumMapping (type, root, defaultNamespace); break;
  101. case SchemaTypes.XmlSerializable: map = ImportXmlSerializableMapping (type, root, defaultNamespace); break;
  102. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  103. }
  104. map.RelatedMaps = relatedMaps;
  105. map.Format = SerializationFormat.Literal;
  106. return map;
  107. }
  108. XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
  109. {
  110. string membersNamespace;
  111. string elementName;
  112. bool includeInSchema = true;
  113. XmlAttributes atts = null;
  114. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  115. if (!typeData.IsListType)
  116. {
  117. if (attributeOverrides != null)
  118. atts = attributeOverrides[typeData.Type];
  119. if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
  120. throw new InvalidOperationException ("XmlRoot and XmlType attributes may not be specified for the type " + typeData.FullTypeName);
  121. }
  122. if (atts == null)
  123. atts = new XmlAttributes (typeData.Type);
  124. if (atts.XmlRoot != null && root == null)
  125. root = atts.XmlRoot;
  126. if (atts.XmlType != null)
  127. {
  128. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
  129. defaultNamespace = atts.XmlType.Namespace;
  130. if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
  131. defaultXmlType = atts.XmlType.TypeName;
  132. includeInSchema = atts.XmlType.IncludeInSchema;
  133. }
  134. membersNamespace = defaultNamespace;
  135. elementName = defaultXmlType;
  136. if (root != null)
  137. {
  138. if (root.ElementName != null && root.ElementName != String.Empty)
  139. elementName = root.ElementName;
  140. if (root.Namespace != null && root.Namespace != String.Empty)
  141. membersNamespace = root.Namespace;
  142. }
  143. if (membersNamespace == null) membersNamespace = "";
  144. XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType, defaultNamespace);
  145. map.IncludeInSchema = includeInSchema;
  146. relatedMaps.Add (map);
  147. return map;
  148. }
  149. XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  150. {
  151. TypeData typeData = TypeTranslator.GetTypeData (type);
  152. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  153. if (map != null) return map;
  154. if (type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract)
  155. throw new InvalidOperationException (type.Name + " cannot be serialized because it does not have a default public constructor");
  156. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  157. helper.RegisterClrType (map, type, map.Namespace);
  158. helper.RegisterSchemaType (map, map.XmlType, map.Namespace);
  159. ClassMap classMap = new ClassMap ();
  160. map.ObjectMap = classMap;
  161. // Import members
  162. // try
  163. // {
  164. ICollection members = GetReflectionMembers (type);
  165. foreach (XmlReflectionMember rmember in members)
  166. {
  167. if (rmember.XmlAttributes.XmlIgnore) continue;
  168. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  169. }
  170. // }
  171. // catch (Exception ex) {
  172. // throw helper.CreateError (map, ex.Message);
  173. // }
  174. // Import derived classes
  175. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  176. for (int n=0; n<includes.Length; n++)
  177. {
  178. Type includedType = includes[n].Type;
  179. if (!includedType.IsSubclassOf(type)) throw helper.CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  180. XmlTypeMapping derived = ImportTypeMapping (includedType, root, defaultNamespace);
  181. map.DerivedTypes.Add (derived);
  182. map.DerivedTypes.AddRange (derived.DerivedTypes);
  183. }
  184. if (type == typeof (object) && includedTypes != null)
  185. {
  186. foreach (Type intype in includedTypes)
  187. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  188. }
  189. // Register this map as a derived class of object
  190. if (typeData.Type != typeof(object))
  191. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  192. if (type.BaseType != null && type.BaseType != typeof(object)) {
  193. map.BaseMap = ImportClassMapping (type.BaseType, root, defaultNamespace);
  194. if (((ClassMap)map.BaseMap.ObjectMap).HasSimpleContent && classMap.ElementMembers.Count != 1)
  195. throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName));
  196. }
  197. return map;
  198. }
  199. string GetTypeNamespace (TypeData typeData, XmlRootAttribute root, string defaultNamespace)
  200. {
  201. string mapNamespace = defaultNamespace;
  202. XmlAttributes atts = null;
  203. if (!typeData.IsListType)
  204. {
  205. if (attributeOverrides != null)
  206. atts = attributeOverrides[typeData.Type];
  207. }
  208. if (atts == null)
  209. atts = new XmlAttributes (typeData.Type);
  210. if (atts.XmlRoot != null && root == null)
  211. root = atts.XmlRoot;
  212. if (atts.XmlType != null)
  213. {
  214. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
  215. mapNamespace = atts.XmlType.Namespace;
  216. }
  217. if (root != null)
  218. {
  219. if (root.Namespace != null && root.Namespace != String.Empty)
  220. mapNamespace = root.Namespace;
  221. }
  222. if (mapNamespace == null) return "";
  223. else return mapNamespace;
  224. }
  225. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  226. {
  227. TypeData typeData = TypeTranslator.GetTypeData (type);
  228. ListMap obmap = new ListMap ();
  229. if (atts == null) atts = new XmlAttributes();
  230. Type itemType = typeData.ListItemType;
  231. // warning: byte[][] should not be considered multiarray
  232. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  233. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  234. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  235. {
  236. if (att.NestingLevel != nestingLevel) continue;
  237. Type elemType = (att.Type != null) ? att.Type : itemType;
  238. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
  239. elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
  240. if (elem.Namespace == null) elem.Namespace = "";
  241. elem.Form = att.Form;
  242. elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
  243. elem.NestingLevel = att.NestingLevel;
  244. if (isMultiArray)
  245. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  246. else if (elem.TypeData.IsComplexType)
  247. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  248. if (att.ElementName != null) elem.ElementName = att.ElementName;
  249. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  250. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  251. list.Add (elem);
  252. }
  253. if (list.Count == 0)
  254. {
  255. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  256. if (isMultiArray)
  257. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  258. else if (elem.TypeData.IsComplexType)
  259. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  260. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  261. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  262. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  263. elem.IsNullable = CanBeNull (elem.TypeData);
  264. list.Add (elem);
  265. }
  266. obmap.ItemInfo = list;
  267. // If there can be different element names (types) in the array, then its name cannot
  268. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  269. string baseName;
  270. if (list.Count > 1)
  271. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  272. else
  273. {
  274. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  275. if (elem.MappedType != null) baseName = TypeTranslator.GetArrayName (elem.MappedType.ElementName);
  276. else baseName = TypeTranslator.GetArrayName (elem.ElementName);
  277. }
  278. // Avoid name colisions
  279. int nameCount = 1;
  280. string name = baseName;
  281. do {
  282. XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
  283. if (foundMap == null) nameCount = -1;
  284. else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
  285. else name = baseName + (nameCount++);
  286. }
  287. while (nameCount != -1);
  288. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  289. map.ObjectMap = obmap;
  290. // Register this map as a derived class of object
  291. helper.RegisterSchemaType (map, name, defaultNamespace);
  292. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  293. return map;
  294. }
  295. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  296. {
  297. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  298. if (map != null) return map;
  299. // Registers the maps for XmlNode and XmlElement
  300. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  301. helper.RegisterClrType (nodeMap, typeof(XmlNode), nodeMap.Namespace);
  302. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  303. helper.RegisterClrType (elemMap, typeof(XmlElement), elemMap.Namespace);
  304. XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
  305. helper.RegisterClrType (elemMap, typeof(XmlText), textMap.Namespace);
  306. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  307. obmap.DerivedTypes.Add (nodeMap);
  308. obmap.DerivedTypes.Add (elemMap);
  309. obmap.DerivedTypes.Add (textMap);
  310. nodeMap.DerivedTypes.Add (elemMap);
  311. nodeMap.DerivedTypes.Add (textMap);
  312. return helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  313. }
  314. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  315. {
  316. TypeData typeData = TypeTranslator.GetTypeData (type);
  317. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  318. if (map != null) return map;
  319. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  320. helper.RegisterClrType (map, type, map.Namespace);
  321. return map;
  322. }
  323. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  324. {
  325. TypeData typeData = TypeTranslator.GetTypeData (type);
  326. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  327. if (map != null) return map;
  328. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  329. helper.RegisterClrType (map, type, map.Namespace);
  330. string [] names = Enum.GetNames (type);
  331. ArrayList members = new ArrayList();
  332. foreach (string name in names)
  333. {
  334. MemberInfo[] mem = type.GetMember (name);
  335. string xmlName = name;
  336. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  337. if (atts.Length > 0) continue;
  338. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  339. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  340. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  341. }
  342. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  343. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
  344. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  345. return map;
  346. }
  347. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  348. {
  349. TypeData typeData = TypeTranslator.GetTypeData (type);
  350. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  351. if (map != null) return map;
  352. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  353. helper.RegisterClrType (map, type, map.Namespace);
  354. return map;
  355. }
  356. public ICollection GetReflectionMembers (Type type)
  357. {
  358. ArrayList members = new ArrayList();
  359. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  360. foreach (PropertyInfo prop in properties)
  361. {
  362. if (!prop.CanRead) continue;
  363. XmlAttributes atts = attributeOverrides[type, prop.Name];
  364. if (atts == null) atts = new XmlAttributes (prop);
  365. if (atts.XmlIgnore) continue;
  366. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  367. members.Add (member);
  368. }
  369. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  370. foreach (FieldInfo field in fields)
  371. {
  372. XmlAttributes atts = attributeOverrides[type, field.Name];
  373. if (atts == null) atts = new XmlAttributes (field);
  374. if (atts.XmlIgnore) continue;
  375. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  376. members.Add (member);
  377. }
  378. return members;
  379. }
  380. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  381. {
  382. XmlTypeMapMember mapMember;
  383. XmlAttributes atts = rmember.XmlAttributes;
  384. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  385. if (atts.XmlAnyAttribute != null)
  386. {
  387. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  388. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  389. {
  390. mapMember = new XmlTypeMapMemberAnyAttribute();
  391. }
  392. else
  393. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  394. }
  395. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  396. {
  397. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  398. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  399. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  400. {
  401. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  402. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember.MemberType, member, atts);
  403. mapMember = member;
  404. }
  405. else
  406. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  407. }
  408. else if (atts.Xmlns)
  409. {
  410. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  411. mapMember = mapNamespaces;
  412. }
  413. else if (atts.XmlAttribute != null)
  414. {
  415. // An attribute
  416. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  417. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  418. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  419. if (atts.XmlAttribute.AttributeName == null)
  420. mapAttribute.AttributeName = rmember.MemberName;
  421. else
  422. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  423. mapAttribute.Form = atts.XmlAttribute.Form;
  424. mapAttribute.Namespace = (atts.XmlAttribute.Namespace != null) ? atts.XmlAttribute.Namespace : "";
  425. if (typeData.IsComplexType)
  426. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  427. typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
  428. mapMember = mapAttribute;
  429. }
  430. else if (typeData.SchemaType == SchemaTypes.Array)
  431. {
  432. // If the member has a single XmlElementAttribute and the type is the type of the member,
  433. // then it is not a flat list
  434. if (atts.XmlElements.Count > 1 ||
  435. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  436. (atts.XmlText != null))
  437. {
  438. // A flat list
  439. // TODO: check that it does not have XmlArrayAttribute
  440. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  441. member.ListMap = new ListMap ();
  442. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  443. member.ElementInfo = member.ListMap.ItemInfo;
  444. mapMember = member;
  445. }
  446. else
  447. {
  448. // A list
  449. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  450. // Creates an ElementInfo that identifies the array instance.
  451. member.ElementInfo = new XmlTypeMapElementInfoList();
  452. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  453. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  454. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  455. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  456. member.ElementInfo.Add (elem);
  457. mapMember = member;
  458. }
  459. }
  460. else
  461. {
  462. // An element
  463. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  464. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  465. mapMember = member;
  466. }
  467. mapMember.DefaultValue = atts.XmlDefaultValue;
  468. mapMember.TypeData = typeData;
  469. mapMember.Name = rmember.MemberName;
  470. return mapMember;
  471. }
  472. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  473. {
  474. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  475. ImportTextElementInfo (list, defaultType, member, atts);
  476. if (atts.XmlElements.Count == 0 && list.Count == 0)
  477. {
  478. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  479. elem.ElementName = defaultName;
  480. elem.Namespace = defaultNamespace;
  481. if (elem.TypeData.IsComplexType)
  482. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  483. list.Add (elem);
  484. }
  485. bool multiType = (atts.XmlElements.Count > 1);
  486. foreach (XmlElementAttribute att in atts.XmlElements)
  487. {
  488. Type elemType = (att.Type != null) ? att.Type : defaultType;
  489. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  490. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  491. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  492. elem.Form = att.Form;
  493. elem.IsNullable = att.IsNullable;
  494. if (elem.TypeData.IsComplexType)
  495. {
  496. if (att.DataType != null) throw new InvalidOperationException ("'" + att.DataType + "' is an invalid value for the XmlElementAttribute.DateTime property. The property may only be specified for primitive types.");
  497. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  498. }
  499. if (att.ElementName != null)
  500. elem.ElementName = att.ElementName;
  501. else if (multiType) {
  502. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  503. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  504. }
  505. else
  506. elem.ElementName = defaultName;
  507. list.Add (elem);
  508. }
  509. return list;
  510. }
  511. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  512. {
  513. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  514. ImportTextElementInfo (list, defaultType, member, atts);
  515. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  516. {
  517. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  518. if (att.Name != null && att.Name != string.Empty) elem.ElementName = att.Name;
  519. else elem.IsUnnamedAnyElement = true;
  520. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  521. list.Add (elem);
  522. }
  523. return list;
  524. }
  525. void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  526. {
  527. if (atts.XmlText != null)
  528. {
  529. member.IsXmlTextCollector = true;
  530. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  531. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  532. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  533. if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
  534. elem.TypeData.SchemaType != SchemaTypes.Enum &&
  535. elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
  536. !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
  537. )
  538. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  539. elem.IsTextElement = true;
  540. elem.WrappedElement = false;
  541. list.Add (elem);
  542. }
  543. }
  544. bool CanBeNull (TypeData type)
  545. {
  546. return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
  547. }
  548. public void IncludeType (Type type)
  549. {
  550. if (type == null)
  551. throw new ArgumentNullException ("type");
  552. if (includedTypes == null) includedTypes = new ArrayList ();
  553. includedTypes.Add (type);
  554. }
  555. #endregion // Methods
  556. }
  557. }