XmlReflectionImporter.cs 26 KB

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