Make some S.X.S types compatible with the SL API (for SDK)
[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; }
111                         set { xmlTypeNamespace = value; }
112                 }
113
114                 internal ArrayList DerivedTypes
115                 {
116                         get { return _derivedTypes; }
117                         set { _derivedTypes = value; }
118                 }
119
120                 internal bool MultiReferenceType
121                 {
122                         get { return multiReferenceType; }
123                         set { multiReferenceType = value; }
124                 }
125
126                 internal XmlTypeMapping BaseMap
127                 {
128                         get { return baseMap; }
129                         set { baseMap = value; }
130                 }
131
132                 internal bool IsSimpleType
133                 {
134                         get { return isSimpleType; }
135                         set { isSimpleType = value; }
136                 }
137
138                 internal string Documentation
139                 {
140                         set { documentation = value; }
141                         get { return documentation; }
142                 }
143
144                 internal bool IncludeInSchema
145                 {
146                         get { return includeInSchema; }
147                         set { includeInSchema = value; }
148                 }
149                 
150                 internal bool IsNullable
151                 {
152                         get { return isNullable; }
153                         set { isNullable = value; }
154                 }
155
156                 internal XmlTypeMapping GetRealTypeMap (Type objectType)
157                 {
158                         if (TypeData.SchemaType == SchemaTypes.Enum)
159                                 return this;
160
161                         // Returns the map for a subtype of this map's type
162                         if (TypeData.Type == objectType) return this;
163                         for (int n=0; n<_derivedTypes.Count; n++) {
164                                 XmlTypeMapping map = (XmlTypeMapping) _derivedTypes[n];
165                                 if (map.TypeData.Type == objectType) return map;
166                         }
167                         
168                         return null;
169                 }
170
171                 internal XmlTypeMapping GetRealElementMap (string name, string ens)
172                 {
173                         if (xmlType == name && xmlTypeNamespace == ens) return this;
174                         foreach (XmlTypeMapping map in _derivedTypes)
175                                 if (map.xmlType == name && map.xmlTypeNamespace == ens) return map;
176                         
177                         return null;
178                 }
179                 
180                 internal void UpdateRoot (XmlQualifiedName qname)
181                 {
182                         if (qname != null) {
183                                 this._elementName = qname.Name;
184                                 this._namespace = qname.Namespace;
185                         }
186                 }
187         }
188
189  
190         // Mapping info for XmlSerializable
191         internal class XmlSerializableMapping : XmlTypeMapping
192         {
193                 XmlSchema _schema;
194 #if NET_2_0 && !MOONLIGHT
195                 XmlSchemaComplexType _schemaType;
196                 XmlQualifiedName _schemaTypeName;
197 #endif
198
199                 internal XmlSerializableMapping(XmlRootAttribute root, string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
200                         : base(elementName, ns, typeData, xmlType, xmlTypeNamespace)
201                 {
202 #if NET_2_0 && !MOONLIGHT
203                         XmlSchemaProviderAttribute schemaProvider = (XmlSchemaProviderAttribute) Attribute.GetCustomAttribute (typeData.Type, typeof (XmlSchemaProviderAttribute));
204
205                         if (schemaProvider != null) {
206                                 string method = schemaProvider.MethodName;
207                                 MethodInfo mi = typeData.Type.GetMethod (method, BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
208                                 if (mi == null)
209                                         throw new InvalidOperationException (String.Format ("Type '{0}' must implement public static method '{1}'", typeData.Type, method));
210                                 if (!typeof (XmlQualifiedName).IsAssignableFrom (mi.ReturnType) &&
211                                     // LAMESPEC: it is undocumented. (We don't have to tell users about it in the error message.)
212                                     // Also do not add such a silly compatibility test to assert that it does not raise an error.
213                                     !typeof (XmlSchemaComplexType).IsAssignableFrom (mi.ReturnType))
214                                         throw new InvalidOperationException (String.Format ("Method '{0}' indicated by XmlSchemaProviderAttribute must have its return type as XmlQualifiedName", method));
215                                 XmlSchemaSet xs = new XmlSchemaSet ();
216                                 object retVal = mi.Invoke (null, new object [] { xs });
217                                 _schemaTypeName = XmlQualifiedName.Empty;
218                                 if (retVal == null)
219                                         return;
220
221                                 if (retVal is XmlSchemaComplexType) {
222                                         _schemaType = (XmlSchemaComplexType) retVal;
223                                         if (!_schemaType.QualifiedName.IsEmpty)
224                                                 _schemaTypeName = _schemaType.QualifiedName;
225                                         else
226                                                 _schemaTypeName = new XmlQualifiedName (xmlType, xmlTypeNamespace);
227                                 }
228                                 else if (retVal is XmlQualifiedName) {
229                                         _schemaTypeName = (XmlQualifiedName)retVal;
230                                 }
231                                 else
232                                         throw new InvalidOperationException (
233                                                 String.Format ("Method {0}.{1}() specified by XmlSchemaProviderAttribute has invalid signature: return type must be compatible with System.Xml.XmlQualifiedName.", typeData.Type.Name, method));
234
235                                 // defaultNamespace at XmlReflectionImporter takes precedence for Namespace, but not for XsdTypeNamespace.
236                                 UpdateRoot (new XmlQualifiedName (root != null ? root.ElementName : _schemaTypeName.Name, root != null ? root.Namespace : Namespace ?? _schemaTypeName.Namespace));
237                                 XmlTypeNamespace = _schemaTypeName.Namespace;
238                                 XmlType = _schemaTypeName.Name;
239
240                                 if (!_schemaTypeName.IsEmpty && xs.Count > 0) {
241                                         XmlSchema [] schemas = new XmlSchema [xs.Count];
242                                         xs.CopyTo (schemas, 0);
243                                         _schema = schemas [0];
244                                 }
245
246                                 return;
247                         }
248 #endif
249 #if NET_2_0 && !MOONLIGHT
250                         IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type, true);
251                         try {
252                                 _schema = serializable.GetSchema();
253                         } catch (Exception) {
254                                 // LAMESPEC: .NET has a bad exception catch and swallows it silently.
255                         }
256 #else
257                         IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type);
258                         _schema = serializable.GetSchema();
259 #endif
260 #if !MOONLIGHT
261                         if (_schema != null) 
262                         {
263                                 if (_schema.Id == null || _schema.Id.Length == 0) 
264                                         throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id.");
265                         }
266 #endif
267                 }
268
269                 internal XmlSchema Schema
270                 {
271                         get { return _schema; }
272                 }
273
274 #if NET_2_0 && !MOONLIGHT
275                 internal XmlSchemaType SchemaType {
276                         get { return _schemaType; }
277                 }
278
279                 internal XmlQualifiedName SchemaTypeName {
280                         get { return _schemaTypeName; }
281                 }
282 #endif
283         }
284  
285
286         // Mapping info for classes and structs
287
288         internal class ClassMap: ObjectMap
289         {
290                 Hashtable _elements = new Hashtable ();
291                 ArrayList _elementMembers;
292                 Hashtable _attributeMembers;
293                 XmlTypeMapMemberAttribute[] _attributeMembersArray;
294                 XmlTypeMapElementInfo[] _elementsByIndex;
295                 ArrayList _flatLists;
296                 ArrayList _allMembers = new ArrayList ();
297                 ArrayList _membersWithDefault;
298                 ArrayList _listMembers;
299                 XmlTypeMapMemberAnyElement _defaultAnyElement;
300                 XmlTypeMapMemberAnyAttribute _defaultAnyAttribute;
301                 XmlTypeMapMemberNamespaces _namespaceDeclarations;
302                 XmlTypeMapMember _xmlTextCollector;
303                 XmlTypeMapMember _returnMember;
304                 bool _ignoreMemberNamespace;
305                 bool _canBeSimpleType = true;
306
307                 public void AddMember (XmlTypeMapMember member)
308                 {
309                         _allMembers.Add (member);
310                         
311                         if (!(member.DefaultValue is System.DBNull) && member.DefaultValue != null) {
312                                 if (_membersWithDefault == null) _membersWithDefault = new ArrayList ();
313                                 _membersWithDefault.Add (member);
314                         }
315                         
316                         if (member.IsReturnValue)
317                                 _returnMember = member;
318                         
319                         if (member is XmlTypeMapMemberAttribute)
320                         {
321                                 XmlTypeMapMemberAttribute atm = (XmlTypeMapMemberAttribute)member;
322                                 if (_attributeMembers == null) _attributeMembers = new Hashtable();
323                                 string key = BuildKey (atm.AttributeName, atm.Namespace);
324                                 if (_attributeMembers.ContainsKey (key))
325                                         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.");
326                                 member.Index = _attributeMembers.Count;
327                                 _attributeMembers.Add (key, member);
328                                 return;
329                         }
330                         else if (member is XmlTypeMapMemberFlatList)
331                         {
332                                 RegisterFlatList ((XmlTypeMapMemberFlatList)member);
333                         }
334                         else if (member is XmlTypeMapMemberAnyElement)
335                         {
336                                 XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement) member;
337                                 if (mem.IsDefaultAny) _defaultAnyElement = mem;
338                                 if (mem.TypeData.IsListType) RegisterFlatList (mem);
339                         }
340                         else if (member is XmlTypeMapMemberAnyAttribute)
341                         {
342                                 _defaultAnyAttribute = (XmlTypeMapMemberAnyAttribute) member;
343                                 return;
344                         }
345                         else if (member is XmlTypeMapMemberNamespaces)
346                         {
347                                 _namespaceDeclarations = (XmlTypeMapMemberNamespaces) member;
348                                 return;
349                         }
350
351                         if (member is XmlTypeMapMemberElement && ((XmlTypeMapMemberElement)member).IsXmlTextCollector)
352                         {
353                                 if (_xmlTextCollector != null) throw new InvalidOperationException ("XmlTextAttribute can only be applied once in a class");
354                                 _xmlTextCollector = member;
355                         }
356
357                         if (_elementMembers == null) {
358                                 _elementMembers = new ArrayList();
359                                 _elements = new Hashtable();
360                         }
361
362                         member.Index = _elementMembers.Count;
363                         _elementMembers.Add (member);
364
365                         ICollection elemsInfo = ((XmlTypeMapMemberElement)member).ElementInfo;
366                         foreach (XmlTypeMapElementInfo elem in elemsInfo)
367                         {
368                                 string key = BuildKey (elem.ElementName, elem.Namespace);
369                                 if (_elements.ContainsKey (key)) 
370                                         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.");
371                                 _elements.Add (key, elem);
372                         }
373                         
374                         if (member.TypeData.IsListType && member.TypeData.Type != null && !member.TypeData.Type.IsArray) {
375                                 if (_listMembers == null) _listMembers = new ArrayList ();
376                                 _listMembers.Add (member);
377                         }
378                 }
379
380                 void RegisterFlatList (XmlTypeMapMemberExpandable member)
381                 {
382                         if (_flatLists == null) _flatLists = new ArrayList ();
383                         member.FlatArrayIndex = _flatLists.Count;
384                         _flatLists.Add (member);
385                 }
386
387                 public XmlTypeMapMemberAttribute GetAttribute (string name, string ns)
388                 {
389                         if (_attributeMembers == null) return null;
390                         return (XmlTypeMapMemberAttribute)_attributeMembers [BuildKey(name,ns)];
391                 }
392
393                 public XmlTypeMapElementInfo GetElement (string name, string ns)
394                 {
395                         if (_elements == null) return null;
396                         return (XmlTypeMapElementInfo)_elements [BuildKey(name,ns)];
397                 }
398                 
399                 public XmlTypeMapElementInfo GetElement (int index)
400                 {
401                         if (_elements == null) return null;
402                         
403                         if (_elementsByIndex == null)
404                         {
405                                 _elementsByIndex = new XmlTypeMapElementInfo [_elementMembers.Count];
406                                 foreach (XmlTypeMapMemberElement mem in _elementMembers)
407                                 {
408                                         if (mem.ElementInfo.Count != 1) 
409                                                 throw new InvalidOperationException ("Read by order only possible for encoded/bare format");
410                                                 
411                                         _elementsByIndex [mem.Index] = (XmlTypeMapElementInfo) mem.ElementInfo [0];
412                                 }
413                         }
414                         
415                         return _elementsByIndex [index];
416                 }
417                 
418                 private string BuildKey (string name, string ns)
419                 {
420                         if (_ignoreMemberNamespace) return name;
421                         else return name + " / " + ns;
422                 }
423                 
424                 public ICollection AllElementInfos
425                 {
426                         get { return _elements.Values; }
427                 }
428                 
429                 
430                 public bool IgnoreMemberNamespace
431                 {
432                         get { return _ignoreMemberNamespace; }
433                         set { _ignoreMemberNamespace = value; }
434                 }
435
436                 public XmlTypeMapMember FindMember (string name)
437                 {
438                         for (int n=0; n<_allMembers.Count; n++)
439                                 if (((XmlTypeMapMember)_allMembers[n]).Name == name) return (XmlTypeMapMember)_allMembers[n];
440                         return null;
441                 }
442
443                 public XmlTypeMapMemberAnyElement DefaultAnyElementMember
444                 {
445                         get { return _defaultAnyElement; }
446                 }
447
448                 public XmlTypeMapMemberAnyAttribute DefaultAnyAttributeMember
449                 {
450                         get { return _defaultAnyAttribute; }
451                 }
452
453                 public XmlTypeMapMemberNamespaces NamespaceDeclarations
454                 {
455                         get { return _namespaceDeclarations; }
456                 }
457
458                 public ICollection AttributeMembers
459                 {
460                         get 
461                         {
462                                 if (_attributeMembers == null) return null;
463                                 if (_attributeMembersArray != null) return _attributeMembersArray;
464                                 
465                                 _attributeMembersArray = new XmlTypeMapMemberAttribute[_attributeMembers.Count];
466                                 foreach (XmlTypeMapMemberAttribute mem in _attributeMembers.Values)
467                                         _attributeMembersArray [mem.Index] = mem;
468                                 return _attributeMembersArray;
469                         }
470                 }
471
472                 public ICollection ElementMembers
473                 {
474                         get { return _elementMembers; }
475                 }
476
477                 public ArrayList AllMembers
478                 {
479                         get { return _allMembers; }
480                 }
481
482                 public ArrayList FlatLists
483                 {
484                         get { return _flatLists; }
485                 }
486                 
487                 public ArrayList MembersWithDefault
488                 {
489                         get { return _membersWithDefault; }
490                 }
491                 
492                 public ArrayList ListMembers
493                 {
494                         get { return _listMembers; }
495                 }
496
497                 public XmlTypeMapMember XmlTextCollector
498                 {
499                         get { return _xmlTextCollector; }
500                 }
501                 
502                 public XmlTypeMapMember ReturnMember
503                 {
504                         get { return _returnMember; }
505                 }
506
507                 public XmlQualifiedName SimpleContentBaseType
508                 {
509                         get
510                         {
511                                 if (!_canBeSimpleType || _elementMembers == null || _elementMembers.Count != 1) return null;
512                                 XmlTypeMapMemberElement member = (XmlTypeMapMemberElement) _elementMembers[0];
513                                 if (member.ElementInfo.Count != 1) return null;
514                                 XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
515                                 if (!einfo.IsTextElement) return null;
516                                 if (member.TypeData.SchemaType == SchemaTypes.Primitive || member.TypeData.SchemaType == SchemaTypes.Enum)
517                                         return new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace);
518                                 return null;
519                         }
520                 }
521                 
522                 public void SetCanBeSimpleType (bool can)
523                 {
524                         _canBeSimpleType = can;
525                 }
526
527                 public bool HasSimpleContent
528                 {
529                         get
530                         {
531                                 return SimpleContentBaseType != null;
532                         }
533                 }
534
535         }
536
537         // Mapping info for arrays and lists
538
539         internal class ListMap: ObjectMap
540         {
541                 XmlTypeMapElementInfoList _itemInfo;
542                 bool _gotNestedMapping;
543                 XmlTypeMapping _nestedArrayMapping;
544                 string _choiceMember;
545
546                 public bool IsMultiArray
547                 {
548                         get
549                         {
550                                 return (NestedArrayMapping != null);
551                         }
552                 }
553
554                 public string ChoiceMember
555                 {
556                         get { return _choiceMember; }
557                         set { _choiceMember = value; }
558                 }
559
560                 public XmlTypeMapping NestedArrayMapping
561                 {
562                         get
563                         {
564                                 if (_gotNestedMapping) return _nestedArrayMapping;
565                                 _gotNestedMapping = true;
566
567                                 _nestedArrayMapping = ((XmlTypeMapElementInfo)_itemInfo[0]).MappedType;
568
569                                 if (_nestedArrayMapping == null) return null;
570                                 
571                                 if (_nestedArrayMapping.TypeData.SchemaType != SchemaTypes.Array) {
572                                         _nestedArrayMapping = null; return null;
573                                 }
574
575                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
576                                         if (elem.MappedType != _nestedArrayMapping) {
577                                                 _nestedArrayMapping = null;
578                                                 return null;
579                                         }
580
581                                 return _nestedArrayMapping;
582                         }
583                 }
584
585                 public XmlTypeMapElementInfoList ItemInfo
586                 {
587
588                         get { return _itemInfo; }
589                         set { _itemInfo = value; }
590                 }
591
592                 public XmlTypeMapElementInfo FindElement (object ob, int index, object memberValue)
593                 {
594                         if (_itemInfo.Count == 1) 
595                                 return (XmlTypeMapElementInfo) _itemInfo[0];
596                         else if (_choiceMember != null && index != -1)
597                         {
598                                 Array values = (Array) XmlTypeMapMember.GetValue (ob, _choiceMember);
599                                 if (values == null || index >= values.Length)
600                                         throw new InvalidOperationException ("Invalid or missing choice enum value in member '" + _choiceMember + "'.");
601                                 object val = values.GetValue (index);
602                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
603                                         if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (val))
604                                                 return elem;
605                         }
606                         else
607                         {
608                                 if (memberValue == null) return null;
609                                 Type type = memberValue.GetType();
610                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
611                                         if (elem.TypeData.Type == type) return elem;
612                         }
613                         return null;
614                 }       
615
616                 public XmlTypeMapElementInfo FindElement (string elementName, string ns)
617                 {
618                         foreach (XmlTypeMapElementInfo elem in _itemInfo)
619                                 if (elem.ElementName == elementName && elem.Namespace == ns) return elem;
620                         return null;
621                 }
622                 
623                 public XmlTypeMapElementInfo FindTextElement ()
624                 {
625                         foreach (XmlTypeMapElementInfo elem in _itemInfo)
626                                 if (elem.IsTextElement) return elem;
627                         return null;
628                 }
629                 
630                 public string GetSchemaArrayName ()
631                 {
632                         XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) _itemInfo[0];
633                         if (einfo.MappedType != null) return TypeTranslator.GetArrayName (einfo.MappedType.XmlType);
634                         else return TypeTranslator.GetArrayName (einfo.TypeData.XmlType);
635                 }
636
637                 public void GetArrayType (int itemCount, out string localName, out string ns)
638                 {
639                         string arrayDim;
640                         if (itemCount != -1) arrayDim = "[" + itemCount + "]";
641                         else arrayDim = "[]";
642
643                         XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) _itemInfo[0];
644                         if (info.TypeData.SchemaType == SchemaTypes.Array)
645                         {
646                                 string nm;
647                                 ((ListMap)info.MappedType.ObjectMap).GetArrayType (-1, out nm, out ns);
648                                 localName = nm + arrayDim;
649                         }
650                         else 
651                         {
652                                 if (info.MappedType != null)
653                                 {
654                                         localName = info.MappedType.XmlType + arrayDim;
655                                         ns = info.MappedType.Namespace;
656                                 }
657                                 else 
658                                 {
659                                         localName = info.TypeData.XmlType + arrayDim;
660                                         ns = info.DataTypeNamespace;
661                                 }
662                         }
663                 }
664
665                 public override bool Equals (object other)
666                 {
667                         ListMap lmap = other as ListMap;
668                         if (lmap == null) return false;
669
670                         if (_itemInfo.Count != lmap._itemInfo.Count) return false;
671                         for (int n=0; n<_itemInfo.Count; n++)
672                                 if (!_itemInfo[n].Equals (lmap._itemInfo[n])) return false;
673                         return true;
674                 }
675
676                 public override int GetHashCode ()
677                 {
678                         return base.GetHashCode ();
679                 }
680         }
681
682         internal class EnumMap: ObjectMap
683         {
684                 readonly EnumMapMember[] _members;
685                 readonly bool _isFlags;
686                 readonly string[] _enumNames = null;
687                 readonly string[] _xmlNames = null;
688                 readonly long[] _values = null;
689
690                 public class EnumMapMember
691                 {
692                         readonly string _xmlName;
693                         readonly string _enumName;
694                         readonly long _value;
695                         string _documentation;
696
697                         public EnumMapMember (string xmlName, string enumName)
698                                 : this (xmlName, enumName, 0)
699                         {
700                         }
701
702                         public EnumMapMember (string xmlName, string enumName, long value)
703                         {
704                                 _xmlName = xmlName;
705                                 _enumName = enumName;
706                                 _value = value;
707                         }
708
709                         public string XmlName
710                         {
711                                 get { return _xmlName; }
712                         }
713
714                         public string EnumName
715                         {
716                                 get { return _enumName; }
717                         }
718
719                         public long Value
720                         {
721                                 get { return _value; }
722                         }
723
724                         public string Documentation
725                         {
726                                 get { return _documentation; }
727                                 set { _documentation = value; }
728                         }
729                 }
730
731                 public EnumMap (EnumMapMember[] members, bool isFlags)
732                 {
733                         _members = members;
734                         _isFlags = isFlags;
735
736                         _enumNames = new string[_members.Length];
737                         _xmlNames = new string[_members.Length];
738                         _values = new long[_members.Length];
739
740                         for (int i = 0; i < _members.Length; i++) {
741                                 EnumMapMember mem = _members[i];
742                                 _enumNames[i] = mem.EnumName;
743                                 _xmlNames[i] = mem.XmlName;
744                                 _values[i] = mem.Value;
745                         }
746                 }
747                 
748                 public bool IsFlags
749                 {
750                         get { return _isFlags; }
751                 }
752
753                 public EnumMapMember[] Members
754                 {
755                         get { return _members; }
756                 }
757
758                 public string[] EnumNames
759                 {
760                         get {
761                                 return _enumNames;
762                         }
763                 }
764
765                 public string[] XmlNames
766                 {
767                         get {
768                                 return _xmlNames;
769                         }
770                 }
771
772                 public long[] Values
773                 {
774                         get {
775                                 return _values;
776                         }
777                 }
778
779                 public string GetXmlName (string typeName, object enumValue)
780                 {
781                         if (enumValue is string) {
782                                 throw new InvalidCastException ();
783                         }
784
785                         long value = 0;
786
787                         try {
788                                 value = ((IConvertible) enumValue).ToInt64 (CultureInfo.CurrentCulture);
789                         } catch (FormatException) {
790                                 throw new InvalidCastException ();
791                         }
792
793                         for (int i = 0; i < Values.Length; i++) {
794                                 if (Values[i] == value)
795                                         return XmlNames[i];
796                         }
797
798                         if (IsFlags && value == 0)
799                                 return string.Empty;
800
801                         string xmlName = string.Empty;
802                         if (IsFlags) {
803 #if NET_2_0
804                                 xmlName = XmlCustomFormatter.FromEnum (value, XmlNames, Values, typeName);
805 #else
806                                 xmlName = XmlCustomFormatter.FromEnum (value, XmlNames, Values);
807 #endif
808                         }
809
810                         if (xmlName.Length == 0) {
811 #if NET_2_0
812                                 throw new InvalidOperationException (string.Format(CultureInfo.CurrentCulture,
813                                         "'{0}' is not a valid value for {1}.", value, typeName));
814 #else
815                                 return value.ToString (CultureInfo.InvariantCulture);
816 #endif
817                         }
818                         return xmlName;
819                 }
820
821                 public string GetEnumName (string typeName, string xmlName)
822                 {
823                         if (_isFlags) {
824                                 xmlName = xmlName.Trim ();
825                                 if (xmlName.Length == 0)
826                                         return "0";
827
828                                 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
829                                 string[] enumNames = xmlName.Split (null);
830                                 foreach (string name in enumNames) {
831                                         if (name == string.Empty) continue;
832                                         string foundEnumValue = null;
833                                         for (int i = 0; i < XmlNames.Length; i++)
834                                                 if (XmlNames[i] == name) {
835                                                         foundEnumValue = EnumNames[i];
836                                                         break;
837                                                 }
838
839                                         if (foundEnumValue != null) {
840                                                 if (sb.Length > 0)
841                                                         sb.Append (',');
842                                                 sb.Append (foundEnumValue);
843                                         } else {
844                                                 throw new InvalidOperationException (string.Format (CultureInfo.CurrentCulture,
845                                                         "'{0}' is not a valid value for {1}.", name, typeName));
846                                         }
847                                 }
848                                 return sb.ToString ();
849                         }
850
851                         foreach (EnumMapMember mem in _members)
852                                 if (mem.XmlName == xmlName) return mem.EnumName;
853                                 
854                         return null;
855                 }
856         }
857 }