Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlTypeMapping.cs
1 //
2 // XmlTypeMapping.cs: 
3 //
4 // Author:
5 //   John Donagher (john@webmeta.com)
6 //   Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // (C) 2002 John Donagher
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Xml;
33 using System;
34 using System.Collections;
35 using System.Globalization;
36 using System.Xml.Schema;
37 using System.Reflection;
38
39 namespace System.Xml.Serialization
40 {
41         public class XmlTypeMapping : XmlMapping
42         {
43                 private string xmlType;
44                 private string xmlTypeNamespace;
45                 TypeData type;
46                 XmlTypeMapping baseMap;
47                 bool multiReferenceType = false;
48                 bool isSimpleType;
49                 string documentation;
50                 bool includeInSchema;
51                 bool isNullable = true;
52
53                 ArrayList _derivedTypes = new ArrayList();
54
55                 internal XmlTypeMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
56                 : base (elementName, ns)
57                 {
58                         this.type = typeData;
59                         this.xmlType = xmlType;
60                         this.xmlTypeNamespace = xmlTypeNamespace;
61                 }
62
63 #if !NET_2_0
64                 public string ElementName
65                 {
66                         get { return _elementName; }
67                 }
68
69                 public string Namespace
70                 {
71                         get { return _namespace; }
72                 }
73 #endif
74
75                 public string TypeFullName
76                 {
77                         get { return type.FullTypeName; }
78                 }
79
80                 public string TypeName
81                 {
82                         get { return type.TypeName; }
83                 }
84
85 #if NET_2_0
86                 public string XsdTypeName
87                 {
88                         get { return XmlType; }
89                 }
90
91                 public string XsdTypeNamespace
92                 {
93                         get { return XmlTypeNamespace; }
94                 }
95 #endif
96
97                 internal TypeData TypeData
98                 {
99                         get { return type; }
100                 }
101
102                 internal string XmlType
103                 {
104                         get { return xmlType; }
105                         set { xmlType = value; }
106                 }
107
108                 internal string XmlTypeNamespace
109                 {
110                         get { return xmlTypeNamespace ?? string.Empty; }
111                         set { xmlTypeNamespace = value; }
112                 }
113
114                 internal bool HasXmlTypeNamespace
115                 {
116                         get { return xmlTypeNamespace != null; }
117                 }
118
119                 internal ArrayList DerivedTypes
120                 {
121                         get { return _derivedTypes; }
122                         set { _derivedTypes = value; }
123                 }
124
125                 internal bool MultiReferenceType
126                 {
127                         get { return multiReferenceType; }
128                         set { multiReferenceType = value; }
129                 }
130
131                 internal XmlTypeMapping BaseMap
132                 {
133                         get { return baseMap; }
134                         set { baseMap = value; }
135                 }
136
137                 internal bool IsSimpleType
138                 {
139                         get { return isSimpleType; }
140                         set { isSimpleType = value; }
141                 }
142
143                 internal string Documentation
144                 {
145                         set { documentation = value; }
146                         get { return documentation; }
147                 }
148
149                 internal bool IncludeInSchema
150                 {
151                         get { return includeInSchema; }
152                         set { includeInSchema = value; }
153                 }
154                 
155                 internal bool IsNullable
156                 {
157                         get { return isNullable; }
158                         set { isNullable = value; }
159                 }
160
161                 internal XmlTypeMapping GetRealTypeMap (Type objectType)
162                 {
163                         if (TypeData.SchemaType == SchemaTypes.Enum)
164                                 return this;
165
166                         // Returns the map for a subtype of this map's type
167                         if (TypeData.Type == objectType) return this;
168                         for (int n=0; n<_derivedTypes.Count; n++) {
169                                 XmlTypeMapping map = (XmlTypeMapping) _derivedTypes[n];
170                                 if (map.TypeData.Type == objectType) return map;
171                         }
172                         
173                         return null;
174                 }
175
176                 internal XmlTypeMapping GetRealElementMap (string name, string ens)
177                 {
178                         if (xmlType == name && XmlTypeNamespace == ens) return this;
179                         foreach (XmlTypeMapping map in _derivedTypes)
180                                 if (map.xmlType == name && map.XmlTypeNamespace == ens) return map;
181                         
182                         return null;
183                 }
184                 
185                 internal void UpdateRoot (XmlQualifiedName qname)
186                 {
187                         if (qname != null) {
188                                 this._elementName = qname.Name;
189                                 this._namespace = qname.Namespace;
190                         }
191                 }
192         }
193
194  
195         // Mapping info for XmlSerializable
196         internal class XmlSerializableMapping : XmlTypeMapping
197         {
198                 XmlSchema _schema;
199 #if NET_2_0 && !MOONLIGHT
200                 XmlSchemaComplexType _schemaType;
201                 XmlQualifiedName _schemaTypeName;
202 #endif
203
204                 internal XmlSerializableMapping(XmlRootAttribute root, string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
205                         : base(elementName, ns, typeData, xmlType, xmlTypeNamespace)
206                 {
207 #if NET_2_0 && !MOONLIGHT
208                         XmlSchemaProviderAttribute schemaProvider = (XmlSchemaProviderAttribute) Attribute.GetCustomAttribute (typeData.Type, typeof (XmlSchemaProviderAttribute));
209
210                         if (schemaProvider != null) {
211                                 string method = schemaProvider.MethodName;
212                                 MethodInfo mi = typeData.Type.GetMethod (method, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
213                                 if (mi == null)
214                                         throw new InvalidOperationException (String.Format ("Type '{0}' must implement public static method '{1}'", typeData.Type, method));
215                                 if (!typeof (XmlQualifiedName).IsAssignableFrom (mi.ReturnType) &&
216                                     // LAMESPEC: it is undocumented. (We don't have to tell users about it in the error message.)
217                                     // Also do not add such a silly compatibility test to assert that it does not raise an error.
218                                     !typeof (XmlSchemaComplexType).IsAssignableFrom (mi.ReturnType))
219                                         throw new InvalidOperationException (String.Format ("Method '{0}' indicated by XmlSchemaProviderAttribute must have its return type as XmlQualifiedName", method));
220                                 XmlSchemaSet xs = new XmlSchemaSet ();
221                                 object retVal = mi.Invoke (null, new object [] { xs });
222                                 _schemaTypeName = XmlQualifiedName.Empty;
223                                 if (retVal == null)
224                                         return;
225
226                                 if (retVal is XmlSchemaComplexType) {
227                                         _schemaType = (XmlSchemaComplexType) retVal;
228                                         if (!_schemaType.QualifiedName.IsEmpty)
229                                                 _schemaTypeName = _schemaType.QualifiedName;
230                                         else
231                                                 _schemaTypeName = new XmlQualifiedName (xmlType, xmlTypeNamespace);
232                                 }
233                                 else if (retVal is XmlQualifiedName) {
234                                         _schemaTypeName = (XmlQualifiedName)retVal;
235                                 }
236                                 else
237                                         throw new InvalidOperationException (
238                                                 String.Format ("Method {0}.{1}() specified by XmlSchemaProviderAttribute has invalid signature: return type must be compatible with System.Xml.XmlQualifiedName.", typeData.Type.Name, method));
239
240                                 // defaultNamespace at XmlReflectionImporter takes precedence for Namespace, but not for XsdTypeNamespace.
241                                 UpdateRoot (new XmlQualifiedName (root != null ? root.ElementName : _schemaTypeName.Name, root != null ? root.Namespace : Namespace ?? _schemaTypeName.Namespace));
242                                 XmlTypeNamespace = _schemaTypeName.Namespace;
243                                 XmlType = _schemaTypeName.Name;
244
245                                 if (!_schemaTypeName.IsEmpty && xs.Count > 0) {
246                                         XmlSchema [] schemas = new XmlSchema [xs.Count];
247                                         xs.CopyTo (schemas, 0);
248                                         _schema = schemas [0];
249                                 }
250
251                                 return;
252                         }
253 #endif
254 #if NET_2_0 && !MOONLIGHT
255                         IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type, true);
256                         try {
257                                 _schema = serializable.GetSchema();
258                         } catch (Exception) {
259                                 // LAMESPEC: .NET has a bad exception catch and swallows it silently.
260                         }
261 #else
262                         IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type);
263                         _schema = serializable.GetSchema();
264 #endif
265 #if !MOONLIGHT
266                         if (_schema != null) 
267                         {
268                                 if (_schema.Id == null || _schema.Id.Length == 0) 
269                                         throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id.");
270                         }
271 #endif
272                 }
273
274                 internal XmlSchema Schema
275                 {
276                         get { return _schema; }
277                 }
278
279 #if NET_2_0 && !MOONLIGHT
280                 internal XmlSchemaType SchemaType {
281                         get { return _schemaType; }
282                 }
283
284                 internal XmlQualifiedName SchemaTypeName {
285                         get { return _schemaTypeName; }
286                 }
287 #endif
288         }
289  
290
291         // Mapping info for classes and structs
292
293         internal class ClassMap: ObjectMap
294         {
295                 Hashtable _elements = new Hashtable ();
296                 ArrayList _elementMembers;
297                 Hashtable _attributeMembers;
298                 XmlTypeMapMemberAttribute[] _attributeMembersArray;
299                 XmlTypeMapElementInfo[] _elementsByIndex;
300                 ArrayList _flatLists;
301                 ArrayList _allMembers = new ArrayList ();
302                 ArrayList _membersWithDefault;
303                 ArrayList _listMembers;
304                 XmlTypeMapMemberAnyElement _defaultAnyElement;
305                 XmlTypeMapMemberAnyAttribute _defaultAnyAttribute;
306                 XmlTypeMapMemberNamespaces _namespaceDeclarations;
307                 XmlTypeMapMember _xmlTextCollector;
308                 XmlTypeMapMember _returnMember;
309                 bool _ignoreMemberNamespace;
310                 bool _canBeSimpleType = true;
311                 bool? _isOrderDependentMap;
312
313                 public void AddMember (XmlTypeMapMember member)
314                 {
315                         // If GlobalIndex has not been set, set it now
316                         if (member.GlobalIndex == -1)
317                                 member.GlobalIndex = _allMembers.Count;
318                         
319                         _allMembers.Add (member);
320                         
321                         if (!(member.DefaultValue is System.DBNull) && member.DefaultValue != null) {
322                                 if (_membersWithDefault == null) _membersWithDefault = new ArrayList ();
323                                 _membersWithDefault.Add (member);
324                         }
325                         
326                         if (member.IsReturnValue)
327                                 _returnMember = member;
328                         
329                         if (member is XmlTypeMapMemberAttribute)
330                         {
331                                 XmlTypeMapMemberAttribute atm = (XmlTypeMapMemberAttribute)member;
332                                 if (_attributeMembers == null) _attributeMembers = new Hashtable();
333                                 string key = BuildKey (atm.AttributeName, atm.Namespace, -1);
334                                 if (_attributeMembers.ContainsKey (key))
335                                         throw new InvalidOperationException ("The XML attribute named '" + atm.AttributeName + "' from namespace '" + atm.Namespace + "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the attribute.");
336                                 member.Index = _attributeMembers.Count;
337                                 _attributeMembers.Add (key, member);
338                                 return;
339                         }
340                         else if (member is XmlTypeMapMemberFlatList)
341                         {
342                                 RegisterFlatList ((XmlTypeMapMemberFlatList)member);
343                         }
344                         else if (member is XmlTypeMapMemberAnyElement)
345                         {
346                                 XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement) member;
347                                 if (mem.IsDefaultAny) _defaultAnyElement = mem;
348                                 if (mem.TypeData.IsListType) RegisterFlatList (mem);
349                         }
350                         else if (member is XmlTypeMapMemberAnyAttribute)
351                         {
352                                 _defaultAnyAttribute = (XmlTypeMapMemberAnyAttribute) member;
353                                 return;
354                         }
355                         else if (member is XmlTypeMapMemberNamespaces)
356                         {
357                                 _namespaceDeclarations = (XmlTypeMapMemberNamespaces) member;
358                                 return;
359                         }
360
361                         if (member is XmlTypeMapMemberElement && ((XmlTypeMapMemberElement)member).IsXmlTextCollector)
362                         {
363                                 if (_xmlTextCollector != null) throw new InvalidOperationException ("XmlTextAttribute can only be applied once in a class");
364                                 _xmlTextCollector = member;
365                         }
366
367                         if (_elementMembers == null) {
368                                 _elementMembers = new ArrayList();
369                                 _elements = new Hashtable();
370                         }
371
372                         member.Index = _elementMembers.Count;
373                         _elementMembers.Add (member);
374
375                         ICollection elemsInfo = ((XmlTypeMapMemberElement)member).ElementInfo;
376                         foreach (XmlTypeMapElementInfo elem in elemsInfo)
377                         {
378                                 string key = BuildKey (elem.ElementName, elem.Namespace, elem.ExplicitOrder);
379                                 if (_elements.ContainsKey (key)) 
380                                         throw new InvalidOperationException ("The XML element named '" + elem.ElementName + "' from namespace '" + elem.Namespace + "' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.");
381                                 _elements.Add (key, elem);
382                         }
383                         
384                         if (member.TypeData.IsListType && member.TypeData.Type != null && !member.TypeData.Type.IsArray) {
385                                 if (_listMembers == null) _listMembers = new ArrayList ();
386                                 _listMembers.Add (member);
387                         }
388                 }
389
390                 void RegisterFlatList (XmlTypeMapMemberExpandable member)
391                 {
392                         if (_flatLists == null) _flatLists = new ArrayList ();
393                         member.FlatArrayIndex = _flatLists.Count;
394                         _flatLists.Add (member);
395                 }
396
397                 public XmlTypeMapMemberAttribute GetAttribute (string name, string ns)
398                 {
399                         if (_attributeMembers == null) return null;
400                         return (XmlTypeMapMemberAttribute)_attributeMembers [BuildKey (name,ns, -1)];
401                 }
402
403                 public XmlTypeMapElementInfo GetElement (string name, string ns, int order)
404                 {
405                         if (_elements == null) return null;
406                         return (XmlTypeMapElementInfo)_elements [BuildKey (name,ns, order)];
407                 }
408                 
409                 public XmlTypeMapElementInfo GetElement (int index)
410                 {
411                         if (_elements == null) return null;
412                         
413                         if (_elementsByIndex == null)
414                         {
415                                 _elementsByIndex = new XmlTypeMapElementInfo [_elementMembers.Count];
416                                 foreach (XmlTypeMapMemberElement mem in _elementMembers)
417                                 {
418                                         if (mem.ElementInfo.Count != 1) 
419                                                 throw new InvalidOperationException ("Read by order only possible for encoded/bare format");
420                                                 
421                                         _elementsByIndex [mem.Index] = (XmlTypeMapElementInfo) mem.ElementInfo [0];
422                                 }
423                         }
424                         if (index >= _elementMembers.Count)
425                                 return null;
426                         return _elementsByIndex [index];
427                 }
428                 
429                 private string BuildKey (string name, string ns, int explicitOrder)
430                 {
431                         if (_ignoreMemberNamespace) return name;
432                         else return name + " / " + ns + (explicitOrder < 0 ? "" : "/" + explicitOrder);
433                 }
434                 
435                 public ICollection AllElementInfos
436                 {
437                         get { return _elements.Values; }
438                 }
439                 
440                 
441                 public bool IgnoreMemberNamespace
442                 {
443                         get { return _ignoreMemberNamespace; }
444                         set { _ignoreMemberNamespace = value; }
445                 }
446
447                 public bool IsOrderDependentMap {
448                         get {
449                                 if (_isOrderDependentMap == null) {
450                                         _isOrderDependentMap = false;
451                                         foreach (XmlTypeMapElementInfo ei in _elements.Values)
452                                                 if (ei.ExplicitOrder >= 0) {
453                                                         _isOrderDependentMap = true;
454                                                         break;
455                                                 }
456                                 }
457                                 return (bool) _isOrderDependentMap;
458                         }
459                 }
460
461                 public XmlTypeMapMember FindMember (string name)
462                 {
463                         for (int n=0; n<_allMembers.Count; n++)
464                                 if (((XmlTypeMapMember)_allMembers[n]).Name == name) return (XmlTypeMapMember)_allMembers[n];
465                         return null;
466                 }
467
468                 public XmlTypeMapMemberAnyElement DefaultAnyElementMember
469                 {
470                         get { return _defaultAnyElement; }
471                 }
472
473                 public XmlTypeMapMemberAnyAttribute DefaultAnyAttributeMember
474                 {
475                         get { return _defaultAnyAttribute; }
476                 }
477
478                 public XmlTypeMapMemberNamespaces NamespaceDeclarations
479                 {
480                         get { return _namespaceDeclarations; }
481                 }
482
483                 public ICollection AttributeMembers
484                 {
485                         get 
486                         {
487                                 if (_attributeMembers == null) return null;
488                                 if (_attributeMembersArray != null) return _attributeMembersArray;
489                                 
490                                 _attributeMembersArray = new XmlTypeMapMemberAttribute[_attributeMembers.Count];
491                                 foreach (XmlTypeMapMemberAttribute mem in _attributeMembers.Values)
492                                         _attributeMembersArray [mem.Index] = mem;
493                                 return _attributeMembersArray;
494                         }
495                 }
496
497                 public ICollection ElementMembers
498                 {
499                         get { return _elementMembers; }
500                 }
501
502                 public ArrayList AllMembers
503                 {
504                         get { return _allMembers; }
505                 }
506
507                 public ArrayList FlatLists
508                 {
509                         get { return _flatLists; }
510                 }
511                 
512                 public ArrayList MembersWithDefault
513                 {
514                         get { return _membersWithDefault; }
515                 }
516                 
517                 public ArrayList ListMembers
518                 {
519                         get { return _listMembers; }
520                 }
521
522                 public XmlTypeMapMember XmlTextCollector
523                 {
524                         get { return _xmlTextCollector; }
525                 }
526                 
527                 public XmlTypeMapMember ReturnMember
528                 {
529                         get { return _returnMember; }
530                 }
531
532                 public XmlQualifiedName SimpleContentBaseType
533                 {
534                         get
535                         {
536                                 if (!_canBeSimpleType || _elementMembers == null || _elementMembers.Count != 1) return null;
537                                 XmlTypeMapMemberElement member = (XmlTypeMapMemberElement) _elementMembers[0];
538                                 if (member.ElementInfo.Count != 1) return null;
539                                 XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
540                                 if (!einfo.IsTextElement) return null;
541                                 if (member.TypeData.SchemaType == SchemaTypes.Primitive || member.TypeData.SchemaType == SchemaTypes.Enum)
542                                         return new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace);
543                                 return null;
544                         }
545                 }
546                 
547                 public void SetCanBeSimpleType (bool can)
548                 {
549                         _canBeSimpleType = can;
550                 }
551
552                 public bool HasSimpleContent
553                 {
554                         get
555                         {
556                                 return SimpleContentBaseType != null;
557                         }
558                 }
559
560         }
561
562         // Mapping info for arrays and lists
563
564         internal class ListMap: ObjectMap
565         {
566                 XmlTypeMapElementInfoList _itemInfo;
567                 bool _gotNestedMapping;
568                 XmlTypeMapping _nestedArrayMapping;
569                 string _choiceMember;
570
571                 public bool IsMultiArray
572                 {
573                         get
574                         {
575                                 return (NestedArrayMapping != null);
576                         }
577                 }
578
579                 public string ChoiceMember
580                 {
581                         get { return _choiceMember; }
582                         set { _choiceMember = value; }
583                 }
584
585                 public XmlTypeMapping NestedArrayMapping
586                 {
587                         get
588                         {
589                                 if (_gotNestedMapping) return _nestedArrayMapping;
590                                 _gotNestedMapping = true;
591
592                                 _nestedArrayMapping = ((XmlTypeMapElementInfo)_itemInfo[0]).MappedType;
593
594                                 if (_nestedArrayMapping == null) return null;
595                                 
596                                 if (_nestedArrayMapping.TypeData.SchemaType != SchemaTypes.Array) {
597                                         _nestedArrayMapping = null; return null;
598                                 }
599
600                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
601                                         if (elem.MappedType != _nestedArrayMapping) {
602                                                 _nestedArrayMapping = null;
603                                                 return null;
604                                         }
605
606                                 return _nestedArrayMapping;
607                         }
608                 }
609
610                 public XmlTypeMapElementInfoList ItemInfo
611                 {
612
613                         get { return _itemInfo; }
614                         set { _itemInfo = value; }
615                 }
616
617                 public XmlTypeMapElementInfo FindElement (object ob, int index, object memberValue)
618                 {
619                         if (_itemInfo.Count == 1) 
620                                 return (XmlTypeMapElementInfo) _itemInfo[0];
621                         else if (_choiceMember != null && index != -1)
622                         {
623                                 Array values = (Array) XmlTypeMapMember.GetValue (ob, _choiceMember);
624                                 if (values == null || index >= values.Length)
625                                         throw new InvalidOperationException ("Invalid or missing choice enum value in member '" + _choiceMember + "'.");
626                                 object val = values.GetValue (index);
627                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
628                                         if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (val))
629                                                 return elem;
630                         }
631                         else
632                         {
633                                 if (memberValue == null) return null;
634                                 Type type = memberValue.GetType();
635                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
636                                         if (elem.TypeData.Type == type) return elem;
637                         }
638                         return null;
639                 }       
640
641                 public XmlTypeMapElementInfo FindElement (string elementName, string ns)
642                 {
643                         foreach (XmlTypeMapElementInfo elem in _itemInfo)
644                                 if (elem.ElementName == elementName && elem.Namespace == ns) return elem;
645                         return null;
646                 }
647                 
648                 public XmlTypeMapElementInfo FindTextElement ()
649                 {
650                         foreach (XmlTypeMapElementInfo elem in _itemInfo)
651                                 if (elem.IsTextElement) return elem;
652                         return null;
653                 }
654                 
655                 public string GetSchemaArrayName ()
656                 {
657                         XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) _itemInfo[0];
658                         if (einfo.MappedType != null) return TypeTranslator.GetArrayName (einfo.MappedType.XmlType);
659                         else return TypeTranslator.GetArrayName (einfo.TypeData.XmlType);
660                 }
661
662                 public void GetArrayType (int itemCount, out string localName, out string ns)
663                 {
664                         string arrayDim;
665                         if (itemCount != -1) arrayDim = "[" + itemCount + "]";
666                         else arrayDim = "[]";
667
668                         XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) _itemInfo[0];
669                         if (info.TypeData.SchemaType == SchemaTypes.Array)
670                         {
671                                 string nm;
672                                 ((ListMap)info.MappedType.ObjectMap).GetArrayType (-1, out nm, out ns);
673                                 localName = nm + arrayDim;
674                         }
675                         else 
676                         {
677                                 if (info.MappedType != null)
678                                 {
679                                         localName = info.MappedType.XmlType + arrayDim;
680                                         ns = info.MappedType.Namespace;
681                                 }
682                                 else 
683                                 {
684                                         localName = info.TypeData.XmlType + arrayDim;
685                                         ns = info.DataTypeNamespace;
686                                 }
687                         }
688                 }
689
690                 public override bool Equals (object other)
691                 {
692                         ListMap lmap = other as ListMap;
693                         if (lmap == null) return false;
694
695                         if (_itemInfo.Count != lmap._itemInfo.Count) return false;
696                         for (int n=0; n<_itemInfo.Count; n++)
697                                 if (!_itemInfo[n].Equals (lmap._itemInfo[n])) return false;
698                         return true;
699                 }
700
701                 public override int GetHashCode ()
702                 {
703                         return base.GetHashCode ();
704                 }
705         }
706
707         internal class EnumMap: ObjectMap
708         {
709                 readonly EnumMapMember[] _members;
710                 readonly bool _isFlags;
711                 readonly string[] _enumNames = null;
712                 readonly string[] _xmlNames = null;
713                 readonly long[] _values = null;
714
715                 public class EnumMapMember
716                 {
717                         readonly string _xmlName;
718                         readonly string _enumName;
719                         readonly long _value;
720                         string _documentation;
721
722                         public EnumMapMember (string xmlName, string enumName)
723                                 : this (xmlName, enumName, 0)
724                         {
725                         }
726
727                         public EnumMapMember (string xmlName, string enumName, long value)
728                         {
729                                 _xmlName = xmlName;
730                                 _enumName = enumName;
731                                 _value = value;
732                         }
733
734                         public string XmlName
735                         {
736                                 get { return _xmlName; }
737                         }
738
739                         public string EnumName
740                         {
741                                 get { return _enumName; }
742                         }
743
744                         public long Value
745                         {
746                                 get { return _value; }
747                         }
748
749                         public string Documentation
750                         {
751                                 get { return _documentation; }
752                                 set { _documentation = value; }
753                         }
754                 }
755
756                 public EnumMap (EnumMapMember[] members, bool isFlags)
757                 {
758                         _members = members;
759                         _isFlags = isFlags;
760
761                         _enumNames = new string[_members.Length];
762                         _xmlNames = new string[_members.Length];
763                         _values = new long[_members.Length];
764
765                         for (int i = 0; i < _members.Length; i++) {
766                                 EnumMapMember mem = _members[i];
767                                 _enumNames[i] = mem.EnumName;
768                                 _xmlNames[i] = mem.XmlName;
769                                 _values[i] = mem.Value;
770                         }
771                 }
772                 
773                 public bool IsFlags
774                 {
775                         get { return _isFlags; }
776                 }
777
778                 public EnumMapMember[] Members
779                 {
780                         get { return _members; }
781                 }
782
783                 public string[] EnumNames
784                 {
785                         get {
786                                 return _enumNames;
787                         }
788                 }
789
790                 public string[] XmlNames
791                 {
792                         get {
793                                 return _xmlNames;
794                         }
795                 }
796
797                 public long[] Values
798                 {
799                         get {
800                                 return _values;
801                         }
802                 }
803
804                 public string GetXmlName (string typeName, object enumValue)
805                 {
806                         if (enumValue is string) {
807                                 throw new InvalidCastException ();
808                         }
809
810                         long value = 0;
811
812                         try {
813                                 value = ((IConvertible) enumValue).ToInt64 (CultureInfo.CurrentCulture);
814                         } catch (FormatException) {
815                                 throw new InvalidCastException ();
816                         }
817
818                         for (int i = 0; i < Values.Length; i++) {
819                                 if (Values[i] == value)
820                                         return XmlNames[i];
821                         }
822
823                         if (IsFlags && value == 0)
824                                 return string.Empty;
825
826                         string xmlName = string.Empty;
827                         if (IsFlags) {
828 #if NET_2_0
829                                 xmlName = XmlCustomFormatter.FromEnum (value, XmlNames, Values, typeName);
830 #else
831                                 xmlName = XmlCustomFormatter.FromEnum (value, XmlNames, Values);
832 #endif
833                         }
834
835                         if (xmlName.Length == 0) {
836 #if NET_2_0
837                                 throw new InvalidOperationException (string.Format(CultureInfo.CurrentCulture,
838                                         "'{0}' is not a valid value for {1}.", value, typeName));
839 #else
840                                 return value.ToString (CultureInfo.InvariantCulture);
841 #endif
842                         }
843                         return xmlName;
844                 }
845
846                 public string GetEnumName (string typeName, string xmlName)
847                 {
848                         if (_isFlags) {
849                                 xmlName = xmlName.Trim ();
850                                 if (xmlName.Length == 0)
851                                         return "0";
852
853                                 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
854                                 string[] enumNames = xmlName.Split (null);
855                                 foreach (string name in enumNames) {
856                                         if (name == string.Empty) continue;
857                                         string foundEnumValue = null;
858                                         for (int i = 0; i < XmlNames.Length; i++)
859                                                 if (XmlNames[i] == name) {
860                                                         foundEnumValue = EnumNames[i];
861                                                         break;
862                                                 }
863
864                                         if (foundEnumValue != null) {
865                                                 if (sb.Length > 0)
866                                                         sb.Append (',');
867                                                 sb.Append (foundEnumValue);
868                                         } else {
869                                                 throw new InvalidOperationException (string.Format (CultureInfo.CurrentCulture,
870                                                         "'{0}' is not a valid value for {1}.", name, typeName));
871                                         }
872                                 }
873                                 return sb.ToString ();
874                         }
875
876                         foreach (EnumMapMember mem in _members)
877                                 if (mem.XmlName == xmlName) return mem.EnumName;
878                                 
879                         return null;
880                 }
881         }
882 }