Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / TypeTranslator.cs
index 2f87760bb7d42f6f39e1f48e491edc742f1afbb4..49cb789696dddf46a2da94a91f941a2356def1d9 100644 (file)
@@ -43,34 +43,46 @@ namespace System.Xml.Serialization
                static Hashtable nameCache;
                static Hashtable primitiveTypes;
                static Hashtable primitiveArrayTypes;
-               static Hashtable primitiveNullableTypes;
+               static Hashtable nullableTypes;
 
 #if TARGET_JVM
-               const string AppDomainCacheName = "System.Xml.Serialization.TypeTranslator.AppDomainCache";
-               static Hashtable AppDomainCache {
-                       get {
-                               Hashtable res = (Hashtable)AppDomain.CurrentDomain.GetData(AppDomainCacheName);
-
-                               if(res == null) {
-                                       lock(AppDomainCacheName) {
-                                               res = (Hashtable)AppDomain.CurrentDomain.GetData(AppDomainCacheName);
-                                               if (res == null) {
-                                                       res = new Hashtable();
-                                                       AppDomain.CurrentDomain.SetData(AppDomainCacheName, res);
-                                               }
+               static readonly object AppDomain_TypeTranslatorCacheLock = new object ();
+               const string AppDomain_nameCacheName = "System.Xml.Serialization.TypeTranslator.nameCache";
+               const string AppDomain_nullableTypesName = "System.Xml.Serialization.TypeTranslator.nullableTypes";
+               
+               static Hashtable AppDomain_nameCache {
+                       get { return GetAppDomainCache (AppDomain_nameCacheName); }
+               }
+
+               static Hashtable AppDomain_nullableTypes {
+                       get { return GetAppDomainCache (AppDomain_nullableTypesName); }
+               }
+
+               static Hashtable GetAppDomainCache(string name) {
+                       Hashtable res = (Hashtable) AppDomain.CurrentDomain.GetData (name);
+
+                       if (res == null) {
+                               lock (AppDomain_TypeTranslatorCacheLock) {
+                                       res = (Hashtable) AppDomain.CurrentDomain.GetData (name);
+                                       if (res == null) {
+                                               res = Hashtable.Synchronized (new Hashtable ());
+                                               AppDomain.CurrentDomain.SetData (name, res);
                                        }
                                }
-
-                               return res;
                        }
+
+                       return res;
                }
 #endif
 
                static TypeTranslator ()
                {
                        nameCache = new Hashtable ();
-                       primitiveArrayTypes = new Hashtable ();
+                       primitiveArrayTypes = Hashtable.Synchronized (new Hashtable ());
 
+#if !TARGET_JVM
+                       nameCache = Hashtable.Synchronized (nameCache);
+#endif
                        // XSD Types with direct map to CLR types
 
                        nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
@@ -86,17 +98,20 @@ namespace System.Xml.Serialization
                        nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
                        nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
                        nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
+#if !MOONLIGHT
                        XmlSchemaPatternFacet guidFacet = new XmlSchemaPatternFacet();
                        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}";
                        nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true, (TypeData)nameCache[typeof (string)], guidFacet));
+#endif
                        nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
                        nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
                        nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true, (TypeData)nameCache[typeof (ushort)], null));
                        nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
                        nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
+#if !MOONLIGHT
                        nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
                        nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
-                       nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
+#endif
 
                        primitiveTypes = new Hashtable();
                        ICollection types = nameCache.Values;
@@ -134,14 +149,15 @@ namespace System.Xml.Serialization
                        primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
                        primitiveTypes.Add ("anyURI", new TypeData (typeof (string), "anyURI", true));
                        primitiveTypes.Add ("base64", new TypeData (typeof (byte[]), "base64", true));
+                       primitiveTypes.Add ("duration", new TypeData (typeof (string), "duration", true));
 
 #if NET_2_0
-                       primitiveNullableTypes = new Hashtable ();
+                       nullableTypes = Hashtable.Synchronized(new Hashtable ());
                        foreach (DictionaryEntry de in primitiveTypes) {
                                TypeData td = (TypeData) de.Value;
                                TypeData ntd = new TypeData (td.Type, td.XmlType, true);
-                               td.IsGenericNullable = true;
-                               primitiveNullableTypes.Add (de.Key, td);
+                               ntd.IsNullable = true;
+                               nullableTypes.Add (de.Key, ntd);
                        }
 #endif
                }
@@ -151,30 +167,20 @@ namespace System.Xml.Serialization
                        return GetTypeData (type, null);
                }
 
-               public static TypeData GetTypeData (Type runtimeType, string xmlDataType)
+               public static TypeData GetTypeData (Type runtimeType, string xmlDataType, bool underlyingEnumType = false)
                {
-                       Type type = runtimeType;
-                       bool isNullableRuntimeType = false;
+                       if (underlyingEnumType && runtimeType.IsEnum)
+                               runtimeType = Enum.GetUnderlyingType (runtimeType);
 
+                       Type type = runtimeType;
+                       bool nullableOverride = false;
 #if NET_2_0
                        // Nullable<T> is serialized as T
                        if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) {
-                               isNullableRuntimeType = true;
+                               nullableOverride = true;
                                type = type.GetGenericArguments () [0];
-
-                               TypeData pt = GetTypeData (type); // beware this recursive call btw ...
-                               if (pt != null) {
-                                       lock (primitiveNullableTypes) {
-                                               TypeData tt = (TypeData) primitiveNullableTypes [pt.XmlType];
-                                               if (tt == null) {
-                                                       tt = new TypeData (type, pt.XmlType, true);
-                                                       primitiveNullableTypes [pt.XmlType] = tt;
-                                               }
-                                               return tt;
-                                       }
-                               }
                        }
-#endif
+
 
                        if ((xmlDataType != null) && (xmlDataType.Length != 0)) {
                                // If the type is an array, xmlDataType specifies the type for the array elements,
@@ -182,7 +188,6 @@ namespace System.Xml.Serialization
                                // that's why the following check is needed.
                                TypeData at = GetPrimitiveTypeData (xmlDataType);
                                if (type.IsArray && type != at.Type) {
-                                       lock (primitiveArrayTypes) {
                                                TypeData tt = (TypeData) primitiveArrayTypes [xmlDataType];
                                                if (tt != null)
                                                        return tt;
@@ -193,39 +198,76 @@ namespace System.Xml.Serialization
                                                }
                                                else
                                                        throw new InvalidOperationException ("Cannot convert values of type '" + type.GetElementType () + "' to '" + xmlDataType + "'");
+                               }
+                               if (nullableOverride){
+                                       TypeData tt = (TypeData) nullableTypes [at.XmlType];
+                                       if (tt == null){
+                                               tt = new TypeData (type, at.XmlType, false);
+                                               tt.IsNullable = true;
+                                               nullableTypes [at.XmlType] = tt;
                                        }
+                                       return tt;
                                }
                                return at;
                        }
 
-                       lock (nameCache) {
+                       if (nullableOverride){
+                               TypeData pt = GetTypeData (type); // beware this recursive call btw ...
+                               if (pt != null) {
+                                               TypeData tt = (TypeData) nullableTypes [pt.XmlType];
+#if TARGET_JVM
+                                               if (tt == null)
+                                                       tt = (TypeData) AppDomain_nullableTypes [pt.XmlType];
+#endif
+                                               if (tt == null) {
+                                                       tt = new TypeData (type, pt.XmlType, false);
+                                                       tt.IsNullable = true;
+#if TARGET_JVM
+                                                       AppDomain_nullableTypes [pt.XmlType] = tt;
+#else
+                                                       nullableTypes [pt.XmlType] = tt;
+#endif
+                                               }
+                                               return tt;
+                               }
+                       }
+#endif
+                       
                                TypeData typeData = nameCache[runtimeType] as TypeData;
                                if (typeData != null) return typeData;
 
 #if TARGET_JVM
-                               Hashtable dynamicCache = AppDomainCache;
+                               Hashtable dynamicCache = AppDomain_nameCache;
                                typeData = dynamicCache[runtimeType] as TypeData;
                                if (typeData != null) return typeData;
 #endif
-                               
+
                                string name;
                                if (type.IsArray) {
                                        string sufix = GetTypeData (type.GetElementType ()).XmlType;
                                        name = GetArrayName (sufix);
                                }
+#if NET_2_0
+                               else if (type.IsGenericType && !type.IsGenericTypeDefinition) {
+                                       name = XmlConvert.EncodeLocalName (type.Name.Substring (0, type.Name.IndexOf ('`'))) + "Of";
+                                       foreach (Type garg in type.GetGenericArguments ())
+                                               name += garg.IsArray || garg.IsGenericType ?
+                                                       GetTypeData (garg).XmlType :
+                                                       CodeIdentifier.MakePascal (XmlConvert.EncodeLocalName (garg.Name));
+                               }
+#endif
                                else 
                                        name = XmlConvert.EncodeLocalName (type.Name);
 
                                typeData = new TypeData (type, name, false);
-                               if (isNullableRuntimeType)
-                                       typeData.IsGenericNullable = true;
+                               if (nullableOverride)
+                                       typeData.IsNullable = true;
 #if TARGET_JVM
                                dynamicCache[runtimeType] = typeData;
 #else
                                nameCache[runtimeType] = typeData;
 #endif
                                return typeData;
-                       }
                }
 
                public static bool IsPrimitive (Type type)
@@ -235,7 +277,17 @@ namespace System.Xml.Serialization
 
                public static TypeData GetPrimitiveTypeData (string typeName)
                {
-                       TypeData td = (TypeData) primitiveTypes[typeName];
+                       return GetPrimitiveTypeData (typeName, false);
+               }
+
+               public static TypeData GetPrimitiveTypeData (string typeName, bool nullable)
+               {
+                       TypeData td = (TypeData) primitiveTypes [typeName];
+                       if (td != null && !td.Type.IsValueType)
+                               return td;
+                       // for 1.x profile, 'nullableTypes' is null
+                       Hashtable table = nullable && nullableTypes != null ? nullableTypes : primitiveTypes;
+                       td = (TypeData) table [typeName];
                        if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
                        return td;
                }
@@ -252,7 +304,7 @@ namespace System.Xml.Serialization
                        
                        if (primType.SchemaType == SchemaTypes.Primitive)
                        {
-                               TypeData newPrim = GetTypeData (primType.Type);
+                               TypeData newPrim = GetTypeData (primType.Type, null);
                                if (newPrim != primType) return newPrim;
                        }
                        return primType;