2 // System.Xml.Serialization.TypeTranslator
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Erik LeBel (eriklebel@yahoo.ca)
7 // Lluis Sanchez Gual (lluis@ximian.com)
9 // (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Erik Lebel
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.Globalization;
37 using System.Xml.Schema;
39 namespace System.Xml.Serialization
41 internal class TypeTranslator
43 static Hashtable nameCache;
44 static Hashtable primitiveTypes;
45 static Hashtable primitiveArrayTypes;
46 static Hashtable nullableTypes;
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";
53 static Hashtable AppDomain_nameCache {
54 get { return GetAppDomainCache (AppDomain_nameCacheName); }
57 static Hashtable AppDomain_nullableTypes {
58 get { return GetAppDomainCache (AppDomain_nullableTypesName); }
61 static Hashtable GetAppDomainCache(string name) {
62 Hashtable res = (Hashtable) AppDomain.CurrentDomain.GetData (name);
65 lock (AppDomain_TypeTranslatorCacheLock) {
66 res = (Hashtable) AppDomain.CurrentDomain.GetData (name);
68 res = Hashtable.Synchronized (new Hashtable ());
69 AppDomain.CurrentDomain.SetData (name, res);
78 static TypeTranslator ()
80 nameCache = new Hashtable ();
81 primitiveArrayTypes = Hashtable.Synchronized (new Hashtable ());
84 nameCache = Hashtable.Synchronized (nameCache);
86 // XSD Types with direct map to CLR types
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));
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));
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));
112 nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
113 nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
116 primitiveTypes = new Hashtable();
117 ICollection types = nameCache.Values;
118 foreach (TypeData td in types)
119 primitiveTypes.Add (td.XmlType, td);
121 // Additional XSD types
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));
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);
165 public static TypeData GetTypeData (Type type)
167 return GetTypeData (type, null);
170 public static TypeData GetTypeData (Type runtimeType, string xmlDataType)
172 Type type = runtimeType;
173 bool nullableOverride = false;
175 // Nullable<T> is serialized as T
176 if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>)) {
177 nullableOverride = true;
178 type = type.GetGenericArguments () [0];
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];
191 if (at.Type == type.GetElementType ()) {
192 tt = new TypeData (type, GetArrayName (at.XmlType), false);
193 primitiveArrayTypes [xmlDataType] = tt;
197 throw new InvalidOperationException ("Cannot convert values of type '" + type.GetElementType () + "' to '" + xmlDataType + "'");
199 if (nullableOverride){
200 TypeData tt = (TypeData) nullableTypes [at.XmlType];
202 tt = new TypeData (type, at.XmlType, false);
203 tt.IsNullable = true;
204 nullableTypes [at.XmlType] = tt;
211 if (nullableOverride){
212 TypeData pt = GetTypeData (type); // beware this recursive call btw ...
214 TypeData tt = (TypeData) nullableTypes [pt.XmlType];
217 tt = (TypeData) AppDomain_nullableTypes [pt.XmlType];
220 tt = new TypeData (type, pt.XmlType, false);
221 tt.IsNullable = true;
223 AppDomain_nullableTypes [pt.XmlType] = tt;
225 nullableTypes [pt.XmlType] = tt;
233 TypeData typeData = nameCache[runtimeType] as TypeData;
234 if (typeData != null) return typeData;
237 Hashtable dynamicCache = AppDomain_nameCache;
238 typeData = dynamicCache[runtimeType] as TypeData;
239 if (typeData != null) return typeData;
244 string sufix = GetTypeData (type.GetElementType ()).XmlType;
245 name = GetArrayName (sufix);
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));
257 name = XmlConvert.EncodeLocalName (type.Name);
259 typeData = new TypeData (type, name, false);
260 if (nullableOverride)
261 typeData.IsNullable = true;
263 dynamicCache[runtimeType] = typeData;
265 nameCache[runtimeType] = typeData;
270 public static bool IsPrimitive (Type type)
272 return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
275 public static TypeData GetPrimitiveTypeData (string typeName)
277 return GetPrimitiveTypeData (typeName, false);
280 public static TypeData GetPrimitiveTypeData (string typeName, bool nullable)
282 TypeData td = (TypeData) primitiveTypes [typeName];
283 if (td != null && !td.Type.IsValueType)
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");
292 public static TypeData FindPrimitiveTypeData (string typeName)
294 return (TypeData) primitiveTypes[typeName];
297 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
299 // Returns the TypeData that is mapped by default to the clr type
300 // that primType represents
302 if (primType.SchemaType == SchemaTypes.Primitive)
304 TypeData newPrim = GetTypeData (primType.Type, null);
305 if (newPrim != primType) return newPrim;
310 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
312 return GetDefaultPrimitiveTypeData (primType) == primType;
315 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
317 TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
321 public static string GetArrayName (string elemName)
323 return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
326 public static string GetArrayName (string elemName, int dimensions)
328 string aname = GetArrayName (elemName);
329 for ( ; dimensions > 1; dimensions--)
330 aname = "ArrayOf" + aname;
334 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
336 int i = arrayType.LastIndexOf (":");
337 if (i == -1) ns = "";
338 else ns = arrayType.Substring (0,i);
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);