svn path=/trunk/mcs/; revision=104772
[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
195                 XmlSchemaComplexType _schemaType;
196                 XmlQualifiedName _schemaTypeName;
197 #endif
198
199                 internal XmlSerializableMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
200                         : base(elementName, ns, typeData, xmlType, xmlTypeNamespace)
201                 {
202 #if NET_2_0
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 (_schemaTypeName.Name, 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                         IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type, true);
250 #if NET_2_0
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                         _schema = serializable.GetSchema();
258 #endif
259                         if (_schema != null) 
260                         {
261                                 if (_schema.Id == null || _schema.Id.Length == 0) 
262                                         throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id.");
263                         }
264                 }
265
266                 internal XmlSchema Schema
267                 {
268                         get { return _schema; }
269                 }
270
271 #if NET_2_0
272                 internal XmlSchemaType SchemaType {
273                         get { return _schemaType; }
274                 }
275
276                 internal XmlQualifiedName SchemaTypeName {
277                         get { return _schemaTypeName; }
278                 }
279 #endif
280         }
281  
282
283         // Mapping info for classes and structs
284
285         internal class ClassMap: ObjectMap
286         {
287                 Hashtable _elements = new Hashtable ();
288                 ArrayList _elementMembers;
289                 Hashtable _attributeMembers;
290                 XmlTypeMapMemberAttribute[] _attributeMembersArray;
291                 XmlTypeMapElementInfo[] _elementsByIndex;
292                 ArrayList _flatLists;
293                 ArrayList _allMembers = new ArrayList ();
294                 ArrayList _membersWithDefault;
295                 ArrayList _listMembers;
296                 XmlTypeMapMemberAnyElement _defaultAnyElement;
297                 XmlTypeMapMemberAnyAttribute _defaultAnyAttribute;
298                 XmlTypeMapMemberNamespaces _namespaceDeclarations;
299                 XmlTypeMapMember _xmlTextCollector;
300                 XmlTypeMapMember _returnMember;
301                 bool _ignoreMemberNamespace;
302                 bool _canBeSimpleType = true;
303
304                 public void AddMember (XmlTypeMapMember member)
305                 {
306                         member.GlobalIndex = _allMembers.Count;
307                         _allMembers.Add (member);
308                         
309                         if (!(member.DefaultValue is System.DBNull) && member.DefaultValue != null) {
310                                 if (_membersWithDefault == null) _membersWithDefault = new ArrayList ();
311                                 _membersWithDefault.Add (member);
312                         }
313                         
314                         if (member.IsReturnValue)
315                                 _returnMember = member;
316                         
317                         if (member is XmlTypeMapMemberAttribute)
318                         {
319                                 XmlTypeMapMemberAttribute atm = (XmlTypeMapMemberAttribute)member;
320                                 if (_attributeMembers == null) _attributeMembers = new Hashtable();
321                                 string key = BuildKey (atm.AttributeName, atm.Namespace);
322                                 if (_attributeMembers.ContainsKey (key))
323                                         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.");
324                                 member.Index = _attributeMembers.Count;
325                                 _attributeMembers.Add (key, member);
326                                 return;
327                         }
328                         else if (member is XmlTypeMapMemberFlatList)
329                         {
330                                 RegisterFlatList ((XmlTypeMapMemberFlatList)member);
331                         }
332                         else if (member is XmlTypeMapMemberAnyElement)
333                         {
334                                 XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement) member;
335                                 if (mem.IsDefaultAny) _defaultAnyElement = mem;
336                                 if (mem.TypeData.IsListType) RegisterFlatList (mem);
337                         }
338                         else if (member is XmlTypeMapMemberAnyAttribute)
339                         {
340                                 _defaultAnyAttribute = (XmlTypeMapMemberAnyAttribute) member;
341                                 return;
342                         }
343                         else if (member is XmlTypeMapMemberNamespaces)
344                         {
345                                 _namespaceDeclarations = (XmlTypeMapMemberNamespaces) member;
346                                 return;
347                         }
348
349                         if (member is XmlTypeMapMemberElement && ((XmlTypeMapMemberElement)member).IsXmlTextCollector)
350                         {
351                                 if (_xmlTextCollector != null) throw new InvalidOperationException ("XmlTextAttribute can only be applied once in a class");
352                                 _xmlTextCollector = member;
353                         }
354
355                         if (_elementMembers == null) {
356                                 _elementMembers = new ArrayList();
357                                 _elements = new Hashtable();
358                         }
359
360                         member.Index = _elementMembers.Count;
361                         _elementMembers.Add (member);
362
363                         ICollection elemsInfo = ((XmlTypeMapMemberElement)member).ElementInfo;
364                         foreach (XmlTypeMapElementInfo elem in elemsInfo)
365                         {
366                                 string key = BuildKey (elem.ElementName, elem.Namespace);
367                                 if (_elements.ContainsKey (key)) 
368                                         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.");
369                                 _elements.Add (key, elem);
370                         }
371                         
372                         if (member.TypeData.IsListType && member.TypeData.Type != null && !member.TypeData.Type.IsArray) {
373                                 if (_listMembers == null) _listMembers = new ArrayList ();
374                                 _listMembers.Add (member);
375                         }
376                 }
377
378                 void RegisterFlatList (XmlTypeMapMemberExpandable member)
379                 {
380                         if (_flatLists == null) _flatLists = new ArrayList ();
381                         member.FlatArrayIndex = _flatLists.Count;
382                         _flatLists.Add (member);
383                 }
384
385                 public XmlTypeMapMemberAttribute GetAttribute (string name, string ns)
386                 {
387                         if (_attributeMembers == null) return null;
388                         return (XmlTypeMapMemberAttribute)_attributeMembers [BuildKey(name,ns)];
389                 }
390
391                 public XmlTypeMapElementInfo GetElement (string name, string ns)
392                 {
393                         if (_elements == null) return null;
394                         return (XmlTypeMapElementInfo)_elements [BuildKey(name,ns)];
395                 }
396                 
397                 public XmlTypeMapElementInfo GetElement (int index)
398                 {
399                         if (_elements == null) return null;
400                         
401                         if (_elementsByIndex == null)
402                         {
403                                 _elementsByIndex = new XmlTypeMapElementInfo [_elementMembers.Count];
404                                 foreach (XmlTypeMapMemberElement mem in _elementMembers)
405                                 {
406                                         if (mem.ElementInfo.Count != 1) 
407                                                 throw new InvalidOperationException ("Read by order only possible for encoded/bare format");
408                                                 
409                                         _elementsByIndex [mem.Index] = (XmlTypeMapElementInfo) mem.ElementInfo [0];
410                                 }
411                         }
412                         
413                         return _elementsByIndex [index];
414                 }
415                 
416                 private string BuildKey (string name, string ns)
417                 {
418                         if (_ignoreMemberNamespace) return name;
419                         else return name + " / " + ns;
420                 }
421                 
422                 public ICollection AllElementInfos
423                 {
424                         get { return _elements.Values; }
425                 }
426                 
427                 
428                 public bool IgnoreMemberNamespace
429                 {
430                         get { return _ignoreMemberNamespace; }
431                         set { _ignoreMemberNamespace = value; }
432                 }
433
434                 public XmlTypeMapMember FindMember (string name)
435                 {
436                         for (int n=0; n<_allMembers.Count; n++)
437                                 if (((XmlTypeMapMember)_allMembers[n]).Name == name) return (XmlTypeMapMember)_allMembers[n];
438                         return null;
439                 }
440
441                 public XmlTypeMapMemberAnyElement DefaultAnyElementMember
442                 {
443                         get { return _defaultAnyElement; }
444                 }
445
446                 public XmlTypeMapMemberAnyAttribute DefaultAnyAttributeMember
447                 {
448                         get { return _defaultAnyAttribute; }
449                 }
450
451                 public XmlTypeMapMemberNamespaces NamespaceDeclarations
452                 {
453                         get { return _namespaceDeclarations; }
454                 }
455
456                 public ICollection AttributeMembers
457                 {
458                         get 
459                         {
460                                 if (_attributeMembers == null) return null;
461                                 if (_attributeMembersArray != null) return _attributeMembersArray;
462                                 
463                                 _attributeMembersArray = new XmlTypeMapMemberAttribute[_attributeMembers.Count];
464                                 foreach (XmlTypeMapMemberAttribute mem in _attributeMembers.Values)
465                                         _attributeMembersArray [mem.Index] = mem;
466                                 return _attributeMembersArray;
467                         }
468                 }
469
470                 public ICollection ElementMembers
471                 {
472                         get { return _elementMembers; }
473                 }
474
475                 public ArrayList AllMembers
476                 {
477                         get { return _allMembers; }
478                 }
479
480                 public ArrayList FlatLists
481                 {
482                         get { return _flatLists; }
483                 }
484                 
485                 public ArrayList MembersWithDefault
486                 {
487                         get { return _membersWithDefault; }
488                 }
489                 
490                 public ArrayList ListMembers
491                 {
492                         get { return _listMembers; }
493                 }
494
495                 public XmlTypeMapMember XmlTextCollector
496                 {
497                         get { return _xmlTextCollector; }
498                 }
499                 
500                 public XmlTypeMapMember ReturnMember
501                 {
502                         get { return _returnMember; }
503                 }
504
505                 public XmlQualifiedName SimpleContentBaseType
506                 {
507                         get
508                         {
509                                 if (!_canBeSimpleType || _elementMembers == null || _elementMembers.Count != 1) return null;
510                                 XmlTypeMapMemberElement member = (XmlTypeMapMemberElement) _elementMembers[0];
511                                 if (member.ElementInfo.Count != 1) return null;
512                                 XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
513                                 if (!einfo.IsTextElement) return null;
514                                 if (member.TypeData.SchemaType == SchemaTypes.Primitive || member.TypeData.SchemaType == SchemaTypes.Enum)
515                                         return new XmlQualifiedName (einfo.TypeData.XmlType, einfo.DataTypeNamespace);
516                                 return null;
517                         }
518                 }
519                 
520                 public void SetCanBeSimpleType (bool can)
521                 {
522                         _canBeSimpleType = can;
523                 }
524
525                 public bool HasSimpleContent
526                 {
527                         get
528                         {
529                                 return SimpleContentBaseType != null;
530                         }
531                 }
532
533         }
534
535         // Mapping info for arrays and lists
536
537         internal class ListMap: ObjectMap
538         {
539                 XmlTypeMapElementInfoList _itemInfo;
540                 bool _gotNestedMapping;
541                 XmlTypeMapping _nestedArrayMapping;
542                 string _choiceMember;
543
544                 public bool IsMultiArray
545                 {
546                         get
547                         {
548                                 return (NestedArrayMapping != null);
549                         }
550                 }
551
552                 public string ChoiceMember
553                 {
554                         get { return _choiceMember; }
555                         set { _choiceMember = value; }
556                 }
557
558                 public XmlTypeMapping NestedArrayMapping
559                 {
560                         get
561                         {
562                                 if (_gotNestedMapping) return _nestedArrayMapping;
563                                 _gotNestedMapping = true;
564
565                                 _nestedArrayMapping = ((XmlTypeMapElementInfo)_itemInfo[0]).MappedType;
566
567                                 if (_nestedArrayMapping == null) return null;
568                                 
569                                 if (_nestedArrayMapping.TypeData.SchemaType != SchemaTypes.Array) {
570                                         _nestedArrayMapping = null; return null;
571                                 }
572
573                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
574                                         if (elem.MappedType != _nestedArrayMapping) {
575                                                 _nestedArrayMapping = null;
576                                                 return null;
577                                         }
578
579                                 return _nestedArrayMapping;
580                         }
581                 }
582
583                 public XmlTypeMapElementInfoList ItemInfo
584                 {
585
586                         get { return _itemInfo; }
587                         set { _itemInfo = value; }
588                 }
589
590                 public XmlTypeMapElementInfo FindElement (object ob, int index, object memberValue)
591                 {
592                         if (_itemInfo.Count == 1) 
593                                 return (XmlTypeMapElementInfo) _itemInfo[0];
594                         else if (_choiceMember != null && index != -1)
595                         {
596                                 Array values = (Array) XmlTypeMapMember.GetValue (ob, _choiceMember);
597                                 if (values == null || index >= values.Length)
598                                         throw new InvalidOperationException ("Invalid or missing choice enum value in member '" + _choiceMember + "'.");
599                                 object val = values.GetValue (index);
600                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
601                                         if (elem.ChoiceValue != null && elem.ChoiceValue.Equals (val))
602                                                 return elem;
603                         }
604                         else
605                         {
606                                 if (memberValue == null) return null;
607                                 Type type = memberValue.GetType();
608                                 foreach (XmlTypeMapElementInfo elem in _itemInfo)
609                                         if (elem.TypeData.Type == type) return elem;
610                         }
611                         return null;
612                 }       
613
614                 public XmlTypeMapElementInfo FindElement (string elementName, string ns)
615                 {
616                         foreach (XmlTypeMapElementInfo elem in _itemInfo)
617                                 if (elem.ElementName == elementName && elem.Namespace == ns) return elem;
618                         return null;
619                 }
620                 
621                 public XmlTypeMapElementInfo FindTextElement ()
622                 {
623                         foreach (XmlTypeMapElementInfo elem in _itemInfo)
624                                 if (elem.IsTextElement) return elem;
625                         return null;
626                 }
627                 
628                 public string GetSchemaArrayName ()
629                 {
630                         XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) _itemInfo[0];
631                         if (einfo.MappedType != null) return TypeTranslator.GetArrayName (einfo.MappedType.XmlType);
632                         else return TypeTranslator.GetArrayName (einfo.TypeData.XmlType);
633                 }
634
635                 public void GetArrayType (int itemCount, out string localName, out string ns)
636                 {
637                         string arrayDim;
638                         if (itemCount != -1) arrayDim = "[" + itemCount + "]";
639                         else arrayDim = "[]";
640
641                         XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) _itemInfo[0];
642                         if (info.TypeData.SchemaType == SchemaTypes.Array)
643                         {
644                                 string nm;
645                                 ((ListMap)info.MappedType.ObjectMap).GetArrayType (-1, out nm, out ns);
646                                 localName = nm + arrayDim;
647                         }
648                         else 
649                         {
650                                 if (info.MappedType != null)
651                                 {
652                                         localName = info.MappedType.XmlType + arrayDim;
653                                         ns = info.MappedType.Namespace;
654                                 }
655                                 else 
656                                 {
657                                         localName = info.TypeData.XmlType + arrayDim;
658                                         ns = info.DataTypeNamespace;
659                                 }
660                         }
661                 }
662
663                 public override bool Equals (object other)
664                 {
665                         ListMap lmap = other as ListMap;
666                         if (lmap == null) return false;
667
668                         if (_itemInfo.Count != lmap._itemInfo.Count) return false;
669                         for (int n=0; n<_itemInfo.Count; n++)
670                                 if (!_itemInfo[n].Equals (lmap._itemInfo[n])) return false;
671                         return true;
672                 }
673
674                 public override int GetHashCode ()
675                 {
676                         return base.GetHashCode ();
677                 }
678         }
679
680         internal class EnumMap: ObjectMap
681         {
682                 readonly EnumMapMember[] _members;
683                 readonly bool _isFlags;
684                 readonly string[] _enumNames = null;
685                 readonly string[] _xmlNames = null;
686                 readonly long[] _values = null;
687
688                 public class EnumMapMember
689                 {
690                         readonly string _xmlName;
691                         readonly string _enumName;
692                         readonly long _value;
693                         string _documentation;
694
695                         public EnumMapMember (string xmlName, string enumName)
696                                 : this (xmlName, enumName, 0)
697                         {
698                         }
699
700                         public EnumMapMember (string xmlName, string enumName, long value)
701                         {
702                                 _xmlName = xmlName;
703                                 _enumName = enumName;
704                                 _value = value;
705                         }
706
707                         public string XmlName
708                         {
709                                 get { return _xmlName; }
710                         }
711
712                         public string EnumName
713                         {
714                                 get { return _enumName; }
715                         }
716
717                         public long Value
718                         {
719                                 get { return _value; }
720                         }
721
722                         public string Documentation
723                         {
724                                 get { return _documentation; }
725                                 set { _documentation = value; }
726                         }
727                 }
728
729                 public EnumMap (EnumMapMember[] members, bool isFlags)
730                 {
731                         _members = members;
732                         _isFlags = isFlags;
733
734                         _enumNames = new string[_members.Length];
735                         _xmlNames = new string[_members.Length];
736                         _values = new long[_members.Length];
737
738                         for (int i = 0; i < _members.Length; i++) {
739                                 EnumMapMember mem = _members[i];
740                                 _enumNames[i] = mem.EnumName;
741                                 _xmlNames[i] = mem.XmlName;
742                                 _values[i] = mem.Value;
743                         }
744                 }
745                 
746                 public bool IsFlags
747                 {
748                         get { return _isFlags; }
749                 }
750
751                 public EnumMapMember[] Members
752                 {
753                         get { return _members; }
754                 }
755
756                 public string[] EnumNames
757                 {
758                         get {
759                                 return _enumNames;
760                         }
761                 }
762
763                 public string[] XmlNames
764                 {
765                         get {
766                                 return _xmlNames;
767                         }
768                 }
769
770                 public long[] Values
771                 {
772                         get {
773                                 return _values;
774                         }
775                 }
776
777                 public string GetXmlName (string typeName, object enumValue)
778                 {
779                         if (enumValue is string) {
780                                 throw new InvalidCastException ();
781                         }
782
783                         long value = 0;
784
785                         try {
786                                 value = ((IConvertible) enumValue).ToInt64 (CultureInfo.CurrentCulture);
787                         } catch (FormatException) {
788                                 throw new InvalidCastException ();
789                         }
790
791                         for (int i = 0; i < Values.Length; i++) {
792                                 if (Values[i] == value)
793                                         return XmlNames[i];
794                         }
795
796                         if (IsFlags && value == 0)
797                                 return string.Empty;
798
799                         string xmlName = string.Empty;
800                         if (IsFlags) {
801 #if NET_2_0
802                                 xmlName = XmlCustomFormatter.FromEnum (value, XmlNames, Values, typeName);
803 #else
804                                 xmlName = XmlCustomFormatter.FromEnum (value, XmlNames, Values);
805 #endif
806                         }
807
808                         if (xmlName.Length == 0) {
809 #if NET_2_0
810                                 throw new InvalidOperationException (string.Format(CultureInfo.CurrentCulture,
811                                         "'{0}' is not a valid value for {1}.", value, typeName));
812 #else
813                                 return value.ToString (CultureInfo.InvariantCulture);
814 #endif
815                         }
816                         return xmlName;
817                 }
818
819                 public string GetEnumName (string typeName, string xmlName)
820                 {
821                         if (_isFlags) {
822                                 xmlName = xmlName.Trim ();
823                                 if (xmlName.Length == 0)
824                                         return "0";
825
826                                 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
827                                 string[] enumNames = xmlName.Split (null);
828                                 foreach (string name in enumNames) {
829                                         if (name == string.Empty) continue;
830                                         string foundEnumValue = null;
831                                         for (int i = 0; i < XmlNames.Length; i++)
832                                                 if (XmlNames[i] == name) {
833                                                         foundEnumValue = EnumNames[i];
834                                                         break;
835                                                 }
836
837                                         if (foundEnumValue != null) {
838                                                 if (sb.Length > 0)
839                                                         sb.Append (',');
840                                                 sb.Append (foundEnumValue);
841                                         } else {
842                                                 throw new InvalidOperationException (string.Format (CultureInfo.CurrentCulture,
843                                                         "'{0}' is not a valid value for {1}.", name, typeName));
844                                         }
845                                 }
846                                 return sb.ToString ();
847                         }
848
849                         foreach (EnumMapMember mem in _members)
850                                 if (mem.XmlName == xmlName) return mem.EnumName;
851                                 
852                         return null;
853                 }
854         }
855 }