* roottypes.cs: Rename from tree.cs.
[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
47 #if TARGET_JVM
48                 const string AppDomainCacheName = "System.Xml.Serialization.TypeTranslator.AppDomainCache";
49                 static Hashtable AppDomainCache {
50                         get {
51                                 Hashtable res = (Hashtable)AppDomain.CurrentDomain.GetData(AppDomainCacheName);
52
53                                 if(res == null) {
54                                         lock(AppDomainCacheName) {
55                                                 res = (Hashtable)AppDomain.CurrentDomain.GetData(AppDomainCacheName);
56                                                 if (res == null) {
57                                                         res = new Hashtable();
58                                                         AppDomain.CurrentDomain.SetData(AppDomainCacheName, res);
59                                                 }
60                                         }
61                                 }
62
63                                 return res;
64                         }
65                 }
66 #endif
67
68                 static TypeTranslator ()
69                 {
70                         nameCache = new Hashtable ();
71                         primitiveArrayTypes = new Hashtable ();
72
73                         // XSD Types with direct map to CLR types
74
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));
99
100                         primitiveTypes = new Hashtable();
101                         ICollection types = nameCache.Values;
102                         foreach (TypeData td in types)
103                                 primitiveTypes.Add (td.XmlType, td);
104
105                         // Additional XSD types
106
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));
136                 }
137
138                 public static TypeData GetTypeData (Type type)
139                 {
140                         return GetTypeData (type, null);
141                 }
142
143                 public static TypeData GetTypeData (Type type, string xmlDataType)
144                 {
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];
153                                                 if (tt != null)
154                                                         return tt;
155                                                 if (at.Type == type.GetElementType ()) {
156                                                         tt = new TypeData (type, GetArrayName (at.XmlType), false);
157                                                         primitiveArrayTypes [xmlDataType] = tt;
158                                                         return tt;
159                                                 }
160                                                 else
161                                                         throw new InvalidOperationException ("Cannot convert values of type '" + type.GetElementType () + "' to '" + xmlDataType + "'");
162                                         }
163                                 }
164                                 return at;
165                         }
166
167                         lock (nameCache) {
168                                 TypeData typeData = nameCache[type] as TypeData;
169                                 if (typeData != null) return typeData;
170
171 #if TARGET_JVM
172                                 Hashtable dynamicCache = AppDomainCache;
173                                 typeData = dynamicCache[type] as TypeData;
174                                 if (typeData != null) return typeData;
175 #endif
176                                 
177                                 string name;
178                                 if (type.IsArray) {
179                                         string sufix = GetTypeData (type.GetElementType ()).XmlType;
180                                         name = GetArrayName (sufix);
181                                 }
182                                 else 
183                                         name = XmlConvert.EncodeLocalName (type.Name);
184
185                                 typeData = new TypeData (type, name, false);
186 #if TARGET_JVM
187                                 dynamicCache[type] = typeData;
188 #else
189                                 nameCache[type] = typeData;
190 #endif
191                                 return typeData;
192                         }
193                 }
194
195                 public static bool IsPrimitive (Type type)
196                 {
197                         return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
198                 }
199
200                 public static TypeData GetPrimitiveTypeData (string typeName)
201                 {
202                         TypeData td = (TypeData) primitiveTypes[typeName];
203                         if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
204                         return td;
205                 }
206
207                 public static TypeData FindPrimitiveTypeData (string typeName)
208                 {
209                         return (TypeData) primitiveTypes[typeName];
210                 }
211
212                 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
213                 {
214                         // Returns the TypeData that is mapped by default to the clr type
215                         // that primType represents
216                         
217                         if (primType.SchemaType == SchemaTypes.Primitive)
218                         {
219                                 TypeData newPrim = GetTypeData (primType.Type);
220                                 if (newPrim != primType) return newPrim;
221                         }
222                         return primType;
223                 }
224
225                 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
226                 {
227                         return GetDefaultPrimitiveTypeData (primType) == primType;
228                 }
229
230                 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
231                 {
232                         TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
233                         return td;
234                 }
235
236                 public static string GetArrayName (string elemName)
237                 {
238                         return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
239                 }
240                 
241                 public static string GetArrayName (string elemName, int dimensions)
242                 {
243                         string aname = GetArrayName (elemName);
244                         for ( ; dimensions > 1; dimensions--)
245                                 aname = "ArrayOf" + aname;
246                         return aname;
247                 }
248                 
249                 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
250                 {
251                         int i = arrayType.LastIndexOf (":");
252                         if (i == -1) ns = "";
253                         else ns = arrayType.Substring (0,i);
254                         
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);
259                 }
260         }
261 }