Merge pull request #439 from mono-soc-2012/garyb/iconfix
[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, bool underlyingEnumType = false)
171                 {
172                         if (underlyingEnumType && runtimeType.IsEnum)
173                                 runtimeType = Enum.GetUnderlyingType (runtimeType);
174
175                         Type type = runtimeType;
176                         bool nullableOverride = false;
177 #if NET_2_0
178                         // Nullable<T> is serialized as T
179                         if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) {
180                                 nullableOverride = true;
181                                 type = type.GetGenericArguments () [0];
182                         }
183
184
185                         if ((xmlDataType != null) && (xmlDataType.Length != 0)) {
186                                 // If the type is an array, xmlDataType specifies the type for the array elements,
187                                 // not for the whole array. The exception is base64Binary, since it is a byte[],
188                                 // that's why the following check is needed.
189                                 TypeData at = GetPrimitiveTypeData (xmlDataType);
190                                 if (type.IsArray && type != at.Type) {
191                                                 TypeData tt = (TypeData) primitiveArrayTypes [xmlDataType];
192                                                 if (tt != null)
193                                                         return tt;
194                                                 if (at.Type == type.GetElementType ()) {
195                                                         tt = new TypeData (type, GetArrayName (at.XmlType), false);
196                                                         primitiveArrayTypes [xmlDataType] = tt;
197                                                         return tt;
198                                                 }
199                                                 else
200                                                         throw new InvalidOperationException ("Cannot convert values of type '" + type.GetElementType () + "' to '" + xmlDataType + "'");
201                                 }
202                                 if (nullableOverride){
203                                         TypeData tt = (TypeData) nullableTypes [at.XmlType];
204                                         if (tt == null){
205                                                 tt = new TypeData (type, at.XmlType, false);
206                                                 tt.IsNullable = true;
207                                                 nullableTypes [at.XmlType] = tt;
208                                         }
209                                         return tt;
210                                 }
211                                 return at;
212                         }
213
214                         if (nullableOverride){
215                                 TypeData pt = GetTypeData (type); // beware this recursive call btw ...
216                                 if (pt != null) {
217                                                 TypeData tt = (TypeData) nullableTypes [pt.XmlType];
218 #if TARGET_JVM
219                                                 if (tt == null)
220                                                         tt = (TypeData) AppDomain_nullableTypes [pt.XmlType];
221 #endif
222                                                 if (tt == null) {
223                                                         tt = new TypeData (type, pt.XmlType, false);
224                                                         tt.IsNullable = true;
225 #if TARGET_JVM
226                                                         AppDomain_nullableTypes [pt.XmlType] = tt;
227 #else
228                                                         nullableTypes [pt.XmlType] = tt;
229 #endif
230                                                 }
231                                                 return tt;
232                                 }
233                         }
234 #endif
235                         
236                                 TypeData typeData = nameCache[runtimeType] as TypeData;
237                                 if (typeData != null) return typeData;
238
239 #if TARGET_JVM
240                                 Hashtable dynamicCache = AppDomain_nameCache;
241                                 typeData = dynamicCache[runtimeType] as TypeData;
242                                 if (typeData != null) return typeData;
243 #endif
244
245                                 string name;
246                                 if (type.IsArray) {
247                                         string sufix = GetTypeData (type.GetElementType ()).XmlType;
248                                         name = GetArrayName (sufix);
249                                 }
250 #if NET_2_0
251                                 else if (type.IsGenericType && !type.IsGenericTypeDefinition) {
252                                         name = XmlConvert.EncodeLocalName (type.Name.Substring (0, type.Name.IndexOf ('`'))) + "Of";
253                                         foreach (Type garg in type.GetGenericArguments ())
254                                                 name += garg.IsArray || garg.IsGenericType ?
255                                                         GetTypeData (garg).XmlType :
256                                                         CodeIdentifier.MakePascal (XmlConvert.EncodeLocalName (garg.Name));
257                                 }
258 #endif
259                                 else 
260                                         name = XmlConvert.EncodeLocalName (type.Name);
261
262                                 typeData = new TypeData (type, name, false);
263                                 if (nullableOverride)
264                                         typeData.IsNullable = true;
265 #if TARGET_JVM
266                                 dynamicCache[runtimeType] = typeData;
267 #else
268                                 nameCache[runtimeType] = typeData;
269 #endif
270                                 return typeData;
271                 }
272
273                 public static bool IsPrimitive (Type type)
274                 {
275                         return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
276                 }
277
278                 public static TypeData GetPrimitiveTypeData (string typeName)
279                 {
280                         return GetPrimitiveTypeData (typeName, false);
281                 }
282
283                 public static TypeData GetPrimitiveTypeData (string typeName, bool nullable)
284                 {
285                         TypeData td = (TypeData) primitiveTypes [typeName];
286                         if (td != null && !td.Type.IsValueType)
287                                 return td;
288                         // for 1.x profile, 'nullableTypes' is null
289                         Hashtable table = nullable && nullableTypes != null ? nullableTypes : primitiveTypes;
290                         td = (TypeData) table [typeName];
291                         if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
292                         return td;
293                 }
294
295                 public static TypeData FindPrimitiveTypeData (string typeName)
296                 {
297                         return (TypeData) primitiveTypes[typeName];
298                 }
299
300                 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
301                 {
302                         // Returns the TypeData that is mapped by default to the clr type
303                         // that primType represents
304                         
305                         if (primType.SchemaType == SchemaTypes.Primitive)
306                         {
307                                 TypeData newPrim = GetTypeData (primType.Type, null);
308                                 if (newPrim != primType) return newPrim;
309                         }
310                         return primType;
311                 }
312
313                 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
314                 {
315                         return GetDefaultPrimitiveTypeData (primType) == primType;
316                 }
317
318                 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
319                 {
320                         TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
321                         return td;
322                 }
323
324                 public static string GetArrayName (string elemName)
325                 {
326                         return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
327                 }
328                 
329                 public static string GetArrayName (string elemName, int dimensions)
330                 {
331                         string aname = GetArrayName (elemName);
332                         for ( ; dimensions > 1; dimensions--)
333                                 aname = "ArrayOf" + aname;
334                         return aname;
335                 }
336                 
337                 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
338                 {
339                         int i = arrayType.LastIndexOf (":");
340                         if (i == -1) ns = "";
341                         else ns = arrayType.Substring (0,i);
342                         
343                         int j = arrayType.IndexOf ("[", i+1);
344                         if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
345                         type = arrayType.Substring (i+1, j-i-1);
346                         dimensions = arrayType.Substring (j);
347                 }
348         }
349 }