XmlReflectionImporter.cs 28 KB

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