BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.Globalization;
37 using System.Xml.Schema;
38
39 namespace System.Xml.Serialization
40 {
41         internal class TypeTranslator
42         {
43                 static Hashtable nameCache;
44                 static Hashtable primitiveTypes;
45                 static Hashtable primitiveArrayTypes;
46                 static Hashtable nullableTypes;
47
48 #if TARGET_JVM
49                 static readonly object AppDomain_TypeTranslatorCacheLock = new object ();
50                 const string AppDomain_nameCacheName = "System.Xml.Serialization.TypeTranslator.nameCache";
51                 const string AppDomain_nullableTypesName = "System.Xml.Serialization.TypeTranslator.nullableTypes";
52                 
53                 static Hashtable AppDomain_nameCache {
54                         get { return GetAppDomainCache (AppDomain_nameCacheName); }
55                 }
56
57                 static Hashtable AppDomain_nullableTypes {
58                         get { return GetAppDomainCache (AppDomain_nullableTypesName); }
59                 }
60
61                 static Hashtable GetAppDomainCache(string name) {
62                         Hashtable res = (Hashtable) AppDomain.CurrentDomain.GetData (name);
63
64                         if (res == null) {
65                                 lock (AppDomain_TypeTranslatorCacheLock) {
66                                         res = (Hashtable) AppDomain.CurrentDomain.GetData (name);
67                                         if (res == null) {
68                                                 res = Hashtable.Synchronized (new Hashtable ());
69                                                 AppDomain.CurrentDomain.SetData (name, res);
70                                         }
71                                 }
72                         }
73
74                         return res;
75                 }
76 #endif
77
78                 static TypeTranslator ()
79                 {
80                         nameCache = new Hashtable ();
81                         primitiveArrayTypes = Hashtable.Synchronized (new Hashtable ());
82
83 #if !TARGET_JVM
84                         nameCache = Hashtable.Synchronized (nameCache);
85 #endif
86                         // XSD Types with direct map to CLR types
87
88                         nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
89                         nameCache.Add (typeof (short), new TypeData (typeof (short), "short", true));
90                         nameCache.Add (typeof (ushort), new TypeData (typeof (ushort), "unsignedShort", true));
91                         nameCache.Add (typeof (int), new TypeData (typeof (int), "int", true));
92                         nameCache.Add (typeof (uint), new TypeData (typeof (uint), "unsignedInt", true));
93                         nameCache.Add (typeof (long), new TypeData (typeof (long), "long", true));
94                         nameCache.Add (typeof (ulong), new TypeData (typeof (ulong), "unsignedLong", true));
95                         nameCache.Add (typeof (float), new TypeData (typeof (float), "float", true));
96                         nameCache.Add (typeof (double), new TypeData (typeof (double), "double", true));
97                         nameCache.Add (typeof (DateTime), new TypeData (typeof (DateTime), "dateTime", true));  // TODO: timeInstant, Xml date, xml time
98                         nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
99                         nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
100                         nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
101 #if !MOONLIGHT
102                         XmlSchemaPatternFacet guidFacet = new XmlSchemaPatternFacet();
103                         guidFacet.Value = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
104                         nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true, (TypeData)nameCache[typeof (string)], guidFacet));
105 #endif
106                         nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
107                         nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
108                         nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true, (TypeData)nameCache[typeof (ushort)], null));
109                         nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
110                         nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
111 #if !MOONLIGHT
112                         nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
113                         nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
114 #endif
115
116                         primitiveTypes = new Hashtable();
117                         ICollection types = nameCache.Values;
118                         foreach (TypeData td in types)
119                                 primitiveTypes.Add (td.XmlType, td);
120
121                         // Additional XSD types
122
123                         primitiveTypes.Add ("date", new TypeData (typeof (DateTime), "date", true));    // TODO: timeInstant
124                         primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
125                         primitiveTypes.Add ("timePeriod", new TypeData (typeof (DateTime), "timePeriod", true));
126                         primitiveTypes.Add ("gDay", new TypeData (typeof (string), "gDay", true));
127                         primitiveTypes.Add ("gMonthDay", new TypeData (typeof (string), "gMonthDay", true));
128                         primitiveTypes.Add ("gYear", new TypeData (typeof (string), "gYear", true));
129                         primitiveTypes.Add ("gYearMonth", new TypeData (typeof (string), "gYearMonth", true));
130                         primitiveTypes.Add ("month", new TypeData (typeof (DateTime), "month", true));
131                         primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
132                         primitiveTypes.Add ("NMTOKENS", new TypeData (typeof (string), "NMTOKENS", true));
133                         primitiveTypes.Add ("Name", new TypeData (typeof (string), "Name", true));
134                         primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
135                         primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
136                         primitiveTypes.Add ("integer", new TypeData (typeof (string), "integer", true));
137                         primitiveTypes.Add ("positiveInteger", new TypeData (typeof (string), "positiveInteger", true));
138                         primitiveTypes.Add ("nonPositiveInteger", new TypeData (typeof (string), "nonPositiveInteger", true));
139                         primitiveTypes.Add ("negativeInteger", new TypeData (typeof (string), "negativeInteger", true));
140                         primitiveTypes.Add ("nonNegativeInteger", new TypeData (typeof (string), "nonNegativeInteger", true));
141                         primitiveTypes.Add ("ENTITIES", new TypeData (typeof (string), "ENTITIES", true));
142                         primitiveTypes.Add ("ENTITY", new TypeData (typeof (string), "ENTITY", true));
143                         primitiveTypes.Add ("hexBinary", new TypeData (typeof (byte[]), "hexBinary", true));
144                         primitiveTypes.Add ("ID", new TypeData (typeof (string), "ID", true));
145                         primitiveTypes.Add ("IDREF", new TypeData (typeof (string), "IDREF", true));
146                         primitiveTypes.Add ("IDREFS", new TypeData (typeof (string), "IDREFS", true));
147                         primitiveTypes.Add ("NOTATION", new TypeData (typeof (string), "NOTATION", true));
148                         primitiveTypes.Add ("token", new TypeData (typeof (string), "token", true));
149                         primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
150                         primitiveTypes.Add ("anyURI", new TypeData (typeof (string), "anyURI", true));
151                         primitiveTypes.Add ("base64", new TypeData (typeof (byte[]), "base64", true));
152                         primitiveTypes.Add ("duration", new TypeData (typeof (string), "duration", true));
153
154 #if NET_2_0
155                         nullableTypes = Hashtable.Synchronized(new Hashtable ());
156                         foreach (DictionaryEntry de in primitiveTypes) {
157                                 TypeData td = (TypeData) de.Value;
158                                 TypeData ntd = new TypeData (td.Type, td.XmlType, true);
159                                 ntd.IsNullable = true;
160                                 nullableTypes.Add (de.Key, ntd);
161                         }
162 #endif
163                 }
164
165                 public static TypeData GetTypeData (Type type)
166                 {
167                         return GetTypeData (type, null);
168                 }
169
170                 public static TypeData GetTypeData (Type runtimeType, string xmlDataType)
171                 {
172                         Type type = runtimeType;
173                         bool nullableOverride = false;
174 #if NET_2_0
175                         // Nullable<T> is serialized as T
176                         if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) {
177                                 nullableOverride = true;
178                                 type = type.GetGenericArguments () [0];
179                         }
180
181
182                         if ((xmlDataType != null) && (xmlDataType.Length != 0)) {
183                                 // If the type is an array, xmlDataType specifies the type for the array elements,
184                                 // not for the whole array. The exception is base64Binary, since it is a byte[],
185                                 // that's why the following check is needed.
186                                 TypeData at = GetPrimitiveTypeData (xmlDataType);
187                                 if (type.IsArray && type != at.Type) {
188                                                 TypeData tt = (TypeData) primitiveArrayTypes [xmlDataType];
189                                                 if (tt != null)
190                                                         return tt;
191                                                 if (at.Type == type.GetElementType ()) {
192                                                         tt = new TypeData (type, GetArrayName (at.XmlType), false);
193                                                         primitiveArrayTypes [xmlDataType] = tt;
194                                                         return tt;
195                                                 }
196                                                 else
197                                                         throw new InvalidOperationException ("Cannot convert values of type '" + type.GetElementType () + "' to '" + xmlDataType + "'");
198                                 }
199                                 if (nullableOverride){
200                                         TypeData tt = (TypeData) nullableTypes [at.XmlType];
201                                         if (tt == null){
202                                                 tt = new TypeData (type, at.XmlType, false);
203                                                 tt.IsNullable = true;
204                                                 nullableTypes [at.XmlType] = tt;
205                                         }
206                                         return tt;
207                                 }
208                                 return at;
209                         }
210
211                         if (nullableOverride){
212                                 TypeData pt = GetTypeData (type); // beware this recursive call btw ...
213                                 if (pt != null) {
214                                                 TypeData tt = (TypeData) nullableTypes [pt.XmlType];
215 #if TARGET_JVM
216                                                 if (tt == null)
217                                                         tt = (TypeData) AppDomain_nullableTypes [pt.XmlType];
218 #endif
219                                                 if (tt == null) {
220                                                         tt = new TypeData (type, pt.XmlType, false);
221                                                         tt.IsNullable = true;
222 #if TARGET_JVM
223                                                         AppDomain_nullableTypes [pt.XmlType] = tt;
224 #else
225                                                         nullableTypes [pt.XmlType] = tt;
226 #endif
227                                                 }
228                                                 return tt;
229                                 }
230                         }
231 #endif
232                         
233                                 TypeData typeData = nameCache[runtimeType] as TypeData;
234                                 if (typeData != null) return typeData;
235
236 #if TARGET_JVM
237                                 Hashtable dynamicCache = AppDomain_nameCache;
238                                 typeData = dynamicCache[runtimeType] as TypeData;
239                                 if (typeData != null) return typeData;
240 #endif
241
242                                 string name;
243                                 if (type.IsArray) {
244                                         string sufix = GetTypeData (type.GetElementType ()).XmlType;
245                                         name = GetArrayName (sufix);
246                                 }
247 #if NET_2_0
248                                 else if (type.IsGenericType && !type.IsGenericTypeDefinition) {
249                                         name = XmlConvert.EncodeLocalName (type.Name.Substring (0, type.Name.IndexOf ('`'))) + "Of";
250                                         foreach (Type garg in type.GetGenericArguments ())
251                                                 name += garg.IsArray || garg.IsGenericType ?
252                                                         GetTypeData (garg).XmlType :
253                                                         CodeIdentifier.MakePascal (XmlConvert.EncodeLocalName (garg.Name));
254                                 }
255 #endif
256                                 else 
257                                         name = XmlConvert.EncodeLocalName (type.Name);
258
259                                 typeData = new TypeData (type, name, false);
260                                 if (nullableOverride)
261                                         typeData.IsNullable = true;
262 #if TARGET_JVM
263                                 dynamicCache[runtimeType] = typeData;
264 #else
265                                 nameCache[runtimeType] = typeData;
266 #endif
267                                 return typeData;
268                 }
269
270                 public static bool IsPrimitive (Type type)
271                 {
272                         return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
273                 }
274
275                 public static TypeData GetPrimitiveTypeData (string typeName)
276                 {
277                         return GetPrimitiveTypeData (typeName, false);
278                 }
279
280                 public static TypeData GetPrimitiveTypeData (string typeName, bool nullable)
281                 {
282                         TypeData td = (TypeData) primitiveTypes [typeName];
283                         if (td != null && !td.Type.IsValueType)
284                                 return td;
285                         // for 1.x profile, 'nullableTypes' is null
286                         Hashtable table = nullable && nullableTypes != null ? nullableTypes : primitiveTypes;
287                         td = (TypeData) table [typeName];
288                         if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
289                         return td;
290                 }
291
292                 public static TypeData FindPrimitiveTypeData (string typeName)
293                 {
294                         return (TypeData) primitiveTypes[typeName];
295                 }
296
297                 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
298                 {
299                         // Returns the TypeData that is mapped by default to the clr type
300                         // that primType represents
301                         
302                         if (primType.SchemaType == SchemaTypes.Primitive)
303                         {
304                                 TypeData newPrim = GetTypeData (primType.Type, null);
305                                 if (newPrim != primType) return newPrim;
306                         }
307                         return primType;
308                 }
309
310                 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
311                 {
312                         return GetDefaultPrimitiveTypeData (primType) == primType;
313                 }
314
315                 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
316                 {
317                         TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
318                         return td;
319                 }
320
321                 public static string GetArrayName (string elemName)
322                 {
323                         return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
324                 }
325                 
326                 public static string GetArrayName (string elemName, int dimensions)
327                 {
328                         string aname = GetArrayName (elemName);
329                         for ( ; dimensions > 1; dimensions--)
330                                 aname = "ArrayOf" + aname;
331                         return aname;
332                 }
333                 
334                 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
335                 {
336                         int i = arrayType.LastIndexOf (":");
337                         if (i == -1) ns = "";
338                         else ns = arrayType.Substring (0,i);
339                         
340                         int j = arrayType.IndexOf ("[", i+1);
341                         if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
342                         type = arrayType.Substring (i+1, j-i-1);
343                         dimensions = arrayType.Substring (j);
344                 }
345         }
346 }