svn path=/branches/mono-1-1-9/mcs/; revision=51207
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlReflectionImporter.cs
1 // 
2 // System.Xml.Serialization.XmlReflectionImporter 
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //   Erik LeBel (eriklebel@yahoo.ca)
7 //   Lluis Sanchez Gual (lluis@ximian.com)
8 //
9 // Copyright (C) Tim Coleman, 2002
10 // (C) 2003 Erik LeBel
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Collections;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Xml.Schema;
38
39 namespace System.Xml.Serialization {
40         public class XmlReflectionImporter {
41
42                 string initialDefaultNamespace;
43                 XmlAttributeOverrides attributeOverrides;
44                 ArrayList includedTypes;
45                 ReflectionHelper helper = new ReflectionHelper();
46                 int arrayChoiceCount = 1;
47                 ArrayList relatedMaps = new ArrayList ();
48                 bool allowPrivateTypes = false;
49
50                 static readonly string errSimple = "Cannot serialize object of type '{0}'. Base " +
51                         "type '{1}' has simpleContent and can be only extended by adding XmlAttribute " +
52                         "elements. Please consider changing XmlText member of the base class to string array";
53
54                 static readonly string errSimple2 = "Cannot serialize object of type '{0}'. " +
55                         "Consider changing type of XmlText member '{1}' from '{2}' to string or string array";
56
57                 #region Constructors
58
59                 public XmlReflectionImporter ()
60                         : this (null, null)
61                 {
62                 }
63
64                 public XmlReflectionImporter (string defaultNamespace)
65                         : this (null, defaultNamespace)
66                 {
67                 }
68
69                 public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
70                         : this (attributeOverrides, null)
71                 {
72                 }
73
74                 public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
75                 {
76                         if (defaultNamespace == null)
77                                 this.initialDefaultNamespace = String.Empty;
78                         else
79                                 this.initialDefaultNamespace = defaultNamespace;
80
81                         if (attributeOverrides == null)
82                                 this.attributeOverrides = new XmlAttributeOverrides();
83                         else
84                                 this.attributeOverrides = attributeOverrides;
85                 }
86
87 /*              void Reset ()
88                 {
89                         helper = new ReflectionHelper();
90                         arrayChoiceCount = 1;
91                 }
92 */
93                 
94                 internal bool AllowPrivateTypes
95                 {
96                         get { return allowPrivateTypes; }
97                         set { allowPrivateTypes = value; }
98                 }
99
100                 #endregion // Constructors
101
102                 #region Methods
103
104                 public XmlMembersMapping ImportMembersMapping (string elementName,
105                         string ns,
106                         XmlReflectionMember [] members,
107                         bool hasWrapperElement)
108                 {
109 //                      Reset ();       Disabled. See ChangeLog
110
111                         XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
112                         for (int n=0; n<members.Length; n++)
113                         {
114                                 XmlTypeMapMember mapMem = CreateMapMember (null, members[n], ns);
115                                 mapping[n] = new XmlMemberMapping (members[n].MemberName, ns, mapMem, false);
116                         }
117                         elementName = XmlConvert.EncodeLocalName (elementName);
118                         XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, false, mapping);
119                         mps.RelatedMaps = relatedMaps;
120                         mps.Format = SerializationFormat.Literal;
121                         Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null;
122                         mps.Source = new MembersSerializationSource (elementName, hasWrapperElement, members, false, true, ns, extraTypes);
123                         if (allowPrivateTypes) mps.Source.CanBeGenerated = false;
124                         return mps;
125                 }
126
127 #if NET_2_0
128                 [MonoTODO]
129                 public XmlMembersMapping ImportMembersMapping (string elementName, 
130                         string ns, 
131                         XmlReflectionMember[] members, 
132                         bool hasWrapperElement, 
133                         bool rpc)
134                 {
135                         throw new NotImplementedException ();
136                 }
137
138                 [MonoTODO]
139                 public XmlMembersMapping ImportMembersMapping (string elementName, 
140                         string ns, 
141                         XmlReflectionMember[] members, 
142                         bool hasWrapperElement, 
143                         bool rpc, 
144                         bool openModel)
145                 {
146                         throw new NotImplementedException ();
147                 }
148 #endif
149
150                 public XmlTypeMapping ImportTypeMapping (Type type)
151                 {
152                         return ImportTypeMapping (type, null, null);
153                 }
154
155                 public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
156                 {
157                         return ImportTypeMapping (type, null, defaultNamespace);
158                 }
159
160                 public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
161                 {
162                         return ImportTypeMapping (type, group, null);
163                 }
164
165                 public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
166                 {
167                         if (type == null)
168                                 throw new ArgumentNullException ("type");
169
170                         if (type == typeof (void))
171                                 throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
172
173                         if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
174                         if (defaultNamespace == null) defaultNamespace = string.Empty;
175
176                         XmlTypeMapping map;
177
178                         switch (TypeTranslator.GetTypeData(type).SchemaType)
179                         {
180                                 case SchemaTypes.Class: map = ImportClassMapping (type, root, defaultNamespace); break;
181                                 case SchemaTypes.Array: map = ImportListMapping (type, root, defaultNamespace, null, 0); break;
182                                 case SchemaTypes.XmlNode: map = ImportXmlNodeMapping (type, root, defaultNamespace); break;
183                                 case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, root, defaultNamespace); break;
184                                 case SchemaTypes.Enum: map = ImportEnumMapping (type, root, defaultNamespace); break;
185                                 case SchemaTypes.XmlSerializable: map = ImportXmlSerializableMapping (type, root, defaultNamespace); break;
186                                 default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
187                         }
188
189                         map.RelatedMaps = relatedMaps;
190                         map.Format = SerializationFormat.Literal;
191                         Type[] extraTypes = includedTypes != null ? (Type[])includedTypes.ToArray(typeof(Type)) : null;
192                         map.Source = new XmlTypeSerializationSource (type, root, attributeOverrides, defaultNamespace, extraTypes);
193                         if (allowPrivateTypes) map.Source.CanBeGenerated = false;
194                         return map;
195                 }
196
197                 XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
198                 {
199                         string rootNamespace = defaultNamespace;
200                         string typeNamespace = null;
201                         string elementName;
202                         bool includeInSchema = true;
203                         XmlAttributes atts = null;
204                         bool nullable = true;
205
206                         if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
207
208                         if (!typeData.IsListType)
209                         {
210                                 if (attributeOverrides != null) 
211                                         atts = attributeOverrides[typeData.Type];
212
213                                 if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
214                                         throw new InvalidOperationException ("XmlRoot and XmlType attributes may not be specified for the type " + typeData.FullTypeName);
215                         }
216
217                         if (atts == null) 
218                                 atts = new XmlAttributes (typeData.Type);
219
220                         if (atts.XmlRoot != null && root == null)
221                                 root = atts.XmlRoot;
222
223                         if (atts.XmlType != null)
224                         {
225                                 if (atts.XmlType.Namespace != null && typeData.SchemaType != SchemaTypes.Enum)
226                                         typeNamespace = atts.XmlType.Namespace;
227
228                                 if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
229                                         defaultXmlType = XmlConvert.EncodeLocalName (atts.XmlType.TypeName);
230                                         
231                                 includeInSchema = atts.XmlType.IncludeInSchema;
232                         }
233
234                         elementName = defaultXmlType;
235
236                         if (root != null)
237                         {
238                                 if (root.ElementName.Length != 0)
239                                         elementName = XmlConvert.EncodeLocalName(root.ElementName);
240                                 if (root.Namespace != null)
241                                         rootNamespace = root.Namespace;
242                                 nullable = root.IsNullable;
243                         }
244
245                         if (rootNamespace == null) rootNamespace = "";
246                         if (typeNamespace == null) typeNamespace = rootNamespace;
247                         
248                         XmlTypeMapping map;
249                         if (typeData.SchemaType == SchemaTypes.XmlSerializable)
250                                 map = new XmlSerializableMapping (elementName, rootNamespace, typeData, defaultXmlType, typeNamespace);
251                         else
252                                 map = new XmlTypeMapping (elementName, rootNamespace, typeData, defaultXmlType, typeNamespace);
253                                 
254                         map.IncludeInSchema = includeInSchema;
255                         map.IsNullable = nullable;
256                         relatedMaps.Add (map);
257                         
258                         return map;
259                 }
260
261                 XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
262                 {
263                         TypeData typeData = TypeTranslator.GetTypeData (type);
264                         XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
265                         if (map != null) return map;
266
267                         if (!allowPrivateTypes)
268                                 ReflectionHelper.CheckSerializableType (type, false);
269                         
270                         map = CreateTypeMapping (typeData, root, null, defaultNamespace);
271                         helper.RegisterClrType (map, type, map.XmlTypeNamespace);
272                         helper.RegisterSchemaType (map, map.XmlType, map.XmlTypeNamespace);
273
274                         // Import members
275
276                         ClassMap classMap = new ClassMap ();
277                         map.ObjectMap = classMap;
278
279 //                      try
280 //                      {
281                                 ICollection members = GetReflectionMembers (type);
282                                 foreach (XmlReflectionMember rmember in members)
283                                 {
284                                         string ns = map.XmlTypeNamespace;
285                                         if (rmember.XmlAttributes.XmlIgnore) continue;
286                                         if (rmember.DeclaringType != null && rmember.DeclaringType != type) {
287                                                 XmlTypeMapping bmap = ImportClassMapping (rmember.DeclaringType, root, defaultNamespace);
288                                                 ns = bmap.XmlTypeNamespace;
289                                         }
290                                         
291                                         XmlTypeMapMember mem = CreateMapMember (type, rmember, ns);
292                                         mem.CheckOptionalValueType (type);
293                                         classMap.AddMember (mem);
294                                 }
295 //                      }
296 //                      catch (Exception ex) {
297 //                              throw helper.CreateError (map, ex.Message);
298 //                      }
299
300                         // Import extra classes
301
302                         if (type == typeof (object) && includedTypes != null)
303                         {
304                                 foreach (Type intype in includedTypes)
305                                         map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
306                         }
307
308                         // Register inheritance relations
309
310                         if (type.BaseType != null)
311                         {
312                                 XmlTypeMapping bmap = ImportClassMapping (type.BaseType, root, defaultNamespace);
313                                 ClassMap cbmap = bmap.ObjectMap as ClassMap;
314                                 
315                                 if (type.BaseType != typeof (object)) {
316                                         map.BaseMap = bmap;
317                                         if (!cbmap.HasSimpleContent)
318                                                 classMap.SetCanBeSimpleType (false);
319                                 }
320                                 
321                                 // At this point, derived classes of this map must be already registered
322                                 
323                                 RegisterDerivedMap (bmap, map);
324                                 
325                                 if (cbmap.HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1)
326                                         throw new InvalidOperationException (String.Format (errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName));
327                         }
328                         
329                         ImportIncludedTypes (type, defaultNamespace);
330                         
331                         if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent)
332                         {
333                                 XmlTypeMapMember mem = classMap.XmlTextCollector;
334                                 if (mem.TypeData.Type != typeof(string) && 
335                                    mem.TypeData.Type != typeof(string[]) && 
336                                    mem.TypeData.Type != typeof(object[]) && 
337                                    mem.TypeData.Type != typeof(XmlNode[]))
338                                    
339                                         throw new InvalidOperationException (String.Format (errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName));
340                         }
341                         
342                         return map;
343                 }
344                 
345                 void RegisterDerivedMap (XmlTypeMapping map, XmlTypeMapping derivedMap)
346                 {
347                         map.DerivedTypes.Add (derivedMap);
348                         map.DerivedTypes.AddRange (derivedMap.DerivedTypes);
349                         
350                         if (map.BaseMap != null)
351                                 RegisterDerivedMap (map.BaseMap, derivedMap);
352                         else {
353                                 XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
354                                 if (obmap != map)
355                                         obmap.DerivedTypes.Add (derivedMap);
356                         }
357                 }
358
359                 string GetTypeNamespace (TypeData typeData, XmlRootAttribute root, string defaultNamespace)
360                 {
361                         string typeNamespace = null;
362                         
363                         XmlAttributes atts = null;
364                         if (!typeData.IsListType)
365                         {
366                                 if (attributeOverrides != null)
367                                         atts = attributeOverrides[typeData.Type];
368                         }
369
370                         if (atts == null)
371                                 atts = new XmlAttributes (typeData.Type);
372
373                         if (atts.XmlType != null)
374                         {
375                                 if (atts.XmlType.Namespace != null && atts.XmlType.Namespace.Length != 0 && typeData.SchemaType != SchemaTypes.Enum)
376                                         typeNamespace = atts.XmlType.Namespace;
377                         }
378
379                         if (typeNamespace != null && typeNamespace.Length != 0) return typeNamespace;
380                         
381                         if (atts.XmlRoot != null && root == null)
382                                 root = atts.XmlRoot;
383
384                         if (root != null)
385                         {
386                                 if (root.Namespace != null && root.Namespace.Length != 0)
387                                         return root.Namespace;
388                         }
389
390                         if (defaultNamespace == null) return "";
391                         else return defaultNamespace;
392                 }
393
394                 XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
395                 {
396                         TypeData typeData = TypeTranslator.GetTypeData (type);
397                         ListMap obmap = new ListMap ();
398
399                         if (!allowPrivateTypes)
400                                 ReflectionHelper.CheckSerializableType (type, true);
401                         
402                         if (atts == null) atts = new XmlAttributes();
403                         Type itemType = typeData.ListItemType;
404
405                         // warning: byte[][] should not be considered multiarray
406                         bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
407
408                         XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
409
410                         foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
411                         {
412                                 if (att.NestingLevel != nestingLevel) continue;
413                                 Type elemType = (att.Type != null) ? att.Type : itemType;
414                                 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
415                                 elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
416                                 if (elem.Namespace == null) elem.Namespace = "";
417                                 elem.Form = att.Form;
418                                 elem.IsNullable = att.IsNullable && CanBeNull (elem.TypeData);
419                                 elem.NestingLevel = att.NestingLevel;
420
421                                 if (isMultiArray) {
422                                         elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
423                                 } else if (elem.TypeData.IsComplexType) {
424                                         elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
425                                 }
426
427                                 if (att.ElementName.Length != 0) {
428                                         elem.ElementName = XmlConvert.EncodeLocalName (att.ElementName);
429                                 } else if (elem.MappedType != null) {
430                                         elem.ElementName = elem.MappedType.ElementName;
431                                 } else {
432                                         elem.ElementName = TypeTranslator.GetTypeData (elemType).XmlType;
433                                 }
434
435                                 list.Add (elem);
436                         }
437
438                         if (list.Count == 0)
439                         {
440                                 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
441                                 if (isMultiArray)
442                                         elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
443                                 else if (elem.TypeData.IsComplexType)
444                                         elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
445
446                                 if (elem.MappedType != null) {
447                                         elem.ElementName = elem.MappedType.XmlType;
448                                 } else {
449                                         elem.ElementName = TypeTranslator.GetTypeData (itemType).XmlType;
450                                 }
451
452                                 elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
453                                 elem.IsNullable = CanBeNull (elem.TypeData);
454                                 list.Add (elem);
455                         }
456
457                         obmap.ItemInfo = list;
458
459                         // If there can be different element names (types) in the array, then its name cannot
460                         // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
461
462                         string baseName;
463                         if (list.Count > 1) {
464                                 baseName = "ArrayOfChoice" + (arrayChoiceCount++);
465                         } else {
466                                 XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo) list[0]);
467                                 if (elem.MappedType != null) {
468                                         baseName = TypeTranslator.GetArrayName (elem.MappedType.XmlType);
469                                 } else {
470                                         baseName = TypeTranslator.GetArrayName (elem.ElementName);
471                                 }
472                         }
473
474                         // Avoid name colisions
475
476                         int nameCount = 1;
477                         string name = baseName;
478
479                         do {
480                                 XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
481                                 if (foundMap == null) nameCount = -1;
482                                 else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
483                                 else name = baseName + (nameCount++);
484                         }
485                         while (nameCount != -1);
486
487                         XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
488                         map.ObjectMap = obmap;
489                         
490                         // Register any of the including types as a derived class of object
491                         XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
492                         
493                         XmlTypeMapping objectMapping = ImportTypeMapping (typeof(object));
494                         for (int i = 0; i < includes.Length; i++)
495                         {
496                                 Type includedType = includes[i].Type;
497                                 objectMapping.DerivedTypes.Add(ImportTypeMapping (includedType, null, defaultNamespace));
498                         }
499                         
500                         // Register this map as a derived class of object
501
502                         helper.RegisterSchemaType (map, name, defaultNamespace);
503                         ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
504
505                         return map;
506                 }
507
508                 XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
509                 {
510                         XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));
511                         if (map != null) return map;
512
513                         map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
514                         helper.RegisterClrType (map, type, map.XmlTypeNamespace);
515                         
516                         if (type.BaseType != null)
517                         {
518                                 XmlTypeMapping bmap = ImportTypeMapping (type.BaseType, root, defaultNamespace);
519                                 if (type.BaseType != typeof (object))
520                                         map.BaseMap = bmap;
521                                 
522                                 RegisterDerivedMap (bmap, map);
523                         }
524
525                         return map;
526                 }
527
528                 XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
529                 {
530                         TypeData typeData = TypeTranslator.GetTypeData (type);
531                         XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
532                         if (map != null) return map;
533                         map = CreateTypeMapping (typeData, root, null, defaultNamespace);
534                         helper.RegisterClrType (map, type, map.XmlTypeNamespace);
535                         return map;
536                 }
537
538                 XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
539                 {
540                         TypeData typeData = TypeTranslator.GetTypeData (type);
541                         XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
542                         if (map != null) return map;
543                         
544                         if (!allowPrivateTypes)
545                                 ReflectionHelper.CheckSerializableType (type, false);
546                                 
547                         map = CreateTypeMapping (typeData, root, null, defaultNamespace);
548                         helper.RegisterClrType (map, type, map.XmlTypeNamespace);
549
550                         string [] names = Enum.GetNames (type);
551                         ArrayList members = new ArrayList();
552                         foreach (string name in names)
553                         {
554                                 MemberInfo[] mem = type.GetMember (name);
555                                 string xmlName = null;
556                                 object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
557                                 if (atts.Length > 0) continue;
558                                 atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
559                                 if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
560                                 if (xmlName == null) xmlName = name;
561                                 members.Add (new EnumMap.EnumMapMember (XmlConvert.EncodeLocalName (xmlName), name));
562                         }
563
564                         bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
565                         map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
566                         ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
567                         return map;
568                 }
569
570                 XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
571                 {
572                         TypeData typeData = TypeTranslator.GetTypeData (type);
573                         XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (typeData, root, defaultNamespace));
574                         if (map != null) return map;
575                         
576                         if (!allowPrivateTypes)
577                                 ReflectionHelper.CheckSerializableType (type, false);
578                                 
579                         map = CreateTypeMapping (typeData, root, null, defaultNamespace);
580                         helper.RegisterClrType (map, type, map.XmlTypeNamespace);
581                         return map;
582                 }
583
584                 void ImportIncludedTypes (Type type, string defaultNamespace)
585                 {
586                         XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
587                         for (int n=0; n<includes.Length; n++)
588                         {
589                                 Type includedType = includes[n].Type;
590                                 ImportTypeMapping (includedType, null, defaultNamespace);
591                         }
592                 }
593
594                 ICollection GetReflectionMembers (Type type)
595                 {
596                         // First we want to find the inheritance hierarchy in reverse order.
597                         Type currentType = type;
598                         ArrayList typeList = new ArrayList();
599                         typeList.Add(currentType);
600                         while (currentType != typeof(object))
601                         {
602                                 currentType = currentType.BaseType; // Read the base type.
603                                 typeList.Insert(0, currentType); // Insert at 0 to reverse the order.
604                         }
605
606                         // Read all Fields via reflection.
607                         ArrayList fieldList = new ArrayList();
608                         FieldInfo[] tfields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
609                         currentType = null;
610                         int currentIndex = 0;
611                         foreach (FieldInfo field in tfields)
612                         {
613                                 // This statement ensures fields are ordered starting from the base type.
614                                 if (currentType != field.DeclaringType)
615                                 {
616                                         currentType = field.DeclaringType;
617                                         currentIndex=0;
618                                 }
619                                 fieldList.Insert(currentIndex++, field);
620                         }
621
622                         // Read all Properties via reflection.
623                         ArrayList propList = new ArrayList();
624                         PropertyInfo[] tprops = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
625                         currentType = null;
626                         currentIndex = 0;
627                         foreach (PropertyInfo prop in tprops)
628                         {
629                                 // This statement ensures properties are ordered starting from the base type.
630                                 if (currentType != prop.DeclaringType)
631                                 {
632                                         currentType = prop.DeclaringType;
633                                         currentIndex = 0;
634                                 }
635                                 if (!prop.CanRead) continue;
636                                 if (prop.GetIndexParameters().Length > 0) continue;
637                                 propList.Insert(currentIndex++, prop);
638                         }
639
640                         ArrayList members = new ArrayList();
641                         int fieldIndex=0;
642                         int propIndex=0;
643                         // We now step through the type hierarchy from the base (object) through
644                         // to the supplied class, as each step outputting all Fields, and then
645                         // all Properties.  This is the exact same ordering as .NET 1.0/1.1.
646                         foreach (Type t in typeList)
647                         {
648                                 // Add any fields matching the current DeclaringType.
649                                 while (fieldIndex < fieldList.Count)
650                                 {
651                                         FieldInfo field = (FieldInfo)fieldList[fieldIndex];
652                                         if (field.DeclaringType==t)
653                                         {
654                                                 fieldIndex++;
655                                                 XmlAttributes atts = attributeOverrides[type, field.Name];
656                                                 if (atts == null) atts = new XmlAttributes (field);
657                                                 if (atts.XmlIgnore) continue;
658                                                 XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
659                                                 member.DeclaringType = field.DeclaringType;
660                                                 members.Add(member);
661                                         }
662                                         else break;
663                                 }
664
665                                 // Add any properties matching the current DeclaringType.
666                                 while (propIndex < propList.Count)
667                                 {
668                                         PropertyInfo prop = (PropertyInfo)propList[propIndex];
669                                         if (prop.DeclaringType==t)
670                                         {
671                                                 propIndex++;
672                                                 XmlAttributes atts = attributeOverrides[type, prop.Name];
673                                                 if (atts == null) atts = new XmlAttributes (prop);
674                                                 if (atts.XmlIgnore) continue;
675                                                 if (!prop.CanWrite && (TypeTranslator.GetTypeData (prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) continue;
676                                                 XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
677                                                 member.DeclaringType = prop.DeclaringType;
678                                                 members.Add(member);
679                                         }
680                                         else break;
681                                 }
682                         }
683                         return members;         
684                 }
685                 
686                 private XmlTypeMapMember CreateMapMember (Type declaringType, XmlReflectionMember rmember, string defaultNamespace)
687                 {
688                         XmlTypeMapMember mapMember;
689                         XmlAttributes atts = rmember.XmlAttributes;
690                         TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
691
692                         if (atts.XmlAnyAttribute != null)
693                         {
694                                 if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
695                                          (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
696                                 {
697                                         mapMember = new XmlTypeMapMemberAnyAttribute();
698                                 }
699                                 else
700                                         throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
701                         }
702                         else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
703                         {
704                                 if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
705                                          (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
706                                          (rmember.MemberType.FullName == "System.Xml.XmlElement"))
707                                 {
708                                         XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
709                                         member.ElementInfo = ImportAnyElementInfo (defaultNamespace, rmember, member, atts);
710                                         mapMember = member;
711                                 }
712                                 else
713                                         throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
714                         }
715                         else if (atts.Xmlns)
716                         {
717                                 XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
718                                 mapMember = mapNamespaces;
719                         }
720                         else if (atts.XmlAttribute != null)
721                         {
722                                 // An attribute
723
724                                 if (atts.XmlElements != null && atts.XmlElements.Count > 0)
725                                         throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
726
727                                 XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
728                                 if (atts.XmlAttribute.AttributeName.Length == 0) 
729                                         mapAttribute.AttributeName = rmember.MemberName;
730                                 else 
731                                         mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
732
733                                 mapAttribute.AttributeName = XmlConvert.EncodeLocalName (mapAttribute.AttributeName);
734
735                                 if (typeData.IsComplexType)
736                                         mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
737                                 
738                                 if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace)
739                                 {
740                                         if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified)
741                                                 throw new InvalidOperationException ("The Form property may not be 'Unqualified' when an explicit Namespace property is present");
742                                         mapAttribute.Form = XmlSchemaForm.Qualified;
743                                         mapAttribute.Namespace = atts.XmlAttribute.Namespace;
744                                 }
745                                 else
746                                 {
747                                         mapAttribute.Form = atts.XmlAttribute.Form;
748                                         if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified)
749                                                 mapAttribute.Namespace = defaultNamespace;
750                                         else
751                                                 mapAttribute.Namespace = "";
752                                 }
753                                 
754                                 typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType);
755                                 mapMember = mapAttribute;
756                         }
757                         else if (typeData.SchemaType == SchemaTypes.Array)
758                         {
759                                 // If the member has a single XmlElementAttribute and the type is the type of the member,
760                                 // then it is not a flat list
761                                 
762                                 if (atts.XmlElements.Count > 1 ||
763                                    (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
764                                    (atts.XmlText != null))
765                                 {
766                                         // A flat list
767
768                                         // TODO: check that it does not have XmlArrayAttribute
769                                         XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
770                                         member.ListMap = new ListMap ();
771                                         member.ListMap.ItemInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName (rmember.MemberName), defaultNamespace, typeData.ListItemType, member, atts);
772                                         member.ElementInfo = member.ListMap.ItemInfo;
773                                         mapMember = member;
774                                 }
775                                 else
776                                 {
777                                         // A list
778
779                                         XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
780
781                                         // Creates an ElementInfo that identifies the array instance. 
782                                         member.ElementInfo = new XmlTypeMapElementInfoList();
783                                         XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
784                                         elem.ElementName = XmlConvert.EncodeLocalName((atts.XmlArray != null && atts.XmlArray.ElementName.Length != 0) ? atts.XmlArray.ElementName : rmember.MemberName);
785                                         elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
786                                         elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
787                                         elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false;
788                                         elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified;
789
790                                         member.ElementInfo.Add (elem);
791                                         mapMember = member;
792                                 }
793                         }
794                         else
795                         {
796                                 // An element
797
798                                 XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
799                                 member.ElementInfo = ImportElementInfo (declaringType, XmlConvert.EncodeLocalName(rmember.MemberName), defaultNamespace, rmember.MemberType, member, atts);
800                                 mapMember = member;
801                         }
802
803                         mapMember.DefaultValue = atts.XmlDefaultValue;
804                         mapMember.TypeData = typeData;
805                         mapMember.Name = rmember.MemberName;
806                         mapMember.IsReturnValue = rmember.IsReturnValue;
807                         return mapMember;
808                 }
809
810                 XmlTypeMapElementInfoList ImportElementInfo (Type cls, string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
811                 {
812                         EnumMap choiceEnumMap = null;
813                         Type choiceEnumType = null;
814                         
815                         XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
816                         ImportTextElementInfo (list, defaultType, member, atts);
817                         
818                         if (atts.XmlChoiceIdentifier != null) {
819                                 if (cls == null)
820                                         throw new InvalidOperationException ("XmlChoiceIdentifierAttribute not supported in this context.");
821                                         
822                                 member.ChoiceMember = atts.XmlChoiceIdentifier.MemberName;
823                                 MemberInfo[] mems = cls.GetMember (member.ChoiceMember, BindingFlags.Instance|BindingFlags.Public);
824                                 
825                                 if (mems.Length == 0)
826                                         throw new InvalidOperationException ("Choice member '" + member.ChoiceMember + "' not found in class '" + cls);
827                                         
828                                 if (mems[0] is PropertyInfo) choiceEnumType = ((PropertyInfo)mems[0]).PropertyType;
829                                 else choiceEnumType = ((FieldInfo)mems[0]).FieldType;
830                                 
831                                 choiceEnumMap = (EnumMap) ImportTypeMapping (choiceEnumType).ObjectMap;
832                         }
833                         
834                         if (atts.XmlElements.Count == 0 && list.Count == 0)
835                         {
836                                 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
837                                 elem.ElementName = defaultName;
838                                 elem.Namespace = defaultNamespace;
839                                 if (elem.TypeData.IsComplexType)
840                                         elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
841                                 list.Add (elem);
842                         }
843
844                         bool multiType = (atts.XmlElements.Count > 1);
845                         foreach (XmlElementAttribute att in atts.XmlElements)
846                         {
847                                 Type elemType = (att.Type != null) ? att.Type : defaultType;
848                                 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
849                                 elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
850                                 elem.Form = att.Form;
851                                 elem.IsNullable = att.IsNullable;
852                                 
853                                 if (elem.IsNullable && elem.TypeData.IsValueType)
854                                         throw new InvalidOperationException ("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName + " in member '" + defaultName + "'");
855                                         
856                                 if (elem.TypeData.IsComplexType)
857                                 {
858                                         if (att.DataType.Length != 0) throw new InvalidOperationException (
859                                                 string.Format(CultureInfo.InvariantCulture, "'{0}' is "
860                                                         + "an invalid value for '{1}.{2}' of type '{3}'. "
861                                                         + "The property may only be specified for primitive types.",
862                                                         att.DataType, cls.FullName, defaultName, 
863                                                         elem.TypeData.FullTypeName));
864                                         elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
865                                 }
866
867                                 if (att.ElementName.Length != 0)  {
868                                         elem.ElementName = XmlConvert.EncodeLocalName(att.ElementName);
869                                 } else if (multiType) {
870                                         if (elem.MappedType != null) {
871                                                 elem.ElementName = elem.MappedType.ElementName;
872                                         } else {
873                                                 elem.ElementName = TypeTranslator.GetTypeData (elemType).XmlType;
874                                         }
875                                 } else {
876                                         elem.ElementName = defaultName;
877                                 }
878
879                                 if (choiceEnumMap != null) {
880                                         string cname = choiceEnumMap.GetEnumName (elem.ElementName);
881                                         if (cname == null) throw new InvalidOperationException ("The '" + choiceEnumType + "' enumeration does not have a value for the element '" + elem.ElementName + "'");
882                                         elem.ChoiceValue = Enum.Parse (choiceEnumType, cname);
883                                 }
884                                         
885                                 list.Add (elem);
886                         }
887                         return list;
888                 }
889
890                 XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts)
891                 {
892                         XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
893
894                         ImportTextElementInfo (list, rmember.MemberType, member, atts);
895
896                         foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
897                         {
898                                 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
899                                 if (att.Name.Length != 0) 
900                                 {
901                                         elem.ElementName = XmlConvert.EncodeLocalName(att.Name);
902                                         elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
903                                 }
904                                 else 
905                                 {
906                                         elem.IsUnnamedAnyElement = true;
907                                         elem.Namespace = defaultNamespace;
908                                         if (att.Namespace != null) 
909                                                 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.");
910                                 }
911                                 list.Add (elem);
912                         }
913                         return list;
914                 }
915
916                 void ImportTextElementInfo (XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
917                 {
918                         if (atts.XmlText != null)
919                         {
920                                 member.IsXmlTextCollector = true;
921                                 if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
922                                 if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText);      // Nodes must be text nodes
923
924                                 XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
925
926                                 if (elem.TypeData.SchemaType != SchemaTypes.Primitive &&
927                                         elem.TypeData.SchemaType != SchemaTypes.Enum &&
928                                     elem.TypeData.SchemaType != SchemaTypes.XmlNode &&
929                                     !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode)
930                                  )
931                                         throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
932
933                                 elem.IsTextElement = true;
934                                 elem.WrappedElement = false;
935                                 list.Add (elem);
936                         }
937                 }
938                 
939                 bool CanBeNull (TypeData type)
940                 {
941                         return (type.SchemaType != SchemaTypes.Primitive || type.Type == typeof (string));
942                 }
943                 
944                 public void IncludeType (Type type)
945                 {
946                         if (type == null)
947                                 throw new ArgumentNullException ("type");
948
949                         if (includedTypes == null) includedTypes = new ArrayList ();
950                         if (!includedTypes.Contains (type))
951                                 includedTypes.Add (type);
952                 }
953
954                 public void IncludeTypes (ICustomAttributeProvider provider)
955                 { 
956                         object[] ats = provider.GetCustomAttributes (typeof(XmlIncludeAttribute), true);
957                         foreach (XmlIncludeAttribute at in ats)
958                                 IncludeType (at.Type);
959                 }
960
961                 #endregion // Methods
962         }
963 }