2004-06-18 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / TypeTranslator.cs
1 //
2 // System.Xml.Serialization.TypeTranslator
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Erik LeBel (eriklebel@yahoo.ca)
7 //  Lluis Sanchez Gual (lluis@ximian.com)
8 //
9 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Erik Lebel
11 //
12
13 using System;
14 using System.Collections;
15 using System.Globalization;
16
17 namespace System.Xml.Serialization
18 {
19         internal class TypeTranslator
20         {
21                 static Hashtable nameCache;
22                 static Hashtable primitiveTypes;
23
24                 static TypeTranslator ()
25                 {
26                         nameCache = new Hashtable ();
27
28                         // XSD Types with direct map to CLR types
29
30                         nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
31                         nameCache.Add (typeof (short), new TypeData (typeof (short), "short", true));
32                         nameCache.Add (typeof (ushort), new TypeData (typeof (ushort), "unsignedShort", true));
33                         nameCache.Add (typeof (int), new TypeData (typeof (int), "int", true));
34                         nameCache.Add (typeof (uint), new TypeData (typeof (uint), "unsignedInt", true));
35                         nameCache.Add (typeof (long), new TypeData (typeof (long), "long", true));
36                         nameCache.Add (typeof (ulong), new TypeData (typeof (ulong), "unsignedLong", true));
37                         nameCache.Add (typeof (float), new TypeData (typeof (float), "float", true));
38                         nameCache.Add (typeof (double), new TypeData (typeof (double), "double", true));
39                         nameCache.Add (typeof (DateTime), new TypeData (typeof (DateTime), "dateTime", true));  // TODO: timeInstant, Xml date, xml time
40                         nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true));
41                         nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
42                         nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
43                         nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
44                         nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
45                         nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
46                         nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true));
47                         nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
48                         nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
49                         nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
50                         nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
51                         nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
52
53                         primitiveTypes = new Hashtable();
54                         ICollection types = nameCache.Values;
55                         foreach (TypeData td in types)
56                                 primitiveTypes.Add (td.XmlType, td);
57
58                         // Additional XSD types
59
60                         primitiveTypes.Add ("date", new TypeData (typeof (DateTime), "date", true));    // TODO: timeInstant
61                         primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
62                         primitiveTypes.Add ("timePeriod", new TypeData (typeof (DateTime), "timePeriod", true));
63                         primitiveTypes.Add ("gDay", new TypeData (typeof (string), "gDay", true));
64                         primitiveTypes.Add ("gMonthDay", new TypeData (typeof (string), "gMonthDay", true));
65                         primitiveTypes.Add ("gYear", new TypeData (typeof (string), "gYear", true));
66                         primitiveTypes.Add ("gYearMonth", new TypeData (typeof (string), "gYearMonth", true));
67                         primitiveTypes.Add ("month", new TypeData (typeof (DateTime), "month", true));
68                         primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
69                         primitiveTypes.Add ("NMTOKENS", new TypeData (typeof (string), "NMTOKENS", true));
70                         primitiveTypes.Add ("Name", new TypeData (typeof (string), "Name", true));
71                         primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
72                         primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
73                         primitiveTypes.Add ("integer", new TypeData (typeof (string), "integer", true));
74                         primitiveTypes.Add ("positiveInteger", new TypeData (typeof (string), "positiveInteger", true));
75                         primitiveTypes.Add ("nonPositiveInteger", new TypeData (typeof (string), "nonPositiveInteger", true));
76                         primitiveTypes.Add ("negativeInteger", new TypeData (typeof (string), "negativeInteger", true));
77                         primitiveTypes.Add ("nonNegativeInteger", new TypeData (typeof (string), "nonNegativeInteger", true));
78                         primitiveTypes.Add ("ENTITIES", new TypeData (typeof (string), "ENTITIES", true));
79                         primitiveTypes.Add ("ENTITY", new TypeData (typeof (string), "ENTITY", true));
80                         primitiveTypes.Add ("hexBinary", new TypeData (typeof (byte[]), "hexBinary", true));
81                         primitiveTypes.Add ("ID", new TypeData (typeof (string), "ID", true));
82                         primitiveTypes.Add ("IDREF", new TypeData (typeof (string), "IDREF", true));
83                         primitiveTypes.Add ("IDREFS", new TypeData (typeof (string), "IDREFS", true));
84                         primitiveTypes.Add ("NOTATION", new TypeData (typeof (string), "NOTATION", true));
85                         primitiveTypes.Add ("token", new TypeData (typeof (string), "token", true));
86                         primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
87                         primitiveTypes.Add ("anyURI", new TypeData (typeof (string), "anyURI", true));
88                 }
89
90                 public static TypeData GetTypeData (Type type)
91                 {
92                         return GetTypeData (type, null);
93                 }
94
95                 public static TypeData GetTypeData (Type type, string xmlDataType)
96                 {
97                         if ((xmlDataType != null) && (xmlDataType.Length != 0)) return GetPrimitiveTypeData (xmlDataType);
98
99                         TypeData typeData = nameCache[type] as TypeData;
100                         if (typeData != null) return typeData;
101                         
102                         string name;
103                         if (type.IsArray) {
104                                 string sufix = GetTypeData (type.GetElementType ()).XmlType;
105                                 name = GetArrayName (sufix);
106                         }
107                         else 
108                                 name = type.Name;
109
110                         typeData = new TypeData (type, name, false);
111                         nameCache[type] = typeData;
112                         return typeData;
113                 }
114
115                 public static bool IsPrimitive (Type type)
116                 {
117                         return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
118                 }
119
120                 public static TypeData GetPrimitiveTypeData (string typeName)
121                 {
122                         TypeData td = (TypeData) primitiveTypes[typeName];
123                         if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
124                         return td;
125                 }
126
127                 public static TypeData FindPrimitiveTypeData (string typeName)
128                 {
129                         return (TypeData) primitiveTypes[typeName];
130                 }
131
132                 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
133                 {
134                         // Returns the TypeData that is mapped by default to the clr type
135                         // that primType represents
136                         
137                         if (primType.SchemaType == SchemaTypes.Primitive)
138                         {
139                                 TypeData newPrim = GetTypeData (primType.Type);
140                                 if (newPrim != primType) return newPrim;
141                         }
142                         return primType;
143                 }
144
145                 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
146                 {
147                         return GetDefaultPrimitiveTypeData (primType) == primType;
148                 }
149
150                 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
151                 {
152                         TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
153                         return td;
154                 }
155
156                 public static string GetArrayName (string elemName)
157                 {
158                         return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
159                 }
160                 
161                 public static string GetArrayName (string elemName, int dimensions)
162                 {
163                         string aname = GetArrayName (elemName);
164                         for ( ; dimensions > 1; dimensions--)
165                                 aname = "ArrayOf" + aname;
166                         return aname;
167                 }
168                 
169                 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
170                 {
171                         int i = arrayType.LastIndexOf (":");
172                         if (i == -1) ns = "";
173                         else ns = arrayType.Substring (0,i);
174                         
175                         int j = arrayType.IndexOf ("[", i+1);
176                         if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
177                         type = arrayType.Substring (i+1, j-i-1);
178                         dimensions = arrayType.Substring (j);
179                 }
180         }
181 }