XmlReflectionImporter.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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, ns, mapMem, false);
  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. ReflectionHelper.CheckSerializableType (type);
  155. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  156. helper.RegisterClrType (map, type, map.Namespace);
  157. helper.RegisterSchemaType (map, map.XmlType, map.Namespace);
  158. ClassMap classMap = new ClassMap ();
  159. map.ObjectMap = classMap;
  160. // Import members
  161. // try
  162. // {
  163. ICollection members = GetReflectionMembers (type);
  164. foreach (XmlReflectionMember rmember in members)
  165. {
  166. if (rmember.XmlAttributes.XmlIgnore) continue;
  167. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  168. }
  169. // }
  170. // catch (Exception ex) {
  171. // throw helper.CreateError (map, ex.Message);
  172. // }
  173. // Import derived classes
  174. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  175. for (int n=0; n<includes.Length; n++)
  176. {
  177. Type includedType = includes[n].Type;
  178. if (!includedType.IsSubclassOf(type)) throw helper.CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  179. XmlTypeMapping derived = ImportTypeMapping (includedType, root, defaultNamespace);
  180. map.DerivedTypes.Add (derived);
  181. map.DerivedTypes.AddRange (derived.DerivedTypes);
  182. }
  183. if (type == typeof (object) && includedTypes != null)
  184. {
  185. foreach (Type intype in includedTypes)
  186. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  187. }
  188. // Register this map as a derived class of object
  189. if (typeData.Type != typeof(object))
  190. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  191. if (type.BaseType != null && type.BaseType != typeof(object)) {
  192. map.BaseMap = ImportClassMapping (type.BaseType, root, defaultNamespace);
  193. if (((ClassMap)map.BaseMap.ObjectMap).HasSimpleContent && classMap.ElementMembers.Count != 1)
  194. throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName));
  195. }
  196. return map;
  197. }
  198. string GetTypeNamespace (TypeData typeData, XmlRootAttribute root, string defaultNamespace)
  199. {
  200. string mapNamespace = defaultNamespace;
  201. XmlAttributes atts = null;
  202. if (!typeData.IsListType)
  203. {
  204. if (attributeOverrides != null)
  205. atts = attributeOverrides[typeData.Type];
  206. }
  207. if (atts == null)
  208. atts = new XmlAttributes (typeData.Type);
  209. if (atts.XmlRoot != null && root == null)
  210. root = atts.XmlRoot;
  211. if (atts.XmlType != null)
  212. {
  213. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
  214. mapNamespace = atts.XmlType.Namespace;
  215. }
  216. if (root != null)
  217. {
  218. if (root.Namespace != null && root.Namespace != String.Empty)
  219. mapNamespace = root.Namespace;
  220. }
  221. if (mapNamespace == null) return "";
  222. else return mapNamespace;
  223. }
  224. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  225. {
  226. TypeData typeData = TypeTranslator.GetTypeData (type);
  227. ListMap obmap = new ListMap ();
  228. if (atts == null) atts = new XmlAttributes();
  229. Type itemType = typeData.ListItemType;
  230. // warning: byte[][] should not be considered multiarray
  231. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  232. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  233. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  234. {
  235. if (att.NestingLevel != nestingLevel) continue;
  236. Type elemType = (att.Type != null) ? att.Type : itemType;
  237. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
  238. elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
  239. if (elem.Namespace == null) elem.Namespace = "";
  240. elem.Form = att.Form;
  241. elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
  242. elem.NestingLevel = att.NestingLevel;
  243. if (isMultiArray)
  244. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  245. else if (elem.TypeData.IsComplexType)
  246. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  247. if (att.ElementName != null) elem.ElementName = att.ElementName;
  248. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  249. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  250. list.Add (elem);
  251. }
  252. if (list.Count == 0)
  253. {
  254. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  255. if (isMultiArray)
  256. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  257. else if (elem.TypeData.IsComplexType)
  258. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  259. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  260. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  261. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  262. elem.IsNullable = CanBeNull (elem.TypeData);
  263. list.Add (elem);
  264. }
  265. obmap.ItemInfo = list;
  266. // If there can be different element names (types) in the array, then its name cannot
  267. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  268. string baseName;
  269. if (list.Count > 1)
  270. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  271. else
  272. {
  273. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  274. if (elem.MappedType != null) baseName = TypeTranslator.GetArrayName (elem.MappedType.ElementName);
  275. else baseName = TypeTranslator.GetArrayName (elem.ElementName);
  276. }
  277. // Avoid name colisions
  278. int nameCount = 1;
  279. string name = baseName;
  280. do {
  281. XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
  282. if (foundMap == null) nameCount = -1;
  283. else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
  284. else name = baseName + (nameCount++);
  285. }
  286. while (nameCount != -1);
  287. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  288. map.ObjectMap = obmap;
  289. // Register this map as a derived class of object
  290. helper.RegisterSchemaType (map, name, defaultNamespace);
  291. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  292. return map;
  293. }
  294. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  295. {
  296. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  297. if (map != null) return map;
  298. // Registers the maps for XmlNode and XmlElement
  299. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  300. helper.RegisterClrType (nodeMap, typeof(XmlNode), nodeMap.Namespace);
  301. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  302. helper.RegisterClrType (elemMap, typeof(XmlElement), elemMap.Namespace);
  303. XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
  304. helper.RegisterClrType (elemMap, typeof(XmlText), textMap.Namespace);
  305. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  306. obmap.DerivedTypes.Add (nodeMap);
  307. obmap.DerivedTypes.Add (elemMap);
  308. obmap.DerivedTypes.Add (textMap);
  309. nodeMap.DerivedTypes.Add (elemMap);
  310. nodeMap.DerivedTypes.Add (textMap);
  311. return helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
  312. }
  313. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  314. {
  315. TypeData typeData = TypeTranslator.GetTypeData (type);
  316. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  317. if (map != null) return map;
  318. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  319. helper.RegisterClrType (map, type, map.Namespace);
  320. return map;
  321. }
  322. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  323. {
  324. TypeData typeData = TypeTranslator.GetTypeData (type);
  325. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  326. if (map != null) return map;
  327. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  328. helper.RegisterClrType (map, type, map.Namespace);
  329. string [] names = Enum.GetNames (type);
  330. ArrayList members = new ArrayList();
  331. foreach (string name in names)
  332. {
  333. MemberInfo[] mem = type.GetMember (name);
  334. string xmlName = name;
  335. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  336. if (atts.Length > 0) continue;
  337. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  338. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  339. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  340. }
  341. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  342. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
  343. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  344. return map;
  345. }
  346. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  347. {
  348. TypeData typeData = TypeTranslator.GetTypeData (type);
  349. XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
  350. if (map != null) return map;
  351. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  352. helper.RegisterClrType (map, type, map.Namespace);
  353. return map;
  354. }
  355. public ICollection GetReflectionMembers (Type type)
  356. {
  357. ArrayList members = new ArrayList();
  358. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  359. foreach (PropertyInfo prop in properties)
  360. {
  361. if (!prop.CanRead) continue;
  362. if (!prop.CanWrite && TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array)
  363. continue;
  364. XmlAttributes atts = attributeOverrides[type, prop.Name];
  365. if (atts == null) atts = new XmlAttributes (prop);
  366. if (atts.XmlIgnore) continue;
  367. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  368. members.Add (member);
  369. }
  370. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  371. foreach (FieldInfo field in fields)
  372. {
  373. XmlAttributes atts = attributeOverrides[type, field.Name];
  374. if (atts == null) atts = new XmlAttributes (field);
  375. if (atts.XmlIgnore) continue;
  376. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  377. members.Add (member);
  378. }
  379. return members;
  380. }
  381. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  382. {
  383. XmlTypeMapMember mapMember;
  384. XmlAttributes atts = rmember.XmlAttributes;
  385. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  386. if (atts.XmlAnyAttribute != null)
  387. {
  388. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  389. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  390. {
  391. mapMember = new XmlTypeMapMemberAnyAttribute();
  392. }
  393. else
  394. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  395. }
  396. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  397. {
  398. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  399. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  400. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  401. {
  402. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  403. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember.MemberType, member, atts);
  404. mapMember = member;
  405. }
  406. else
  407. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  408. }
  409. else if (atts.Xmlns)
  410. {
  411. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  412. mapMember = mapNamespaces;
  413. }
  414. else if (atts.XmlAttribute != null)
  415. {
  416. // An attribute
  417. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  418. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  419. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  420. if (atts.XmlAttribute.AttributeName == null)
  421. mapAttribute.AttributeName = rmember.MemberName;
  422. else
  423. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  424. mapAttribute.Form = atts.XmlAttribute.Form;
  425. mapAttribute.Namespace = (atts.XmlAttribute.Namespace != null) ? atts.XmlAttribute.Namespace : "";
  426. if (typeData.IsComplexType)
  427. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  428. typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
  429. mapMember = mapAttribute;
  430. }
  431. else if (typeData.SchemaType == SchemaTypes.Array)
  432. {
  433. // If the member has a single XmlElementAttribute and the type is the type of the member,
  434. // then it is not a flat list
  435. if (atts.XmlElements.Count > 1 ||
  436. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  437. (atts.XmlText != null))
  438. {
  439. // A flat list
  440. // TODO: check that it does not have XmlArrayAttribute
  441. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  442. member.ListMap = new ListMap ();
  443. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  444. member.ElementInfo = member.ListMap.ItemInfo;
  445. mapMember = member;
  446. }
  447. else
  448. {
  449. // A list
  450. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  451. // Creates an ElementInfo that identifies the array instance.
  452. member.ElementInfo = new XmlTypeMapElementInfoList();
  453. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  454. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  455. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  456. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  457. member.ElementInfo.Add (elem);
  458. mapMember = member;
  459. }
  460. }
  461. else
  462. {
  463. // An element
  464. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  465. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  466. mapMember = member;
  467. }
  468. mapMember.DefaultValue = atts.XmlDefaultValue;
  469. mapMember.TypeData = typeData;
  470. mapMember.Name = rmember.MemberName;
  471. return mapMember;
  472. }
  473. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  474. {
  475. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  476. ImportTextElementInfo (list, defaultType, member, atts);
  477. if (atts.XmlElements.Count == 0 && list.Count == 0)
  478. {
  479. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  480. elem.ElementName = defaultName;
  481. elem.Namespace = defaultNamespace;
  482. if (elem.TypeData.IsComplexType)
  483. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  484. list.Add (elem);
  485. }
  486. bool multiType = (atts.XmlElements.Count > 1);
  487. foreach (XmlElementAttribute att in atts.XmlElements)
  488. {
  489. Type elemType = (att.Type != null) ? att.Type : defaultType;
  490. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  491. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  492. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  493. elem.Form = att.Form;
  494. elem.IsNullable = att.IsNullable;
  495. if (elem.TypeData.IsComplexType)
  496. {
  497. 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.");
  498. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  499. }
  500. if (att.ElementName != null)
  501. elem.ElementName = att.ElementName;
  502. else if (multiType) {
  503. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  504. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  505. }
  506. else
  507. elem.ElementName = defaultName;
  508. list.Add (elem);
  509. }
  510. return list;
  511. }
  512. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  513. {
  514. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  515. ImportTextElementInfo (list, defaultType, member, atts);
  516. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  517. {
  518. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  519. if (att.Name != null && att.Name != string.Empty) elem.ElementName = att.Name;
  520. else elem.IsUnnamedAnyElement = true;
  521. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  522. list.Add (elem);
  523. }
  524. return list;
  525. }
  526. void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  527. {
  528. if (atts.XmlText != null)
  529. {
  530. member.IsXmlTextCollector = true;
  531. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  532. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  533. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  534. if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
  535. elem.TypeData.SchemaType != SchemaTypes.Enum &&
  536. elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
  537. !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
  538. )
  539. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  540. elem.IsTextElement = true;
  541. elem.WrappedElement = false;
  542. list.Add (elem);
  543. }
  544. }
  545. bool CanBeNull (TypeData type)
  546. {
  547. return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
  548. }
  549. public void IncludeType (Type type)
  550. {
  551. if (type == null)
  552. throw new ArgumentNullException ("type");
  553. if (includedTypes == null) includedTypes = new ArrayList ();
  554. includedTypes.Add (type);
  555. }
  556. #endregion // Methods
  557. }
  558. }