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;
48 const string AppDomainCacheName = "System.Xml.Serialization.TypeTranslator.AppDomainCache";
49 static Hashtable AppDomainCache {
51 Hashtable res = (Hashtable)AppDomain.CurrentDomain.GetData(AppDomainCacheName);
54 lock(AppDomainCacheName) {
55 res = (Hashtable)AppDomain.CurrentDomain.GetData(AppDomainCacheName);
57 res = new Hashtable();
58 AppDomain.CurrentDomain.SetData(AppDomainCacheName, res);
68 static TypeTranslator ()
70 nameCache = new Hashtable ();
71 primitiveArrayTypes = new Hashtable ();
73 // XSD Types with direct map to CLR types
75 nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
76 nameCache.Add (typeof (short), new TypeData (typeof (short), "short", true));
77 nameCache.Add (typeof (ushort), new TypeData (typeof (ushort), "unsignedShort", true));
78 nameCache.Add (typeof (int), new TypeData (typeof (int), "int", true));
79 nameCache.Add (typeof (uint), new TypeData (typeof (uint), "unsignedInt", true));
80 nameCache.Add (typeof (long), new TypeData (typeof (long), "long", true));
81 nameCache.Add (typeof (ulong), new TypeData (typeof (ulong), "unsignedLong", true));
82 nameCache.Add (typeof (float), new TypeData (typeof (float), "float", true));
83 nameCache.Add (typeof (double), new TypeData (typeof (double), "double", true));
84 nameCache.Add (typeof (DateTime), new TypeData (typeof (DateTime), "dateTime", true)); // TODO: timeInstant, Xml date, xml time
85 nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
86 nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
87 nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
88 XmlSchemaPatternFacet guidFacet = new XmlSchemaPatternFacet();
89 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}";
90 nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true, (TypeData)nameCache[typeof (string)], guidFacet));
91 nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
92 nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
93 nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true, (TypeData)nameCache[typeof (ushort)], null));
94 nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
95 nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
96 nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
97 nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
98 nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
100 primitiveTypes = new Hashtable();
101 ICollection types = nameCache.Values;
102 foreach (TypeData td in types)
103 primitiveTypes.Add (td.XmlType, td);
105 // Additional XSD types
107 primitiveTypes.Add ("date", new TypeData (typeof (DateTime), "date", true)); // TODO: timeInstant
108 primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
109 primitiveTypes.Add ("timePeriod", new TypeData (typeof (DateTime), "timePeriod", true));
110 primitiveTypes.Add ("gDay", new TypeData (typeof (string), "gDay", true));
111 primitiveTypes.Add ("gMonthDay", new TypeData (typeof (string), "gMonthDay", true));
112 primitiveTypes.Add ("gYear", new TypeData (typeof (string), "gYear", true));
113 primitiveTypes.Add ("gYearMonth", new TypeData (typeof (string), "gYearMonth", true));
114 primitiveTypes.Add ("month", new TypeData (typeof (DateTime), "month", true));
115 primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
116 primitiveTypes.Add ("NMTOKENS", new TypeData (typeof (string), "NMTOKENS", true));
117 primitiveTypes.Add ("Name", new TypeData (typeof (string), "Name", true));
118 primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
119 primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
120 primitiveTypes.Add ("integer", new TypeData (typeof (string), "integer", true));
121 primitiveTypes.Add ("positiveInteger", new TypeData (typeof (string), "positiveInteger", true));
122 primitiveTypes.Add ("nonPositiveInteger", new TypeData (typeof (string), "nonPositiveInteger", true));
123 primitiveTypes.Add ("negativeInteger", new TypeData (typeof (string), "negativeInteger", true));
124 primitiveTypes.Add ("nonNegativeInteger", new TypeData (typeof (string), "nonNegativeInteger", true));
125 primitiveTypes.Add ("ENTITIES", new TypeData (typeof (string), "ENTITIES", true));
126 primitiveTypes.Add ("ENTITY", new TypeData (typeof (string), "ENTITY", true));
127 primitiveTypes.Add ("hexBinary", new TypeData (typeof (byte[]), "hexBinary", true));
128 primitiveTypes.Add ("ID", new TypeData (typeof (string), "ID", true));
129 primitiveTypes.Add ("IDREF", new TypeData (typeof (string), "IDREF", true));
130 primitiveTypes.Add ("IDREFS", new TypeData (typeof (string), "IDREFS", true));
131 primitiveTypes.Add ("NOTATION", new TypeData (typeof (string), "NOTATION", true));
132 primitiveTypes.Add ("token", new TypeData (typeof (string), "token", true));
133 primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
134 primitiveTypes.Add ("anyURI", new TypeData (typeof (string), "anyURI", true));
135 primitiveTypes.Add ("base64", new TypeData (typeof (byte[]), "base64", true));
138 public static TypeData GetTypeData (Type type)
140 return GetTypeData (type, null);
143 public static TypeData GetTypeData (Type type, string xmlDataType)
145 if ((xmlDataType != null) && (xmlDataType.Length != 0)) {
146 // If the type is an array, xmlDataType specifies the type for the array elements,
147 // not for the whole array. The exception is base64Binary, since it is a byte[],
148 // that's why the following check is needed.
149 TypeData at = GetPrimitiveTypeData (xmlDataType);
150 if (type.IsArray && type != at.Type) {
151 lock (primitiveArrayTypes) {
152 TypeData tt = (TypeData) primitiveArrayTypes [xmlDataType];
155 if (at.Type == type.GetElementType ()) {
156 tt = new TypeData (type, GetArrayName (at.XmlType), false);
157 primitiveArrayTypes [xmlDataType] = tt;
161 throw new InvalidOperationException ("Cannot convert values of type '" + type.GetElementType () + "' to '" + xmlDataType + "'");
168 TypeData typeData = nameCache[type] as TypeData;
169 if (typeData != null) return typeData;
172 Hashtable dynamicCache = AppDomainCache;
173 typeData = dynamicCache[type] as TypeData;
174 if (typeData != null) return typeData;
179 string sufix = GetTypeData (type.GetElementType ()).XmlType;
180 name = GetArrayName (sufix);
183 name = XmlConvert.EncodeLocalName (type.Name);
185 typeData = new TypeData (type, name, false);
187 dynamicCache[type] = typeData;
189 nameCache[type] = typeData;
195 public static bool IsPrimitive (Type type)
197 return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
200 public static TypeData GetPrimitiveTypeData (string typeName)
202 TypeData td = (TypeData) primitiveTypes[typeName];
203 if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
207 public static TypeData FindPrimitiveTypeData (string typeName)
209 return (TypeData) primitiveTypes[typeName];
212 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
214 // Returns the TypeData that is mapped by default to the clr type
215 // that primType represents
217 if (primType.SchemaType == SchemaTypes.Primitive)
219 TypeData newPrim = GetTypeData (primType.Type);
220 if (newPrim != primType) return newPrim;
225 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
227 return GetDefaultPrimitiveTypeData (primType) == primType;
230 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
232 TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
236 public static string GetArrayName (string elemName)
238 return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
241 public static string GetArrayName (string elemName, int dimensions)
243 string aname = GetArrayName (elemName);
244 for ( ; dimensions > 1; dimensions--)
245 aname = "ArrayOf" + aname;
249 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
251 int i = arrayType.LastIndexOf (":");
252 if (i == -1) ns = "";
253 else ns = arrayType.Substring (0,i);
255 int j = arrayType.IndexOf ("[", i+1);
256 if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
257 type = arrayType.Substring (i+1, j-i-1);
258 dimensions = arrayType.Substring (j);