XmlReflectionImporter.cs 26 KB

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