XmlReflectionImporter.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. 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. XmlAttributes atts = attributeOverrides[type, prop.Name];
  363. if (atts == null) atts = new XmlAttributes (prop);
  364. if (atts.XmlIgnore) continue;
  365. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  366. members.Add (member);
  367. }
  368. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  369. foreach (FieldInfo field in fields)
  370. {
  371. XmlAttributes atts = attributeOverrides[type, field.Name];
  372. if (atts == null) atts = new XmlAttributes (field);
  373. if (atts.XmlIgnore) continue;
  374. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  375. members.Add (member);
  376. }
  377. return members;
  378. }
  379. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  380. {
  381. XmlTypeMapMember mapMember;
  382. XmlAttributes atts = rmember.XmlAttributes;
  383. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  384. if (atts.XmlAnyAttribute != null)
  385. {
  386. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  387. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  388. {
  389. mapMember = new XmlTypeMapMemberAnyAttribute();
  390. }
  391. else
  392. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  393. }
  394. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  395. {
  396. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  397. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  398. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  399. {
  400. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  401. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember.MemberType, member, atts);
  402. mapMember = member;
  403. }
  404. else
  405. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  406. }
  407. else if (atts.Xmlns)
  408. {
  409. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  410. mapMember = mapNamespaces;
  411. }
  412. else if (atts.XmlAttribute != null)
  413. {
  414. // An attribute
  415. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  416. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  417. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  418. if (atts.XmlAttribute.AttributeName == null)
  419. mapAttribute.AttributeName = rmember.MemberName;
  420. else
  421. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  422. mapAttribute.Form = atts.XmlAttribute.Form;
  423. mapAttribute.Namespace = (atts.XmlAttribute.Namespace != null) ? atts.XmlAttribute.Namespace : "";
  424. if (typeData.IsComplexType)
  425. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  426. typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
  427. mapMember = mapAttribute;
  428. }
  429. else if (typeData.SchemaType == SchemaTypes.Array)
  430. {
  431. // If the member has a single XmlElementAttribute and the type is the type of the member,
  432. // then it is not a flat list
  433. if (atts.XmlElements.Count > 1 ||
  434. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  435. (atts.XmlText != null))
  436. {
  437. // A flat list
  438. // TODO: check that it does not have XmlArrayAttribute
  439. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  440. member.ListMap = new ListMap ();
  441. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  442. member.ElementInfo = member.ListMap.ItemInfo;
  443. mapMember = member;
  444. }
  445. else
  446. {
  447. // A list
  448. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  449. // Creates an ElementInfo that identifies the array instance.
  450. member.ElementInfo = new XmlTypeMapElementInfoList();
  451. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  452. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  453. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  454. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  455. member.ElementInfo.Add (elem);
  456. mapMember = member;
  457. }
  458. }
  459. else
  460. {
  461. // An element
  462. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  463. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  464. mapMember = member;
  465. }
  466. mapMember.DefaultValue = atts.XmlDefaultValue;
  467. mapMember.TypeData = typeData;
  468. mapMember.Name = rmember.MemberName;
  469. return mapMember;
  470. }
  471. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  472. {
  473. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  474. ImportTextElementInfo (list, defaultType, member, atts);
  475. if (atts.XmlElements.Count == 0 && list.Count == 0)
  476. {
  477. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  478. elem.ElementName = defaultName;
  479. elem.Namespace = defaultNamespace;
  480. if (elem.TypeData.IsComplexType)
  481. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  482. list.Add (elem);
  483. }
  484. bool multiType = (atts.XmlElements.Count > 1);
  485. foreach (XmlElementAttribute att in atts.XmlElements)
  486. {
  487. Type elemType = (att.Type != null) ? att.Type : defaultType;
  488. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  489. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  490. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  491. elem.Form = att.Form;
  492. elem.IsNullable = att.IsNullable;
  493. if (elem.TypeData.IsComplexType)
  494. {
  495. 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.");
  496. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  497. }
  498. if (att.ElementName != null)
  499. elem.ElementName = att.ElementName;
  500. else if (multiType) {
  501. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  502. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  503. }
  504. else
  505. elem.ElementName = defaultName;
  506. list.Add (elem);
  507. }
  508. return list;
  509. }
  510. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  511. {
  512. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  513. ImportTextElementInfo (list, defaultType, member, atts);
  514. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  515. {
  516. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  517. if (att.Name != null && att.Name != string.Empty) elem.ElementName = att.Name;
  518. else elem.IsUnnamedAnyElement = true;
  519. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  520. list.Add (elem);
  521. }
  522. return list;
  523. }
  524. void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  525. {
  526. if (atts.XmlText != null)
  527. {
  528. member.IsXmlTextCollector = true;
  529. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  530. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  531. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  532. if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
  533. elem.TypeData.SchemaType != SchemaTypes.Enum &&
  534. elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
  535. !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
  536. )
  537. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  538. elem.IsTextElement = true;
  539. elem.WrappedElement = false;
  540. list.Add (elem);
  541. }
  542. }
  543. bool CanBeNull (TypeData type)
  544. {
  545. return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
  546. }
  547. public void IncludeType (Type type)
  548. {
  549. if (type == null)
  550. throw new ArgumentNullException ("type");
  551. if (includedTypes == null) includedTypes = new ArrayList ();
  552. includedTypes.Add (type);
  553. }
  554. #endregion // Methods
  555. }
  556. }