XmlReflectionImporter.cs 24 KB

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