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