Add MIT license to System.XML.DLL
[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
38 namespace System.Xml.Serialization
39 {
40         internal class TypeTranslator
41         {
42                 static Hashtable nameCache;
43                 static Hashtable primitiveTypes;
44
45                 static TypeTranslator ()
46                 {
47                         nameCache = new Hashtable ();
48
49                         // XSD Types with direct map to CLR types
50
51                         nameCache.Add (typeof (bool), new TypeData (typeof (bool), "boolean", true));
52                         nameCache.Add (typeof (short), new TypeData (typeof (short), "short", true));
53                         nameCache.Add (typeof (ushort), new TypeData (typeof (ushort), "unsignedShort", true));
54                         nameCache.Add (typeof (int), new TypeData (typeof (int), "int", true));
55                         nameCache.Add (typeof (uint), new TypeData (typeof (uint), "unsignedInt", true));
56                         nameCache.Add (typeof (long), new TypeData (typeof (long), "long", true));
57                         nameCache.Add (typeof (ulong), new TypeData (typeof (ulong), "unsignedLong", true));
58                         nameCache.Add (typeof (float), new TypeData (typeof (float), "float", true));
59                         nameCache.Add (typeof (double), new TypeData (typeof (double), "double", true));
60                         nameCache.Add (typeof (DateTime), new TypeData (typeof (DateTime), "dateTime", true));  // TODO: timeInstant, Xml date, xml time
61                         nameCache.Add (typeof (Guid), new TypeData (typeof (Guid), "guid", true));
62                         nameCache.Add (typeof (decimal), new TypeData (typeof (decimal), "decimal", true));
63                         nameCache.Add (typeof (XmlQualifiedName), new TypeData (typeof (XmlQualifiedName), "QName", true));
64                         nameCache.Add (typeof (string), new TypeData (typeof (string), "string", true));
65                         nameCache.Add (typeof (byte), new TypeData (typeof (byte), "unsignedByte", true));
66                         nameCache.Add (typeof (sbyte), new TypeData (typeof (sbyte), "byte", true));
67                         nameCache.Add (typeof (char), new TypeData (typeof (char), "char", true));
68                         nameCache.Add (typeof (object), new TypeData (typeof (object), "anyType", false));
69                         nameCache.Add (typeof (byte[]), new TypeData (typeof (byte[]), "base64Binary", true));
70                         nameCache.Add (typeof (XmlNode), new TypeData (typeof (XmlNode), "XmlNode", false));
71                         nameCache.Add (typeof (XmlElement), new TypeData (typeof (XmlElement), "XmlElement", false));
72                         nameCache.Add (typeof (TimeSpan), new TypeData (typeof (TimeSpan), "duration", true));
73
74                         primitiveTypes = new Hashtable();
75                         ICollection types = nameCache.Values;
76                         foreach (TypeData td in types)
77                                 primitiveTypes.Add (td.XmlType, td);
78
79                         // Additional XSD types
80
81                         primitiveTypes.Add ("date", new TypeData (typeof (DateTime), "date", true));    // TODO: timeInstant
82                         primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
83                         primitiveTypes.Add ("timePeriod", new TypeData (typeof (DateTime), "timePeriod", true));
84                         primitiveTypes.Add ("gDay", new TypeData (typeof (string), "gDay", true));
85                         primitiveTypes.Add ("gMonthDay", new TypeData (typeof (string), "gMonthDay", true));
86                         primitiveTypes.Add ("gYear", new TypeData (typeof (string), "gYear", true));
87                         primitiveTypes.Add ("gYearMonth", new TypeData (typeof (string), "gYearMonth", true));
88                         primitiveTypes.Add ("month", new TypeData (typeof (DateTime), "month", true));
89                         primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
90                         primitiveTypes.Add ("NMTOKENS", new TypeData (typeof (string), "NMTOKENS", true));
91                         primitiveTypes.Add ("Name", new TypeData (typeof (string), "Name", true));
92                         primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
93                         primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
94                         primitiveTypes.Add ("integer", new TypeData (typeof (string), "integer", true));
95                         primitiveTypes.Add ("positiveInteger", new TypeData (typeof (string), "positiveInteger", true));
96                         primitiveTypes.Add ("nonPositiveInteger", new TypeData (typeof (string), "nonPositiveInteger", true));
97                         primitiveTypes.Add ("negativeInteger", new TypeData (typeof (string), "negativeInteger", true));
98                         primitiveTypes.Add ("nonNegativeInteger", new TypeData (typeof (string), "nonNegativeInteger", true));
99                         primitiveTypes.Add ("ENTITIES", new TypeData (typeof (string), "ENTITIES", true));
100                         primitiveTypes.Add ("ENTITY", new TypeData (typeof (string), "ENTITY", true));
101                         primitiveTypes.Add ("hexBinary", new TypeData (typeof (byte[]), "hexBinary", true));
102                         primitiveTypes.Add ("ID", new TypeData (typeof (string), "ID", true));
103                         primitiveTypes.Add ("IDREF", new TypeData (typeof (string), "IDREF", true));
104                         primitiveTypes.Add ("IDREFS", new TypeData (typeof (string), "IDREFS", true));
105                         primitiveTypes.Add ("NOTATION", new TypeData (typeof (string), "NOTATION", true));
106                         primitiveTypes.Add ("token", new TypeData (typeof (string), "token", true));
107                         primitiveTypes.Add ("normalizedString", new TypeData (typeof (string), "normalizedString", true));
108                         primitiveTypes.Add ("anyURI", new TypeData (typeof (string), "anyURI", true));
109                 }
110
111                 public static TypeData GetTypeData (Type type)
112                 {
113                         return GetTypeData (type, null);
114                 }
115
116                 public static TypeData GetTypeData (Type type, string xmlDataType)
117                 {
118                         if ((xmlDataType != null) && (xmlDataType.Length != 0)) return GetPrimitiveTypeData (xmlDataType);
119
120                         TypeData typeData = nameCache[type] as TypeData;
121                         if (typeData != null) return typeData;
122                         
123                         string name;
124                         if (type.IsArray) {
125                                 string sufix = GetTypeData (type.GetElementType ()).XmlType;
126                                 name = GetArrayName (sufix);
127                         }
128                         else 
129                                 name = type.Name;
130
131                         typeData = new TypeData (type, name, false);
132                         nameCache[type] = typeData;
133                         return typeData;
134                 }
135
136                 public static bool IsPrimitive (Type type)
137                 {
138                         return GetTypeData (type).SchemaType == SchemaTypes.Primitive;
139                 }
140
141                 public static TypeData GetPrimitiveTypeData (string typeName)
142                 {
143                         TypeData td = (TypeData) primitiveTypes[typeName];
144                         if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
145                         return td;
146                 }
147
148                 public static TypeData FindPrimitiveTypeData (string typeName)
149                 {
150                         return (TypeData) primitiveTypes[typeName];
151                 }
152
153                 public static TypeData GetDefaultPrimitiveTypeData (TypeData primType)
154                 {
155                         // Returns the TypeData that is mapped by default to the clr type
156                         // that primType represents
157                         
158                         if (primType.SchemaType == SchemaTypes.Primitive)
159                         {
160                                 TypeData newPrim = GetTypeData (primType.Type);
161                                 if (newPrim != primType) return newPrim;
162                         }
163                         return primType;
164                 }
165
166                 public static bool IsDefaultPrimitiveTpeData (TypeData primType)
167                 {
168                         return GetDefaultPrimitiveTypeData (primType) == primType;
169                 }
170
171                 public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
172                 {
173                         TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
174                         return td;
175                 }
176
177                 public static string GetArrayName (string elemName)
178                 {
179                         return "ArrayOf" + Char.ToUpper (elemName [0], CultureInfo.InvariantCulture) + elemName.Substring (1);
180                 }
181                 
182                 public static string GetArrayName (string elemName, int dimensions)
183                 {
184                         string aname = GetArrayName (elemName);
185                         for ( ; dimensions > 1; dimensions--)
186                                 aname = "ArrayOf" + aname;
187                         return aname;
188                 }
189                 
190                 public static void ParseArrayType (string arrayType, out string type, out string ns, out string dimensions)
191                 {
192                         int i = arrayType.LastIndexOf (":");
193                         if (i == -1) ns = "";
194                         else ns = arrayType.Substring (0,i);
195                         
196                         int j = arrayType.IndexOf ("[", i+1);
197                         if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
198                         type = arrayType.Substring (i+1, j-i-1);
199                         dimensions = arrayType.Substring (j);
200                 }
201         }
202 }