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